-
Notifications
You must be signed in to change notification settings - Fork 216
/
redirect.php
56 lines (48 loc) · 1.43 KB
/
redirect.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
<?php
/*
* First authored by Brian Cray
* License: http://creativecommons.org/licenses/by/3.0/
* Contact the author at http://briancray.com/
*/
ini_set('display_errors', 0);
if(!preg_match('|^[0-9a-zA-Z]{1,6}$|', $_GET['url']))
{
die('That is not a valid short url');
}
require('config.php');
$shortened_id = getIDFromShortenedURL($_GET['url']);
if(CACHE)
{
$long_url = file_get_contents(CACHE_DIR . $shortened_id);
if(empty($long_url) || !preg_match('|^https?://|', $long_url))
{
$long_url = mysql_result(mysql_query('SELECT long_url FROM ' . DB_TABLE . ' WHERE id="' . mysql_real_escape_string($shortened_id) . '"'), 0, 0);
@mkdir(CACHE_DIR, 0777);
$handle = fopen(CACHE_DIR . $shortened_id, 'w+');
fwrite($handle, $long_url);
fclose($handle);
}
}
else
{
$long_url = mysql_result(mysql_query('SELECT long_url FROM ' . DB_TABLE . ' WHERE id="' . mysql_real_escape_string($shortened_id) . '"'), 0, 0);
}
if(TRACK)
{
mysql_query('UPDATE ' . DB_TABLE . ' SET referrals=referrals+1 WHERE id="' . mysql_real_escape_string($shortened_id) . '"');
}
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $long_url);
exit;
function getIDFromShortenedURL ($string, $base = ALLOWED_CHARS)
{
$length = strlen($base);
$size = strlen($string) - 1;
$string = str_split($string);
$out = strpos($base, array_pop($string));
foreach($string as $i => $char)
{
$out += strpos($base, $char) * pow($length, $size - $i);
}
return $out;
}