From 5f9d23e24a9157d66eaa33d69db80f6df5321ee2 Mon Sep 17 00:00:00 2001 From: Brian Rogers Date: Fri, 20 May 2022 13:49:53 -0600 Subject: [PATCH] adding serialization methods and versions to docblocks in helpers --- src/helpers/global_functions.php | 114 +++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/src/helpers/global_functions.php b/src/helpers/global_functions.php index db6096b..4b795a6 100644 --- a/src/helpers/global_functions.php +++ b/src/helpers/global_functions.php @@ -5,6 +5,8 @@ if (! function_exists('snake2Title')) { * Convert a snake case string to a title with spaces * and every word capitalized. * + * @since 1.0.0 + * * @param string $stakeSlug A snake case string, commonly a slug * * @return string @@ -22,6 +24,8 @@ if (! function_exists('carbon')) { * It will attempt to find a timezone in the current * session but default to UTC. * + * @since 1.0.0 + * * @param string|null $timestring * * @return \Carbon\Carbon @@ -40,6 +44,8 @@ if (! function_exists('jddayofweek')) { /** * Returns the day of the week. Can return a string or an integer depending on the mode. * + * @since 1.0.0 + * * @param int|null $intDay * @param int $mode * @@ -59,10 +65,106 @@ if (! function_exists('jddayofweek')) { } } +if (! function_exists('is_serialized')) { + /** + * Check a value to find if it was serialized. + * + * @since 1.0.0 + * + * @param mixed $data + * @param bool $strict + * + * @return bool + */ + function is_serialized($data, bool $strict = true): bool + { + // 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) { + return false; + } + if (':' !== $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 (!$semicolon && !$brace) { + return false; + } + // But neither must be in the first X characters. + if ($semicolon && $semicolon < 3) { + return false; + } + if ($brace && $brace < 4) { + return false; + } + } + $token = $data[0]; + switch ($token) { + case 's': + if ($strict) { + if ('"' !== substr($data, -2, 1)) { + return false; + } + } elseif (!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; + } +} + +if (! function_exists('maybe_unserialize')) { + /** + * Unserialize data only if it was serialized. Will return + * an array if it was a serialized string, otherwise it + * will return whatever was passed into the function + * leaving it untouched. + * + * @since 1.0.0 + * + * @param mixed $data + * + * @return mixed + */ + function maybe_unserialize($data) + { + if (is_serialized($data)) { // Don't attempt to unserialize data that wasn't serialized going in. + return @unserialize(trim($data)); + } + return $data; + } +} + if (! function_exists('cel2Fah')) { /** * Convert from celsius to fahrenheit. * + * @since 1.0.0 + * * @param float|int|string $celsius * @param int $precision * @@ -78,6 +180,8 @@ if (! function_exists('fah2Cel')) { /** * Convert from fahrenheit to celsius. * + * @since 1.0.0 + * * @param float|int|string $fahrenheit * @param int $precision * @@ -93,6 +197,8 @@ if (! function_exists('meters2Miles')) { /** * Convert from meters to miles. * + * @since 1.0.0 + * * @param float|int|string $meters * @param int $precision * @@ -108,6 +214,8 @@ if (! function_exists('kilometers2Miles')) { /** * Convert from kilometers to meters. * + * @since 1.0.0 + * * @param float|int|string $kilometers * @param int $precision * @@ -123,6 +231,8 @@ if (! function_exists('m2Km')) { /** * Convert from meters to kilometers. * + * @since 1.0.0 + * * @param float|int|string $meters * @param int $precision * @@ -138,6 +248,8 @@ if (! function_exists('mm2Inches')) { /** * Convert from milimeters to inches. * + * @since 1.0.0 + * * @param float|int|string $milimeters * @param int $precision * @@ -153,6 +265,8 @@ if (! function_exists('pa2Mbar')) { /** * Convert from pascals to milibars. * + * @since 1.0.0 + * * @param float|int|string $pascals * @param int $precision *