diff options
author | Derek Allard <derek.allard@ellislab.com> | 2010-01-23 21:03:27 +0100 |
---|---|---|
committer | Derek Allard <derek.allard@ellislab.com> | 2010-01-23 21:03:27 +0100 |
commit | 472dd21a7bc60147afff7bf100bbb6c9f339d0fc (patch) | |
tree | 48c834abc062a6d4fd2b0b5cb5814f07a75e0247 | |
parent | 9e94ee10520a9d95eab1b1a0e9c9b27d39b7214a (diff) |
Added alpha, and sha1 string types to random_string() in the String Helper.
-rw-r--r-- | system/helpers/string_helper.php | 25 | ||||
-rw-r--r-- | user_guide/changelog.html | 1 | ||||
-rw-r--r-- | user_guide/helpers/string_helper.html | 14 |
3 files changed, 29 insertions, 11 deletions
diff --git a/system/helpers/string_helper.php b/system/helpers/string_helper.php index 67cbd0eb6..4767f0b5f 100644 --- a/system/helpers/string_helper.php +++ b/system/helpers/string_helper.php @@ -186,22 +186,27 @@ if ( ! function_exists('reduce_multiples')) * Useful for generating passwords or hashes. * * @access public - * @param string type of random string. Options: alunum, numeric, nozero, unique + * @param string type of random string. basic, alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1 * @param integer number of characters * @return string - */ + */ if ( ! function_exists('random_string')) -{ +{ function random_string($type = 'alnum', $len = 8) { switch($type) { + case 'basic' : return mt_rand(); + break; case 'alnum' : case 'numeric' : case 'nozero' : + case 'alpha' : switch ($type) { + case 'alpha' : $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + break; case 'alnum' : $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; break; case 'numeric' : $pool = '0123456789'; @@ -217,12 +222,22 @@ if ( ! function_exists('random_string')) } return $str; break; - case 'unique' : return md5(uniqid(mt_rand())); + case 'unique' : + case 'md5' : + + return md5(uniqid(mt_rand())); + break; + case 'encrypt' : + case 'sha1' : + + $CI =& get_instance(); + $CI->load->helper('security'); + + return do_hash(uniqid(mt_rand(), TRUE), 'sha1'); break; } } } - // ------------------------------------------------------------------------ /** diff --git a/user_guide/changelog.html b/user_guide/changelog.html index b8601ab3a..4214a0de5 100644 --- a/user_guide/changelog.html +++ b/user_guide/changelog.html @@ -86,6 +86,7 @@ SVN Revision: </p> <li>Modified the second parameter of <kbd>directory_map()</kbd> in the <a href="helpers/directory_helper.html">Directory Helper</a> to accept an integer to specify recursion depth.</li> <li>Modified <kbd>delete_files()</kbd> in the <a href="helpers/file_helper.html">File Helper</a> to return FALSE on failure.</li> <li>Added an optional second parameter to <kbd>byte_format()</kbd> in the <a href="helpers/number_helper.html">Number Helper</a> to allow for decimal precision.</li> + <li>Added alpha, and sha1 string types to <kbd>random_string()</kbd> in the <a href="helpers/string_helper.html">String Helper</a>.</li> </ul> </li> <li>Other Changes diff --git a/user_guide/helpers/string_helper.html b/user_guide/helpers/string_helper.html index 36fa40f98..b799390c3 100644 --- a/user_guide/helpers/string_helper.html +++ b/user_guide/helpers/string_helper.html @@ -74,13 +74,15 @@ String Helper <p>The first parameter specifies the type of string, the second parameter specifies the length. The following choices are available:</p> - + alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1 <ul> -<li><strong>alnum</strong>: Alpha-numeric string with lower and uppercase characters.</li> -<li><strong>numeric</strong>: Numeric string.</li> -<li><strong>nozero</strong>: Numeric string with no zeros.</li> -<li><strong>unique</strong>: Encrypted with MD5 and uniqid(). Note: The length parameter is not available for this type. -Returns a fixed length 32 character string.</li> + <li><strong>alpha</strong>: A string with lower and uppercase letters only.</li> + <li><strong>alnum</strong>: Alpha-numeric string with lower and uppercase characters.</li> + <li><strong>numeric</strong>: Numeric string.</li> + <li><strong>nozero</strong>: Numeric string with no zeros.</li> + <li><strong>unique</strong>: Encrypted with MD5 and uniqid(). Note: The length parameter is not available for this type. + Returns a fixed length 32 character string.</li> + <li><strong>sha1</strong>: An encrypted random number based on <kbd>do_hash()</kbd> from the <a href="security_helper.html">security helper</a>.</li> </ul> <p>Usage example:</p> |