Changeset 3434874
- Timestamp:
- 01/08/2026 05:55:22 AM (3 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
scratch-win-giveaways-for-website-facebook/trunk/includes/compat/polyfills.php
r3434870 r3434874 3 3 * Polyfills for PHP 5.4-8.4 compatibility 4 4 * Provides backward-compatible functions for PHP 5.4-5.5 5 * 5 * 6 6 * @package Scratch & Win 7 7 * @since 2.9.3 … … 15 15 * hash_equals() polyfill for PHP < 5.6 16 16 * Timing attack safe string comparison 17 * 17 * 18 18 * @link https://www.php.net/manual/en/function.hash-equals.php 19 19 * @param string $known_string The string of known length to compare against … … 24 24 function hash_equals($known_string, $user_string) { 25 25 if (!is_string($known_string)) { 26 trigger_error('hash_equals(): Expected known_string to be a string', E_USER_WARNING); 27 return false; 28 } 29 26 return false; 27 } 28 30 29 if (!is_string($user_string)) { 31 trigger_error('hash_equals(): Expected user_string to be a string', E_USER_WARNING); 32 return false; 33 } 34 30 return false; 31 } 32 35 33 $known_len = strlen($known_string); 36 34 $user_len = strlen($user_string); 37 35 38 36 if ($known_len !== $user_len) { 39 37 return false; 40 38 } 41 39 42 40 $result = 0; 43 41 for ($i = 0; $i < $known_len; $i++) { 44 42 $result |= ord($known_string[$i]) ^ ord($user_string[$i]); 45 43 } 46 44 47 45 return $result === 0; 48 46 } … … 52 50 * Safe session start wrapper for PHP 5.4-8.4 53 51 * Properly checks session status and headers before starting session 54 * 52 * 55 53 * @return bool True if session started or already active, false otherwise 56 54 */ … … 61 59 return false; 62 60 } 63 61 64 62 // For PHP 5.4+ use session_status() 65 63 if (function_exists('session_status')) { … … 72 70 return @session_start(); 73 71 } 74 72 75 73 // Fallback for older PHP (pre-5.4, but we support 5.4+) 76 74 if (session_id()) { 77 75 return true; 78 76 } 79 77 80 78 return @session_start(); 81 79 } … … 85 83 * Safe JSON decode with error handling 86 84 * Returns default value if JSON is invalid 87 * 85 * 88 86 * @param string $json The JSON string to decode 89 87 * @param bool $assoc Return associative array when true … … 96 94 return $default; 97 95 } 98 96 99 97 $result = json_decode($json, $assoc); 100 98 101 99 if (json_last_error() !== JSON_ERROR_NONE) { 102 100 return $default; 103 101 } 104 102 105 103 return $result !== null ? $result : $default; 106 104 } … … 110 108 * Safe strtolower wrapper for PHP 8.0+ compatibility 111 109 * Handles null values without throwing errors 112 * 110 * 113 111 * @param mixed $string The string to convert to lowercase 114 112 * @return string Lowercase string or empty string if input is invalid … … 119 117 return ''; 120 118 } 121 119 122 120 return strtolower((string)$string); 123 121 } … … 126 124 /** 127 125 * Check if WC_VERSION constant is defined and optionally compare versions 128 * 126 * 129 127 * @param string $version Version to compare against (optional) 130 128 * @param string $operator Comparison operator (optional) … … 136 134 return false; 137 135 } 138 136 139 137 if ($version === null) { 140 138 return true; 141 139 } 142 140 143 141 return version_compare(WC_VERSION, $version, $operator); 144 142 } … … 152 150 * Safe strlen() wrapper for PHP 8.1+ compatibility 153 151 * PHP 8.1+: Passing null to strlen() is deprecated 154 * 152 * 155 153 * @param mixed $string The string to measure 156 154 * @return int Length of the string, or 0 if invalid … … 168 166 * Safe trim() wrapper for PHP 8.1+ compatibility 169 167 * PHP 8.1+: Passing null to trim() is deprecated 170 * 168 * 171 169 * @param mixed $string The string to trim 172 170 * @param string $characters Characters to trim (optional) … … 185 183 * Safe substr() wrapper for PHP 8.1+ compatibility 186 184 * PHP 8.1+: Passing null to substr() is deprecated 187 * 185 * 188 186 * @param mixed $string The input string 189 187 * @param int $offset Start position … … 206 204 * Safe str_replace() wrapper for PHP 8.1+ compatibility 207 205 * PHP 8.1+: Passing null to str_replace() is deprecated 208 * 206 * 209 207 * @param mixed $search Value to search for 210 208 * @param mixed $replace Value to replace with … … 235 233 * Safe strpos() wrapper for PHP 8.1+ compatibility 236 234 * PHP 8.1+: Passing null to strpos() is deprecated 237 * 235 * 238 236 * @param mixed $haystack The string to search in 239 237 * @param mixed $needle The string to search for … … 253 251 * Safe htmlspecialchars() wrapper for PHP 8.1+ compatibility 254 252 * PHP 8.1+: Passing null to htmlspecialchars() is deprecated 255 * 253 * 256 254 * @param mixed $string The string to convert 257 255 * @param int $flags Flags (optional) … … 275 273 * Safe stripslashes() wrapper for PHP 8.1+ compatibility 276 274 * PHP 8.1+: Passing null to stripslashes() is deprecated 277 * 275 * 278 276 * @param mixed $string The string to process 279 277 * @return string String with backslashes stripped … … 291 289 * Safe explode() wrapper for PHP 8.1+ compatibility 292 290 * PHP 8.1+: Passing null to explode() is deprecated 293 * 291 * 294 292 * @param string $separator The boundary string 295 293 * @param mixed $string The input string … … 314 312 * Also validates array parameter 315 313 * Supports both implode($glue, $array) and implode($array) syntax 316 * 314 * 317 315 * @param string|array $separator The boundary string or array (if single argument) 318 316 * @param mixed $array The array to join (optional) … … 326 324 $separator = ''; 327 325 } 328 326 329 327 if (!is_array($array)) { 330 328 return ''; … … 340 338 * Safe strtotime() wrapper for PHP 8.1+ compatibility 341 339 * PHP 8.1+: Passing null to strtotime() is deprecated 342 * 340 * 343 341 * @param mixed $datetime The datetime string 344 342 * @param int|null $baseTimestamp Base timestamp (optional) … … 360 358 * Safe preg_match() wrapper for PHP 8.1+ compatibility 361 359 * PHP 8.1+: Passing null to preg_match() is deprecated 362 * 360 * 363 361 * @param string $pattern The pattern to search for 364 362 * @param mixed $subject The input string … … 386 384 /** 387 385 * Polyfill for array_key_first() (PHP 7.3+) 388 * 386 * 389 387 * @param array $array Input array 390 388 * @return mixed|null First key or null if array is empty … … 401 399 /** 402 400 * Polyfill for array_key_last() (PHP 7.3+) 403 * 401 * 404 402 * @param array $array Input array 405 403 * @return mixed|null Last key or null if array is empty … … 417 415 * Polyfill for str_contains() (PHP 8.0+) 418 416 * Safe version that handles null parameters 419 * 417 * 420 418 * @param mixed $haystack The string to search in 421 419 * @param mixed $needle The string to search for … … 433 431 /** 434 432 * Polyfill for str_starts_with() (PHP 8.0+) 435 * 433 * 436 434 * @param mixed $haystack The string to check 437 435 * @param mixed $needle The substring to check for … … 451 449 /** 452 450 * Polyfill for str_ends_with() (PHP 8.0+) 453 * 451 * 454 452 * @param mixed $haystack The string to check 455 453 * @param mixed $needle The substring to check for
Note: See TracChangeset
for help on using the changeset viewer.