3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Core\Config\Util;
27 * Retrieved from https://github.com/WordPress/wordpress-develop/blob/6.1/src/wp-includes/functions.php
32 * Checks if the value needs to get unserialized and returns the unserialized value
34 * @param mixed $value A possibly serialized value
36 * @return mixed The unserialized value
38 public static function maybeUnserialize($value)
40 // This checks for possible multiple serialized values
41 while (static::isSerialized($value)) {
43 $value = @unserialize($value);
45 // If there's no change after the unserialize call, break the loop (avoid endless loops)
46 if ($oldValue === $value) {
55 * Checks value to find if it was serialized.
57 * If $data is not a string, then returned value will always be false.
58 * Serialized data is always a string.
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.
63 * @return bool False if not serialized and true if it was.
64 * @since 6.1.0 Added Enum support.
68 public static function isSerialized($data, bool $strict = true): bool
70 // If it isn't a string, it isn't serialized.
71 if (!is_string($data)) {
78 if (strlen($data) < 4) {
81 if (':' !== $data[1]) {
85 $lastc = substr($data, -1);
86 if (';' !== $lastc && '}' !== $lastc) {
90 $semicolon = strpos($data, ';');
91 $brace = strpos($data, '}');
92 // Either ; or } must exist.
93 if (false === $semicolon && false === $brace) {
96 // But neither must be in the first X characters.
97 if (false !== $semicolon && $semicolon < 3) {
100 if (false !== $brace && $brace < 4) {
108 if ('"' !== substr($data, -2, 1)) {
111 } elseif (false === strpos($data, '"')) {
114 // Or else fall through.
119 return (bool)preg_match("/^{$token}:[0-9]+:/s", $data);
123 $end = $strict ? '$' : '';
124 return (bool)preg_match("/^{$token}:[0-9.E+-]+;$end/", $data);