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