2022-03-09 10:41:12 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
// SQL Information and Related
|
|
|
|
|
$SQL = array();
|
|
|
|
|
$SQL['Host'] = "database.svc.xaymar.com";
|
|
|
|
|
$SQL['Database'] = "com.xaymar.www";
|
|
|
|
|
$SQL['Username'] = "com.xaymar.www";
|
|
|
|
|
$SQL['Password'] = "G_5R_ZXm6Uek9DEt";
|
|
|
|
|
$SQL['Persistent'] = true; // Persistent database connection. (Recommended)
|
|
|
|
|
|
|
|
|
|
{ // MySQL Database Connection
|
|
|
|
|
// Try and connect to the database (otherwise die).
|
|
|
|
|
ini_set("mysqli.allow_persistent", $SQL['Persistent']);
|
|
|
|
|
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
|
|
|
|
|
$SQL['Handle'] = new mysqli($SQL['Host'], $SQL['Username'], $SQL['Password'], $SQL['Database']);
|
|
|
|
|
if ($SQL['Handle']->connect_errno) {
|
|
|
|
|
header("500 Internal Server Error");
|
|
|
|
|
die($SQL['Handle']->connect_errno." ".$SQL['Handle']->connect_error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{ // Prepare a few statements.
|
|
|
|
|
$STATEMENTS = array();
|
|
|
|
|
$STATEMENTS["LookUp"] = <<<STR
|
|
|
|
|
SELECT `target`
|
|
|
|
|
FROM `redirects`
|
|
|
|
|
WHERE
|
|
|
|
|
(`disabled` = false)
|
|
|
|
|
AND (`path` = ?)
|
|
|
|
|
ORDER BY
|
|
|
|
|
`date_created` DESC
|
|
|
|
|
STR;
|
|
|
|
|
$STATEMENTS["Insert"] = <<<STR
|
|
|
|
|
INSERT INTO `redirects`
|
|
|
|
|
SET
|
|
|
|
|
`path` = ?
|
|
|
|
|
STR;
|
|
|
|
|
|
|
|
|
|
// Convert text into actual prepared statements.
|
|
|
|
|
foreach($STATEMENTS as $k => $v) {
|
|
|
|
|
$STATEMENTS[$k] = $SQL['Handle']->prepare($v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-15 07:55:22 +02:00
|
|
|
{ // Try and redirect user according to the full provided URI
|
|
|
|
|
$url = urldecode($_SERVER['REQUEST_URI']);
|
|
|
|
|
$url_parts = parse_url($url);
|
2022-03-09 10:41:12 +01:00
|
|
|
|
2022-05-15 07:55:22 +02:00
|
|
|
// Try and identify a redirect by the full URI.
|
|
|
|
|
$STATEMENTS['LookUp']->bind_param('s', $url);
|
|
|
|
|
if (!$STATEMENTS['LookUp']->execute()) {
|
2022-03-09 10:41:12 +01:00
|
|
|
header("Service Unavailable", true, 503);
|
2022-05-15 07:55:22 +02:00
|
|
|
header("Retry-After: 30", true);
|
2022-03-09 10:41:12 +01:00
|
|
|
die($SQL['Handle']->errno." ".$SQL['Handle']->error);
|
|
|
|
|
} else {
|
2022-05-15 07:55:22 +02:00
|
|
|
$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);
|
|
|
|
|
}
|
2022-03-09 10:41:12 +01:00
|
|
|
}
|
2022-05-15 07:55:22 +02:00
|
|
|
$res->close();
|
2022-03-09 10:41:12 +01:00
|
|
|
}
|
2022-05-15 07:55:22 +02:00
|
|
|
|
|
|
|
|
// Try and identify a redirect by the path.
|
2022-06-21 19:57:02 +02:00
|
|
|
$STATEMENTS['LookUp']->bind_param('s', $url_parts["path"]);
|
2022-05-15 07:55:22 +02:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-09 10:41:12 +01:00
|
|
|
} else {
|
2022-06-21 19:57:02 +02:00
|
|
|
$STATEMENTS['Insert']->bind_param('s', $url_parts["path"]);
|
2022-05-15 07:55:22 +02:00
|
|
|
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);
|
|
|
|
|
}
|
2022-03-09 10:41:12 +01:00
|
|
|
}
|
2022-05-15 07:55:22 +02:00
|
|
|
$res->close();
|
2022-03-09 10:41:12 +01:00
|
|
|
}
|
2022-05-15 07:55:22 +02:00
|
|
|
|
|
|
|
|
// In all other cases, just redirect to 404.
|
|
|
|
|
header("Location: https://www.xaymar.com/404.html");
|
2022-03-09 10:41:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
die();
|