ApplyFiltersTrait.php
2 months ago
ArrayableTrait.php
7 months ago
BatchSizeCalculateTrait.php
7 months ago
BearerTokenTrait.php
4 months ago
BenchmarkTrait.php
2 years ago
BooleanTransientTrait.php
5 years ago
DatabaseSearchReplaceTrait.php
2 weeks ago
DbRowsGeneratorTrait.php
3 years ago
DebugLogTrait.php
1 year ago
DeveloperTimerTrait.php
8 months ago
EndOfLinePlaceholderTrait.php
1 year ago
EventLoggerTrait.php
1 month ago
FileScanToCacheTrait.php
10 months ago
FormatTrait.php
11 months ago
HttpRequestTrait.php
8 months ago
HydrateTrait.php
1 year ago
I18nTrait.php
1 year ago
IpResolverTrait.php
10 months ago
MaintenanceTrait.php
5 months ago
MemoryExhaustTrait.php
2 years ago
MySQLRowsGeneratorTrait.php
7 months ago
NoticesTrait.php
1 day ago
PropertyConstructor.php
5 years ago
RenameTmpDirectoryTrait.php
5 months ago
ResourceTrait.php
4 months ago
RestRequestTrait.php
3 months ago
RestoreFileExclusionTrait.php
1 year ago
SerializeTrait.php
1 year ago
SetTimeLimitTrait.php
1 month ago
SlashTrait.php
1 year ago
SqlCommentTrait.php
3 months ago
TablePrefixValidator.php
3 months ago
UrlTrait.php
1 year ago
ValueGetterTrait.php
1 year ago
WindowsOsTrait.php
1 year ago
SerializeTrait.php
76 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Traits; |
| 4 | |
| 5 | trait SerializeTrait |
| 6 | { |
| 7 | /** |
| 8 | * @see https://developer.wordpress.org/reference/functions/is_serialized/ |
| 9 | * @return bool |
| 10 | */ |
| 11 | protected function isSerialized(string $data, bool $strict = true): bool |
| 12 | { |
| 13 | if (!is_string($data)) { |
| 14 | return false; |
| 15 | } |
| 16 | |
| 17 | $data = trim($data); |
| 18 | if ($data === 'N;') { |
| 19 | return true; |
| 20 | } |
| 21 | |
| 22 | if (strlen($data) < 4) { |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | if ($data[1] !== ':') { |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | if ($strict) { |
| 31 | $lastc = substr($data, -1); |
| 32 | if ($lastc !== ';' && $lastc !== '}') { |
| 33 | return false; |
| 34 | } |
| 35 | } else { |
| 36 | $semicolon = strpos($data, ';'); |
| 37 | $brace = strpos($data, '}'); |
| 38 | if ($semicolon === false && $brace === false) { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | if ($semicolon !== false && $semicolon < 3) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | if ($brace !== false && $brace < 4) { |
| 47 | return false; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | $token = $data[0]; |
| 52 | switch ($token) { |
| 53 | case 's': |
| 54 | if ($strict) { |
| 55 | if ('"' !== substr($data, -2, 1)) { |
| 56 | return false; |
| 57 | } |
| 58 | } elseif (function_exists('str_contains') && !str_contains($data, '"') || strpos($data, '"') === false) { |
| 59 | return false; |
| 60 | } |
| 61 | // Or else fall through. |
| 62 | case 'a': |
| 63 | case 'O': |
| 64 | case 'E': |
| 65 | return (bool) preg_match("/^{$token}:[0-9]+:/s", $data); |
| 66 | case 'b': |
| 67 | case 'i': |
| 68 | case 'd': |
| 69 | $end = $strict ? '$' : ''; |
| 70 | return (bool) preg_match("/^{$token}:[0-9.E+-]+;$end/", $data); |
| 71 | } |
| 72 | |
| 73 | return false; |
| 74 | } |
| 75 | } |
| 76 |