1 | <?php |
||||
2 | /** |
||||
3 | * This file is part of the O2System Framework package. |
||||
4 | * |
||||
5 | * For the full copyright and license information, please view the LICENSE |
||||
6 | * file that was distributed with this source code. |
||||
7 | * |
||||
8 | * @author Steeve Andrian Salim |
||||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
10 | */ |
||||
11 | // ------------------------------------------------------------------------ |
||||
12 | |||||
13 | if ( ! function_exists('is_php')) { |
||||
14 | /** |
||||
15 | * is_php |
||||
16 | * |
||||
17 | * Determines if the current version of PHP is equal to or greater than the supplied value |
||||
18 | * |
||||
19 | * @param string |
||||
20 | * |
||||
21 | * @return bool TRUE if the current version is $version or higher |
||||
22 | */ |
||||
23 | function is_php($version) |
||||
24 | { |
||||
25 | static $_is_php; |
||||
26 | $version = (string)$version; |
||||
27 | |||||
28 | if ( ! isset($_is_php[ $version ])) { |
||||
29 | $_is_php[ $version ] = version_compare(PHP_VERSION, $version, '>='); |
||||
30 | } |
||||
31 | |||||
32 | return $_is_php[ $version ]; |
||||
33 | } |
||||
34 | } |
||||
35 | |||||
36 | // ------------------------------------------------------------------------ |
||||
37 | |||||
38 | if ( ! function_exists('is_true')) { |
||||
39 | /** |
||||
40 | * is_true |
||||
41 | * |
||||
42 | * Helper function to test boolean TRUE. |
||||
43 | * |
||||
44 | * @param mixed $test |
||||
45 | * |
||||
46 | * @return bool |
||||
47 | */ |
||||
48 | function is_true($test) |
||||
49 | { |
||||
50 | return (bool)($test === true); |
||||
51 | } |
||||
52 | } |
||||
53 | |||||
54 | // ------------------------------------------------------------------------ |
||||
55 | |||||
56 | if ( ! function_exists('is_false')) { |
||||
57 | /** |
||||
58 | * is_false |
||||
59 | * |
||||
60 | * Helper function to test boolean FALSE. |
||||
61 | * |
||||
62 | * @param mixed $test |
||||
63 | * |
||||
64 | * @return bool |
||||
65 | */ |
||||
66 | function is_false($test) |
||||
67 | { |
||||
68 | return (bool)($test === false); |
||||
69 | } |
||||
70 | } |
||||
71 | |||||
72 | // ------------------------------------------------------------------------ |
||||
73 | |||||
74 | if ( ! function_exists('is_really_writable')) { |
||||
75 | /** |
||||
76 | * is_really_writable |
||||
77 | * |
||||
78 | * Tests for file writability |
||||
79 | * |
||||
80 | * is_writable() returns TRUE on Windows servers when you really can't write to |
||||
81 | * the file, based on the read-only attribute. is_writable() is also unreliable |
||||
82 | * on Unix servers if safe_mode is on. |
||||
83 | * |
||||
84 | * @link https://bugs.php.net/bug.php?id=54709 |
||||
85 | * |
||||
86 | * @param string |
||||
87 | * |
||||
88 | * @return bool |
||||
89 | */ |
||||
90 | function is_really_writable($file) |
||||
91 | { |
||||
92 | // If we're on a Unix server with safe_mode off we call is_writable |
||||
93 | if (DIRECTORY_SEPARATOR === '/' && (is_php('5.4') || ! ini_get('safe_mode'))) { |
||||
94 | return is_writable($file); |
||||
95 | } |
||||
96 | |||||
97 | /* For Windows servers and safe_mode "on" installations we'll actually |
||||
98 | * write a file then read it. Bah... |
||||
99 | */ |
||||
100 | if (is_dir($file)) { |
||||
101 | $file = rtrim($file, '/') . '/' . md5(mt_rand()); |
||||
102 | if (($fp = @fopen($file, 'ab')) === false) { |
||||
103 | return false; |
||||
104 | } |
||||
105 | |||||
106 | fclose($fp); |
||||
107 | @chmod($file, 0777); |
||||
0 ignored issues
–
show
|
|||||
108 | @unlink($file); |
||||
0 ignored issues
–
show
It seems like you do not handle an error condition for
unlink() . This can introduce security issues, and is generally not recommended.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
If you suppress an error, we recommend checking for the error condition explicitly: // For example instead of
@mkdir($dir);
// Better use
if (@mkdir($dir) === false) {
throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
![]() |
|||||
109 | |||||
110 | return true; |
||||
111 | } elseif ( ! is_file($file) || ($fp = @fopen($file, 'ab')) === false) { |
||||
112 | return false; |
||||
113 | } |
||||
114 | |||||
115 | fclose($fp); |
||||
116 | |||||
117 | return true; |
||||
118 | } |
||||
119 | } |
||||
120 | |||||
121 | // ------------------------------------------------------------------------ |
||||
122 | |||||
123 | if ( ! function_exists('is_https')) { |
||||
124 | /** |
||||
125 | * is_https |
||||
126 | * |
||||
127 | * Determines if the application is accessed via an encrypted |
||||
128 | * (HTTPS) connection. |
||||
129 | * |
||||
130 | * @return bool |
||||
131 | */ |
||||
132 | function is_https() |
||||
133 | { |
||||
134 | if ( ! empty($_SERVER[ 'HTTPS' ]) && strtolower($_SERVER[ 'HTTPS' ]) !== 'off') { |
||||
135 | return true; |
||||
136 | } elseif (isset($_SERVER[ 'HTTP_X_FORWARDED_PROTO' ]) && $_SERVER[ 'HTTP_X_FORWARDED_PROTO' ] === 'https') { |
||||
137 | return true; |
||||
138 | } elseif ( ! empty($_SERVER[ 'HTTP_FRONT_END_HTTPS' ]) && strtolower( |
||||
139 | $_SERVER[ 'HTTP_FRONT_END_HTTPS' ] |
||||
140 | ) !== 'off' |
||||
141 | ) { |
||||
142 | return true; |
||||
143 | } |
||||
144 | |||||
145 | return false; |
||||
146 | } |
||||
147 | } |
||||
148 | |||||
149 | // ------------------------------------------------------------------------ |
||||
150 | |||||
151 | if ( ! function_exists('is_cli')) { |
||||
152 | /** |
||||
153 | * is_cli |
||||
154 | * |
||||
155 | * Test to see if a request was made from the command line. |
||||
156 | * |
||||
157 | * @return bool |
||||
158 | */ |
||||
159 | function is_cli() |
||||
160 | { |
||||
161 | return (php_sapi_name() === 'cli'); |
||||
162 | } |
||||
163 | } |
||||
164 | |||||
165 | // ------------------------------------------------------------------------ |
||||
166 | |||||
167 | if ( ! function_exists('is_ajax')) { |
||||
168 | /** |
||||
169 | * is_ajax |
||||
170 | * |
||||
171 | * Test to see if a request an ajax request. |
||||
172 | * |
||||
173 | * @return bool |
||||
174 | */ |
||||
175 | function is_ajax() |
||||
176 | { |
||||
177 | return ( ! empty($_SERVER[ 'HTTP_X_REQUESTED_WITH' ]) && strtolower( |
||||
178 | $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] |
||||
179 | ) === 'xmlhttprequest'); |
||||
180 | } |
||||
181 | } |
||||
182 | |||||
183 | // ------------------------------------------------------------------------ |
||||
184 | |||||
185 | if ( ! function_exists('remove_invisible_characters')) { |
||||
186 | /** |
||||
187 | * remove_invisible_characters |
||||
188 | * |
||||
189 | * This prevents sandwiching null characters |
||||
190 | * between ascii characters, like Java\0script. |
||||
191 | * |
||||
192 | * @param string |
||||
193 | * @param bool |
||||
194 | * |
||||
195 | * @return string |
||||
196 | */ |
||||
197 | function remove_invisible_characters($str, $url_encoded = true) |
||||
198 | { |
||||
199 | $non_displayables = []; |
||||
200 | |||||
201 | // every control character except newline (dec 10), |
||||
202 | // carriage return (dec 13) and horizontal tab (dec 09) |
||||
203 | if ($url_encoded) { |
||||
204 | $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15 |
||||
205 | $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31 |
||||
206 | } |
||||
207 | |||||
208 | $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127 |
||||
209 | |||||
210 | do { |
||||
211 | $str = preg_replace($non_displayables, '', $str, -1, $count); |
||||
212 | } while ($count); |
||||
213 | |||||
214 | return $str; |
||||
215 | } |
||||
216 | } |
||||
217 | |||||
218 | // ------------------------------------------------------------------------ |
||||
219 | |||||
220 | if ( ! function_exists('function_usable')) { |
||||
221 | /** |
||||
222 | * function_usable |
||||
223 | * |
||||
224 | * Executes a function_exists() check, and if the Suhosin PHP |
||||
225 | * extension is loaded - checks whether the function that is |
||||
226 | * checked might be disabled in there as well. |
||||
227 | * |
||||
228 | * This is useful as function_exists() will return FALSE for |
||||
229 | * functions disabled via the *disable_functions* php.ini |
||||
230 | * setting, but not for *suhosin.executor.func.blacklist* and |
||||
231 | * *suhosin.executor.disable_eval*. These settings will just |
||||
232 | * terminate script execution if a disabled function is executed. |
||||
233 | * |
||||
234 | * The above described behavior turned out to be a bug in Suhosin, |
||||
235 | * but even though a fix was commited for 0.9.34 on 2012-02-12, |
||||
236 | * that version is yet to be released. This function will therefore |
||||
237 | * be just temporary, but would probably be kept for a few years. |
||||
238 | * |
||||
239 | * @link http://www.hardened-php.net/suhosin/ |
||||
240 | * |
||||
241 | * @param string $function_name Function to check for |
||||
242 | * |
||||
243 | * @return bool TRUE if the function exists and is safe to call, |
||||
244 | * FALSE otherwise. |
||||
245 | */ |
||||
246 | function function_usable($function_name) |
||||
247 | { |
||||
248 | static $suhosinFuncBlacklist; |
||||
249 | |||||
250 | if (function_exists($function_name)) { |
||||
251 | if ( ! isset($suhosinFuncBlacklist)) { |
||||
252 | if (extension_loaded('suhosin')) { |
||||
253 | $suhosinFuncBlacklist = explode(',', trim(ini_get('suhosin.executor.func.blacklist'))); |
||||
254 | |||||
255 | if ( ! in_array('eval', $suhosinFuncBlacklist, true) && ini_get( |
||||
256 | 'suhosin.executor.disable_eval' |
||||
257 | ) |
||||
258 | ) { |
||||
259 | $suhosinFuncBlacklist[] = 'eval'; |
||||
260 | } |
||||
261 | } else { |
||||
262 | $suhosinFuncBlacklist = []; |
||||
263 | } |
||||
264 | } |
||||
265 | |||||
266 | return ! in_array($function_name, $suhosinFuncBlacklist, true); |
||||
267 | } |
||||
268 | |||||
269 | return false; |
||||
270 | } |
||||
271 | } |
||||
272 | |||||
273 | // ------------------------------------------------------------------------ |
||||
274 | |||||
275 | if ( ! function_exists('apache_request_headers')) { |
||||
276 | function apache_request_headers() |
||||
277 | { |
||||
278 | $headers = []; |
||||
279 | $http_regex = '/\AHTTP_/'; |
||||
280 | foreach ($_SERVER as $server_key => $server_value) { |
||||
281 | if (preg_match($http_regex, $server_key)) { |
||||
282 | $header_key = preg_replace($http_regex, '', $server_key); |
||||
283 | $matches_regex = []; |
||||
0 ignored issues
–
show
|
|||||
284 | // do some nasty string manipulations to restore the original letter case |
||||
285 | // this should work in most cases |
||||
286 | $matches_regex = explode('_', $header_key); |
||||
287 | if (count($matches_regex) > 0 and strlen($header_key) > 2) { |
||||
288 | foreach ($matches_regex as $match_key => $match_value) { |
||||
289 | $matches_regex[ $match_key ] = ucfirst($match_value); |
||||
290 | } |
||||
291 | $header_key = implode('-', $matches_regex); |
||||
292 | } |
||||
293 | $headers[ $header_key ] = $server_value; |
||||
294 | } |
||||
295 | } |
||||
296 | |||||
297 | return ($headers); |
||||
298 | } |
||||
299 | } |
||||
300 | |||||
301 | |||||
302 | if ( ! function_exists('path_to_url')) { |
||||
303 | /** |
||||
304 | * path_to_url |
||||
305 | * |
||||
306 | * @param $path |
||||
307 | * |
||||
308 | * @return string |
||||
309 | */ |
||||
310 | function path_to_url($path) |
||||
311 | { |
||||
312 | $path = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path); |
||||
313 | $root_dir = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $_SERVER[ 'DOCUMENT_ROOT' ]); |
||||
314 | $base_dir = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, dirname($_SERVER[ 'SCRIPT_FILENAME' ])); |
||||
315 | |||||
316 | $root_dir = rtrim($root_dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
||||
317 | $base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
||||
318 | |||||
319 | if (is_dir(DIRECTORY_SEPARATOR . 'private' . $base_dir)) { |
||||
320 | $root_dir = DIRECTORY_SEPARATOR . 'private' . $root_dir; |
||||
321 | $base_dir = DIRECTORY_SEPARATOR . 'private' . $base_dir; |
||||
322 | } |
||||
323 | |||||
324 | $root_dir = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $root_dir); |
||||
325 | $base_dir = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $base_dir); |
||||
326 | |||||
327 | $base_url = is_https() ? 'https' : 'http'; |
||||
328 | |||||
329 | if (isset($_SERVER[ 'HTTP_HOST' ])) { |
||||
330 | $base_url .= '://' . $_SERVER[ 'HTTP_HOST' ]; |
||||
331 | } elseif (isset($_SERVER[ 'SERVER_NAME' ])) { |
||||
332 | |||||
333 | // Add server name |
||||
334 | $base_url .= '://' . $_SERVER[ 'SERVER_NAME' ]; |
||||
335 | |||||
336 | // Add server port if needed |
||||
337 | $base_url .= $_SERVER[ 'SERVER_PORT' ] !== '80' ? ':' . $_SERVER[ 'SERVER_PORT' ] : ''; |
||||
338 | } |
||||
339 | |||||
340 | // Add base path |
||||
341 | $base_url .= '/' . str_replace($root_dir, '', $base_dir); |
||||
342 | $base_url = str_replace(DIRECTORY_SEPARATOR, '/', $base_url); |
||||
343 | $base_url = trim($base_url, '/') . '/'; |
||||
344 | |||||
345 | if(strpos($path, 'resources') !== false && defined('PATH_RESOURCES')) { |
||||
346 | $path_url = 'resources' . '/' . str_replace(PATH_RESOURCES, '', $path); |
||||
0 ignored issues
–
show
|
|||||
347 | } elseif(strpos($path, 'public') !== false && defined('PATH_PUBLIC')) { |
||||
348 | $path_url = str_replace(PATH_PUBLIC, '', $path); |
||||
0 ignored issues
–
show
|
|||||
349 | } elseif(strpos($path, 'storage') !== false && defined('PATH_RESOURCES')) { |
||||
350 | $path_url = 'storage' . '/' . str_replace(PATH_RESOURCES, '', $path); |
||||
351 | } else { |
||||
352 | $path_url = str_replace($base_dir, '', $path); |
||||
353 | } |
||||
354 | |||||
355 | $path_url = str_replace(['/', '\\'], '/', $path_url); |
||||
356 | |||||
357 | return $base_url . str_replace(DIRECTORY_SEPARATOR, '/', $path_url); |
||||
358 | } |
||||
359 | } |
||||
360 | |||||
361 | // ------------------------------------------------------------------------ |
||||
362 | |||||
363 | if ( ! function_exists('get_namespace')) { |
||||
364 | /** |
||||
365 | * get_namespace |
||||
366 | * |
||||
367 | * @param $class |
||||
368 | * |
||||
369 | * @return string |
||||
370 | */ |
||||
371 | function get_namespace($class) |
||||
372 | { |
||||
373 | $class = is_object($class) ? get_class($class) : prepare_class_name($class); |
||||
374 | |||||
375 | $x_class = explode('\\', $class); |
||||
376 | array_pop($x_class); |
||||
377 | |||||
378 | return trim(implode('\\', $x_class), '\\') . '\\'; |
||||
379 | } |
||||
380 | } |
||||
381 | |||||
382 | // ------------------------------------------------------------------------ |
||||
383 | |||||
384 | if ( ! function_exists('get_class_name')) { |
||||
385 | /** |
||||
386 | * get_class_name |
||||
387 | * |
||||
388 | * @param $class |
||||
389 | * |
||||
390 | * @return mixed|string |
||||
391 | */ |
||||
392 | function get_class_name($class) |
||||
393 | { |
||||
394 | $class = is_object($class) ? get_class($class) : prepare_class_name($class); |
||||
395 | |||||
396 | if (strpos($class, 'anonymous') !== false) { |
||||
397 | return $class; |
||||
398 | } else { |
||||
399 | $x_class = explode('\\', $class); |
||||
400 | |||||
401 | return end($x_class); |
||||
402 | } |
||||
403 | } |
||||
404 | } |
||||
405 | |||||
406 | // ------------------------------------------------------------------------ |
||||
407 | |||||
408 | if ( ! function_exists('prepare_class_name')) { |
||||
409 | /** |
||||
410 | * prepare_class_name |
||||
411 | * |
||||
412 | * @param $class |
||||
413 | * |
||||
414 | * @return string |
||||
415 | */ |
||||
416 | function prepare_class_name($class) |
||||
417 | { |
||||
418 | $class = str_replace(['/', DIRECTORY_SEPARATOR, '.php'], ['\\', '\\', ''], $class); |
||||
419 | $class = trim($class); |
||||
420 | |||||
421 | $segments = explode('\\', $class); |
||||
422 | |||||
423 | if (count($segments) > 1) { |
||||
424 | if ($segments[ 0 ] === $segments[ 1 ]) { |
||||
425 | array_shift($segments); |
||||
426 | } |
||||
427 | } |
||||
428 | |||||
429 | $segments = array_map('studlycase', $segments); |
||||
430 | |||||
431 | return implode('\\', $segments); |
||||
432 | } |
||||
433 | } |
||||
434 | |||||
435 | // ------------------------------------------------------------------------ |
||||
436 | |||||
437 | if ( ! function_exists('prepare_filename')) { |
||||
438 | /** |
||||
439 | * prepare_filename |
||||
440 | * |
||||
441 | * @param $filename |
||||
442 | * @param null $ext |
||||
0 ignored issues
–
show
|
|||||
443 | * |
||||
444 | * @return string |
||||
445 | */ |
||||
446 | function prepare_filename($filename, $ext = null) |
||||
447 | { |
||||
448 | $filename = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $filename); |
||||
449 | $filename = trim($filename, DIRECTORY_SEPARATOR); |
||||
450 | |||||
451 | $segments = explode(DIRECTORY_SEPARATOR, $filename); |
||||
452 | $segments = array_map('studlycase', $segments); |
||||
453 | |||||
454 | return implode(DIRECTORY_SEPARATOR, $segments) . $ext; |
||||
455 | } |
||||
456 | } |
||||
457 | |||||
458 | // ------------------------------------------------------------------------ |
||||
459 | |||||
460 | |||||
461 | if ( ! function_exists('prepare_namespace')) { |
||||
462 | |||||
463 | /** |
||||
464 | * prepare_namespace |
||||
465 | * |
||||
466 | * Return a valid namespace class |
||||
467 | * |
||||
468 | * @param string $class class name with namespace |
||||
469 | * |
||||
470 | * @return string valid string namespace |
||||
471 | */ |
||||
472 | function prepare_namespace($class, $get_namespace = true) |
||||
473 | { |
||||
474 | return ($get_namespace === true ? get_namespace($class) : prepare_class_name($class)); |
||||
475 | } |
||||
476 | } |
||||
477 | |||||
478 | // ------------------------------------------------------------------------ |
||||
479 | |||||
480 | if ( ! function_exists('http_parse_headers')) { |
||||
481 | |||||
482 | /** |
||||
483 | * http_parse_headers |
||||
484 | * |
||||
485 | * @param $raw_headers |
||||
486 | * |
||||
487 | * @return array |
||||
488 | */ |
||||
489 | function http_parse_headers($raw_headers) |
||||
490 | { |
||||
491 | $headers = []; |
||||
492 | $key = ''; // [+] |
||||
493 | |||||
494 | foreach (explode("\n", $raw_headers) as $i => $h) { |
||||
495 | $h = explode(':', $h, 2); |
||||
496 | |||||
497 | if (isset($h[ 1 ])) { |
||||
498 | if ( ! isset($headers[ $h[ 0 ] ])) { |
||||
499 | $headers[ $h[ 0 ] ] = trim($h[ 1 ]); |
||||
500 | } elseif (is_array($headers[ $h[ 0 ] ])) { |
||||
501 | $headers[ $h[ 0 ] ] = array_merge($headers[ $h[ 0 ] ], [trim($h[ 1 ])]); // [+] |
||||
502 | } else { |
||||
503 | $headers[ $h[ 0 ] ] = array_merge([$headers[ $h[ 0 ] ]], [trim($h[ 1 ])]); // [+] |
||||
504 | } |
||||
505 | |||||
506 | $key = $h[ 0 ]; // [+] |
||||
507 | } else // [+] |
||||
508 | { // [+] |
||||
509 | if (substr($h[ 0 ], 0, 1) == "\t") // [+] |
||||
510 | { |
||||
511 | $headers[ $key ] .= "\r\n\t" . trim($h[ 0 ]); |
||||
512 | } // [+] |
||||
513 | elseif ( ! $key) // [+] |
||||
514 | { |
||||
515 | $headers[ 0 ] = trim($h[ 0 ]); |
||||
516 | } |
||||
517 | trim($h[ 0 ]); // [+] |
||||
518 | } // [+] |
||||
519 | } |
||||
520 | |||||
521 | return $headers; |
||||
522 | } |
||||
523 | } |
||||
524 | |||||
525 | // ------------------------------------------------------------------------ |
||||
526 | |||||
527 | if ( ! function_exists('is_html')) { |
||||
528 | /** |
||||
529 | * is_html |
||||
530 | * |
||||
531 | * Determine if string is HTML |
||||
532 | * |
||||
533 | * @param $string |
||||
534 | * |
||||
535 | * @return bool |
||||
536 | */ |
||||
537 | function is_html($string) |
||||
538 | { |
||||
539 | return (bool)($string !== strip_tags($string) ? true : false); |
||||
540 | } |
||||
541 | } |
||||
542 | |||||
543 | // ------------------------------------------------------------------------ |
||||
544 | |||||
545 | if ( ! function_exists('is_serialized')) { |
||||
546 | /** |
||||
547 | * is_serialized |
||||
548 | * |
||||
549 | * Check is the string is serialized array |
||||
550 | * |
||||
551 | * @param string $string Source string |
||||
552 | * |
||||
553 | * @return bool |
||||
554 | */ |
||||
555 | function is_serialized($string) |
||||
556 | { |
||||
557 | if ( ! is_string($string)) { |
||||
0 ignored issues
–
show
|
|||||
558 | return false; |
||||
559 | } |
||||
560 | if (trim($string) == '') { |
||||
561 | return false; |
||||
562 | } |
||||
563 | if (preg_match("/^(i|s|a|o|d)(.*);/si", $string)) { |
||||
564 | $is_valid = @unserialize($string); |
||||
565 | |||||
566 | if (empty($is_valid)) { |
||||
567 | return false; |
||||
568 | } |
||||
569 | |||||
570 | return true; |
||||
571 | } |
||||
572 | |||||
573 | return false; |
||||
574 | } |
||||
575 | } |
||||
576 | // ------------------------------------------------------------------------ |
||||
577 | |||||
578 | if ( ! function_exists('is_json')) { |
||||
579 | /** |
||||
580 | * is_json |
||||
581 | * |
||||
582 | * Check is the string is json array or object |
||||
583 | * |
||||
584 | * @item string or array |
||||
585 | * @return boolean (true or false) |
||||
586 | */ |
||||
587 | function is_json($string) |
||||
588 | { |
||||
589 | // make sure provided input is of type string |
||||
590 | if ( ! is_string($string)) { |
||||
591 | return false; |
||||
592 | } |
||||
593 | // trim white spaces |
||||
594 | $string = trim($string); |
||||
595 | // get first character |
||||
596 | $first_char = substr($string, 0, 1); |
||||
597 | // get last character |
||||
598 | $last_char = substr($string, -1); |
||||
599 | // check if there is a first and last character |
||||
600 | if ( ! $first_char || ! $last_char) { |
||||
601 | return false; |
||||
602 | } |
||||
603 | // make sure first character is either { or [ |
||||
604 | if ($first_char !== '{' && $first_char !== '[') { |
||||
605 | return false; |
||||
606 | } |
||||
607 | // make sure last character is either } or ] |
||||
608 | if ($last_char !== '}' && $last_char !== ']') { |
||||
609 | return false; |
||||
610 | } |
||||
611 | // let's leave the rest to PHP. |
||||
612 | // try to decode string |
||||
613 | json_decode($string); |
||||
614 | // check if error occurred |
||||
615 | $is_valid = json_last_error() === JSON_ERROR_NONE; |
||||
616 | |||||
617 | return $is_valid; |
||||
618 | } |
||||
619 | } |
||||
620 | |||||
621 | // ------------------------------------------------------------------------ |
||||
622 | |||||
623 | if ( ! function_exists('is_parse_string')) { |
||||
624 | /** |
||||
625 | * is_parse_string |
||||
626 | * |
||||
627 | * @return string |
||||
628 | * |
||||
629 | * @params string $string |
||||
630 | */ |
||||
631 | function is_parse_string($string) |
||||
632 | { |
||||
633 | if (preg_match('[=]', $string)) { |
||||
634 | return true; |
||||
0 ignored issues
–
show
|
|||||
635 | } |
||||
636 | |||||
637 | return false; |
||||
0 ignored issues
–
show
|
|||||
638 | } |
||||
639 | } |
||||
640 | |||||
641 | // ------------------------------------------------------------------------ |
||||
642 | |||||
643 | if ( ! function_exists('is_multidimensional_array')) { |
||||
644 | /** |
||||
645 | * is_multidimensional_array |
||||
646 | * |
||||
647 | * Checks if the given array is multidimensional. |
||||
648 | * |
||||
649 | * @param array $array |
||||
650 | * |
||||
651 | * @return bool |
||||
652 | */ |
||||
653 | function is_multidimensional_array(array $array) |
||||
654 | { |
||||
655 | if (count($array) != count($array, COUNT_RECURSIVE)) { |
||||
656 | return true; |
||||
657 | } |
||||
658 | |||||
659 | return false; |
||||
660 | } |
||||
661 | } |
||||
662 | |||||
663 | // ------------------------------------------------------------------------ |
||||
664 | |||||
665 | if ( ! function_exists('is_associative_array')) { |
||||
666 | /** |
||||
667 | * is_associative_array |
||||
668 | * |
||||
669 | * Check if the given array is associative. |
||||
670 | * |
||||
671 | * @param array $array |
||||
672 | * |
||||
673 | * @return bool |
||||
674 | */ |
||||
675 | function is_associative_array(array $array) |
||||
676 | { |
||||
677 | if ($array == []) { |
||||
678 | return true; |
||||
679 | } |
||||
680 | $keys = array_keys($array); |
||||
681 | if (array_keys($keys) !== $keys) { |
||||
682 | foreach ($keys as $key) { |
||||
683 | if ( ! is_numeric($key)) { |
||||
684 | return true; |
||||
685 | } |
||||
686 | } |
||||
687 | } |
||||
688 | |||||
689 | return false; |
||||
690 | } |
||||
691 | } |
||||
692 | |||||
693 | // ------------------------------------------------------------------------ |
||||
694 | |||||
695 | if ( ! function_exists('is_indexed_array')) { |
||||
696 | /** |
||||
697 | * is_indexed_array |
||||
698 | * |
||||
699 | * Check if an array has a numeric index. |
||||
700 | * |
||||
701 | * @param array $array |
||||
702 | * |
||||
703 | * @return bool |
||||
704 | */ |
||||
705 | function is_indexed_array(array $array) |
||||
706 | { |
||||
707 | if ($array == []) { |
||||
708 | return true; |
||||
709 | } |
||||
710 | |||||
711 | return ! is_associative_array($array); |
||||
712 | } |
||||
713 | } |
||||
714 | |||||
715 | // ------------------------------------------------------------------------ |
||||
716 | |||||
717 | if ( ! function_exists('error_code_string')) { |
||||
718 | /** |
||||
719 | * error_code_string |
||||
720 | * |
||||
721 | * @param int $code |
||||
722 | * |
||||
723 | * @return string |
||||
724 | */ |
||||
725 | function error_code_string($code) |
||||
726 | { |
||||
727 | static $errors = []; |
||||
728 | |||||
729 | if (empty($errors)) { |
||||
730 | $errors = require(str_replace('Helpers', 'Config', __DIR__) . DIRECTORY_SEPARATOR . 'Errors.php'); |
||||
731 | } |
||||
732 | |||||
733 | $string = null; |
||||
734 | |||||
735 | if (isset($errors[ $code ])) { |
||||
736 | $string = $errors[ $code ]; |
||||
737 | } |
||||
738 | |||||
739 | return strtoupper($string); |
||||
740 | } |
||||
741 | } |
If you suppress an error, we recommend checking for the error condition explicitly: