| 1 |
<?php |
| 2 |
namespace ILJ\Helper; |
| 3 |
|
| 4 |
use function ILJ\ilj_try_include_file; |
| 5 |
|
| 6 |
if (!class_exists('Brumann\Polyfill\DisallowedClassesSubstitutor')) ilj_try_include_file('vendor/brumann/polyfill-unserialize/src/DisallowedClassesSubstitutor.php', 'require_once'); |
| 7 |
if (!class_exists('Brumann\Polyfill\Unserialize')) ilj_try_include_file('vendor/brumann/polyfill-unserialize/src/Unserialize.php', 'require_once'); |
| 8 |
|
| 9 |
/** |
| 10 |
* Loader |
| 11 |
* |
| 12 |
* Class for miscellaneous methods. |
| 13 |
* |
| 14 |
* @package ILJ\Helper |
| 15 |
*/ |
| 16 |
final class Misc { |
| 17 |
|
| 18 |
/** |
| 19 |
* Unserializes data only if it was serialized. |
| 20 |
* |
| 21 |
* @param string $data Data that might be unserialized. |
| 22 |
* @return mixed Unserialized data can be any type. |
| 23 |
*/ |
| 24 |
public static function maybe_unserialize($data) { |
| 25 |
if (is_serialized($data)) { // Don't attempt to unserialize data that wasn't serialized going in. |
| 26 |
return self::unserialize(trim($data), false); |
| 27 |
} |
| 28 |
return $data; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Unserialize data while maintaining compatibility across PHP versions due to different number of arguments required by PHP's "unserialize" function |
| 33 |
* |
| 34 |
* @param string $serialized_data Data to be unserialized, should be one that is already serialized |
| 35 |
* @param boolean|array $allowed_classes Either an array of class names which should be accepted, false to accept no classes, or true to accept all classes |
| 36 |
* @param integer $max_depth The maximum depth of structures permitted during unserialization, and is intended to prevent stack overflows |
| 37 |
* @return mixed Unserialized data can be any of types (integer, float, boolean, string, array or object) |
| 38 |
*/ |
| 39 |
public static function unserialize($serialized_data, $allowed_classes = false, $max_depth = 0) { |
| 40 |
if (version_compare(PHP_VERSION, '5.2', '<=')) { |
| 41 |
$result = unserialize($serialized_data); // For PHP 5.2 users, the search-replace feature has been removed, meaning that any input provided in this context will not undergo search-replace processing |
| 42 |
} else { |
| 43 |
$result = call_user_func(array('Brumann\Polyfill\Unserialize', 'unserialize'), $serialized_data, array('allowed_classes' => $allowed_classes, 'max_depth' => $max_depth)); |
| 44 |
} |
| 45 |
return $result; |
| 46 |
} |
| 47 |
} |
| 48 |
|