redirect: Add counting
This commit is contained in:
+69
-58
@@ -35,6 +35,14 @@ INSERT INTO `redirects`
|
|||||||
SET
|
SET
|
||||||
`path` = ?
|
`path` = ?
|
||||||
STR;
|
STR;
|
||||||
|
$STATEMENTS["Increment"] = <<<STR
|
||||||
|
UPDATE `redirects`
|
||||||
|
SET
|
||||||
|
`uses` = `uses` + 1
|
||||||
|
WHERE
|
||||||
|
(`disabled` = false)
|
||||||
|
AND (`path` = ?)
|
||||||
|
STR;
|
||||||
|
|
||||||
// Convert text into actual prepared statements.
|
// Convert text into actual prepared statements.
|
||||||
foreach($STATEMENTS as $k => $v) {
|
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 = urldecode($_SERVER['REQUEST_URI']);
|
||||||
$url_parts = parse_url($url);
|
$url_parts = parse_url($url);
|
||||||
|
|
||||||
|
$location = null;
|
||||||
// Try and identify a redirect by the full URI.
|
// Try and identify a redirect by the full URI.
|
||||||
$STATEMENTS['LookUp']->bind_param('s', $url);
|
$location = redirect_lookup($url);
|
||||||
if (!$STATEMENTS['LookUp']->execute()) {
|
if ($location == null) {
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try and identify a redirect by the path.
|
// Try and identify a redirect by the path.
|
||||||
$STATEMENTS['LookUp']->bind_param('s', $url_parts["path"]);
|
$location = redirect_lookup($url_parts["path"]);
|
||||||
if (!$STATEMENTS['LookUp']->execute()) {
|
}
|
||||||
header("Service Unavailable", true, 503);
|
if ($location != null) {
|
||||||
header("Retry-After: 30", true);
|
header("Location: ".$location, true, 307);
|
||||||
die($SQL['Handle']->errno." ".$SQL['Handle']->error);
|
|
||||||
} else {
|
} else {
|
||||||
$res = $STATEMENTS['LookUp']->get_result();
|
header("Location: https://www.xaymar.com/404.html");
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// In all other cases, just redirect to 404.
|
// In all other cases, just redirect to 404.
|
||||||
header("Location: https://www.xaymar.com/404.html");
|
header("Location: https://www.xaymar.com/404.html");
|
||||||
|
} catch(Exception $e) {
|
||||||
|
header("Service Unavailable", true, 503);
|
||||||
|
header("Retry-After: 30", true);
|
||||||
|
die($e);
|
||||||
}
|
}
|
||||||
|
|
||||||
die();
|
die();
|
||||||
Reference in New Issue
Block a user