PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.10.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.10.0
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / vendor / wpfluent / framework / src / WPFluent / Support / Serializer.php

Serializer.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.10.0, at vendor/wpfluent/framework/src/WPFluent/Support/Serializer.php

125 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\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