Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. <?php
  2.  
  3. // Namespace
  4. namespace Modules;
  5.  
  6. /**
  7. * PasswordGenerator
  8. * @author Jan Doušek <mail@jandousek.cz>
  9. */
  10.  
  11. class PasswordGenerator {
  12.  
  13. /* @var array */
  14. private $adjectives = ['female' => [], 'male' => []];
  15.  
  16. /* @var array */
  17. private $nouns = ['female' => [], 'male' => []];
  18.  
  19. /* @var array */
  20. private $verbs = [];
  21.  
  22. /* @var object */
  23. public $page = null;
  24.  
  25. /**
  26. * Constructor
  27. * @param void
  28. * @return void
  29. */
  30.  
  31. public function __construct() {
  32.  
  33. // Set page
  34. $this->page = new \Leximo\Post(['template' => GeneratorPage::POST_TEMPLATE]);
  35.  
  36. // Set female adjectives
  37. $this->adjectives['female'] = explode("\n", $this->page->adjectives_female);
  38.  
  39. // Set male adjectives
  40. $this->adjectives['male'] = explode("\n", $this->page->adjectives_male);
  41.  
  42. // Set female nouns
  43. $this->nouns['female'] = explode("\n", $this->page->nouns_female);
  44.  
  45. // Set male adjectives
  46. $this->nouns['male'] = explode("\n", $this->page->nouns_male);
  47.  
  48. // Set verbs
  49. $this->verbs = explode("\n", $this->page->verbs);
  50.  
  51. }
  52.  
  53. /**
  54. * Get word
  55. * @param string $type
  56. * @param string $gender
  57. * @param bool $ucfirst
  58. * @return string
  59. */
  60.  
  61. private function getWord($type, $gender, $ucfirst = false) {
  62.  
  63. // Check type and set array
  64. if(in_array($type, ['adjective', 'noun'])) {
  65.  
  66. // Check gender
  67. $array = $this->{$type . 's'}[($gender ? 'female' : 'male')];
  68. }
  69.  
  70. // Set verbs
  71. else if ($type == 'verb') {
  72. $array = $this->verbs;
  73. }
  74.  
  75. // Return with ucfirst
  76. if($ucfirst) {
  77. return ucfirst($array[array_rand($array)]);
  78. }
  79.  
  80. // Return plain
  81. else {
  82. return array_rand($array[array_rand($array)]);
  83. }
  84.  
  85. }
  86.  
  87. /**
  88. * Get adjective
  89. * @param string $gender
  90. * @param bool $ucfirst
  91. * @return string
  92. */
  93.  
  94. public function getAdjective($gender, $ucfirst = false) {
  95. return $this->getWord('adjective', $gender, $ucfirst);
  96. }
  97.  
  98. /**
  99. * Get noun
  100. * @param bool $ucfirst
  101. * @return string
  102. */
  103.  
  104. public function getNoun($gender, $ucfirst = false) {
  105. return $this->getWord('noun', $gender, $ucfirst);
  106. }
  107.  
  108. /**
  109. * Get verb
  110. * @param bool $ucfirst
  111. * @return string
  112. */
  113.  
  114. public function getVerb($ucfirst = false) {
  115. return $this->getWord('verb', $ucfirst);
  116. }
  117.  
  118. /**
  119. * Generate
  120. * @param bool $ucfirst
  121. * @return string
  122. */
  123.  
  124. public function generate($ucfirst = true) {
  125.  
  126. // Set gender
  127. $gender = array_rand(['female', 'male']);
  128.  
  129. // Return password
  130. return preg_replace("/\r|\n/", '', $this->getWord('adjective', $gender, $ucfirst) . $this->getWord('noun', $gender, $ucfirst) . $this->getWord('verb', $gender, $ucfirst));
  131.  
  132. }
  133.  
  134. /**
  135. * Conplicate
  136. * @param bool $password
  137. * @return string
  138. */
  139.  
  140. public function complicate($password, $length = 12) {
  141.  
  142. // Explode plain password by capitals
  143. $str1 = array_values(array_filter(preg_split('/(?=[A-Z])/', $password)));
  144.  
  145. // Create second password
  146. $str2 = str_split(\Leximo\Generate::password($length, true, false), 1);
  147.  
  148. // Split second password into columns
  149. $str2 = array_chunk($str2, ceil(count($str2) / count($str1)));
  150.  
  151. // Join subarays in second password into string
  152. foreach($str2 as $key=>$val) $str2[$key] = implode('', $val);
  153.  
  154. // Append the shorter string to the longer string
  155. for($x = 0; $x < count($str1); $x++) {
  156. $str1[$x] .= $str2[$x];
  157. }
  158.  
  159. // Return
  160. return implode('', $str1);
  161.  
  162. }
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement