| 1 |
<?php |
| 2 |
// @codingStandardsIgnoreFile |
| 3 |
/** |
| 4 |
* Array utilities. |
| 5 |
* |
| 6 |
* Copyright: © 2009-2011 |
| 7 |
* {@link http://websharks-inc.com/ WebSharks, Inc.} |
| 8 |
* (coded in the USA) |
| 9 |
* |
| 10 |
* Released under the terms of the GNU General Public License. |
| 11 |
* You should have received a copy of the GNU General Public License, |
| 12 |
* along with this software. In the main directory, see: /licensing/ |
| 13 |
* If not, see: {@link http://www.gnu.org/licenses/}. |
| 14 |
* |
| 15 |
* @package s2Member\Utilities |
| 16 |
* @since 3.5 |
| 17 |
*/ |
| 18 |
if(!defined('WPINC')) // MUST have WordPress. |
| 19 |
exit ("Do not access this file directly."); |
| 20 |
|
| 21 |
if (!class_exists ("c_ws_plugin__s2member_utils_arrays")) |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Array utilities. |
| 25 |
* |
| 26 |
* @package s2Member\Utilities |
| 27 |
* @since 3.5 |
| 28 |
*/ |
| 29 |
class c_ws_plugin__s2member_utils_arrays |
| 30 |
{ |
| 31 |
/** |
| 32 |
* Extends ``array_unique()`` to support multi-dimensional arrays. |
| 33 |
* |
| 34 |
* @package s2Member\Utilities |
| 35 |
* @since 3.5 |
| 36 |
* |
| 37 |
* @param array $array Expects an incoming array. |
| 38 |
* @return array Returns the ``$array`` after having reduced it to a unique set of values. |
| 39 |
*/ |
| 40 |
public static function array_unique ($array = FALSE) |
| 41 |
{ |
| 42 |
$array = (array)$array; |
| 43 |
|
| 44 |
foreach ($array as &$value) |
| 45 |
$value = serialize ($value); |
| 46 |
|
| 47 |
$array = array_unique ($array); |
| 48 |
|
| 49 |
foreach ($array as &$value) |
| 50 |
$value = unserialize ($value); |
| 51 |
|
| 52 |
return $array; |
| 53 |
} |
| 54 |
/** |
| 55 |
* Searches an array *(or even a multi-dimensional array)* using a regular expression match against array values. |
| 56 |
* |
| 57 |
* @package s2Member\Utilities |
| 58 |
* @since 3.5 |
| 59 |
* |
| 60 |
* @param string $regex A regular expression to look for inside the array. |
| 61 |
* @return bool True if the regular expression matched at least one value in the array, else false. |
| 62 |
*/ |
| 63 |
public static function regex_in_array($regex = FALSE, $array = FALSE) |
| 64 |
{ |
| 65 |
if (is_string ($regex) && strlen ($regex) && is_array($array)) |
| 66 |
{ |
| 67 |
foreach ($array as $value) |
| 68 |
{ |
| 69 |
if (is_array($value) /* Recursive function call? */) |
| 70 |
{ |
| 71 |
if (c_ws_plugin__s2member_utils_arrays::regex_in_array($regex, $value)) |
| 72 |
return true; |
| 73 |
} |
| 74 |
else if (is_string ($value) /* Must be a string. */) |
| 75 |
{ |
| 76 |
if (@preg_match ($regex, $value)) |
| 77 |
return true; |
| 78 |
} |
| 79 |
} |
| 80 |
return false; |
| 81 |
} |
| 82 |
else // False. |
| 83 |
return false; |
| 84 |
} |
| 85 |
/** |
| 86 |
* Searches an array *(or even a multi-dimensional array)* of regular expressions, to match against a string value. |
| 87 |
* |
| 88 |
* @package s2Member\Utilities |
| 89 |
* @since 3.5 |
| 90 |
* |
| 91 |
* @param string $string A string to test against. |
| 92 |
* @param array $array An array of regex patterns to match against ``$string``. |
| 93 |
* @return bool True if at least one regular expression in the ``$array`` matched ``$string``, else false. |
| 94 |
*/ |
| 95 |
public static function in_regex_array($string = FALSE, $array = FALSE) |
| 96 |
{ |
| 97 |
if (is_string ($string) && strlen ($string) && is_array($array)) |
| 98 |
{ |
| 99 |
foreach ($array as $value) |
| 100 |
{ |
| 101 |
if (is_array($value) /* Recursive function call. */) |
| 102 |
{ |
| 103 |
if (c_ws_plugin__s2member_utils_arrays::in_regex_array($string, $value)) |
| 104 |
return true; |
| 105 |
} |
| 106 |
else if (is_string ($value) /* Must be a string. */) |
| 107 |
{ |
| 108 |
if (@preg_match ($value, $string)) |
| 109 |
return true; |
| 110 |
} |
| 111 |
} |
| 112 |
return false; |
| 113 |
} |
| 114 |
else // False. |
| 115 |
return false; |
| 116 |
} |
| 117 |
/** |
| 118 |
* Removes all null values from an array *(or even a multi-dimensional array)*. |
| 119 |
* |
| 120 |
* @package s2Member\Utilities |
| 121 |
* @since 111101 |
| 122 |
* |
| 123 |
* @param array $array An input array. |
| 124 |
* @return array Returns the ``$array`` after having reduced its set of values. |
| 125 |
*/ |
| 126 |
public static function remove_nulls ($array = FALSE) |
| 127 |
{ |
| 128 |
$array = (array)$array; |
| 129 |
|
| 130 |
foreach ($array as $key => &$value) |
| 131 |
{ |
| 132 |
if (is_array($value) /* Recursive function call here. */) |
| 133 |
$value = c_ws_plugin__s2member_utils_arrays::remove_nulls ($value); |
| 134 |
|
| 135 |
else if (is_null /* Is it null? */ ($value)) |
| 136 |
unset($array[$key]); |
| 137 |
} |
| 138 |
return $array; |
| 139 |
} |
| 140 |
/** |
| 141 |
* Removes all 0-byte strings from an array *(or even a multi-dimensional array)*. |
| 142 |
* |
| 143 |
* @package s2Member\Utilities |
| 144 |
* @since 111216 |
| 145 |
* |
| 146 |
* @param array $array An input array. |
| 147 |
* @return array Returns the ``$array`` after having reduced its set of values. |
| 148 |
*/ |
| 149 |
public static function remove_0b_strings ($array = FALSE) |
| 150 |
{ |
| 151 |
$array = (array)$array; |
| 152 |
|
| 153 |
foreach ($array as $key => &$value) |
| 154 |
{ |
| 155 |
if (is_array($value) /* Recursive function call here. */) |
| 156 |
$value = c_ws_plugin__s2member_utils_arrays::remove_0b_strings ($value); |
| 157 |
|
| 158 |
else if (is_string ($value) && !strlen ($value)) |
| 159 |
unset($array[$key]); |
| 160 |
} |
| 161 |
return $array; |
| 162 |
} |
| 163 |
/** |
| 164 |
* Forces string values on each array value *(also supports multi-dimensional arrays)*. |
| 165 |
* |
| 166 |
* @package s2Member\Utilities |
| 167 |
* @since 111101 |
| 168 |
* |
| 169 |
* @param array $array An input array. |
| 170 |
* @return array Returns the ``$array`` after having forced it to set of string values. |
| 171 |
*/ |
| 172 |
public static function force_strings ($array = FALSE) |
| 173 |
{ |
| 174 |
$array = (array)$array; |
| 175 |
|
| 176 |
foreach ($array as &$value) |
| 177 |
{ |
| 178 |
if (is_array($value) /* Recursive function call here. */) |
| 179 |
$value = c_ws_plugin__s2member_utils_arrays::force_strings ($value); |
| 180 |
|
| 181 |
else if (!is_string ($value) /* String? */) |
| 182 |
$value = (string)$value; |
| 183 |
} |
| 184 |
return $array; |
| 185 |
} |
| 186 |
/** |
| 187 |
* Forces integer values on each array value *(also supports multi-dimensional arrays)*. |
| 188 |
* |
| 189 |
* @package s2Member\Utilities |
| 190 |
* @since 111101 |
| 191 |
* |
| 192 |
* @param array $array An input array. |
| 193 |
* @return array Returns the ``$array`` after having forced it to set of integer values. |
| 194 |
*/ |
| 195 |
public static function force_integers ($array = array()) |
| 196 |
{ |
| 197 |
$array = (array)$array; |
| 198 |
|
| 199 |
foreach ($array as &$value) |
| 200 |
{ |
| 201 |
if (is_array($value) /* Recursive function call here. */) |
| 202 |
$value = c_ws_plugin__s2member_utils_arrays::force_integers ($value); |
| 203 |
|
| 204 |
else if (!is_integer ($value) /* Integer? */) |
| 205 |
$value = (int)$value; |
| 206 |
} |
| 207 |
return $array; |
| 208 |
} |
| 209 |
/** |
| 210 |
* Sorts arrays *(also supports multi-dimensional arrays)* by key, low to high. |
| 211 |
* |
| 212 |
* @package s2Member\Utilities |
| 213 |
* @since 111205 |
| 214 |
* |
| 215 |
* @param array $array An input array. |
| 216 |
* @param int $flags Optional. Can be used to modify the sorting behavior. |
| 217 |
* See: {@link http://www.php.net/manual/en/function.ksort.php} |
| 218 |
* @return Unlike PHP's ``ksort()``, this function returns the array, and does NOT work on a reference. |
| 219 |
*/ |
| 220 |
public static function ksort_deep ($array = FALSE, $flags = SORT_REGULAR) |
| 221 |
{ |
| 222 |
$array = (array)$array; |
| 223 |
ksort /* Sort by key. */ ($array, $flags); |
| 224 |
|
| 225 |
foreach ($array as &$value) |
| 226 |
if (is_array($value) /* Recursive function call here. */) |
| 227 |
$value = c_ws_plugin__s2member_utils_arrays::ksort_deep ($value, $flags); |
| 228 |
|
| 229 |
return /* Now return the array. */ $array; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Like maybe_unserialize(), but skips objects. |
| 234 |
* |
| 235 |
* @package s2Member\Utilities |
| 236 |
* @since 250801 |
| 237 |
* |
| 238 |
* @param mixed $value The value to be unserialized. |
| 239 |
* @return mixed The unserialized value, or original if not serialized or blocked. |
| 240 |
*/ |
| 241 |
public static function maybe_unserialize($value) { |
| 242 |
if (!is_string($value) || !is_serialized($value)) { |
| 243 |
return $value; |
| 244 |
} |
| 245 |
|
| 246 |
$value = trim($value); |
| 247 |
|
| 248 |
if (version_compare(PHP_VERSION, '7.0.0', '>=')) { |
| 249 |
return @unserialize($value, ['allowed_classes' => false]); |
| 250 |
} |
| 251 |
|
| 252 |
if (strpos($value, 'O:') !== false) { |
| 253 |
do_action('ws_plugin__s2member_security_object_detected', $value); |
| 254 |
return null; |
| 255 |
} |
| 256 |
|
| 257 |
return @unserialize($value); |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|