]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Util/SerializeUtil.php
remove maybeSerialize (always serialize)
[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 maybeUnserialize($value)
32         {
33                 if (static::isSerialized($value)) {
34                         return @unserialize(trim($value));
35                 }
36
37                 return $value;
38         }
39
40         /**
41          * Checks value to find if it was serialized.
42          *
43          * If $data is not a string, then returned value will always be false.
44          * Serialized data is always a string.
45          *
46          * @param mixed $data   Value to check to see if was serialized.
47          * @param bool  $strict Optional. Whether to be strict about the end of the string. Default true.
48          *
49          * @return bool False if not serialized and true if it was.
50          * @since 6.1.0 Added Enum support.
51          *
52          * @since 2.0.5
53          */
54         public static function isSerialized($data, bool $strict = true): bool
55         {
56                 // If it isn't a string, it isn't serialized.
57                 if (!is_string($data)) {
58                         return false;
59                 }
60                 $data = trim($data);
61                 if ('N;' === $data) {
62                         return true;
63                 }
64                 if (strlen($data) < 4) {
65                         return false;
66                 }
67                 if (':' !== $data[1]) {
68                         return false;
69                 }
70                 if ($strict) {
71                         $lastc = substr($data, -1);
72                         if (';' !== $lastc && '}' !== $lastc) {
73                                 return false;
74                         }
75                 } else {
76                         $semicolon = strpos($data, ';');
77                         $brace     = strpos($data, '}');
78                         // Either ; or } must exist.
79                         if (false === $semicolon && false === $brace) {
80                                 return false;
81                         }
82                         // But neither must be in the first X characters.
83                         if (false !== $semicolon && $semicolon < 3) {
84                                 return false;
85                         }
86                         if (false !== $brace && $brace < 4) {
87                                 return false;
88                         }
89                 }
90                 $token = $data[0];
91                 switch ($token) {
92                         case 's':
93                                 if ($strict) {
94                                         if ('"' !== substr($data, -2, 1)) {
95                                                 return false;
96                                         }
97                                 } elseif (false === strpos($data, '"')) {
98                                         return false;
99                                 }
100                         // Or else fall through.
101                         // no break
102                         case 'a':
103                         case 'O':
104                         case 'E':
105                                 return (bool)preg_match("/^{$token}:[0-9]+:/s", $data);
106                         case 'b':
107                         case 'i':
108                         case 'd':
109                                 $end = $strict ? '$' : '';
110                                 return (bool)preg_match("/^{$token}:[0-9.E+-]+;$end/", $data);
111                 }
112                 return false;
113         }
114 }