<?php /** * Where redirect happens */ require_once "../Library/Tiny.php"; require_once "../Library/functions.php"; require_once "../Library/LogMessage.php"; $url = isset($_GET['url']) ? $_GET['url'] : null; $url = rtrim($url, '/'); $url = explode('/', $url); if (count($url) == 1) { try { $tiny = new Tiny(); $tiny->registerRequest($url[0]); $request = $tiny->getRequestUrl($url[0]); $urlObj = json_decode($request); if ($urlObj->status == 'success') { redirectPage($urlObj->url); } else { redirectPage("error.php"); } } catch (Exception $e) { LogMessage::exception($e->getMessage(), "run.php", "run.php"); redirectPage("404.php"); } } else { redirectPage("error.php"); }
/** * registers all the URL requests gotten on the site * @param $url * @throws Exception */ public function registerRequest($url) { try { $query = "INSERT INTO requests (tiny_url, created_at, user_agent, server_ip, remote_ip)\n VALUES ( :tinyUrl , NOW(), :user_agent, :server_ip, :remote_ip)"; $sql = $this->connection->prepare($query); $sql->bindParam(":tinyUrl", $url, PDO::PARAM_STR); $sql->bindParam(":user_agent", $_SERVER["HTTP_USER_AGENT"], PDO::PARAM_STR); $sql->bindParam(":server_ip", $_SERVER["SERVER_ADDR"], PDO::PARAM_STR); $sql->bindParam(":remote_ip", $_SERVER["REMOTE_ADDR"], PDO::PARAM_STR); $sql->execute(); } catch (Exception $e) { LogMessage::exception($e->getMessage(), __FUNCTION__, __CLASS__); throw new Exception("Error registering new URL", 3); } }