From e30e5df3c46f501db7986777d78b62b91f92acd4 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Mon, 25 Jul 2022 00:15:28 +0200 Subject: [PATCH] redirect: Add counting --- redirect.php | 129 ++++++++++++++++++++++++++++----------------------- 1 file changed, 70 insertions(+), 59 deletions(-) diff --git a/redirect.php b/redirect.php index 92c733c..1669b2b 100644 --- a/redirect.php +++ b/redirect.php @@ -35,6 +35,14 @@ INSERT INTO `redirects` SET `path` = ? STR; +$STATEMENTS["Increment"] = << $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_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) { - 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(); + $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"]); } - - // 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); + if ($location != null) { + header("Location: ".$location, true, 307); } 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(); + 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); + die($e); } die(); \ No newline at end of file