Random.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace Tools\Utility;
  3. /**
  4. * Random Lib
  5. *
  6. * @author Mark Scherer
  7. * @license http://opensource.org/licenses/mit-license.php MIT
  8. */
  9. class Random {
  10. /**
  11. * Not for security relevant functionality - here use random_bytes()/random_bytes()
  12. *
  13. * @param int $min
  14. * @param int $max
  15. * @return int Random int value
  16. */
  17. public static function int($min = 0, $max = 999) {
  18. return random_int($min, $max);
  19. }
  20. /**
  21. * Randomly return one of the values provided
  22. * careful: only works with numerical keys (0 based!)
  23. *
  24. * @param array $array
  25. * @param int|null $minPosition
  26. * @param int|null $maxPosition
  27. * @param bool $integerKeys
  28. * @return mixed
  29. */
  30. public static function arrayValue(array $array, $minPosition = null, $maxPosition = null, $integerKeys = false) {
  31. if ($integerKeys) {
  32. $max = count($array) - 1;
  33. return $array[static::int(0, $max)];
  34. }
  35. $keys = array_keys($array);
  36. $values = array_values($array);
  37. $max = count($keys) - 1;
  38. return $values[static::int(0, $max)];
  39. }
  40. /**
  41. * Generates a password
  42. *
  43. * @param int $length Password length
  44. * @return string
  45. * @link https://github.com/CakeDC/users/blob/master/models/user.php#L498
  46. */
  47. public static function pronounceablePwd($length = 10) {
  48. mt_srand((int)(double)microtime() * 1000000);
  49. $password = '';
  50. $vowels = ['a', 'e', 'i', 'o', 'u'];
  51. $cons = ['b', 'c', 'd', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'u', 'v', 'w', 'tr',
  52. 'cr', 'br', 'fr', 'th', 'dr', 'ch', 'ph', 'wr', 'st', 'sp', 'sw', 'pr', 'sl', 'cl'];
  53. for ($i = 0; $i < $length; $i++) {
  54. $password .= $cons[random_int(0, 31)] . $vowels[random_int(0, 4)];
  55. }
  56. return substr($password, 0, $length);
  57. }
  58. /**
  59. * Generates random passwords.
  60. *
  61. * @param int $length (necessary!)
  62. * @param string|null $chars
  63. * @return string Password
  64. */
  65. public static function pwd($length, $chars = null) {
  66. if ($chars === null) {
  67. $chars = '234567890abcdefghijkmnopqrstuvwxyz'; // ABCDEFGHIJKLMNOPQRSTUVWXYZ
  68. }
  69. $i = 0;
  70. $password = '';
  71. $max = strlen($chars) - 1;
  72. while ($i < $length) {
  73. $password .= $chars[random_int(0, $max)];
  74. $i++;
  75. }
  76. return $password;
  77. }
  78. /**
  79. * Generates a random string of a given type and length.
  80. * $str = Text::random(); // 8 character random string
  81. *
  82. * The following types are supported:
  83. *
  84. * alnum
  85. * : Upper and lower case a-z, 0-9
  86. *
  87. * alpha
  88. * : Upper and lower case a-z
  89. *
  90. * hexdec
  91. * : Hexadecimal characters a-f, 0-9
  92. *
  93. * distinct
  94. * : Uppercase characters and numbers that cannot be confused
  95. *
  96. * You can also create a custom type by providing the "pool" of characters
  97. * as the type.
  98. *
  99. * @param string $type Type of pool, or a string of characters to use as the pool
  100. * @param int $length of string to return
  101. * @return string
  102. */
  103. public static function generate($type = 'alnum', $length = 8) {
  104. switch ($type) {
  105. case 'alnum':
  106. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  107. break;
  108. case 'alpha':
  109. $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  110. break;
  111. case 'hexdec':
  112. $pool = '0123456789abcdef';
  113. break;
  114. case 'numeric':
  115. $pool = '0123456789';
  116. break;
  117. case 'nozero':
  118. $pool = '123456789';
  119. break;
  120. case 'distinct':
  121. $pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
  122. break;
  123. default:
  124. $pool = (string)$type;
  125. break;
  126. }
  127. // Split the pool into an array of characters
  128. $pool = str_split($pool, 1);
  129. // Largest pool key
  130. $max = count($pool) - 1;
  131. $str = '';
  132. for ($i = 0; $i < $length; $i++) {
  133. // Select a random character from the pool and add it to the string
  134. $str .= $pool[random_int(0, $max)];
  135. }
  136. // Make sure alnum strings contain at least one letter and one digit
  137. if ($type === 'alnum' && $length > 1) {
  138. if (ctype_alpha($str)) {
  139. // Add a random digit
  140. $str[random_int(0, $length - 1)] = chr(random_int(48, 57));
  141. } elseif (ctype_digit($str)) {
  142. // Add a random letter
  143. $str[random_int(0, $length - 1)] = chr(random_int(65, 90));
  144. }
  145. }
  146. return $str;
  147. }
  148. }