LEPTON CMS 7.2.0
feel free to keep it strictly simple...
Loading...
Searching...
No Matches
function.random_string.php
Go to the documentation of this file.
1<?php
2
18// include secure.php to protect this file and the whole CMS!
19if(!defined("SEC_FILE")){define("SEC_FILE",'/framework/secure.php' );}
20if (defined('LEPTON_PATH')) {
21 include LEPTON_PATH.SEC_FILE;
22} else {
23 $oneback = "../";
24 $root = $oneback;
25 $level = 1;
26 while (($level < 10) && (!file_exists($root.SEC_FILE))) {
27 $root .= $oneback;
28 $level += 1;
29 }
30 if (file_exists($root.SEC_FILE)) {
31 include $root.SEC_FILE;
32 } else {
33 trigger_error(sprintf("[ <b>%s</b> ] Can't include secure.php!", $_SERVER['SCRIPT_NAME']), E_USER_ERROR);
34 }
35}
36// end include secure file
37
38
76function random_string(int $iNumOfChars = 8, string $aType="alphanum"): string
77{
78 switch(strtolower($aType))
79 {
80 case 'alphanum':
81 $salt = 'abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
82 break;
83
84 case 'alpha':
85 case 'chars':
86 $salt = 'abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
87 break;
88
89 case 'hex':
90 $salt = 'ABCDEF0123456789';
91 break;
92
93 case 'hex-lower':
94 $salt = 'abcdef0123456789';
95 break;
96
97 case 'lower':
98 $salt = 'abcefghijklmnopqrstuvwxyz';
99 break;
100
101 case 'num':
102 $salt = '1234567890';
103 break;
104
105 case 'pass':
106 $salt = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-.";
107 break;
108
109 default:
110 $salt = (is_array($aType)) ? implode("", $aType) : (string) $aType ;
111 break;
112 }
113
114 $max = strlen($salt);
115 if ($iNumOfChars > $max)
116 {
117 do
118 {
119 $salt .= $salt;
120 } while (strlen($salt) < $iNumOfChars);
121 }
122
123 return substr(str_shuffle($salt), 0, $iNumOfChars);
124}
random_string(int $iNumOfChars=8, string $aType="alphanum")