-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclass-linkshorter.php
330 lines (262 loc) · 10.7 KB
/
class-linkshorter.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/**
* LinkShorter - A class by Gabboxl made to short links using the most famous link shortener APIs under license GNU AGPLv3.
*
*Contacts: t.me/gabbo_xl (Telegram)
*
*Copyright (C) 2018 Gabboxl
*
* This program (LinkShorter) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program (LinkShorter) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
require 'settings.php'; //we include all credentials variables
class linkshorter
{
public function __construct($service, $link, $domain = null, $advert_type = null)
{
$methods = ['adfly', 'bitly', 'adfocus', 'googl', 'shinkin', 'shortest'];
if (!in_array($service, $methods)) {
$this->setError("Invalid service: $service");
return;
} elseif ($link == '' or $link == null) {
$this->setError('The link is not set.');
return;
}
$this->$service($link, $domain, $advert_type); //the domain and the advert_type are only avaible for the adfly method
}
private function setError($msg)
{
$this->error = $msg;
$this->hasError = true;
}
public function getError()
{
if (!isset($this->error)) {
$this->hasError = 'false';
return 'none';
}
return $this->error;
}
public function getLink()
{
if (isset($this->response)) {
return $this->response;
}
}
private function adfly($url, $domain = 'adf.ly', $advert_type = 'int')
{
global $apiKeyAdfly;
global $uIdAdfly;
// base api url
$api = 'http://api.adf.ly/api.php?';
// api queries
$query = [
'key' => $apiKeyAdfly,
'uid' => $uIdAdfly,
'advert_type' => $advert_type,
'domain' => $domain,
'url' => $url,
];
// full api url with query string
$api = $api.http_build_query($query);
// get data
$dataz = file_get_contents($api);
if (strpos(' '.$dataz, 'http')) {
$this->response = $dataz;
} else {
$jzon = json_decode($dataz, true);
if (isset($jzon['errors'][0]['msg'])) {
$this->setError($jzon['errors'][0]['msg']);
} else {
if (isset($jzon['warnings'][0]['msg'])) {
$this->setError($jzon['warnings'][0]['msg']);
}
}
}
}
private function adfocus($url)
{
global $adfocKey;
//now we add the http:// to the url if it hasn't to avoid the relative error (0)
if (!strpos(' '.$url, 'http://') or !strpos(' '.$url, 'https://')) {
$url = 'http://'.$url;
}
$adfoch = file_get_contents("http://adfoc.us/api/?key=$adfocKey&url=$url");
if (strpos(' '.$adfoch, 'http')) {
$this->response = $adfoch;
} else {
$this->setError("$adfoch (The credentials are INVALID, OR there IS NOT the http(s):// before the link.)");
}
}
private function shinkin($url)
{
global $shinkid;
global $shinktoken;
$data = file_get_contents("http://shink.me/api/0/id/$shinkid/auth_token/$shinktoken?s=$url");
$data = json_decode($data);
if ($data->error != 0) {
$this->setError($data->error);
return;
}
$this->response = 'http://shink.me/'.$data->hash;
}
private function googl($url)
{
global $apiKeygoogle;
$postData = ['longUrl' => $url];
$jsonData = json_encode($postData);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, "https://www.googleapis.com/urlshortener/v1/url?key=$apiKeygoogle");
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, ['Content-type:application/json']);
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
$response = curl_exec($curlObj);
// Change the response json string to object
$json = json_decode($response);
curl_close($curlObj);
if (!isset($json->id)) {
$this->setError('REASON: '.$json->errors->reason.'<br>'.'MESSAGE: '.$json->errors->message.'<br>'.'LOCATION: '.$json->errors->location.'<br>'.'CODE: '.$json->code);
return;
}
$this->response = $json->id;
}
private function bitly($url)
{
global $userbitly;
global $keybitly;
$query = [
'version' => '2.0.1',
'longUrl' => $url,
'login' => $userbitly, // login variable
'apiKey' => $keybitly, // api key variable
];
$query = http_build_query($query);
$req = curl_init();
curl_setopt($req, CURLOPT_URL, 'http://api.bit.ly/shorten?'.$query);
curl_setopt($req, CURLOPT_HEADER, 0);
curl_setopt($req, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($req);
curl_close($req);
$response = json_decode($response);
if ($response->errorCode == 0 && $response->statusCode == 'OK') {
$this->response = $response->results->{$url}->shortUrl;
} else {
$this->setError($response->errorCode);
// return null;
}
}
private function shortest($url)
{
global $shortestkey;
$curl_url = "https://api.shorte.st/s/$shortestkey/$url";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$json = json_decode($result);
if ($json->status != 'ok') {
$this->setError("The shorte.st's api key may be not correct.");
return;
}
$this->response = $json->shortenedUrl;
}
private function tinyurl($url)
{
if (!strpos(' '.$url, 'http://') or !strpos(' '.$url, 'https://')) { //we add the http:// at the url if it hasn't becouse it will cause an error
$url = 'http://'.$url;
}
$data = file_get_contents('http://tinyurl.com/api-create.php?url='.$url);
$this->response = $data;
}
private function isgd($url, $shorturl = null)
{
//This function returns an array giving the results of your shortening
//If successful $result["shortURL"] will give your new shortened URL
//If unsuccessful $result["errorMessage"] will give an explanation of why
//and $result["errorCode"] will give a code indicating the type of error
//See https://v.gd/apishorteningreference.php#errcodes for an explanation of what the
//error codes mean. In addition to that list this function can return an
//error code of -1 meaning there was an internal error e.g. if it failed
//to fetch the API page.
$url = urlencode($url);
$basepath = 'https://is.gd/create.php?format=simple';
$result = [];
$result['errorCode'] = -1;
$result['shortURL'] = null;
$result['errorMessage'] = null;
//We need to set a context with ignore_errors on otherwise PHP doesn't fetch
//page content for failure HTTP status codes (v.gd needs this to return error
//messages when using simple format)
$opts = ['http' => ['ignore_errors' => true]];
$context = stream_context_create($opts);
if ($shorturl) {
$path = $basepath."&shorturl=$shorturl&url=$url";
} else {
$path = $basepath."&url=$url";
}
$response = @file_get_contents($path, false, $context);
if (!isset($http_response_header)) {
$this->setError('Local error: Failed to fetch API page');
return;
}
//Hacky way of getting the HTTP status code from the response headers
if (!preg_match('{[0-9]{3}}', $http_response_header[0], $httpStatus)) {
$this->setError('Local error: Failed to extract HTTP status from result request');
return;
}
$errorCode = -1;
switch ($httpStatus[0]) {
case 200:
$errorCode = 0;
break;
case 400:
$errorCode = 1;
break;
case 406:
$errorCode = 2;
break;
case 502:
$errorCode = 3;
break;
case 503:
$errorCode = 4;
break;
}
if ($errorCode == -1) {
$this->setError('Local error: Unexpected response code received from server');
return;
}
$result['errorCode'] = $errorCode;
if ($errorCode == 0) {
$result['shortURL'] = $this->response;
} else {
$this->setError($result['errorMessage']);
}
}
//below line would be how to request a custom URL instead of an automatically generated one
//in this case asking for https://v.gd/mytesturl
//$result = vgdShorten("https://www.reddit.com/","mytesturl");
/*
if($result["errorCode"]==3)
{
//Error code 3 means your app has exceeded our rate limit.
//In a real app you'd take some action here to prevent it
//from using v.gd again for 1 minute or so.
}
*/
}