-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshorturl.php
164 lines (141 loc) · 3.92 KB
/
shorturl.php
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
159
160
161
162
163
164
<?php
/**
* rbib/util/string/encode/Bijection.php
* $Date:: #$
* @version $Rev$
* @package
*/
// namespace rbib\util\string\encode;
// class Bijection
class shorturl
{
/**
* Char Set for short URL
*/
const BIJ_SET = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
/**
* Checker for short URL
*
* @var array
*/
private $mAlphabetChecker = array(
50,
51,
);
/**
* The secret string
*
* @var string
*/
private $mAlphabet = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
/**
* Create random string
*
* @param int $loop
* @return string
*/
public function cryptAlphabet($loop = 60)
{
$mAlphabet = self::BIJ_SET;
for ($i = 0; $i < $loop; $i++) {
str_shuffle($mAlphabet);
}
return $mAlphabet;
}
/**
* Return an encoded URI
*
* @param int|string $id id (only positive integer accepted) to be encoded
* @param bool $checkDigit checkDigit (true) adds redundancy check digit or not
* @return string|false
*/
public function encode($id, $checkDigit = true)
{
if (!is_bool($checkDigit) || !is_int($id) && !is_string($id)) {
return false;
}
if (!$id || ((int) $id) != $id || $id < 0) {
return false;
} else {
$id = (int) $id;
}
list($mAlphabetArr, $mBase) = $this->getSecret($this->mAlphabet);
if ($checkDigit) {
$checker = $this->getCheckDigit($id, $mAlphabetArr);
} else {
$checker = '';
}
$shortURI = '';
while ($id > 0) {
$shortURI = $mAlphabetArr[$id % $mBase] . $shortURI;
$id = (int) ($id / $mBase);
}
return $checker . $shortURI;
}
/**
* Return id from an encoded URI
*
* @param string $shortURI encoded URI
* @param bool $checkDigit true detects redundancy check digit or not
* @return int|false
*/
public function decode($shortURI = '', $checkDigit = true)
{
if (!is_string($shortURI) || !is_bool($checkDigit)) {
return false;
}
$checkerLen = count($this->mAlphabetChecker);
if ($checkDigit && strlen($shortURI) < $checkerLen + 1) {
return false;
}
$id = 0;
list($mAlphabetArr, $mBase) = $this->getSecret($this->mAlphabet);
$encodedStr = $checkDigit ? substr($shortURI, $checkerLen) : $shortURI;
foreach (str_split($encodedStr) as $value) {
$id = $id * $mBase + array_search($value, $mAlphabetArr);
}
if ($id < 0) {
return false;
}
if (!$checkDigit) {
return $id;
} else {
$checker = $this->getCheckDigit($id, $mAlphabetArr);
}
if ($checker === null || $checker !== substr($shortURI, 0, $checkerLen)) {
return false;
}
return $id;
}
/**
*
* @param string $string
* @return array
*/
protected function getSecret($string)
{
return array(str_split($string), strlen($string));
}
/**
*
* @param int $id
* @param array $mAlphabetArr
* @return string|null
*/
protected function getCheckDigit($id, array $mAlphabetArr)
{
if (!is_int($id)) {
return null;
}
$checkDigit = '';
foreach ($this->mAlphabetChecker as $value) {
$index = $id % $value;
if (isset($mAlphabetArr[$index])) {
$checkDigit = $checkDigit . $mAlphabetArr[$index];
} else {
return null;
}
}
return $checkDigit;
}
}