forked from cydrobolt/polr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
196 lines (176 loc) · 6.51 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/*
# Copyright (C) 2013-2015 Chaoyi Zha
# Polr is an open-source project licensed under the GPL.
# The above copyright notice and the following license are applicable to
# the entire project, unless explicitly defined otherwise.
# http://github.com/cydrobolt/polr
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
# See http://www.gnu.org/copyleft/gpl.html for the full text of the
# license.
#
*/
/*
* API Reference:
* POST/GET arguments:
* Required: url - the url to perform action on
* Required: apikey - the APIKey provided for authentication
* Required: action - action to perform, either lookup or shorten
* Optional: temp - whether to treat the URL as temporary or not
*/
$reqargs['nosession'] = true;
require_once('lib-core.php'); //Fetch Config
// 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 Google Safe Browsing object
if (is_string($_REQUEST['apikey']) && is_string($_REQUEST['action']) && is_string($_REQUEST['url'])) {
$apikey = $mysqli->real_escape_string($_REQUEST['apikey']); //Sanitize input
$action = $mysqli->real_escape_string($_REQUEST['action']);
$url_api = $mysqli->real_escape_string($_REQUEST['url']);
} else {
header("HTTP/1.0 400 Bad Request");
die("Error: No value specified, or wrong data type.");
}
// Check API key
$query = "SELECT `valid`,`quota` FROM `api` WHERE apikey='{$apikey}'";
$result = $mysqli->query($query) or showerror();
$validrow = mysqli_fetch_assoc($result);
$userquota = $validrow['quota'];
//check if valid
if (!$validrow['valid']) {
$api_key_valid = 0;
} else {
$api_key_valid = 1;
}
if (!$api_key_valid) {
header("HTTP/1.0 401 Unauthorized"); // Invalid key received
die('401 Unauthorized');
}
if (!filter_var($url_api, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) && $action != "lookup") {
header("HTTP/1.0 400 Bad Request");
echo "Error: URL is not valid"; // URL not well formatted, but allow if action is lookup
die();
}
// Check URL against Google Safe Browsing
/*
$isbl = $dnsbl->isbl($url_api);
if ($isbl === "malware" || $isbl === "phishing") {
header("HTTP/1.0 401 Unauthorized");
echo "Polr does not shorten potentially malicious URLs";
die();
}
*/
function lookup($urltl) {
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'];
}
function exquota($apikey, $quota) {
/*
Check if a user is exceeding their allocated quota.
Returns false if they are not exeeding their quota, or
true if they are past their 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 new URLs created 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;
if (($total_queries+$total_queries_temp) >= $quota) {
return true;
}
else {
return false;
}
}
function shorten($urlr, $t = 'false') {
global $mysqli;
global $wsa;
global $apikey;
global $ip;
$protocol = '://';
$isshort = array('polr.me', '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)");
}
}
$query1 = "SELECT `rid` FROM `redirinfo` WHERE `rurl`='{$urlr}' AND `iscustom`='no'";
$result = $mysqli->query($query1);
$row = mysqli_fetch_assoc($result);
$existing = $row['rid'];
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}');";
}
$result2r = $mysqli->query($query2) or showerror();
return "http://{$wsa}/{$baseval}";
} else {
$query1 = "SELECT baseval FROM redirinfo WHERE rurl='{$urlr}'";
$result = $mysqli->query($query1);
$row = mysqli_fetch_assoc($result);
$baseval = $row['baseval'];
return "http://{$wsa}/{$baseval}";
}
}
/*
* Check whether the user is exceeding his quota
*/
$isexeeding = exquota($apikey, $userquota);
if ($isexeeding) {
header("HTTP/1.0 503 Service Unavailable");
die('Hey, slow down! Exeeding your per minute quota. Try again in around a minute.');
}
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();
}
echo shorten($url_api);
die();
} else if ($action == "lookup") {
$looked_up_url = lookup($url_api);
if (!$looked_up_url) {
header("HTTP/1.0 404 Not Found");
die("404 Not Found");
} else {
echo $looked_up_url;
}
die();
} else {
die("Invalid Action");
}