-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.php
82 lines (60 loc) · 1.87 KB
/
index.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
<?php
require __DIR__.'/vendor/autoload.php';
use Endroid\QrCode\QrCode;
function smallHash($text)
{
$t = rtrim(base64_encode(hash('crc32', $text, true)), '=');
return strtr($t, '+/', '-_');
}
if (!empty($_GET['url'])) {
$url = $_GET['url'];
if (strpos($url, 'http://') !== 0 && strpos($url, 'https://') !== 0) {
$url = 'http://'.$url;
}
$urlhash = smallHash($url);
$hashfolder = substr($urlhash, 0, 2);
$hashfile = substr($urlhash, 2);
$hashfolderpath = './db/'.$hashfolder;
$hashfilepath = $hashfolderpath.'/'.$hashfile;
mkdir($hashfolderpath, 0700, true);
file_put_contents($hashfilepath, $url);
$shortUrl = $_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].'?'.$urlhash;
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
$shortUrl = 'https://'.$shortUrl;
} else {
$shortUrl = 'http://'.$shortUrl;
}
$qrcode = (new QrCode())->setText($shortUrl);
$content = '<a href="'.$shortUrl.'">'.$shortUrl.'</a><br>'
.'<img src="data:'.$qrcode->getContentType().';base64,'.base64_encode($qrcode->get()).'">';
} elseif (!empty($_GET)) {
$urlhash = key($_GET);
$hashfolder = substr($urlhash, 0, 2);
$hashfile = substr($urlhash, 2);
$hashfolderpath = './db/'.$hashfolder;
$hashfilepath = $hashfolderpath.'/'.$hashfile;
$findfiles = glob($hashfilepath);
if (!empty($findfiles)) {
$fullfilepath = current($findfiles);
header('Location:'.file_get_contents($fullfilepath));
return 0;
}
$content = 'No link match this identifier.';
} else {
$content = '<form method="get">
Enter your URL: <input type="text" name="url"><input type="submit" value="Submit">
</form>';
}
//actual page below
?>
<!DOCTYPE html>
<html>
<head>
<title>Shuri</title>
</head>
<body>
<div id="content">
<?= $content ?>
</div>
</body>
</html>