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
31 public static function maybeUnserialize($value)
33 if (static::isSerialized($value)) {
34 return @unserialize(trim($value));
41 * Checks value to find if it was serialized.
43 * If $data is not a string, then returned value will always be false.
44 * Serialized data is always a string.
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.
49 * @return bool False if not serialized and true if it was.
50 * @since 6.1.0 Added Enum support.
54 public static function isSerialized($data, bool $strict = true): bool
56 // If it isn't a string, it isn't serialized.
57 if (!is_string($data)) {
64 if (strlen($data) < 4) {
67 if (':' !== $data[1]) {
71 $lastc = substr($data, -1);
72 if (';' !== $lastc && '}' !== $lastc) {
76 $semicolon = strpos($data, ';');
77 $brace = strpos($data, '}');
78 // Either ; or } must exist.
79 if (false === $semicolon && false === $brace) {
82 // But neither must be in the first X characters.
83 if (false !== $semicolon && $semicolon < 3) {
86 if (false !== $brace && $brace < 4) {
94 if ('"' !== substr($data, -2, 1)) {
97 } elseif (false === strpos($data, '"')) {
100 // Or else fall through.
105 return (bool)preg_match("/^{$token}:[0-9]+:/s", $data);
109 $end = $strict ? '$' : '';
110 return (bool)preg_match("/^{$token}:[0-9.E+-]+;$end/", $data);