-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.php
More file actions
281 lines (223 loc) · 5.65 KB
/
functions.php
File metadata and controls
281 lines (223 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
namespace sequence\functions {
/**
* Archaic and simple debug function.
* No longer used due to output automatically being monospaced and included in all error pages. Output is normally
* considered an error except in certain conditions, so if you need to dump a variable while debugging, just echo
* it out. No seriously, try it.
*
* @param mixed ...$variables
*/
function dump(...$variables) {
foreach ($variables as $variable) {
echo '<pre>';
var_dump($variable);
echo '</pre>';
}
}
/**
* @param $data
*
* @return string
*/
function base64url_encode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
/**
* @param $data
*
* @return string
*/
function base64url_decode($data) {
return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data)%4, '=', STR_PAD_RIGHT));
}
/**
* Wrapper for json_decode. It makes associative arrays by default.
*
* @param string $json
* @param boolean $assoc
*
* @return mixed
*/
function json_decode($json, $assoc = true) {
return \json_decode($json, $assoc);
}
/**
* Automatically read and parse a JSON file into associative arrays.
*
* @param string $filename
*
* @return mixed
*/
function file_get_json($filename) {
$json = \file_get_contents($filename);
if ($json !== false) {
return json_decode($json);
}
return false;
}
/**
* Sort an indexed array into an associative array with keys being the last path component.
*
* @param array $files
*
* @return array
*/
function files_sort($files) {
$index = [];
foreach ($files as $file) {
$index[basename($file)] = $file;
}
return $index;
}
function starts_with($haystack, $needle) {
return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== false;
}
function ends_with($haystack, $needle) {
return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false);
}
/**
* Automatically filter out any parent and current directory references in a path.
*
* @param string $path
*
* @return string
*/
function path_normalize($path) {
$path = preg_replace(['/^\\/+|[!"&\'\\-.\\/:;=?@\\\\_]+$/', '/\\/\\/+/'], ['', '/'], $path);
$segments = explode('/', $path);
for ($i = 0, $length = count($segments); $i < $length; ++$i) {
if ($segments[$i] === '.') {
goto current_directory;
}
if ($segments[$i] === '..') {
if ($i > 0) {
array_splice($segments, $i - 1, 2);
$i -= 2;
$length -= 2;
continue;
}
goto current_directory;
}
continue;
current_directory:
array_splice($segments, $i, 1);
--$i;
--$length;
}
return implode('/', $segments);
}
/**
* Automatically convert all tabs to spaces and make all line-feeds unix style.
* Excellent for displaying pre-formatted monospace text.
*
* @param string $text
* @param int $tab
*
* @return string
*/
function text_normalize($text, $tab = 4) {
// Normalize all line breaks to line feeds, then split the text into an array at each line break.
$text = explode("\n", str_replace(["\r\n", "\r"], "\n", $text));
// Convert tabs to spaces.
for ($i = 0, $j = count($text); $i < $j; ++$i) {
$post = $text[$i];
$line = '';
// Used to calculate the size of each tab without having to recount the entire length of the string.
$length = 0;
while (($position = mb_strpos($post, "\t")) !== false) {
// Split string into two, stripping the tab in the process.
$pre = $position ? mb_substr($post, 0, $position) : '';
$post = mb_substr($post, $position + 1);
$length += mb_strlen($pre);
$size = $tab - ($length%$tab);
$length += $size;
$line .= $pre.str_repeat(' ', $size);
}
$text[$i] = $line.$post;
}
return implode("\n", $text);
}
/**
* Format all text for display in HTML.
*
* @param string $text
*
* @return string
*/
function text_format($text) {
return htmlspecialchars($text, ENT_COMPAT | ENT_DISALLOWED | ENT_HTML5);
}
/**
* Convert a stack trace to a string.
*
* @param array $trace
*
* @return string
*/
function trace_text($trace) {
$objects = [];
$argument = function ($variable) use (&$argument, &$objects) {
switch (strtolower(gettype($variable))) {
case 'boolean':
$variable = $variable ? 'true' : 'false';
break;
case 'string':
$variable = "'$variable'";
break;
case 'array':
$values = [];
foreach ($variable as $value) {
$values[] = $argument($value);
}
$variable = '['.implode(', ', $values).']';
break;
case 'object':
$objects[] = $variable;
$variable = 'Object #'.count($objects);
break;
case 'resource':
$variable = (string)$variable;
break;
case 'null':
$variable = 'null';
break;
case 'unknown type':
default:
$variable = 'unknown';
}
return $variable;
};
$lines = [];
foreach ($trace as $number => $function) {
if (isset($function['file'])) {
$line = "#$number $function[file]($function[line]): ";
} else {
$line = "#$number: ";
}
if (isset($function['class'])) {
$line .= "$function[class]$function[type]";
}
$line .= "$function[function](";
$arguments = [];
if (isset($function['args'])) {
foreach ($function['args'] as $value) {
$arguments[] = $argument($value);
}
}
$line .= implode(', ', $arguments).')';
$lines[] = $line;
}
$text = implode("\n", $lines);
if (!empty($objects)) {
ob_start();
foreach ($objects as $number => $object) {
echo "\n\nObject #".++$number.': ';
var_dump($object);
}
return $text.ob_get_clean();
} else {
}
return $text;
}
}