Skip to content

Commit

Permalink
replace mt_srand() by \Random\Engine\Mt19937 calls for php >= 8.2
Browse files Browse the repository at this point in the history
These functions will be deprecated in PHP 8.3
See https://wiki.php.net/rfc/deprecations_php_8_3 which gives an example
of implementation for newer version of php
  • Loading branch information
tenzap committed Sep 24, 2023
1 parent 6179781 commit 181465c
Showing 1 changed file with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,31 @@
* @link http://code.google.com/p/jaxl
*/

// Based on: https://3v4l.org/B18he from https://wiki.php.net/rfc/deprecations_php_8_3
function my_mt_srand($seed = null) {
if (version_compare(PHP_VERSION, '8.2.0') >= 0) {
$GLOBALS['my_mt_rand'] = new \Random\Randomizer(new \Random\Engine\Mt19937($seed));
} else {
mt_srand($seed);
}
}

function my_mt_rand($min = null, $max = null) {
if (version_compare(PHP_VERSION, '8.2.0') >= 0) {
if (!isset($GLOBALS['my_mt_rand'])) {
$GLOBALS['my_mt_rand'] = new \Random\Randomizer(new \Random\Engine\Mt19937());
}

if ($min === null && $max === null) {
return $GLOBALS['my_mt_rand']->nextInt();
}

return $GLOBALS['my_mt_rand']->getInt($min, $max);
} else {
return mt_rand($min, $max);
}
}

/**
* Jaxl Utility Class
*/
Expand Down Expand Up @@ -128,9 +153,9 @@ public static function implodeData($data) {

public static function generateNonce() {
$str = '';
mt_srand((double) microtime()*10000000);
my_mt_srand((double) microtime()*10000000);
for($i=0; $i<32; $i++)
$str .= chr(mt_rand(0, 255));
$str .= chr(my_mt_rand(0, 255));
return $str;
}

Expand Down

0 comments on commit 181465c

Please sign in to comment.