forked from todordev/ITPShare-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshorturl.php
190 lines (151 loc) · 5.79 KB
/
shorturl.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
<?php
/**
* @package ITPShare
* @subpackage Plugins
* @copyright Copyright (C) 2014 Todor Iliev <[email protected]>. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*/
// no direct access
defined('_JEXEC') or die;
class ItpSharePluginShortUrl
{
private $url;
private $shortUrl;
private $apiKey;
private $service;
private $login;
public function __construct($url, $options = array())
{
$this->url = $url;
if (!empty($options)) {
$this->bindOptions($options);
}
}
private function bindOptions($options)
{
if (isset($options['api_key'])) {
$this->apiKey = $options['api_key'];
}
if (isset($options['service'])) {
$this->service = $options['service'];
}
if (isset($options['login'])) {
$this->login = $options['login'];
}
}
public function getUrl()
{
// Check for installed CURL library
$installedLibraries = get_loaded_extensions();
if (!in_array('curl', $installedLibraries)) {
throw new Exception(JText::_("PLG_CONTENT_ITPSHARE_ERROR_CURL_MISSING"));
}
switch ($this->service) {
case "jmp":
$this->getBitlyURL("j.mp");
break;
case "bitlycom":
$this->getBitlyURL("bitly.com");
break;
case "tinycc":
$this->getTinyURL();
break;
case "google":
$this->getGoogleURL();
break;
default: // bit.ly
$this->getBitlyURL("bit.ly");
break;
}
return $this->shortUrl;
}
/**
* Get a short URL from Bitly
*
* @param string $domain - bit.ly, j.mp, bitly.com
*
* @throws Exception
*/
protected function getBitlyURL($domain = "bit.ly")
{
$url = "http://api.bitly.com/v3/shorten?login=" . $this->login . "&apiKey=" . $this->apiKey . "&longUrl=" . rawurldecode(html_entity_decode($this->url, ENT_COMPAT, 'UTF-8')) . "&format=json&domain=" . $domain;
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, $url);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
//As the API is on https, set the value for CURLOPT_SSL_VERIFYPEER to false. This will stop cURL from verifying the SSL certificate.
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
$response = curl_exec($curlObj);
curl_close($curlObj);
if (!empty($response)) {
$json = json_decode($response);
if ($json->status_code != 200) {
$errorMessage = "[Bitly service] Message: " . $json->status_txt;
throw new Exception($errorMessage);
} else {
$this->shortUrl = $json->data->url;
}
} else {
throw new Exception(JText::_("PLG_CONTENT_ITPSHARE_ERROR_UNKNOWN_ERROR"));
}
}
/**
* Get a short URL from Tiny.CC
*/
protected function getTinyURL()
{
$url = "http://tiny.cc/?c=rest_api&m=shorten&version=2.0.3&format=json&shortUrl=&longUrl=" . rawurldecode(html_entity_decode($this->url, ENT_COMPAT, 'UTF-8')) . "&login=" . $this->login . "&apiKey=" . $this->apiKey;
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, $url);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
//As the API is on https, set the value for CURLOPT_SSL_VERIFYPEER to false. This will stop cURL from verifying the SSL certificate.
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
$response = curl_exec($curlObj);
curl_close($curlObj);
if (!empty($response)) {
$json = json_decode($response);
if (!empty($json->errorCode)) {
$errorMessage = "[TinyCC service] Message: " . $json->errorMessage;
throw new Exception($errorMessage);
} else {
$this->shortUrl = $json->results->short_url;
}
} else {
throw new Exception(JText::_("PLG_CONTENT_ITPSHARE_ERROR_UNKNOWN_ERROR"));
}
}
/**
* Get a short url from Goo.gl
*/
protected function getGoogleURL()
{
$postData = array(
'longUrl' => rawurldecode(html_entity_decode($this->url, ENT_COMPAT, 'UTF-8')),
'key' => $this->apiKey
);
$jsonData = json_encode($postData);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
//As the API is on https, set the value for CURLOPT_SSL_VERIFYPEER to false. This will stop cURL from verifying the SSL certificate.
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
$response = curl_exec($curlObj);
curl_close($curlObj);
if (!empty($response)) {
$json = json_decode($response);
if (!empty($json->error)) {
$errorMessage = "[Goo.gl service] Message: " . $json->error->message . "; Location: " . $json->error->errors[0]->location;
throw new Exception($errorMessage);
} else {
$this->shortUrl = $json->id;
}
} else {
throw new Exception(JText::_("PLG_CONTENT_ITPSHARE_ERROR_UNKNOWN_ERROR"));
}
}
}