/password/class.chip_password_generator.php
PHP | 347 lines | 161 code | 49 blank | 137 comment | 16 complexity | 81a90fa2a7181f02ac38984eb99a9153 MD5 | raw file
- <?php
- /*
- |-----------------
- | Author: Life.Object
- | E-Mail: life.object@gmail.com
- | Website: http://www.tutorialchip.com/
- | Help: http://www.tutorialchip.com/php-password-generator-class/
- | Version: 1.0
- | Released: December 02, 2010
- | Updated: December 02, 2010
- |------------------
- */
-
- class chip_password_generator {
-
- /*
- |---------------------------
- | Properties
- |---------------------------
- */
-
- private $args = array(
- 'length' => 8,
- 'alpha_upper_include' => TRUE,
- 'alpha_lower_include' => TRUE,
- 'number_include' => TRUE,
- 'symbol_include' => TRUE,
- );
-
- private $alpha_upper = array( "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
- private $alpha_lower = array( "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
- private $number = array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 );
- private $symbol = array( "-", "_", "^", "~", "@", "&", "|", "=", "+", ";", "!", ",", "(", ")", "{", "}", "[", "]", ".", "?", "%", "*", "#" );
-
- private $translation = array(
- 'a'=>'Alpha',
- 'b'=>'Bravo',
- 'c'=>'Charlie',
- 'd'=>'Delta',
- 'e'=>'Echo',
- 'f'=>'Foxtrot',
- 'g'=>'Golf',
- 'h'=>'Hotel',
- 'i'=>'India',
- 'j'=>'Juliet',
- 'k'=>'Kilo',
- 'l'=>'Lima',
- 'm'=>'Mike',
- 'n'=>'November',
- 'o'=>'Oscar',
- 'p'=>'Papa',
- 'q'=>'Quebec',
- 'r'=>'Romeo',
- 's'=>'Sierra',
- 't'=>'Tango',
- 'u'=>'Uniform',
- 'v'=>'Victor',
- 'w'=>'Whiskey',
- 'x'=>'X-ray',
- 'y'=>'Yankee',
- 'z'=>'Zulu',
- 0=>'Zero',
- 1=>'One',
- 2=>'Two',
- 3=>'Three',
- 4=>'Four',
- 5=>'Five',
- 6=>'Six',
- 7=>'Seven',
- 8=>'Eight',
- 9=>'Nine',
- "-"=>'Dash',
- "_"=>'Underscore',
- "^"=>'Carat',
- "~"=>'Tilda',
- "@"=>'At Sign',
- "&"=>'Ampersand',
- "|"=>'Pipe',
- "="=>'Equals',
- "+"=>'Plus',
- ";"=>'Semicolon',
- "!"=>'Exclamation mark',
- ","=>'Comma',
- "("=>'Left Paren',
- ")"=>'Right Paren',
- "{"=>'Left Curly Brace',
- "}"=>'Right Curly Brace',
- "["=>'Left Square Bracket',
- "]"=>'Right Square Bracket',
- "."=>'Period',
- "?"=>'Question Mark',
- "%"=>'Percentage Sign',
- "*"=>'Asterix',
- "#"=>'Hash Mark'
-
- );
- /*
- |---------------------------
- | Constructor
- |
- | @public
- | @param array $args
- |
- |---------------------------
- */
-
- public function __construct( $args = array() ) {
-
- $this->set_args( $args );
-
- }
-
- public function speakable($password){
- $letters = str_split ($password);
- $output="";
- foreach ($letters as $letter){
- if (ctype_alnum($letter) && !is_numeric($letter) && $this->is_upper($letter)) {
- $output.='UPPER CASE '. strtoupper($letter). ' AS IN '.strtoupper($this->translation[strtolower($letter)]);
- } elseif (ctype_alnum($letter) && !is_numeric($letter) && !$this->is_upper($letter)){
- $output.='Lower case '.$letter.' as in '.strtolower($this->translation[strtolower($letter)]);
- } elseif (is_numeric($letter)) {
- $output.='The number '.$letter .' ('.$this->translation[strtolower($letter)].') ';
- }else{
- $output.='The symbol '.$letter .' ('.$this->translation[strtolower($letter)].') ';
- }
- $output.= '<br>';
- }
- $output = substr($output, 0, -4);
- return $output;
- }
-
-
- function is_upper($str) {
- $chr = mb_substr ($str, 0, 1, "UTF-8");
- return mb_strtolower($chr, "UTF-8") != $chr;
- }
-
- /*
- |---------------------------
- | Print variable in readable format
- |
- | @public
- | @param string|array|object $var
- |
- |---------------------------
- */
-
- public function chip_print( $var ) {
-
- echo "<pre>";
- print_r($var);
- echo "</pre>";
-
- }
-
- /*
- |---------------------------
- | Update default arguments
- | It will update default array of class i.e $args
- |
- | @private
- | @param array $args - input arguments
- | @param array $defatuls - default arguments
- | @return array
- |
- |---------------------------
- */
-
- private function chip_parse_args( $args = array(), $defaults = array() ) {
- return array_merge( $defaults, $args );
- }
-
- /*
- |---------------------------
- | Set default arguments
- | It will set default array of class i.e $args
- |
- | @private
- | @param array $args
- | @return 0
- |
- |---------------------------
- */
-
- private function set_args( $args = array() ) {
-
- $defaults = $this->get_args();
- $args = $this->chip_parse_args( $args, $defaults );
- $this->args = $args;
- }
-
- /*
- |---------------------------
- | Get default arguments
- | It will get default array of class i.e $args
- |
- | @public
- | @return array
- |
- |---------------------------
- */
-
- public function get_args() {
- return $this->args;
- }
-
- /*
- |---------------------------
- | Get Alpha Upper Array
- | It will get default array of $alpha_upper
- |
- | @private
- | @return array
- |
- |---------------------------
- */
-
- private function get_alpha_upper() {
- return $this->alpha_upper;
- }
-
- /*
- |---------------------------
- | Get Alpha Lower Array
- | It will get default array of $alpha_lower
- |
- | @private
- | @return array
- |
- |---------------------------
- */
-
- private function get_alpha_lower() {
- return $this->alpha_lower;
- }
-
- /*
- |---------------------------
- | Get Number Array
- | It will get default array of $number
- |
- | @private
- | @return array
- |
- |---------------------------
- */
-
- private function get_number() {
- return $this->number;
- }
-
- /*
- |---------------------------
- | Get Symbol Array
- | It will get default array of $symbol
- |
- | @private
- | @return array
- |
- |---------------------------
- */
-
- private function get_symbol() {
- return $this->symbol;
- }
-
- /*
- |---------------------------
- | Generate Password
- | It will generate password
- |
- | @private
- | @return array
- |
- |---------------------------
- */
-
- private function set_password() {
-
- /* Temporary Array */
- $temp = array();
-
- /* Arguments */
- $args = $this->get_args();
- extract($args);
-
- /* Alpha Upper */
- if( $alpha_upper_include == TRUE ) {
- $alpha_upper = $this->get_alpha_upper();
- $temp = array_merge( $temp, $alpha_upper);
- }
-
- /* Alpha Lower */
- if( $alpha_lower_include == TRUE ) {
- $alpha_lower = $this->get_alpha_lower();
- $temp = array_merge( $temp, $alpha_lower);
- }
-
- /* Number */
- if( $number_include == TRUE ) {
- $number = $this->get_number();
- $temp = array_merge( $temp, $number);
- }
-
- /* Symbol */
- if( $symbol_include == TRUE ) {
- $symbol = $this->get_symbol();
- $temp = array_merge( $temp, $symbol);
- }
-
- /* Shuffle */
- shuffle($temp);
-
- /* Make Password */
- $password = '';
- foreach( $temp as $val ) {
-
- /* Password */
- $password .= $val;
-
- /* Length */
- if( strlen($password) == $length ) {
- break;
- }
- }
-
- return $password;
-
- }
-
- /*
- |---------------------------
- | Generate Password
- | It will generate password
- |
- | @public
- | @return array
- |
- |---------------------------
- */
-
- public function get_password() {
- return $this->set_password();
- }
-
-
- }