RandomLib.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /**
  3. * Random Lib
  4. *
  5. * @author Mark Scherer
  6. * @license MIT
  7. * 2009-07-24 ms
  8. */
  9. class RandomLib {
  10. /**
  11. * @param int $min
  12. * @param int $max
  13. * @return random int value
  14. * @access static
  15. */
  16. public static function int($min = 0, $max = 999) {
  17. return mt_rand($min, $max);
  18. }
  19. /**
  20. * @param float $min
  21. * @param float $max
  22. * @return random float value
  23. * @access static
  24. */
  25. public static function float($min = 0.0, $max = 999.0) {
  26. $rand = rand (1, 358);
  27. return $rand * cos($rand);
  28. }
  29. /**
  30. * randomly return one of the values provided
  31. * careful: only works with numerical keys (0 based!)
  32. * @access static
  33. */
  34. public static function arrayValue($array, $minPosition = null, $maxPosition = null, $integerKeys = false) {
  35. if (empty($array)) {
  36. return null;
  37. }
  38. if ($integerKeys) {
  39. $max = count($array) - 1;
  40. return $array[self::int(0, $max)];
  41. }
  42. $keys = array_keys($array);
  43. $values = array_values($array);
  44. $max = count($keys) - 1;
  45. return $values[self::int(0, $max)];
  46. }
  47. public static function arrayValues($array, $minAmount = null, $maxAmount = null) {
  48. //NOT IMPORTANT
  49. }
  50. //TODO
  51. /**
  52. * 1950-01-01 - 2050-12-31
  53. */
  54. public static function date($min = null, $max = null, $formatReturn = null) {
  55. if ($min === null && $max === null)
  56. $res = time();
  57. elseif ($min > 0 && $max === null)
  58. $res = $min;
  59. elseif ($min > 0 && $max > 0)
  60. $res = self::int($min, $max);
  61. else
  62. $res = time();
  63. $res = 0;
  64. $formatReturnAs = FORMAT_DB_DATETIME;
  65. if ($formatReturn !== null) {
  66. if ($formatReturn === false) {
  67. return $res;
  68. }
  69. $formatReturnAs = $formatReturn;
  70. }
  71. return date($formatReturnAs);
  72. }
  73. //TODO
  74. /**
  75. * 00:00:00 - 23:59:59
  76. */
  77. public static function time($min = null, $max = null, $formatReturn = null) {
  78. $res = 0;
  79. //$returnValueAs = FORMAT_DB_TIME;
  80. if ($formatReturn !== null) {
  81. if ($formatReturn === false) {
  82. return $res;
  83. }
  84. }
  85. }
  86. /**
  87. * Returns a date of birth within the specified age range
  88. * @method dob(int $min, $int $max)
  89. * @public
  90. * @param (int) $min minimum age in years
  91. * @param (int) $max maximum age in years
  92. * @return (string) $dob a db (ISO) format datetime string
  93. */
  94. public static function dob($min = 18, $max = 100) {
  95. $dob_year = date('Y') - (self::int($min, $max));
  96. $dob_month = self::int(1, 12);
  97. if ($dob_month == 2) {
  98. // leap year?
  99. if ($age_years % 4 || $age_years % 400)
  100. $max_days = 29;
  101. else
  102. $max_days = 28;
  103. } elseif (in_array($dob_month, array(4, 6, 9, 11)))
  104. $max_days = 30;
  105. else
  106. $max_days = 31;
  107. $dob_day = self::int(1, $max_days);
  108. $dob = sprintf("%4d-%02d-%02d", $dob_year, $dob_month, $dob_day);
  109. return $dob;
  110. }
  111. /**
  112. * Generates a password
  113. *
  114. * @param int $length Password length
  115. * @link https://github.com/CakeDC/users/blob/master/models/user.php#L498
  116. * @return string
  117. */
  118. public static function pronounceablePwd($length = 10) {
  119. srand((double)microtime() * 1000000);
  120. $password = '';
  121. $vowels = array("a", "e", "i", "o", "u");
  122. $cons = array("b", "c", "d", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "u", "v", "w", "tr",
  123. "cr", "br", "fr", "th", "dr", "ch", "ph", "wr", "st", "sp", "sw", "pr", "sl", "cl");
  124. for ($i = 0; $i < $length; $i++) {
  125. $password .= $cons[mt_rand(0, 31)] . $vowels[mt_rand(0, 4)];
  126. }
  127. return substr($password, 0, $length);
  128. }
  129. /**
  130. * @deprecated!
  131. */
  132. public static function pwd($type = null, $length = null) {
  133. return self::randomPwd($type, $length);
  134. }
  135. /**
  136. * returns auto-generated password
  137. * @param string $type: user, ...
  138. * @param int $length (if no type is submitted)
  139. * @return pwd on success, empty string otherwise
  140. * @static
  141. * 2009-12-26 ms
  142. */
  143. public static function randomPwd($type = null, $length = null) {
  144. if (!empty($type) && $type == 'user') {
  145. return self::generatePassword(6);
  146. }
  147. if (!empty($length)) {
  148. return self::generatePassword($length);
  149. }
  150. return '';
  151. }
  152. /**
  153. * //TODO: move to password lib?
  154. * generate random passwords
  155. * @param int $lenght (necessary!)
  156. * @return pwd
  157. * @static
  158. * 2009-12-26 ms
  159. */
  160. public static function generatePassword($length, $chars = null) {
  161. if ($chars === null) {
  162. $chars = '234567890abcdefghijkmnopqrstuvwxyz'; // ABCDEFGHIJKLMNOPQRSTUVWXYZ
  163. }
  164. $i = 0;
  165. $password = '';
  166. $max = strlen($chars) - 1;
  167. while ($i < $length) {
  168. $password .= $chars[mt_rand(0, $max)];
  169. $i++;
  170. }
  171. return $password;
  172. }
  173. /**
  174. * Few years ago i had written Joomla component AutoContent. That component generates new unique content and add it to Joomla site. In general, this is not good tool, because it does black SEO things. If search engine (ex. Google) finds that there is autogenerated text without sense then site can be banned
  175. * @link http://www.gelembjuk.com/index.php?option=com_content&view=article&id=60&catid=37:php&Itemid=56
  176. */
  177. public static function sentences($sentences, $wordscount = 2) {
  178. $hash = array();
  179. $resultsentences = array();
  180. for ($i = 0; $i < count($sentences); $i++) {
  181. $words = split(' ', trim($sentences[$i]));
  182. for ($k = 0; $k < count($words) - $wordscount; $k++) {
  183. $prefix = trim(join(' ', array_slice($words, $k, $wordscount)));
  184. if ($prefix == '')
  185. continue;
  186. if (empty($hash[$prefix])) {
  187. $hash[$prefix] = array($words[$k + $wordscount]);
  188. for ($j = $i + 1; $j < count($sentences); $j++) {
  189. if (preg_match('/' . ereg_replace('/', '\/', preg_quote($prefix)) . '(.*)$/', $sentences[$j], $m)) {
  190. $w = split(' ', trim($m[1]));
  191. if (count($w) > 0 && trim($w[0]) != '')
  192. array_push($hash[$prefix], trim($w[0]));
  193. }
  194. }
  195. }
  196. }
  197. }
  198. $prefixes = array_keys($hash);
  199. $stpr = array();
  200. foreach ($prefixes as $pr) {
  201. if ($pr[0] == strtoupper($pr[0]))
  202. array_push($stpr, $pr);
  203. }
  204. for ($i = 0; $i < count($sentences); $i++) {
  205. $p = $stpr[rand(0, count($stpr) - 1)];
  206. $sent = split(' ', $p);
  207. $cc = count(split(' ', $sentences[$i]));
  208. $j = 0;
  209. do {
  210. $w = $hash[$p][rand(0, count($hash[$p]) - 1)];
  211. array_push($sent, $w);
  212. $j++;
  213. $p = join(' ', array_slice($sent, $j, $wordscount));
  214. } while (strrpos($w, '.') != strlen($w) - 1 && $j < $cc * 2);
  215. $sn = join(' ', $sent);
  216. if ($sn[strlen($sn) - 1] != '.')
  217. $sn .= '.';
  218. array_push($resultsentences, $sn);
  219. }
  220. return $resultsentences;
  221. }
  222. /**
  223. * other implemantations
  224. */
  225. /**
  226. * Generates a random string of a given type and length.
  227. * $str = Text::random(); // 8 character random string
  228. *
  229. * The following types are supported:
  230. *
  231. * alnum
  232. * : Upper and lower case a-z, 0-9
  233. *
  234. * alpha
  235. * : Upper and lower case a-z
  236. *
  237. * hexdec
  238. * : Hexadecimal characters a-f, 0-9
  239. *
  240. * distinct
  241. * : Uppercase characters and numbers that cannot be confused
  242. *
  243. * You can also create a custom type by providing the "pool" of characters
  244. * as the type.
  245. *
  246. * @param string a type of pool, or a string of characters to use as the pool
  247. * @param integer length of string to return
  248. * @return string
  249. * @uses UTF8::split
  250. */
  251. public static function random($type = 'alnum', $length = 8) {
  252. switch ($type) {
  253. case 'alnum':
  254. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  255. break;
  256. case 'alpha':
  257. $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  258. break;
  259. case 'hexdec':
  260. $pool = '0123456789abcdef';
  261. break;
  262. case 'numeric':
  263. $pool = '0123456789';
  264. break;
  265. case 'nozero':
  266. $pool = '123456789';
  267. break;
  268. case 'distinct':
  269. $pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
  270. break;
  271. default:
  272. $pool = (string )$type;
  273. //$utf8 = ! UTF8::is_ascii($pool);
  274. break;
  275. }
  276. // Split the pool into an array of characters
  277. $pool = ($utf8 === true) ? str_split($pool, 1) : str_split($pool, 1);
  278. // Largest pool key
  279. $max = count($pool) - 1;
  280. $str = '';
  281. for ($i = 0; $i < $length; $i++) {
  282. // Select a random character from the pool and add it to the string
  283. $str .= $pool[mt_rand(0, $max)];
  284. }
  285. // Make sure alnum strings contain at least one letter and one digit
  286. if ($type === 'alnum' and $length > 1) {
  287. if (ctype_alpha($str)) {
  288. // Add a random digit
  289. $str[mt_rand(0, $length - 1)] = chr(mt_rand(48, 57));
  290. } elseif (ctype_digit($str)) {
  291. // Add a random letter
  292. $str[mt_rand(0, $length - 1)] = chr(mt_rand(65, 90));
  293. }
  294. }
  295. return $str;
  296. }
  297. }