-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathID.php
More file actions
158 lines (151 loc) · 5.24 KB
/
ID.php
File metadata and controls
158 lines (151 loc) · 5.24 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
150
151
152
153
154
155
156
157
158
<?php
/**
* ID
*
* PHP class to generate identification numbers and strings. IDs are generated
* with a control character using Luhn mod N, to enable verification of IDs
* programmatically.
*
* Examples can be found here: https://github.com/jop-io/ID
*
* @license https://opensource.org/licenses/MIT MIT
* @version 0.1
* @author Jonas Persson <jonas@rgv2.net>
*/
class ID {
private $lenght;
private $codeMap;
private $charMap;
private $pool;
private $type;
private $radix;
/**
* Constructor.
*
* @param string $type
* @param integer $lenght
*/
public function __construct($type = "", $lenght = 0) {
$this->SetPoolType($type);
$this->SetLength($lenght);
}
/**
* Generate an ID.
*
* @return string The ID
*/
public function Generate() {
$output = "";
for ($i = 0; $i < $this->lenght-1; $i++) {
$position = rand(0, $this->radix-1);
$output .= substr($this->pool, $position, 1);
}
return $output . $this->GenerateControlCharacter($output);
}
/**
* Verify an ID.
*
* The verification is done in three steps:
*
* 1. Check the lenght of the ID
* 2. Make sure the ID only have character from the active pool set
* 3. Calculate and compare the ID's control character
*
* @param string $input The ID to be verified
* @return boolean Returns true if ID is valid, otherwise false
*/
public function Validate($input = "") {
if ($this->type === "safe") {
$input = mb_strtoupper($input, "UTF-8");
}
if (mb_strlen($input, "UTF-8") !== $this->lenght) {
return false;
}
$chars = preg_split('/(?<!^)(?!$)/u', $input);
foreach ($chars as $char) {
if (!isset($this->charMap[$char])) {
return false;
}
}
return $this->ValidateControlCharacter($input);
}
/**
* Set the pool type to be used for generation of IDs. The following pool
* types are available:
*
* "alphanum" = 0-9, a-z, A-Z
* "aplha" = a-z, A-Z
* "lower" = a-z
* "upper" = A-Z
* "numeric" = 0-9
* "nozero" = 1-9
* * "safe" = 1-9, BCDFGHJKLMNPQRSTVWXZ
*
* * Safe means that vowels has been removed to prevent the risk of
* inappropriate words being generated within the ID. All letters are
* in uppercase to ease reading for humans. The digit "0" (zero) has
* also been removed to prevent confusion with the letter "O".
*
* @param string $type Pool type to be used, defaults to "alphanum" (aA-zZ, 0-9)
*/
public function SetPoolType($type = "") {
$pools = [
"alphanum" => "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
"alpha" => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
"lower" => "abcdefghijklmnopqrstuvwxyz",
"upper" => "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"numeric" => "0123456789",
"nozero" => "123456789",
"safe" => "123456789BCDFGHJKLMNPQRSTVWXZ"
];
$this->type = isset($pools[strtolower($type)]) ? strtolower($type) : "alphanum";
$this->pool = $pools[$this->type];
$this->radix = strlen($this->pool);
$this->codeMap = str_split($this->pool);
$this->charMap = array_flip(str_split($this->pool));
}
/**
* Set length of ID. Length is used both for generation and verification of
* IDs. The length must be at least 2 (two). Defaults to 32 (thirty-two).
*
* @param integer $lenght Must be at least 2, defaults to 32
*/
public function SetLength($lenght = 0) {
$this->lenght = intval($lenght) > 1 ? intval($lenght) : 32;
}
/**
* Generates a control character using Luhn mod N.
*
* @param string $input The ID, without control character
* @return string A control character
*/
private function GenerateControlCharacter($input = "") {
$chars = str_split($input);
$factor = 2;
$sum = 0;
for ($i = count($chars)-1; $i >= 0; $i--) {
$addend = $factor * $this->charMap[$chars[$i]];
$factor = $factor === 2 ? 1 : 2;
$sum += intval($addend/$this->radix)+($addend%$this->radix);
}
$code = ($this->radix-($sum%$this->radix))%$this->radix;
return $this->codeMap[$code];
}
/**
* Validates an IDs control character using Luhn mod N.
*
* @param string $input The complete ID, including the control character
* @return boolean Returns true if the control character is valid, otherwise false
*/
private function ValidateControlCharacter($input = "") {
$chars = str_split($input);
$factor = 1;
$sum = 0;
for ($i = count($chars)-1; $i >= 0; $i--) {
$addend = $factor * $this->charMap[$chars[$i]];
$factor = $factor === 2 ? 1 : 2;
$sum += intval($addend/$this->radix)+($addend%$this->radix);
}
return $sum % $this->radix === 0;
}
}