-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkShrink.php
More file actions
executable file
·84 lines (72 loc) · 2.32 KB
/
Copy pathLinkShrink.php
File metadata and controls
executable file
·84 lines (72 loc) · 2.32 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
<?php
class LinkShrink
{
private $redis;
private $shrinkerUrl = 'http://perfnerd.de/7val-ls';
public function __construct()
{
$this->redis = $this->redisConnect();
}
private function redisConnect()
{
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
return $redis;
}
public function checkLinkExists($shortLink)
{
if ($shortLink != '') {
if ($this->redis->exists($shortLink) > 0) {
return true;
}
} else {
return false;
}
}
public function shrinkUrls(string $url, string $campaignSource, string $campaignMedium, string $campaignName, string $campaignTerm, string $campaignContent, string $customLink = '')
{
$sources = [];
if (preg_match('/.*\[([0-9,].+)\]$/', $campaignSource, $matches) && $customLink == '') {
if (count($numbers = explode(',', $matches[1])) == 2) {
for ($i = $numbers[0]; $i <= $numbers[1]; $i++) {
$sources[] = $i;
}
$campaignSource = substr($campaignSource, 0, strpos($campaignSource, '['));
}
} else {
$sources[] = '';
}
foreach ($sources as $postfix) {
$analyticsUrls[] = $url . '/?utm_source=' . rawurlencode($campaignSource) . $postfix . '&utm_medium=' .rawurlencode($campaignMedium) . '&utm_campaign=' . rawurlencode($campaignName) . '&utm_term=' . rawurlencode($campaignTerm) . '&utm_content=' . rawurlencode($campaignContent);
}
foreach ($analyticsUrls as $anUrl) {
$shortUrl = $this->generateShortLink($anUrl);
if (!empty($customLink) && !$this->checkLinkExists($customLink)) {
$shortUrl = $this->shrinkerUrl . '/' . $customLink;
}
$urls[] = ['longurl' => $anUrl, 'shorturl' => $shortUrl];
$this->saveLink($anUrl, $shortUrl);
}
return $urls;
}
public function expandUrl(string $shortUrl) : string
{
return $this->redis->get($shortUrl);
}
public function saveLink(string $longLink, string $shortLink) : bool
{
return $this->redis->set($shortLink, $longLink);
}
public function generateShortLink(string $longLink) : string
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
do {
for ($i = 0; $i < 5; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
} while ($this->checkLinkExists($randomString));
return $this->shrinkerUrl. '/' . $randomString;
}
}