-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyGenerator.php
More file actions
149 lines (136 loc) · 3.3 KB
/
KeyGenerator.php
File metadata and controls
149 lines (136 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php if ( ! defined('DENY_ACCESS')) exit('403: No direct file access allowed');
/**
* A Bright CMS
*
* Open source, lightweight, web application framework and content management
* system in PHP.
*
* @package A Bright CMS
* @author Gabriel Liwerant
*/
/**
* KeyGenerator Class
*
* We can generate a key from some standard key strings stored in a property or
* we can generate a key from a passed key string argument.
*
* @subpackage core
* @author Gabriel Liwerant
*/
class KeyGenerator
{
/**
* Error codes for KeyGenerator
*/
const EXPECTED_STRING = 1001;
/**
* Standard strings for use in composing a new key.
*
* @var array $_standard_key_type
*/
private $_standard_key_type = array(
'digital' => '0123456789',
'alpha_lower' => 'abcdefghijklmnopqrstuvwxyz',
'alpha_upper' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'symbol' => '~!@#$%^&*()_-+=|{[}]:;<,>.?'
);
/**
* Nothing to see here...
*/
public function __construct()
{
//
}
/**
* Generates a random key.
*
* We can choose the size of the generated key, as well as what string
* components we want to compose it.
*
* @param integer $size Length of the key to generate
* @param string $generator_string Values to use in composing the key
*
* @return string Generated key
*/
private function _generateKey($size, $generator_string)
{
// Choose a random position in the generator string and build with
// that single character, repeating until we reach the maximum size.
$key = null;
for ($i = 0; $i <= $size; $i++)
{
$key .= substr(
$generator_string,
mt_rand(1, strlen($generator_string)),
1
);
}
return $key;
}
/**
* Gets the key type from our array of standard key strings.
*
* @param string $type Array index to search for in standard key types
*
* @return string Generator string for key making
*/
private function _getStandardKeyType($type)
{
return $this->_standard_key_type[$type];
}
/**
* Composes a string to use in generating the key from our standard strings.
*
* @param integer $size Length of the key to generate
* @param array $type_arr Array indexes to use in composing the key
*
* @return string Generated key
*/
public function generateKeyFromStandard(
$size,
$type_arr = array(
'digital',
'alpha_lower',
'alpha_upper',
'symbol'
)
)
{
$generator_string = null;
if (is_array($type_arr))
{
foreach ($type_arr as $type)
{
$generator_string .= $this->_getStandardKeyType($type);
}
}
else
{
$generator_string = $this->_getStandardKeyType($type);
}
return $this->_generateKey($size, $generator_string);
}
/**
* Sends a user-entered string to compose a key.
*
* @param integer $size Length of the key to generate
* @param string $key_string Values to use in composing the key
*
* @return string Generated key
*/
public function generateKeyFromArgument($size, $key_string)
{
if (is_string($key_string))
{
$generator_string = $key_string;
return $this->_generateKey($size, $generator_string);
}
else
{
throw ApplicationFactory::makeException('KeyGenerator Exception', self::EXPECTED_STRING);
//throw new Exception('KeyGenerator Exception', self::EXPECTED_STRING);
}
}
}
// End of KeyGenerator Class
/* EOF system/core/KeyGenerator.php */