Skip to content

Commit 67f7eb9

Browse files
committed
Fix upcoming deprecation of mt_rand on newer php versions
* provide a workaround function in code/Common like is_php does * closes bcit-ci/CodeIgniter#6275 * related to pocketarc/codeigniter#3 (comment)
1 parent c844c2e commit 67f7eb9

File tree

7 files changed

+42
-10
lines changed

7 files changed

+42
-10
lines changed

appsys/core/Common.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,38 @@ function is_php($version = '5.0.0')
5757

5858
// ------------------------------------------------------------------------
5959

60+
/**
61+
* implements internally the rand specific function based on PHP version, used internally
62+
*
63+
* @access public
64+
* @param int or 0
65+
* @param int or mt_getrandmax()
66+
* @return int
67+
*/
68+
if ( ! function_exists('php_rand'))
69+
{
70+
/**
71+
* implements internally the rand specific function based on PHP version, used internally
72+
*
73+
* @param int or 0
74+
* @param int or mt_getrandmax()
75+
* @return int
76+
*/
77+
function php_rand($min = NULL, $max = NULL)
78+
{
79+
if(!is_int($min) $min = 0;
80+
if(!is_int($max) $max = mt_getrandmax();
81+
82+
if( is_php('7.1.99') ) return random_int($min, $max);
83+
84+
if (!function_exists('mcrypt_create_iv')) trigger_error( 'mcrypt must be loaded for random_int to work', E_USER_WARNING );
85+
86+
return mt_rand($min, $max);
87+
}
88+
}
89+
90+
// ------------------------------------------------------------------------
91+
6092
/**
6193
* Tests for file writability
6294
*

appsys/core/Security.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ public function xss_hash()
488488
if ($this->_xss_hash == '')
489489
{
490490
mt_srand();
491-
$this->_xss_hash = md5(time() + mt_rand(0, 1999999999));
491+
$this->_xss_hash = md5(time() + php_rand(0, 1999999999));
492492
}
493493

494494
return $this->_xss_hash;

appsys/helpers/captcha_helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ function create_captcha($data = '', $img_path = '', $img_url = '', $font_path =
187187
{
188188
for ($i = 0; $i < $word_length; $i++)
189189
{
190-
$word .= $pool[mt_rand(0, $rand_max)];
190+
$word .= $pool[php_rand(0, $rand_max)];
191191
}
192192
}
193193
elseif ( ! is_string($word))

appsys/helpers/string_helper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ function random_string($type = 'alnum', $len = 8)
197197
{
198198
switch($type)
199199
{
200-
case 'basic' : return mt_rand();
200+
case 'basic' : return php_rand();
201201
break;
202202
case 'alnum' :
203203
case 'numeric' :
@@ -219,22 +219,22 @@ function random_string($type = 'alnum', $len = 8)
219219
$str = '';
220220
for ($i=0; $i < $len; $i++)
221221
{
222-
$str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
222+
$str .= substr($pool, php_rand(0, strlen($pool) -1), 1);
223223
}
224224
return $str;
225225
break;
226226
case 'unique' :
227227
case 'md5' :
228228

229-
return md5(uniqid(mt_rand()));
229+
return md5(uniqid(php_rand()));
230230
break;
231231
case 'encrypt' :
232232
case 'sha1' :
233233

234234
$CI =& get_instance();
235235
$CI->load->helper('security');
236236

237-
return do_hash(uniqid(mt_rand(), TRUE), 'sha1');
237+
return do_hash(uniqid(php_rand(), TRUE), 'sha1');
238238
break;
239239
}
240240
}

appsys/libraries/Encrypt.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ function _xor_encode($string, $key)
247247
$rand = '';
248248
while (strlen($rand) < 32)
249249
{
250-
$rand .= mt_rand(0, mt_getrandmax());
250+
$rand .= php_rand(0, mt_getrandmax());
251251
}
252252

253253
$rand = $this->hash($rand);

appsys/libraries/Session.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ function sess_create()
328328
$sessid = '';
329329
while (strlen($sessid) < 32)
330330
{
331-
$sessid .= mt_rand(0, mt_getrandmax());
331+
$sessid .= php_rand(0, mt_getrandmax());
332332
}
333333

334334
// To make the session ID even more secure we'll combine it with the user's IP
@@ -375,7 +375,7 @@ function sess_update()
375375
$new_sessid = '';
376376
while (strlen($new_sessid) < 32)
377377
{
378-
$new_sessid .= mt_rand(0, mt_getrandmax());
378+
$new_sessid .= php_rand(0, mt_getrandmax());
379379
}
380380

381381
// To make the session ID even more secure we'll combine it with the user's IP

appsys/libraries/Upload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ public function set_filename($path, $filename)
397397
if ($this->encrypt_name == TRUE)
398398
{
399399
mt_srand();
400-
$filename = md5(uniqid(mt_rand())).$this->file_ext;
400+
$filename = md5(uniqid(php_rand())).$this->file_ext;
401401
}
402402

403403
if ( ! file_exists($path.$filename)) // if ($this->overwrite === TRUE OR ! file_exists($path.$filename)) // permite que asigne nombre nuevo no importa si esta permitido

0 commit comments

Comments
 (0)