1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omnimail\Utils; |
4
|
|
|
|
5
|
|
|
class Token |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var array |
9
|
|
|
*/ |
10
|
|
|
private static $characters = [ |
11
|
|
|
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'o', 'j', 'k', 'l', |
12
|
|
|
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', |
13
|
|
|
'E', 'F', 'G', 'H', 'O', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', |
14
|
|
|
'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' |
15
|
|
|
]; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Generate a random string |
19
|
|
|
* |
20
|
|
|
* @param int $length |
21
|
|
|
* @param mixed $type |
22
|
|
|
* @param bool $caseSensitive |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
|
|
public static function generate($length = 128, $type = 'alnum', $caseSensitive = true) |
26
|
|
|
{ |
27
|
|
|
$characters = self::$characters; |
28
|
|
|
|
29
|
|
|
// Provided characters |
30
|
|
|
if (is_array($type)) { |
31
|
|
|
$characters = $type; |
32
|
|
|
} // Hexa decimal |
33
|
|
|
elseif ($type === 'hexa') { |
34
|
|
|
$characters = array_merge(array_slice(self::$characters, 52), array_slice(self::$characters, 26, 6)); |
35
|
|
|
} // Case insensitive alpha numeric |
36
|
|
|
elseif ($type === 'alnum' && $caseSensitive === false) { |
37
|
|
|
$characters = array_slice(self::$characters, 26); |
38
|
|
|
} // Alphabet only |
39
|
|
|
elseif ($type === 'alpha') { |
40
|
|
|
$characters = array_slice(self::$characters, 0, 26); |
41
|
|
|
} // Numbers only |
42
|
|
|
elseif ($type === 'digit' || $type === 'numeric') { |
43
|
|
|
$characters = array_slice(self::$characters, 52); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return self::make($characters, $length); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Make the random string |
51
|
|
|
* |
52
|
|
|
* @param string $characters |
53
|
|
|
* @param int $length |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
private static function make($characters, $length) |
57
|
|
|
{ |
58
|
|
|
$token = ''; |
59
|
|
|
|
60
|
|
|
do { |
61
|
|
|
$token .= |
62
|
|
|
$characters[(int)floor(hexdec(bin2hex(openssl_random_pseudo_bytes(1, $strong))) % count($characters))]; |
63
|
|
|
} while (strlen($token) < $length); |
64
|
|
|
|
65
|
|
|
return $token; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: