diff --git a/lib/deprecated.php b/lib/deprecated.php index 4fc4fbfe0..8c53e550b 100644 --- a/lib/deprecated.php +++ b/lib/deprecated.php @@ -188,3 +188,55 @@ function datediff($interval, $datefrom, $dateto, $using_timestamps = false) return $datediff; } + + + +/** + * Porting della funzione random_int() per rendere Mpdf retrocompatibile con PHP 5.6 + */ +if (!function_exists('random_int')) { + function random_int($min, $max) { + if (!function_exists('mcrypt_create_iv')) { + trigger_error( + 'mcrypt must be loaded for random_int to work', + E_USER_WARNING + ); + return null; + } + + if (!is_int($min) || !is_int($max)) { + trigger_error('$min and $max must be integer values', E_USER_NOTICE); + $min = (int)$min; + $max = (int)$max; + } + + if ($min > $max) { + trigger_error('$max can\'t be lesser than $min', E_USER_WARNING); + return null; + } + + $range = $counter = $max - $min; + $bits = 1; + + while ($counter >>= 1) { + ++$bits; + } + + $bytes = (int)max(ceil($bits/8), 1); + $bitmask = pow(2, $bits) - 1; + + if ($bitmask >= PHP_INT_MAX) { + $bitmask = PHP_INT_MAX; + } + + do { + $result = hexdec( + bin2hex( + mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM) + ) + ) & $bitmask; + } while ($result > $range); + + return $result + $min; + } +}