Created
November 23, 2016 15:40
-
-
Save pentagonal/a78cf639c6d243de34719daecc7ad321 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Utility | |
{ | |
/* --------------------------------------------------------------------------------* | |
| Serialize Helper | | |
| | | |
| Custom From WordPress Core wp-includes/functions.php | | |
|---------------------------------------------------------------------------------| | |
*/ | |
/** | |
* Serialize data, if needed. @uses for ( uncompress serialize values ) | |
* | |
* @param mixed $data Data that might be serialized. | |
* @return mixed A scalar data | |
*/ | |
public static function maybeSerialize($data) | |
{ | |
if (is_array($data) || is_object($data)) { | |
return @serialize($data); | |
} | |
// Double serialization is required for backward compatibility. | |
if (static::isSerialized($data, false)) { | |
return serialize($data); | |
} | |
return $data; | |
} | |
/** | |
* Unserialize value only if it was serialized. | |
* | |
* @param string $original Maybe unserialized original, if is needed. | |
* @return mixed Unserialized data can be any type. | |
*/ | |
public static function maybeUnserialize($original) | |
{ | |
// don't attempt to unserialize data that wasn't serialized going in | |
if (static::isSerialized($original)) { | |
return @unserialize($original); | |
} | |
return $original; | |
} | |
/** | |
* Check value to find if it was serialized. | |
* If $data is not an string, then returned value will always be false. | |
* Serialized data is always a string. | |
* | |
* @param mixed $data Value to check to see if was serialized. | |
* @param bool $strict Optional. Whether to be strict about the end of the string. Defaults true. | |
* @return bool False if not serialized and true if it was. | |
*/ | |
public static function isSerialized($data, $strict = true) | |
{ | |
/* if it isn't a string, it isn't serialized | |
------------------------------------------- */ | |
if (! is_string($data)) { | |
return false; | |
} | |
$data = trim($data); | |
if ('N;' == $data) { | |
return true; | |
} | |
if (strlen($data) < 4 || ':' !== $data[1]) { | |
return false; | |
} | |
if ($strict) { | |
$lastc = substr($data, -1); | |
if (';' !== $lastc && '}' !== $lastc) { | |
return false; | |
} | |
} else { | |
$semicolon = strpos($data, ';'); | |
$brace = strpos($data, '}'); | |
// Either ; or } must exist. | |
if (false === $semicolon && false === $brace | |
|| false !== $semicolon && $semicolon < 3 | |
|| false !== $brace && $brace < 4 | |
) { | |
return false; | |
} | |
} | |
$token = $data[0]; | |
switch ($token) { | |
/** @noinspection PhpMissingBreakStatementInspection */ | |
case 's': | |
if ($strict) { | |
if ('"' !== substr($data, -2, 1)) { | |
return false; | |
} | |
} elseif (false === strpos($data, '"')) { | |
return false; | |
} | |
// or else fall through | |
case 'a': | |
case 'O': | |
return (bool) preg_match("/^{$token}:[0-9]+:/s", $data); | |
case 'b': | |
case 'i': | |
case 'd': | |
$end = $strict ? '$' : ''; | |
return (bool) preg_match("/^{$token}:[0-9.E-]+;$end/", $data); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment