Files
com.xaymar.www/redirect.php
T

127 lines
3.1 KiB
PHP
Raw Normal View History

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;
2022-07-25 00:15:28 +02:00
$STATEMENTS["Increment"] = <<<STR
UPDATE `redirects`
SET
`uses` = `uses` + 1
WHERE
(`disabled` = false)
AND (`path` = ?)
STR;
2022-03-09 10:41:12 +01:00
// Convert text into actual prepared statements.
foreach($STATEMENTS as $k => $v) {
$STATEMENTS[$k] = $SQL['Handle']->prepare($v);
}
}
2022-07-25 00:15:28 +02:00
function insert_lookup($url) {
global $STATEMENTS;
2022-08-13 15:41:35 +02:00
global $SQL;
2022-07-25 00:15:28 +02:00
$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;
2022-08-13 15:41:35 +02:00
global $SQL;
2022-07-25 00:15:28 +02:00
$STATEMENTS['Increment']->bind_param('s', $url);
if (!$STATEMENTS['Increment']->execute()) {
throw new Exception($SQL['Handle']->errno." ".$SQL['Handle']->error);
}
}
function redirect_lookup($url) {
global $STATEMENTS;
2022-08-13 15:41:35 +02:00
global $SQL;
2022-03-09 10:41:12 +01:00
2022-05-15 07:55:22 +02:00
$STATEMENTS['LookUp']->bind_param('s', $url);
if (!$STATEMENTS['LookUp']->execute()) {
2022-07-25 00:15:28 +02:00
throw new Exception("".$SQL['Handle']->errno." ".$SQL['Handle']->error);
}
$output = null;
$res = $STATEMENTS['LookUp']->get_result();
if ($res->num_rows == 0) {
insert_lookup($url);
2022-03-09 10:41:12 +01:00
} else {
2022-07-25 00:15:28 +02:00
while($obj = $res->fetch_object()) {
if ($obj->target != null) {
$output = $obj->target;
increment_lookup($url);
break;
2022-05-15 07:55:22 +02:00
}
2022-03-09 10:41:12 +01:00
}
}
2022-05-15 07:55:22 +02:00
2022-07-25 00:15:28 +02:00
$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);
$location = null;
2022-08-13 15:41:35 +02:00
// Try and identify a redirect by the full URI.
2022-07-25 00:15:28 +02:00
$location = redirect_lookup($url);
if ($location == null) {
// Try and identify a redirect by the path.
$location = redirect_lookup($url_parts["path"]);
}
if ($location != null) {
header("Location: ".$location, true, 307);
2022-05-15 07:55:22 +02:00
} else {
2022-07-25 00:15:28 +02:00
header("Location: https://www.xaymar.com/404.html");
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-07-25 00:15:28 +02:00
} catch(Exception $e) {
2022-08-13 15:41:35 +02:00
//header("Service Unavailable", true, 503);
header("Retry-After: 30", true, 503);
echo($e);
2022-03-09 10:41:12 +01:00
}
2022-08-13 15:41:35 +02:00
//die();