1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soleo\UrlShortener; |
4
|
|
|
|
5
|
|
|
class Shorty |
6
|
|
|
{ |
7
|
|
|
private $conn; |
8
|
|
|
|
9
|
5 |
|
public function __construct(ConnectionInterface $conn) |
10
|
|
|
{ |
11
|
5 |
|
$this->conn = $conn; |
12
|
5 |
|
} |
13
|
|
|
|
14
|
4 |
|
public function getShortUrl($longURL) |
15
|
|
|
{ |
16
|
|
|
// validate Long URL first and normalize it |
17
|
4 |
|
if (strlen($longURL) <= 3) { |
18
|
1 |
|
throw new \InvalidArgumentException("URL is malformated!"); |
19
|
|
|
} |
20
|
|
|
|
21
|
3 |
|
$currentDomain = "localhost"; |
22
|
|
|
// Lookup |
23
|
3 |
|
$slug = $this->conn->reverseLookup($longURL); |
24
|
|
|
|
25
|
3 |
|
$fullURL = 'http://'.$currentDomain.'/'; |
26
|
3 |
|
if ($slug) { |
27
|
2 |
|
return $fullURL.$slug; |
28
|
|
|
} else { |
29
|
|
|
// add new records to DB |
30
|
1 |
|
$slug = $this->generateId(); |
31
|
1 |
|
$this->conn->addNewRecord($longURL, $slug); |
32
|
1 |
|
return $fullURL.$slug; |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
public function getLongUrl($slug, $update = false) |
37
|
|
|
{ |
38
|
|
|
// filter out invalid string |
39
|
2 |
|
$slug = preg_replace('[^A_Za_z0-9]', '', $slug); |
40
|
2 |
|
$longURL = $this->conn->lookup($slug, $update); |
41
|
2 |
|
return $longURL; |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
private function generateId() |
45
|
|
|
{ |
46
|
1 |
|
$url_num_id = $this->conn->getIncrementUid(); |
47
|
1 |
|
$slug = $this->alphaID($url_num_id); |
48
|
1 |
|
return $slug; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
private function alphaID($in, $to_num = false, $pad_up = 3, $passKey = "prephe.ro") |
52
|
|
|
{ |
53
|
1 |
|
$index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
54
|
1 |
|
$i = []; |
55
|
1 |
|
$p = []; |
56
|
1 |
|
if ($passKey !== null) { |
57
|
|
|
// Although this function's purpose is to just make the |
58
|
|
|
// ID short - and not so much secure, |
59
|
|
|
// with this patch by Simon Franz (http://blog.snaky.org/) |
60
|
|
|
// you can optionally supply a password to make it harder |
61
|
|
|
// to calculate the corresponding numeric ID |
62
|
|
|
|
63
|
1 |
|
for ($n = 0; $n<strlen($index); $n++) { |
64
|
1 |
|
$i[] = substr($index, $n, 1); |
65
|
1 |
|
} |
66
|
|
|
|
67
|
1 |
|
$passhash = hash('sha256', $passKey); |
68
|
1 |
|
$passhash = (strlen($passhash) < strlen($index)) |
69
|
1 |
|
? hash('sha512', $passKey) |
70
|
1 |
|
: $passhash; |
71
|
|
|
|
72
|
1 |
|
for ($n=0; $n < strlen($index); $n++) { |
73
|
1 |
|
$p[] = substr($passhash, $n, 1); |
74
|
1 |
|
} |
75
|
|
|
|
76
|
1 |
|
array_multisort($p, SORT_DESC, $i); |
77
|
1 |
|
$index = implode($i); |
78
|
1 |
|
} |
79
|
|
|
|
80
|
1 |
|
$base = strlen($index); |
81
|
|
|
|
82
|
1 |
|
if ($to_num) { |
83
|
|
|
// Digital number <<-- alphabet letter code |
84
|
|
|
$in = strrev($in); |
85
|
|
|
$out = 0; |
86
|
|
|
$len = strlen($in) - 1; |
87
|
|
|
for ($t = 0; $t <= $len; $t++) { |
88
|
|
|
$bcpow = bcpow($base, $len - $t); |
89
|
|
|
$out = $out + strpos($index, substr($in, $t, 1)) * $bcpow; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
View Code Duplication |
if (is_numeric($pad_up)) { |
|
|
|
|
93
|
|
|
$pad_up--; |
94
|
|
|
if ($pad_up > 0) { |
95
|
|
|
$out -= pow($base, $pad_up); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
$out = sprintf('%F', $out); |
99
|
|
|
$out = substr($out, 0, strpos($out, '.')); |
100
|
|
|
} else { |
101
|
|
|
// Digital number -->> alphabet letter code |
102
|
1 |
View Code Duplication |
if (is_numeric($pad_up)) { |
|
|
|
|
103
|
1 |
|
$pad_up--; |
104
|
1 |
|
if ($pad_up > 0) { |
105
|
1 |
|
$in += pow($base, $pad_up); |
106
|
1 |
|
} |
107
|
1 |
|
} |
108
|
|
|
|
109
|
1 |
|
$out = ""; |
110
|
1 |
|
for ($t = floor(log($in, $base)); $t >= 0; $t--) { |
111
|
1 |
|
$bcp = bcpow($base, $t); |
112
|
1 |
|
$a = floor($in / $bcp) % $base; |
113
|
1 |
|
$out = $out . substr($index, $a, 1); |
114
|
1 |
|
$in = $in - ($a * $bcp); |
115
|
1 |
|
} |
116
|
1 |
|
$out = strrev($out); // reverse |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
return $out; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.