LEPTON CMS 7.0.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
73function random_string(int $iNumOfChars = 8, string $aType="alphanum"): string
74{
75 switch(strtolower($aType))
76 {
77 case 'alphanum':
78 $salt = 'abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
79 break;
80
81 case 'alpha':
82 case 'chars':
83 $salt = 'abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
84 break;
85
86 case 'lower':
87 $salt = 'abcefghijklmnopqrstuvwxyz';
88 break;
89
90 case 'num':
91 $salt = '1234567890';
92 break;
93
94 case 'pass':
95 $salt = "abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890?!&_-*%@:.|";
96 break;
97
98 default:
99 $salt = (is_array($aType)) ? implode("", $aType) : (string) $aType ;
100 break;
101 }
102
103 $max = strlen($salt);
104 if ($iNumOfChars > $max) {
105 do {
106 $salt .= $salt;
107 } while (strlen($salt) < $iNumOfChars);
108 }
109
110 return substr(str_shuffle($salt), 0, $iNumOfChars);
111}
random_string(int $iNumOfChars=8, string $aType="alphanum")