From e2ed1845001d8c4c8236fddcd5eec8ab34518c7e Mon Sep 17 00:00:00 2001 From: "Dr. Simon A. Xaies" Date: Wed, 26 Jun 2019 18:02:57 +1000 Subject: [PATCH] Jump Shortened URL API - Version 2.1.9! --- .htaccess | 16 +++++++++------- functions.php | 32 +++++++++++++++++++++++++++++++- index.php | 8 ++++++-- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/.htaccess b/.htaccess index 4f7d103..53a52da 100755 --- a/.htaccess +++ b/.htaccess @@ -7,7 +7,6 @@ ## ## - RewriteEngine On RewriteCond %{HTTP_HOST} !(.*?).([a-z0-9]){1,64}.(localhost|snails.email)$ RewriteCond %{HTTP_HOST} ([a-z0-9]){1,64}.(localhost|snails.email)$ @@ -24,16 +23,19 @@ RewriteRule ^/(media|css|jquery|images)/(.*?)/(.*?).(.*?)$ ./assets/$1/$2/$3.$4 RewriteRule ^/(media|css|jquery|images)/(.*?).(.*?)$ ./assets/$1/$2.$3 [L,NC,QSA] RewriteEngine On -RewriteCond %{HTTP_HOST} !(.*?).([a-z0-9]){1,64}.(localhost|snails.email)$ RewriteCond %{HTTP_HOST} ([a-z0-9]){1,64}.(localhost|snails.email)$ -RewriteCond %{REQUEST_URI} !^assets/(.*?)$ -RewriteCond %{REQUEST_URI} !^([a-z0-9]{2})/(.*?)$ -RewriteRule ^favicon.ico$ ./assets/images/favicon.ico [L,NC,QSA] -RewriteRule ^icon.png$ ./assets/images/icon.png [L,NC,QSA] -RewriteRule ^url.api$ ./index.php?action=url [L,NC,QSA] +RewriteRule ^(php|json|serial|xml)/(.*?)$ ./index.php?action=data&response=$1&item=$2 [L,NC,QSA] RewriteEngine On RewriteCond %{HTTP_HOST} (.*?).([a-z0-9]){1,64}.(localhost|snails.email)$ +RewriteRule ^(php|json|serial|xml)$ ./index.php?response=$1&action=data&subdomain=1 [L,NC,QSA] RewriteRule ^$ ./index.php?action=jump&subdomain=1 [L,NC,QSA] +RewriteEngine On +RewriteCond %{HTTP_HOST} !(.*?).([a-z0-9]){1,64}.(localhost|snails.email)$ +RewriteCond %{HTTP_HOST} ([a-z0-9]){1,64}.(localhost|snails.email)$ +RewriteCond %{REQUEST_URI} ^(favicon|icon|url).(ico|png|api)$ +RewriteRule ^favicon.ico$ ./assets/images/favicon.ico [L,NC,QSA] +RewriteRule ^icon.png$ ./assets/images/icon.png [L,NC,QSA] +RewriteRule ^url.api$ ./index.php?action=url [L,NC,QSA] diff --git a/functions.php b/functions.php index 800c1f8..6c17277 100755 --- a/functions.php +++ b/functions.php @@ -621,7 +621,7 @@ function jumpShortenURL($url = '') if ($values['last'] + $values['inactive'] < microtime(true)) unset($jumps[$finger]); } - $result = $jumps[$hash = md5($url.$referee.microtime(true))] = array("created" => microtime(true), "last" => microtime(true), 'inactive' => (API_DROP_DAYS_INACTIVE * (3600 * 24)), "short" => API_PROTOCOL.API_HOSTNAME.'/v2/'.$referee, "domain" => API_PROTOCOL.$referee.'.'.API_HOSTNAME, 'url' => $url, 'referee' => $referee); + $result = $jumps[$hash = md5($url.$referee.microtime(true))] = array("created" => microtime(true), "last" => microtime(true), 'inactive' => (API_DROP_DAYS_INACTIVE * (3600 * 24)), "short" => API_PROTOCOL.API_HOSTNAME.'/v2/'.$referee, "domain" => API_PROTOCOL.$referee.'.'.API_HOSTNAME, 'url' => $url, 'referee' => $referee, 'timezone' => date_default_timezone_get(), 'data' => array('php' => API_PROTOCOL.API_HOSTNAME.'/php/'.$referee, 'json' => API_PROTOCOL.API_HOSTNAME.'/json/'.$referee, 'serial' => API_PROTOCOL.API_HOSTNAME.'/serial/'.$referee, 'xml' => API_PROTOCOL.API_HOSTNAME.'/xml/'.$referee)); writeRawFile($file, json_encode($jumps)); return $result; } @@ -674,6 +674,36 @@ function jumpFromShortenURL($hash = '') exit(0); } +/** + * + * @param unknown_type $url + * @return multitype:number unknown |multitype:string number + */ +function dataFromShortenURL($hash = '') +{ + $hostname = array_reverse(explode('.', $_SERVER['HTTP_HOST'])); + if (!is_dir(API_PATH_IO_REFEREE)) + mkdirSecure(API_PATH_IO_REFEREE, 0777); + if (!is_file($file = API_PATH_IO_REFEREE . DIRECTORY_SEPARATOR . basename(__DIR__) . '.json')) + $jumps = array(); + else + $jumps = json_decode(file_get_contents($file), true); + foreach($jumps as $finger => $values) + { + if (isset($values['last']) && isset($values['inactive'])) + if ($values['last'] + $values['inactive'] < microtime(true)) + unset($jumps[$finger]); + } + foreach($jumps as $finger => $values) + { + if (strtolower($values['referee']) == strtolower($hash)) + { + return $values; + } + } + return array(); +} + /** * * @param unknown_type $url diff --git a/index.php b/index.php index d403f3b..f18f80d 100755 --- a/index.php +++ b/index.php @@ -32,10 +32,10 @@ * URI Path Finding of API URL Source Locality */ $action = (isset($_REQUEST['action']) && !empty($_REQUEST['action']) ? $_REQUEST['action'] : 'default'); - if ($action == 'jump' && $_REQUEST['subdomain'] == true) { + if (($action == 'jump' || $action == 'data') && $_REQUEST['subdomain'] == true) { $parts = explode(".", $_SERVER['HTTP_HOST']); $item = $parts[0]; - } elseif ($action == 'jump' && !empty($_REQUEST['item'])) + } elseif (($action == 'jump' || $action == 'data') && !empty($_REQUEST['item'])) $item = $_REQUEST['item']; http_response_code(200); @@ -51,7 +51,11 @@ case 'url': $data = jumpShortenURL($_REQUEST['url']); break; + case 'data': + $data = dataFromShortenURL($item); + break; } + $response = (isset($_REQUEST['response'])?$_REQUEST['response']:'raw'); // Send Responses switch ($response)