Index: branches/5.3.x/core/units/helpers/user_helper.php =================================================================== diff -u -N -r15483 -r15649 --- branches/5.3.x/core/units/helpers/user_helper.php (.../user_helper.php) (revision 15483) +++ branches/5.3.x/core/units/helpers/user_helper.php (.../user_helper.php) (revision 15649) @@ -1,6 +1,6 @@ Update() ? '' : 'restore_impossible'; } - } + + /** + * Generates random string + * + * @param int $length + * @param bool $special_chars + * @param bool $extra_special_chars + * @return string + * @access public + */ + public function generateRandomString($length = 12, $special_chars = true, $extra_special_chars = false) + { + $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; + + if ( $special_chars ) { + $chars .= '!@#$%^&*()'; + } + + if ( $extra_special_chars ) { + $chars .= '-_ []{}<>~`+=,.;:/?|'; + } + + $password = ''; + + for ($i = 0; $i < $length; $i++) { + $password .= substr($chars, $this->_generateRandomNumber(0, strlen($chars) - 1), 1); + } + + return $password; + } + + /** + * Generates a random number + * + * @param int $min Lower limit for the generated number (optional, default is 0) + * @param int $max Upper limit for the generated number (optional, default is 4294967295) + * @return int A random number between min and max + * @access protected + */ + protected function _generateRandomNumber($min = 0, $max = 0) + { + static $rnd_value = ''; + + // Reset $rnd_value after 14 uses + // 32(md5) + 40(sha1) + 40(sha1) / 8 = 14 random numbers from $rnd_value + if ( strlen($rnd_value) < 8 ) { + $random_seed = $this->Application->getDBCache('random_seed'); + $rnd_value = md5(uniqid(microtime() . mt_rand(), true) . $random_seed); + $rnd_value .= sha1($rnd_value); + $rnd_value .= sha1($rnd_value . $random_seed); + $random_seed = md5($random_seed . $rnd_value); + $this->Application->setDBCache('random_seed', $random_seed); + } + + // Take the first 8 digits for our value + $value = substr($rnd_value, 0, 8); + + // Strip the first eight, leaving the remainder for the next call to wp_rand(). + $rnd_value = substr($rnd_value, 8); + + $value = abs(hexdec($value)); + + // Reduce the value to be within the min - max range + // 4294967295 = 0xffffffff = max random number + if ( $max != 0 ) { + $value = $min + (($max - $min + 1) * ($value / (4294967295 + 1))); + } + + return abs(intval($value)); + } + } \ No newline at end of file