shuri/index.php

67 lines
1.4 KiB
PHP
Raw Normal View History

2013-07-09 19:47:59 +02:00
<?php
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);
$content = '<a href="./?'.$urlhash.'">http://'.$_SERVER['HTTP_HOST'].'/?'.$urlhash.'</a>';
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>