Improve automatic redirections

This commit is contained in:
Michael Fabian 'Xaymar' Dirks
2022-05-15 07:55:22 +02:00
parent 5096a4b689
commit dc164e4763
+58 -22
View File
@@ -42,38 +42,74 @@ STR;
}
}
// Redirect the user according to found information
$name = urldecode($_SERVER['REQUEST_URI']);
$STATEMENTS['LookUp']->bind_param('s', $name);
{ // 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) {
// Insert the undiscovered URL into the redirect database.
$STATEMENTS['Insert']->bind_param('s', $name);
if (!$STATEMENTS['Insert']->execute()) {
header("Service Unavailable", true, 503);
die($SQL['Handle']->errno." ".$SQL['Handle']->error);
} else {
if (!$STATEMENTS['Insert']->get_result()) {
header("Service Unavailable", true, 503);
die($SQL['Handle']->errno." ".$SQL['Handle']->error);
}
}
header("Location: https://www.xaymar.com/404.html");
} else {
if ($res->num_rows != 0) {
while($obj = $res->fetch_object()) {
if ($obj->target != null) {
header("Location: ".$obj->target, true, 307);
} else {
header("Location: https://www.xaymar.com/404.html");
$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.
$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);
} 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();
}
// In all other cases, just redirect to 404.
header("Location: https://www.xaymar.com/404.html");
}
die();