]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Util/SerializeUtil.php
67451a9011ce343e00155c8e29155aea9cf50089
[friendica.git] / src / Core / Config / Util / SerializeUtil.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core\Config\Util;
23
24 /**
25  * Serialize utils
26  *
27  * Retrieved from https://github.com/WordPress/wordpress-develop/blob/6.1/src/wp-includes/functions.php
28  */
29 class SerializeUtil
30 {
31         public static function maybeSerialize($value): string
32         {
33                 if (is_array($value) || is_object($value)) {
34                         return serialize($value);
35                 }
36
37                 if (static::isSerialized($value, false)) {
38                         return serialize($value);
39                 }
40
41                 return $value;
42         }
43
44         public static function maybeUnserialize($value)
45         {
46                 if (static::isSerialized($value)) {
47                         return @unserialize(trim($value));
48                 }
49
50                 return $value;
51         }
52
53         /**
54          * Checks value to find if it was serialized.
55          *
56          * If $data is not a string, then returned value will always be false.
57          * Serialized data is always a string.
58          *
59          * @param mixed $data   Value to check to see if was serialized.
60          * @param bool  $strict Optional. Whether to be strict about the end of the string. Default true.
61          *
62          * @return bool False if not serialized and true if it was.
63          * @since 6.1.0 Added Enum support.
64          *
65          * @since 2.0.5
66          */
67         public static function isSerialized($data, bool $strict = true): bool
68         {
69                 // If it isn't a string, it isn't serialized.
70                 if (!is_string($data)) {
71                         return false;
72                 }
73                 $data = trim($data);
74                 if ('N;' === $data) {
75                         return true;
76                 }
77                 if (strlen($data) < 4) {
78                         return false;
79                 }
80                 if (':' !== $data[1]) {
81                         return false;
82                 }
83                 if ($strict) {
84                         $lastc = substr($data, -1);
85                         if (';' !== $lastc && '}' !== $lastc) {
86                                 return false;
87                         }
88                 } else {
89                         $semicolon = strpos($data, ';');
90                         $brace     = strpos($data, '}');
91                         // Either ; or } must exist.
92                         if (false === $semicolon && false === $brace) {
93                                 return false;
94                         }
95                         // But neither must be in the first X characters.
96                         if (false !== $semicolon && $semicolon < 3) {
97                                 return false;
98                         }
99                         if (false !== $brace && $brace < 4) {
100                                 return false;
101                         }
102                 }
103                 $token = $data[0];
104                 switch ($token) {
105                         case 's':
106                                 if ($strict) {
107                                         if ('"' !== substr($data, -2, 1)) {
108                                                 return false;
109                                         }
110                                 } elseif (false === strpos($data, '"')) {
111                                         return false;
112                                 }
113                         // Or else fall through.
114                         // no break
115                         case 'a':
116                         case 'O':
117                         case 'E':
118                                 return (bool)preg_match("/^{$token}:[0-9]+:/s", $data);
119                         case 'b':
120                         case 'i':
121                         case 'd':
122                                 $end = $strict ? '$' : '';
123                                 return (bool)preg_match("/^{$token}:[0-9.E+-]+;$end/", $data);
124                 }
125                 return false;
126         }
127 }