redirect: Add counting
This commit is contained in:
+70
-59
@@ -35,6 +35,14 @@ INSERT INTO `redirects`
|
||||
SET
|
||||
`path` = ?
|
||||
STR;
|
||||
$STATEMENTS["Increment"] = <<<STR
|
||||
UPDATE `redirects`
|
||||
SET
|
||||
`uses` = `uses` + 1
|
||||
WHERE
|
||||
(`disabled` = false)
|
||||
AND (`path` = ?)
|
||||
STR;
|
||||
|
||||
// Convert text into actual prepared statements.
|
||||
foreach($STATEMENTS as $k => $v) {
|
||||
@@ -42,74 +50,77 @@ STR;
|
||||
}
|
||||
}
|
||||
|
||||
{ // Try and redirect user according to the full provided URI
|
||||
function insert_lookup($url) {
|
||||
global $STATEMENTS;
|
||||
|
||||
$STATEMENTS['Insert']->bind_param('s', $url);
|
||||
if (!$STATEMENTS['Insert']->execute()) {
|
||||
throw new Exception($SQL['Handle']->errno." ".$SQL['Handle']->error);
|
||||
} else if (!$STATEMENTS['Insert']->get_result()) {
|
||||
throw new Exception($SQL['Handle']->errno." ".$SQL['Handle']->error);
|
||||
}
|
||||
}
|
||||
|
||||
function increment_lookup($url) {
|
||||
global $STATEMENTS;
|
||||
|
||||
$STATEMENTS['Increment']->bind_param('s', $url);
|
||||
if (!$STATEMENTS['Increment']->execute()) {
|
||||
throw new Exception($SQL['Handle']->errno." ".$SQL['Handle']->error);
|
||||
} else if (!$STATEMENTS['Increment']->get_result()) {
|
||||
throw new Exception($SQL['Handle']->errno." ".$SQL['Handle']->error);
|
||||
}
|
||||
}
|
||||
|
||||
function redirect_lookup($url) {
|
||||
global $STATEMENTS;
|
||||
|
||||
$STATEMENTS['LookUp']->bind_param('s', $url);
|
||||
if (!$STATEMENTS['LookUp']->execute()) {
|
||||
throw new Exception("".$SQL['Handle']->errno." ".$SQL['Handle']->error);
|
||||
}
|
||||
|
||||
$output = null;
|
||||
$res = $STATEMENTS['LookUp']->get_result();
|
||||
if ($res->num_rows == 0) {
|
||||
insert_lookup($url);
|
||||
} else {
|
||||
while($obj = $res->fetch_object()) {
|
||||
if ($obj->target != null) {
|
||||
$output = $obj->target;
|
||||
increment_lookup($url);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$res->close();
|
||||
return $output;
|
||||
}
|
||||
|
||||
try { // Try and redirect user according to the full provided URI
|
||||
$url = urldecode($_SERVER['REQUEST_URI']);
|
||||
$url_parts = parse_url($url);
|
||||
|
||||
// Try and identify a redirect by the full URI.
|
||||
$STATEMENTS['LookUp']->bind_param('s', $url);
|
||||
if (!$STATEMENTS['LookUp']->execute()) {
|
||||
header("Service Unavailable", true, 503);
|
||||
header("Retry-After: 30", true);
|
||||
die($SQL['Handle']->errno." ".$SQL['Handle']->error);
|
||||
} else {
|
||||
$res = $STATEMENTS['LookUp']->get_result();
|
||||
if ($res->num_rows != 0) {
|
||||
while($obj = $res->fetch_object()) {
|
||||
if ($obj->target != null) {
|
||||
header("Location: ".$obj->target, true, 307);
|
||||
$res->close();
|
||||
die();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$STATEMENTS['Insert']->bind_param('s', $url);
|
||||
if (!$STATEMENTS['Insert']->execute()) {
|
||||
header("Service Unavailable", true, 503);
|
||||
header("Retry-After: 30", true);
|
||||
die($SQL['Handle']->errno." ".$SQL['Handle']->error);
|
||||
} else if (!$STATEMENTS['Insert']->get_result()) {
|
||||
header("Service Unavailable", true, 503);
|
||||
header("Retry-After: 30", true);
|
||||
die($SQL['Handle']->errno." ".$SQL['Handle']->error);
|
||||
}
|
||||
}
|
||||
$res->close();
|
||||
$location = null;
|
||||
// Try and identify a redirect by the full URI.
|
||||
$location = redirect_lookup($url);
|
||||
if ($location == null) {
|
||||
// Try and identify a redirect by the path.
|
||||
$location = redirect_lookup($url_parts["path"]);
|
||||
}
|
||||
|
||||
// Try and identify a redirect by the path.
|
||||
$STATEMENTS['LookUp']->bind_param('s', $url_parts["path"]);
|
||||
if (!$STATEMENTS['LookUp']->execute()) {
|
||||
header("Service Unavailable", true, 503);
|
||||
header("Retry-After: 30", true);
|
||||
die($SQL['Handle']->errno." ".$SQL['Handle']->error);
|
||||
if ($location != null) {
|
||||
header("Location: ".$location, true, 307);
|
||||
} else {
|
||||
$res = $STATEMENTS['LookUp']->get_result();
|
||||
if ($res->num_rows != 0) {
|
||||
while($obj = $res->fetch_object()) {
|
||||
if ($obj->target != null) {
|
||||
header("Location: ".$obj->target, true, 307);
|
||||
$res->close();
|
||||
die();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$STATEMENTS['Insert']->bind_param('s', $url_parts["path"]);
|
||||
if (!$STATEMENTS['Insert']->execute()) {
|
||||
header("Service Unavailable", true, 503);
|
||||
header("Retry-After: 30", true);
|
||||
die($SQL['Handle']->errno." ".$SQL['Handle']->error);
|
||||
} else if (!$STATEMENTS['Insert']->get_result()) {
|
||||
header("Service Unavailable", true, 503);
|
||||
header("Retry-After: 30", true);
|
||||
die($SQL['Handle']->errno." ".$SQL['Handle']->error);
|
||||
}
|
||||
}
|
||||
$res->close();
|
||||
header("Location: https://www.xaymar.com/404.html");
|
||||
}
|
||||
|
||||
// In all other cases, just redirect to 404.
|
||||
header("Location: https://www.xaymar.com/404.html");
|
||||
} catch(Exception $e) {
|
||||
header("Service Unavailable", true, 503);
|
||||
header("Retry-After: 30", true);
|
||||
die($e);
|
||||
}
|
||||
|
||||
die();
|
||||
Reference in New Issue
Block a user