Skip to content

Commit

Permalink
Added polyfills for php7.4-8.1 (#2946)
Browse files Browse the repository at this point in the history
  • Loading branch information
sreichel committed Jan 16, 2023
1 parent c891fac commit 6ce1e15
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 112 deletions.
110 changes: 0 additions & 110 deletions app/code/core/Mage/Core/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,113 +327,3 @@ function is_dir_writeable($dir)
}
return false;
}

if (!function_exists('sys_get_temp_dir')) {
// Based on http://www.phpit.net/
// article/creating-zip-tar-archives-dynamically-php/2/
/**
* @return bool|string
*/
function sys_get_temp_dir()
{
// Try to get from environment variable
if (!empty($_ENV['TMP'])) {
return realpath($_ENV['TMP']);
} elseif (!empty($_ENV['TMPDIR'])) {
return realpath($_ENV['TMPDIR']);
} elseif (!empty($_ENV['TEMP'])) {
return realpath($_ENV['TEMP']);
} else {
// Try to use system's temporary directory
// as random name shouldn't exist
$temp_file = tempnam(md5(uniqid(rand(), true)), '');
if ($temp_file) {
$temp_dir = realpath(dirname($temp_file));
unlink($temp_file);
return $temp_dir;
} else {
return false;
}
}
}
}

if (!function_exists('hash_equals')) {
/**
* Compares two strings using the same time whether they're equal or not.
* A difference in length will leak
*
* @param string $known_string
* @param string $user_string
* @return bool Returns true when the two strings are equal, false otherwise.
*/
function hash_equals($known_string, $user_string)
{
$result = 0;

if (!is_string($known_string)) {
trigger_error("hash_equals(): Expected known_string to be a string", E_USER_WARNING);
return false;
}

if (!is_string($user_string)) {
trigger_error("hash_equals(): Expected user_string to be a string", E_USER_WARNING);
return false;
}

if (strlen($known_string) != strlen($user_string)) {
return false;
}

for ($i = 0; $i < strlen($known_string); $i++) {
$result |= (ord($known_string[$i]) ^ ord($user_string[$i]));
}

return $result === 0;
}
}

/**
* polyfill for PHP 8.0 function "str_contains"
*/
if (!function_exists('str_contains')) {
/**
* @param string $haystack
* @param string $needle
* @return bool
*/
function str_contains($haystack, $needle)
{
return $needle === '' || strpos($haystack, $needle) !== false;
}
}

/**
* polyfill for PHP 8.0 function "str_starts_with"
*/
if (!function_exists('str_starts_with')) {
/**
* @param string $haystack
* @param string $needle
* @return bool
*/
function str_starts_with($haystack, $needle)
{
return strncmp($haystack, $needle, \strlen($needle)) === 0;
}
}

/**
* polyfill for PHP 8.0 function "str_ends_with"
*/
if (!function_exists('str_ends_with')) {
/**
* @param string $haystack
* @param string $needle
* @return bool
*/
function str_ends_with($haystack, $needle)
{
return $needle === '' || ($haystack !== '' && substr_compare($haystack, $needle, -\strlen($needle)) === 0);
}
}
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
"pelago/emogrifier": "^7.0",
"phpseclib/mcrypt_compat": "^2.0.3",
"phpseclib/phpseclib": "^3.0.14",
"shardj/zf1-future": "^1.21"
"shardj/zf1-future": "^1.21",
"symfony/polyfill-php74": "^1.27",
"symfony/polyfill-php80": "^1.27",
"symfony/polyfill-php81": "^1.27"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^1.0.0",
Expand Down
82 changes: 81 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6ce1e15

Please sign in to comment.