2014-02-14 16:02:59 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
//POLR API - by http://github.com/cydrobolt/polr
|
2014-06-21 15:27:18 +02:00
|
|
|
//@Author: Cydrobolt and sckasturi
|
2014-02-14 16:02:59 +01:00
|
|
|
|
2014-06-21 15:27:18 +02:00
|
|
|
/*
|
|
|
|
* Reference:
|
|
|
|
* Request Vars Listing:
|
|
|
|
* ReqEndpoint: url - the url to perform action on
|
|
|
|
* ReqEndpoint: apikey - the APIKey provided for authentication
|
|
|
|
* ReqEndpoint: action - action to perform, either lookup or shorten
|
|
|
|
* OptEndpoint: temp - whether the URL is temporary or not
|
|
|
|
*/
|
|
|
|
$reqargs['nosession'] = true;
|
2014-12-28 20:52:06 +01:00
|
|
|
require_once('lib-core.php'); //Fetch Config
|
2014-06-21 15:27:18 +02:00
|
|
|
require_once('dnsbl.php'); //Load Google SafeBrowsing Script
|
|
|
|
|
|
|
|
$protocol = '://';
|
|
|
|
if (!strstr($_REQUEST['url'], $protocol)) {
|
|
|
|
$urlr = "http" . $protocol . $_REQUEST['url']; //add http:// if :// not there
|
|
|
|
}
|
|
|
|
|
|
|
|
$dnsbl = new dnsbl(); //create a gsb object
|
|
|
|
if (is_string($_REQUEST['apikey']) && is_string($_REQUEST['action']) && is_string($_REQUEST['url'])) {
|
2014-02-14 16:02:59 +01:00
|
|
|
$apikey = $mysqli->real_escape_string($_REQUEST['apikey']); //Sanitize input
|
|
|
|
$action = $mysqli->real_escape_string($_REQUEST['action']);
|
|
|
|
$url_api = $mysqli->real_escape_string($_REQUEST['url']);
|
2014-06-21 15:27:18 +02:00
|
|
|
} else {
|
|
|
|
header("HTTP/1.0 400 Bad Request");
|
|
|
|
die("Error: No value specified, or wrong data type.");
|
2014-02-14 16:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//checking API key:
|
2014-06-21 15:27:18 +02:00
|
|
|
$query = "SELECT `valid`,`quota` FROM `api` WHERE apikey='$apikey'";
|
2014-02-14 16:02:59 +01:00
|
|
|
$result = $mysqli->query($query) or showerror();
|
2014-06-21 15:27:18 +02:00
|
|
|
$validrow = mysqli_fetch_assoc($result);
|
|
|
|
$userquota = $validrow['quota'];
|
2014-02-14 16:02:59 +01:00
|
|
|
//check if valid
|
2014-06-21 15:27:18 +02:00
|
|
|
if (!$validrow['valid']) {
|
2014-02-14 16:02:59 +01:00
|
|
|
$api_key_valid = 0;
|
2014-06-21 15:27:18 +02:00
|
|
|
} else {
|
2014-02-14 16:02:59 +01:00
|
|
|
$api_key_valid = 1;
|
|
|
|
}
|
|
|
|
|
2014-06-21 15:27:18 +02:00
|
|
|
if (!$api_key_valid) {
|
|
|
|
header("HTTP/1.0 401 Unauthorized"); //Access denied - invalid key
|
|
|
|
die('401 Unauthorized');
|
2014-02-14 16:02:59 +01:00
|
|
|
}
|
|
|
|
|
2014-06-21 15:27:18 +02:00
|
|
|
if (!filter_var($url_api, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) && $action != "lookup") {
|
|
|
|
header("HTTP/1.0 400 Bad Request");
|
2014-02-14 16:02:59 +01:00
|
|
|
echo "Error: URL is not valid"; //URL not well formatted, but allow if action is lookup
|
|
|
|
die();
|
|
|
|
}
|
2014-06-21 15:27:18 +02:00
|
|
|
//Check if URL given is malware/phishing
|
|
|
|
|
|
|
|
$isbl = $dnsbl->isbl($url_api);
|
|
|
|
if ($isbl === "malware" || $isbl === "phishing") {
|
|
|
|
header("HTTP/1.0 401 Unauthorized");
|
|
|
|
echo "Polr does not shorten potentially malicious URLs"; //If link tests positive to possible malware/phish, then block
|
|
|
|
die();
|
|
|
|
}
|
2014-02-14 16:02:59 +01:00
|
|
|
|
2014-06-21 15:27:18 +02:00
|
|
|
function lookup($urltl) {
|
2014-02-14 16:02:59 +01:00
|
|
|
global $mysqli;
|
|
|
|
$val = $mysqli->real_escape_string($urltl);
|
|
|
|
$query = "SELECT rurl FROM redirinfo WHERE baseval='{$val}'";
|
|
|
|
$result = $mysqli->query($query) or die("QUERY ERROR");
|
|
|
|
$row = mysqli_fetch_assoc($result);
|
|
|
|
return $row['rurl'];
|
|
|
|
}
|
|
|
|
|
2014-06-21 15:27:18 +02:00
|
|
|
function exquota($apikey, $quota) {
|
|
|
|
|
|
|
|
if ($quota < 1) {
|
|
|
|
return false; // if quota is negative, then no quota
|
|
|
|
}
|
|
|
|
|
|
|
|
global $mysqli;
|
|
|
|
$last_min = time()-60;
|
|
|
|
$query = "SELECT `rurl` FROM `redirinfo` WHERE user='APIKEY-{$apikey}' AND UNIX_TIMESTAMP(date) > $last_min;";
|
|
|
|
$result = $mysqli->query($query) or showerror();
|
|
|
|
$total_queries = $mysqli->affected_rows; //get the amount of queries in the past minute
|
|
|
|
$query = "SELECT `rurl` FROM `redirinfo-temp` WHERE user='APIKEY-{$apikey}' AND UNIX_TIMESTAMP(date) > $last_min;";
|
|
|
|
$result = $mysqli->query($query) or showerror();
|
|
|
|
$total_queries_temp = $mysqli->affected_rows; //get the amount of queries to temp in the past minute
|
|
|
|
if (($total_queries+$total_queries_temp) >= $quota) {
|
|
|
|
return true; // if met/exeeding quota
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function shorten($urlr, $t = 'false') {
|
2014-02-14 16:02:59 +01:00
|
|
|
global $mysqli;
|
|
|
|
global $wsa;
|
2014-06-21 15:27:18 +02:00
|
|
|
global $apikey;
|
|
|
|
global $ip;
|
|
|
|
|
|
|
|
$protocol = '://';
|
|
|
|
$isshort = array('polr.cf', 'bit.ly', 'is.gd', 'tiny.cc', 'adf.ly', 'ur1.ca', 'goo.gl', 'ow.ly', 'j.mp', 't.co');
|
|
|
|
foreach ($isshort as $url_shorteners) {
|
|
|
|
if (strstr($urlr, $protocol . $url_shorteners)) {
|
|
|
|
header("HTTP/1.0 400 Bad Request");
|
|
|
|
die("400 Bad Request (URL Already a ShortURL)");
|
|
|
|
}
|
|
|
|
}
|
2014-02-14 16:02:59 +01:00
|
|
|
$query1 = "SELECT rid FROM redirinfo WHERE rurl='{$urlr}'";
|
|
|
|
$result = $mysqli->query($query1);
|
|
|
|
$row = mysqli_fetch_assoc($result);
|
|
|
|
$existing = $row['rid'];
|
2014-06-21 15:27:18 +02:00
|
|
|
if (!$existing) {
|
|
|
|
if ($t != 'false') {
|
|
|
|
//if tempurl
|
|
|
|
$query1 = "SELECT MAX(rid) AS rid FROM `redirinfo-temp`;";
|
|
|
|
$result = $mysqli->query($query1);
|
|
|
|
$row = mysqli_fetch_assoc($result);
|
|
|
|
$ridr = $row['rid'];
|
|
|
|
$baseval = "t-" . (string) (base_convert($ridr + 1, 10, 36));
|
|
|
|
$query2 = "INSERT INTO `redirinfo-temp` (baseval,user,rurl,ip) VALUES ('{$baseval}','APIKEY-{$apikey}','{$urlr}','{$ip}');";
|
|
|
|
} else {
|
|
|
|
//if NOT tempurl
|
|
|
|
$query1 = "SELECT MAX(rid) AS rid FROM redirinfo;";
|
|
|
|
$result = $mysqli->query($query1);
|
|
|
|
$row = mysqli_fetch_assoc($result);
|
|
|
|
$ridr = $row['rid'];
|
|
|
|
$baseval = base_convert($ridr + 1, 10, 36);
|
|
|
|
$query2 = "INSERT INTO redirinfo (baseval,user,rurl,ip) VALUES ('{$baseval}','APIKEY-{$apikey}','{$urlr}','{$ip}');";
|
|
|
|
}
|
|
|
|
|
2014-02-14 16:02:59 +01:00
|
|
|
$result2r = $mysqli->query($query2) or showerror();
|
|
|
|
return "http://{$wsa}/{$baseval}";
|
2014-06-21 15:27:18 +02:00
|
|
|
} else {
|
2014-02-14 16:02:59 +01:00
|
|
|
$query1 = "SELECT baseval FROM redirinfo WHERE rurl='{$urlr}'";
|
|
|
|
$result = $mysqli->query($query1);
|
|
|
|
$row = mysqli_fetch_assoc($result);
|
2014-06-21 15:27:18 +02:00
|
|
|
$baseval = $row['baseval'];
|
|
|
|
return "http://{$wsa}/{$baseval}";
|
2014-02-14 16:02:59 +01:00
|
|
|
}
|
|
|
|
}
|
2014-06-21 15:27:18 +02:00
|
|
|
/*
|
|
|
|
* One last check!
|
|
|
|
* See whether the user is exeeding his quota
|
|
|
|
*/
|
2014-02-14 16:02:59 +01:00
|
|
|
|
2014-06-21 15:27:18 +02:00
|
|
|
$isexeeding = exquota($apikey, $userquota);
|
|
|
|
if ($isexeeding) {
|
|
|
|
header("HTTP/1.0 503 Service Unavailable");
|
|
|
|
die('Hey, slow down! Exeeding your perminute quota. Try again in around a minute.');
|
|
|
|
// don't let them shorten :>
|
|
|
|
}
|
2014-02-14 16:02:59 +01:00
|
|
|
|
2014-06-21 15:27:18 +02:00
|
|
|
// API execute actions. Promised, no more checks :)
|
|
|
|
|
|
|
|
if ($action == "shorten") {
|
|
|
|
if (isset($_REQUEST['temp'])) {
|
|
|
|
$ist = $mysqli->real_escape_string($_REQUEST['temp']);
|
|
|
|
$ist = strtolower($ist);
|
|
|
|
}
|
|
|
|
if (($ist == 'true') || ($ist == 'false')) {
|
|
|
|
echo shorten($url_api, $ist);
|
|
|
|
die();
|
|
|
|
}
|
2014-02-14 16:02:59 +01:00
|
|
|
echo shorten($url_api);
|
|
|
|
die();
|
2014-06-21 15:27:18 +02:00
|
|
|
} else if ($action == "lookup") {
|
2014-02-14 16:02:59 +01:00
|
|
|
$looked_up_url = lookup($url_api);
|
2014-06-21 15:27:18 +02:00
|
|
|
if (!$looked_up_url) {
|
2014-02-14 16:02:59 +01:00
|
|
|
header("HTTP/1.0 404 Not Found");
|
2014-06-21 15:27:18 +02:00
|
|
|
die("404 Not Found");
|
|
|
|
} else {
|
2014-02-14 16:02:59 +01:00
|
|
|
echo $looked_up_url;
|
|
|
|
}
|
|
|
|
die();
|
2014-06-21 15:27:18 +02:00
|
|
|
} else {
|
2014-02-14 16:02:59 +01:00
|
|
|
die("Invalid Action");
|
|
|
|
}
|
|
|
|
|