-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.php
202 lines (154 loc) · 6.15 KB
/
Main.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
<?php
namespace IdnoPlugins\Bitly {
class Main extends \Idno\Common\Plugin {
public static $AUTHORIZATION_ENDPOINT = 'https://bitly.com/oauth/authorize';
public static $TOKEN_ENDPOINT = 'https://api-ssl.bitly.com/oauth/access_token';
public static function getRedirectUrl() {
return \Idno\Core\site()->config()->url . 'bitly/callback';
}
public static function getState() {
return md5(\Idno\Core\site()->config()->site_secret . \Idno\Core\site()->config()->url . dirname(__FILE__));
}
/**
* Connect to Bitly
* @return bool|\IdnoPlugins\Bitly\Client
*/
function connect() {
if (!empty(\Idno\Core\site()->config()->bitly)) {
$api = new Client(
\Idno\Core\site()->config()->bitly['client_id'], \Idno\Core\site()->config()->bitly['client_secret']
);
return $api;
}
return false;
}
/**
* Can the current user use Bitly?
* @return bool
*/
function hasBitly($user = null) {
if (!$user) $user=\Idno\Core\site()->session()->currentUser();
if (!empty($user) && $user->bitly) {
return true;
}
return false;
}
function hasGAT() {
return isset(\Idno\Core\site()->config->config['bitly']['generic_access_token']);
}
function registerPages() {
// Register admin settings
\Idno\Core\Idno::site()->routes()->addRoute('admin/bitly', '\IdnoPlugins\Bitly\Pages\Admin');
\Idno\Core\site()->template()->extendTemplate('admin/menu/items', 'admin/bitly/menu');
\Idno\Core\Idno::site()->routes()->addRoute('account/bitly', '\IdnoPlugins\Bitly\Pages\Account');
\Idno\Core\site()->template()->extendTemplate('account/menu/items', 'account/bitly/menu');
// Register the callback URL
\Idno\Core\Idno::site()->routes()->addRoute('bitly/callback', '\IdnoPlugins\Bitly\Pages\Callback');
// Listen to link text expand
\Idno\Core\Idno::site()->events()->addListener('url/expandintext', function(\Idno\Core\Event $event) {
if ($object = $event->data()['object']) {
if ($owner = $object->getOwner()) {
// See if the owner has bitly, or if we've got a global shortner, if not, don't bother
if ($this->hasBitly($owner) || $this->hasGAT()) {
// Get body
$body = $event->response();
// Load/create lookup table
$urls = $object->url_expansion_lookup;
if (!$urls) $urls = [];
$updated = false;
// Find urls
if (preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $body, $matches))
{
foreach($matches[0] as $match) {
\Idno\Core\Idno::site()->logging()->debug("Attempting to expand link: $match");
$longurl = false;
if (isset($urls[md5($match)])) {
\Idno\Core\Idno::site()->logging()->debug("Already have $match as " . $urls[md5($match)]);
$longurl = $urls[md5($match)];
} else {
// We haven't encountered this url, so try and expand it
// Try user's bitly
if ($this->hasBitly($owner) ) {
$result = \Idno\Core\Webservice::get('https://api-ssl.bitly.com/v3/expand', [
'access_token' => $owner->bitly['access_token'],
'shortUrl' => $match
]);
$result = json_decode($result['content']);
if (isset($result->data->expand[0]->long_url)) {
$longurl = $result->data->expand[0]->long_url;
$updated = true;
\Idno\Core\Idno::site()->logging()->debug("Found $match as $longurl");
} else if ((isset($result->error->data->expand[0])) && ($result->data->expand[0]->error == 'NOT_FOUND')) {
\Idno\Core\Idno::site()->logging()->debug("Short url for $match not found, probably a long url already");
$longurl = $match;
$updated = true;
}
}
// Now try GAT
if (!$longurl && $this->hasGAT()) {
$result = \Idno\Core\Webservice::get('https://api-ssl.bitly.com/v3/expand', [
'access_token' => \Idno\Core\site()->config->config['bitly']['generic_access_token'],
'shortUrl' => $match
]);
$result = json_decode($result['content']);
if (isset($result->data->expand[0]->long_url)) {
$longurl = $result->data->expand[0]->long_url;
$updated = true;
\Idno\Core\Idno::site()->logging()->debug("Found $match as $longurl");
} else if ((isset($result->data->expand[0]->error)) && ($result->data->expand[0]->error == 'NOT_FOUND')) {
\Idno\Core\Idno::site()->logging()->debug("Short url for $match not found, probably a long url already");
$longurl = $match;
$updated = true;
}
}
}
// Save expanded form, or that we failed.
$urls[md5($match)] = $longurl;
// Now do a replace
if ($longurl!==false) {
$body = str_replace($match, $longurl, $body);
}
}
}
// Save updated urls list
if ((!empty($urls)) && ($updated))
{
$object->url_expansion_lookup = $urls;
$object->save();
}
// Update body
$event->setResponse($body);
}
}
}
});
// Handle shorten event
\Idno\Core\Idno::site()->events()->addListener('url/shorten', function(\Idno\Core\Event $event) {
// Try user binding first
if ($this->hasBitly()) {
$access_token = \Idno\Core\site()->session()->currentUser()->bitly['access_token'];
// Generic access token shorten
} else if (isset(\Idno\Core\site()->config->config['bitly'])) {
// Shorten with GAT
$access_token = \Idno\Core\site()->config->config['bitly']['generic_access_token'];
}
if ($access_token) {
$url = $event->response();
try {
$result = \Idno\Core\Webservice::get('https://api-ssl.bitly.com/v3/shorten', [
'access_token' => $access_token,
'longUrl' => $url
]);
$result = json_decode($result['content']);
if ($result && $result->status_txt == 'OK') {
$event->setResponse($result->data->url);
} else
throw new \Exception("There was a problem shortening that link...");
} catch (\Exception $e) {
\Idno\Core\site()->session()->addMessage($e->getMessage(), 'alert-warn');
}
}
});
}
}
}