PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/password/class.chip_password_generator.php

https://gitlab.com/pelanne/php-password-generator
PHP | 347 lines | 161 code | 49 blank | 137 comment | 16 complexity | 81a90fa2a7181f02ac38984eb99a9153 MD5 | raw file
  1. <?php
  2. /*
  3. |-----------------
  4. | Author: Life.Object
  5. | E-Mail: life.object@gmail.com
  6. | Website: http://www.tutorialchip.com/
  7. | Help: http://www.tutorialchip.com/php-password-generator-class/
  8. | Version: 1.0
  9. | Released: December 02, 2010
  10. | Updated: December 02, 2010
  11. |------------------
  12. */
  13. class chip_password_generator {
  14. /*
  15. |---------------------------
  16. | Properties
  17. |---------------------------
  18. */
  19. private $args = array(
  20. 'length' => 8,
  21. 'alpha_upper_include' => TRUE,
  22. 'alpha_lower_include' => TRUE,
  23. 'number_include' => TRUE,
  24. 'symbol_include' => TRUE,
  25. );
  26. 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");
  27. 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");
  28. private $number = array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 );
  29. private $symbol = array( "-", "_", "^", "~", "@", "&", "|", "=", "+", ";", "!", ",", "(", ")", "{", "}", "[", "]", ".", "?", "%", "*", "#" );
  30. private $translation = array(
  31. 'a'=>'Alpha',
  32. 'b'=>'Bravo',
  33. 'c'=>'Charlie',
  34. 'd'=>'Delta',
  35. 'e'=>'Echo',
  36. 'f'=>'Foxtrot',
  37. 'g'=>'Golf',
  38. 'h'=>'Hotel',
  39. 'i'=>'India',
  40. 'j'=>'Juliet',
  41. 'k'=>'Kilo',
  42. 'l'=>'Lima',
  43. 'm'=>'Mike',
  44. 'n'=>'November',
  45. 'o'=>'Oscar',
  46. 'p'=>'Papa',
  47. 'q'=>'Quebec',
  48. 'r'=>'Romeo',
  49. 's'=>'Sierra',
  50. 't'=>'Tango',
  51. 'u'=>'Uniform',
  52. 'v'=>'Victor',
  53. 'w'=>'Whiskey',
  54. 'x'=>'X-ray',
  55. 'y'=>'Yankee',
  56. 'z'=>'Zulu',
  57. 0=>'Zero',
  58. 1=>'One',
  59. 2=>'Two',
  60. 3=>'Three',
  61. 4=>'Four',
  62. 5=>'Five',
  63. 6=>'Six',
  64. 7=>'Seven',
  65. 8=>'Eight',
  66. 9=>'Nine',
  67. "-"=>'Dash',
  68. "_"=>'Underscore',
  69. "^"=>'Carat',
  70. "~"=>'Tilda',
  71. "@"=>'At Sign',
  72. "&"=>'Ampersand',
  73. "|"=>'Pipe',
  74. "="=>'Equals',
  75. "+"=>'Plus',
  76. ";"=>'Semicolon',
  77. "!"=>'Exclamation mark',
  78. ","=>'Comma',
  79. "("=>'Left Paren',
  80. ")"=>'Right Paren',
  81. "{"=>'Left Curly Brace',
  82. "}"=>'Right Curly Brace',
  83. "["=>'Left Square Bracket',
  84. "]"=>'Right Square Bracket',
  85. "."=>'Period',
  86. "?"=>'Question Mark',
  87. "%"=>'Percentage Sign',
  88. "*"=>'Asterix',
  89. "#"=>'Hash Mark'
  90. );
  91. /*
  92. |---------------------------
  93. | Constructor
  94. |
  95. | @public
  96. | @param array $args
  97. |
  98. |---------------------------
  99. */
  100. public function __construct( $args = array() ) {
  101. $this->set_args( $args );
  102. }
  103. public function speakable($password){
  104. $letters = str_split ($password);
  105. $output="";
  106. foreach ($letters as $letter){
  107. if (ctype_alnum($letter) && !is_numeric($letter) && $this->is_upper($letter)) {
  108. $output.='UPPER CASE '. strtoupper($letter). ' AS IN '.strtoupper($this->translation[strtolower($letter)]);
  109. } elseif (ctype_alnum($letter) && !is_numeric($letter) && !$this->is_upper($letter)){
  110. $output.='Lower case '.$letter.' as in '.strtolower($this->translation[strtolower($letter)]);
  111. } elseif (is_numeric($letter)) {
  112. $output.='The number '.$letter .' ('.$this->translation[strtolower($letter)].') ';
  113. }else{
  114. $output.='The symbol '.$letter .' ('.$this->translation[strtolower($letter)].') ';
  115. }
  116. $output.= '<br>';
  117. }
  118. $output = substr($output, 0, -4);
  119. return $output;
  120. }
  121. function is_upper($str) {
  122. $chr = mb_substr ($str, 0, 1, "UTF-8");
  123. return mb_strtolower($chr, "UTF-8") != $chr;
  124. }
  125. /*
  126. |---------------------------
  127. | Print variable in readable format
  128. |
  129. | @public
  130. | @param string|array|object $var
  131. |
  132. |---------------------------
  133. */
  134. public function chip_print( $var ) {
  135. echo "<pre>";
  136. print_r($var);
  137. echo "</pre>";
  138. }
  139. /*
  140. |---------------------------
  141. | Update default arguments
  142. | It will update default array of class i.e $args
  143. |
  144. | @private
  145. | @param array $args - input arguments
  146. | @param array $defatuls - default arguments
  147. | @return array
  148. |
  149. |---------------------------
  150. */
  151. private function chip_parse_args( $args = array(), $defaults = array() ) {
  152. return array_merge( $defaults, $args );
  153. }
  154. /*
  155. |---------------------------
  156. | Set default arguments
  157. | It will set default array of class i.e $args
  158. |
  159. | @private
  160. | @param array $args
  161. | @return 0
  162. |
  163. |---------------------------
  164. */
  165. private function set_args( $args = array() ) {
  166. $defaults = $this->get_args();
  167. $args = $this->chip_parse_args( $args, $defaults );
  168. $this->args = $args;
  169. }
  170. /*
  171. |---------------------------
  172. | Get default arguments
  173. | It will get default array of class i.e $args
  174. |
  175. | @public
  176. | @return array
  177. |
  178. |---------------------------
  179. */
  180. public function get_args() {
  181. return $this->args;
  182. }
  183. /*
  184. |---------------------------
  185. | Get Alpha Upper Array
  186. | It will get default array of $alpha_upper
  187. |
  188. | @private
  189. | @return array
  190. |
  191. |---------------------------
  192. */
  193. private function get_alpha_upper() {
  194. return $this->alpha_upper;
  195. }
  196. /*
  197. |---------------------------
  198. | Get Alpha Lower Array
  199. | It will get default array of $alpha_lower
  200. |
  201. | @private
  202. | @return array
  203. |
  204. |---------------------------
  205. */
  206. private function get_alpha_lower() {
  207. return $this->alpha_lower;
  208. }
  209. /*
  210. |---------------------------
  211. | Get Number Array
  212. | It will get default array of $number
  213. |
  214. | @private
  215. | @return array
  216. |
  217. |---------------------------
  218. */
  219. private function get_number() {
  220. return $this->number;
  221. }
  222. /*
  223. |---------------------------
  224. | Get Symbol Array
  225. | It will get default array of $symbol
  226. |
  227. | @private
  228. | @return array
  229. |
  230. |---------------------------
  231. */
  232. private function get_symbol() {
  233. return $this->symbol;
  234. }
  235. /*
  236. |---------------------------
  237. | Generate Password
  238. | It will generate password
  239. |
  240. | @private
  241. | @return array
  242. |
  243. |---------------------------
  244. */
  245. private function set_password() {
  246. /* Temporary Array */
  247. $temp = array();
  248. /* Arguments */
  249. $args = $this->get_args();
  250. extract($args);
  251. /* Alpha Upper */
  252. if( $alpha_upper_include == TRUE ) {
  253. $alpha_upper = $this->get_alpha_upper();
  254. $temp = array_merge( $temp, $alpha_upper);
  255. }
  256. /* Alpha Lower */
  257. if( $alpha_lower_include == TRUE ) {
  258. $alpha_lower = $this->get_alpha_lower();
  259. $temp = array_merge( $temp, $alpha_lower);
  260. }
  261. /* Number */
  262. if( $number_include == TRUE ) {
  263. $number = $this->get_number();
  264. $temp = array_merge( $temp, $number);
  265. }
  266. /* Symbol */
  267. if( $symbol_include == TRUE ) {
  268. $symbol = $this->get_symbol();
  269. $temp = array_merge( $temp, $symbol);
  270. }
  271. /* Shuffle */
  272. shuffle($temp);
  273. /* Make Password */
  274. $password = '';
  275. foreach( $temp as $val ) {
  276. /* Password */
  277. $password .= $val;
  278. /* Length */
  279. if( strlen($password) == $length ) {
  280. break;
  281. }
  282. }
  283. return $password;
  284. }
  285. /*
  286. |---------------------------
  287. | Generate Password
  288. | It will generate password
  289. |
  290. | @public
  291. | @return array
  292. |
  293. |---------------------------
  294. */
  295. public function get_password() {
  296. return $this->set_password();
  297. }
  298. }