127 lines
3.1 KiB
PHP
127 lines
3.1 KiB
PHP
<?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;
|
|
$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) {
|
|
$STATEMENTS[$k] = $SQL['Handle']->prepare($v);
|
|
}
|
|
}
|
|
|
|
function insert_lookup($url) {
|
|
global $STATEMENTS;
|
|
global $SQL;
|
|
|
|
$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;
|
|
global $SQL;
|
|
|
|
$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;
|
|
global $SQL;
|
|
|
|
$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);
|
|
|
|
$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"]);
|
|
}
|
|
if ($location != null) {
|
|
header("Location: ".$location, true, 307);
|
|
} else {
|
|
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, 503);
|
|
echo($e);
|
|
}
|
|
|
|
//die();
|