RandomLib.php 8.1 KB

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