|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Soleo\UrlShortener; |
|
4
|
|
|
|
|
5
|
|
|
class Shorty |
|
6
|
|
|
{ |
|
7
|
|
|
private $conn; |
|
8
|
|
|
|
|
9
|
|
|
public function __construct(ConnectionInterface $conn) |
|
10
|
|
|
{ |
|
11
|
|
|
$this->conn = $conn; |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
public function getShortUrl($longURL) |
|
15
|
|
|
{ |
|
16
|
|
|
// validate Long URL first and normalize it |
|
17
|
|
|
if (strlen($longURL) <= 3) { |
|
18
|
|
|
throw new \InvalidArgumentException("URL is malformated!"); |
|
19
|
|
|
} |
|
20
|
|
|
$currentDomain = "localhost"; |
|
21
|
|
|
// Lookup |
|
22
|
|
|
$slug = $this->conn->reverseLookup($longURL); |
|
23
|
|
|
|
|
24
|
|
|
$fullURL = 'http://'.$currentDomain.'/'; |
|
25
|
|
|
if ($slug) { |
|
26
|
|
|
return $fullURL.$slug; |
|
27
|
|
|
} else { |
|
28
|
|
|
// add new records to DB |
|
29
|
|
|
$slug = $this->generateId($longURL); |
|
30
|
|
|
var_dump($slug); |
|
|
|
|
|
|
31
|
|
|
$this->conn->addNewRecord($longURL, $slug); |
|
32
|
|
|
return $fullURL.$slug; |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function getLongUrl($slug) |
|
37
|
|
|
{ |
|
38
|
|
|
// filter out invalid string |
|
39
|
|
|
$slug = preg_replace('[^A_Za_z0-9]', '', $slug); |
|
40
|
|
|
|
|
41
|
|
|
$longURL = $this->conn->lookup($slug); |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
return $longURL; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
private function generateId($longURL) |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
$url_num_id = $this->conn->getIncrementUid(); |
|
49
|
|
|
$slug = $this->alphaID($url_num_id); |
|
50
|
|
|
return $slug; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function alphaID($in, $to_num = false, $pad_up = 3, $passKey = "prephe.ro") |
|
54
|
|
|
{ |
|
55
|
|
|
$index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|
56
|
|
|
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
|
|
|
for ($n = 0; $n<strlen($index); $n++) { |
|
64
|
|
|
$i[] = substr($index, $n, 1); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$passhash = hash('sha256', $passKey); |
|
68
|
|
|
$passhash = (strlen($passhash) < strlen($index)) |
|
69
|
|
|
? hash('sha512', $passKey) |
|
70
|
|
|
: $passhash; |
|
71
|
|
|
|
|
72
|
|
|
for ($n=0; $n < strlen($index); $n++) { |
|
73
|
|
|
$p[] = substr($passhash, $n, 1); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
array_multisort($p, SORT_DESC, $i); |
|
|
|
|
|
|
77
|
|
|
$index = implode($i); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$base = strlen($index); |
|
81
|
|
|
|
|
82
|
|
|
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
|
|
View Code Duplication |
if (is_numeric($pad_up)) { |
|
|
|
|
|
|
103
|
|
|
$pad_up--; |
|
104
|
|
|
if ($pad_up > 0) { |
|
105
|
|
|
$in += pow($base, $pad_up); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$out = ""; |
|
110
|
|
|
for ($t = floor(log($in, $base)); $t >= 0; $t--) { |
|
111
|
|
|
$bcp = bcpow($base, $t); |
|
112
|
|
|
$a = floor($in / $bcp) % $base; |
|
113
|
|
|
$out = $out . substr($index, $a, 1); |
|
114
|
|
|
$in = $in - ($a * $bcp); |
|
115
|
|
|
} |
|
116
|
|
|
$out = strrev($out); // reverse |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $out; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|