-
Notifications
You must be signed in to change notification settings - Fork 47
/
index.php
71 lines (63 loc) · 1.91 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
<?php
include('-/config.php');
include('-/db.php');
$token = (isset($_GET['token']) ? $_GET['token'] : '');
$show_stats = (isset($_GET['stats']) OR strrpos($token, '/stats') !== false);
if (RECORD_URL_STATS OR $show_stats) {
include('-/stats.php');
}
/*
* DEVELOPERS:
* Note the following possible redir_type values:
* - 'auto' - Automatically assigned slug. 301 redirect on access.
* - 'custom' - Manually set slug. 301 redirect on access.
* - 'alias' - Its 'url' is really just another slug. Do a recursive lookup to redirect on access.
* - 'gone' - Access results in a 410; should never change
*/
// Redirect lookup
while($token != '') // Loop so we can handle aliases
{
// Look up slug
// TODO: Use PDO::prepare in "The other index.php"
$stmt = $db->prepare('SELECT * FROM '.DB_PREFIX.'urls WHERE BINARY custom_url = BINARY :slug AND custom_url = :slug LIMIT 1');
$stmt->execute(array('slug'=>$token));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($stmt AND $row)
{
if(RECORD_URL_STATS)
record_stats($db, $row['id']);
if($row['redir_type'] == 'gone') {
header($_SERVER['SERVER_PROTOCOL'].' 410 Gone');
die('The redirection in question no longer exists.');
} elseif($row['redir_type'] == 'alias') {
// Handle aliases, and watch out for infinite loops
if($row['url'] != $token)
{
$token = $row['url'];
continue;
}
else {
// Incorrectly configured. "Should never happen"
$token = '';
break;
}
} else {
// Handle standard redirections, both custom and auto-assigned
header($_SERVER['SERVER_PROTOCOL'].' 301 Moved Permanently');
header('Location:'.$row['url']);
exit();
}
//Unreachable, thanks to "else"
}
else
{
// 404!
// no redirect
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
header('Status:404');
die('404: Nothing found for '.htmlentities($token));
}
}
if(defined('HOMEPAGE_URL') && HOMEPAGE_URL)
header("Location: ".HOMEPAGE_URL);
exit;