| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Framework\Support; |
| 4 |
|
| 5 |
use RuntimeException; |
| 6 |
use InvalidArgumentException; |
| 7 |
|
| 8 |
class Serializer |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Serialize data into the specified format. |
| 12 |
* |
| 13 |
* @param mixed $data Data to serialize. |
| 14 |
* @param string $format Serialization format ('json' or 'php'). |
| 15 |
* @param int|null $jsonOptions Options for JSON encoding |
| 16 |
* (default: JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES). |
| 17 |
* @return string Serialized data. |
| 18 |
* @throws InvalidArgumentException If the format is unsupported. |
| 19 |
*/ |
| 20 |
public static function serialize($data, $format = 'json', $jsonOptions = null) |
| 21 |
{ |
| 22 |
$jsonOptions ??= JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES; |
| 23 |
|
| 24 |
if ($format === 'json') { |
| 25 |
return json_encode($data, $jsonOptions); |
| 26 |
} elseif ($format === 'php') { |
| 27 |
if (static::isSerialized($data)) { |
| 28 |
return $data; |
| 29 |
} |
| 30 |
return serialize($data); |
| 31 |
} |
| 32 |
|
| 33 |
throw new InvalidArgumentException("Unsupported format: {$format}"); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Deserialize data from JSON or PHP serialized format. |
| 38 |
* |
| 39 |
* @param string $data The serialized data. |
| 40 |
* @param mixed $option The option: |
| 41 |
* |
| 42 |
* - For JSON, true/1 (decode as array), |
| 43 |
* false/0 (decode as object), default false. |
| 44 |
* |
| 45 |
* - For PHP serialized data, an array of allowed |
| 46 |
* classes or true/false to allow/disallow all classes. |
| 47 |
* |
| 48 |
* @return mixed The deserialized data. |
| 49 |
* @throws RuntimeException If the data cannot be deserialized. |
| 50 |
*/ |
| 51 |
public static function deserialize($data, $option = false) |
| 52 |
{ |
| 53 |
if (static::isJson($data)) { |
| 54 |
$decoded = json_decode($data, $option); |
| 55 |
|
| 56 |
if (json_last_error() !== JSON_ERROR_NONE) { |
| 57 |
throw new RuntimeException( |
| 58 |
'Invalid JSON: ' . json_last_error_msg() |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
return $decoded; |
| 63 |
} |
| 64 |
|
| 65 |
if (static::isSerialized($data)) { |
| 66 |
set_error_handler(function ($errno, $errstr) { |
| 67 |
if ( |
| 68 |
$errno === E_WARNING && strpos( |
| 69 |
$errstr, 'unserialize(): Error at offset' |
| 70 |
) !== false |
| 71 |
) { |
| 72 |
throw new RuntimeException( |
| 73 |
'Invalid serialized data or potential security risk.' |
| 74 |
); |
| 75 |
} |
| 76 |
}); |
| 77 |
|
| 78 |
try { |
| 79 |
$result = unserialize($data, [ |
| 80 |
'allowed_classes' => $option |
| 81 |
]); |
| 82 |
|
| 83 |
if ($result === false && $data !== serialize(false)) { |
| 84 |
throw new RuntimeException( |
| 85 |
'Invalid serialized data or potential security risk.' |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
return $result; |
| 90 |
} catch (RuntimeException $e) { |
| 91 |
throw $e; |
| 92 |
} finally { |
| 93 |
restore_error_handler(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
throw new RuntimeException( |
| 98 |
'Invalid data format: Neither JSON nor serialized PHP data.' |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Check if the data is a valid JSON string. |
| 104 |
* |
| 105 |
* @param string $data Data to check. |
| 106 |
* @return bool True if the data is valid JSON. |
| 107 |
*/ |
| 108 |
public static function isJson($data) |
| 109 |
{ |
| 110 |
json_decode($data); |
| 111 |
return (json_last_error() === JSON_ERROR_NONE); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Check if the data is a serialized PHP string. |
| 116 |
* |
| 117 |
* @param string $data Data to check. |
| 118 |
* @return bool True if the data is serialized. |
| 119 |
*/ |
| 120 |
public static function isSerialized($data) |
| 121 |
{ |
| 122 |
return is_serialized($data); |
| 123 |
} |
| 124 |
} |
| 125 |
|