shuri/index.php

83 lines
1.9 KiB
PHP
Raw Permalink Normal View History

2013-07-09 19:47:59 +02:00
<?php
2017-02-07 13:15:26 +01:00
require __DIR__.'/vendor/autoload.php';
use Endroid\QrCode\QrCode;
function smallHash($text)
{
$t = rtrim(base64_encode(hash('crc32', $text, true)), '=');
return strtr($t, '+/', '-_');
}
2016-12-13 13:16:35 +01:00
if (!empty($_GET['url'])) {
$url = $_GET['url'];
2013-07-09 19:47:59 +02:00
2016-12-13 13:16:35 +01:00
if (strpos($url, 'http://') !== 0 && strpos($url, 'https://') !== 0) {
$url = 'http://'.$url;
}
2013-07-09 19:47:59 +02:00
$urlhash = smallHash($url);
2013-07-09 19:47:59 +02:00
2016-12-13 13:16:35 +01:00
$hashfolder = substr($urlhash, 0, 2);
$hashfile = substr($urlhash, 2);
2013-07-09 19:47:59 +02:00
2016-12-13 13:16:35 +01:00
$hashfolderpath = './db/'.$hashfolder;
$hashfilepath = $hashfolderpath.'/'.$hashfile;
2013-07-09 19:47:59 +02:00
2016-12-13 13:16:35 +01:00
mkdir($hashfolderpath, 0700, true);
2013-07-09 19:47:59 +02:00
2016-12-13 13:16:35 +01:00
file_put_contents($hashfilepath, $url);
2017-02-07 13:07:39 +01:00
2018-04-26 16:28:26 -04:00
$shortUrl = $_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].'?'.$urlhash;
2017-02-07 13:07:39 +01:00
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
$shortUrl = 'https://'.$shortUrl;
} else {
$shortUrl = 'http://'.$shortUrl;
}
2017-02-07 13:15:26 +01:00
$qrcode = (new QrCode())->setText($shortUrl);
$content = '<a href="'.$shortUrl.'">'.$shortUrl.'</a><br>'
2017-02-07 13:15:26 +01:00
.'<img src="data:'.$qrcode->getContentType().';base64,'.base64_encode($qrcode->get()).'">';
2016-12-13 13:16:35 +01:00
} elseif (!empty($_GET)) {
$urlhash = key($_GET);
2013-07-09 19:47:59 +02:00
2016-12-13 13:16:35 +01:00
$hashfolder = substr($urlhash, 0, 2);
$hashfile = substr($urlhash, 2);
2013-07-09 19:47:59 +02:00
2016-12-13 13:16:35 +01:00
$hashfolderpath = './db/'.$hashfolder;
$hashfilepath = $hashfolderpath.'/'.$hashfile;
2013-07-09 19:47:59 +02:00
2016-12-13 13:37:49 +01:00
$findfiles = glob($hashfilepath);
2013-07-09 19:47:59 +02:00
2016-12-13 13:38:51 +01:00
if (!empty($findfiles)) {
$fullfilepath = current($findfiles);
2013-07-09 19:47:59 +02:00
2016-12-13 13:38:51 +01:00
header('Location:'.file_get_contents($fullfilepath));
return 0;
2016-12-13 13:16:35 +01:00
}
2013-07-09 19:47:59 +02:00
2016-12-13 13:38:51 +01:00
$content = 'No link match this identifier.';
2014-02-15 12:16:33 +01:00
} else {
2016-12-13 13:16:35 +01:00
$content = '<form method="get">
2014-02-15 12:16:33 +01:00
Enter your URL: <input type="text" name="url"><input type="submit" value="Submit">
</form>';
2013-07-09 19:47:59 +02:00
}
2014-02-15 12:16:33 +01:00
//actual page below
?>
<!DOCTYPE html>
<html>
<head>
<title>Shuri</title>
</head>
<body>
<div id="content">
2016-12-13 13:20:30 +01:00
<?= $content ?>
2014-02-15 12:16:33 +01:00
</div>
</body>
</html>