-
Notifications
You must be signed in to change notification settings - Fork 19
/
Main.php
395 lines (354 loc) · 20.9 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
namespace IdnoPlugins\Facebook {
use Idno\Core\Idno;
use Idno\Core\Webservice;
class Main extends \Idno\Common\Plugin
{
public $endpoint = 'me';
function registerPages()
{
// Deauth URL
Idno::site()->addPageHandler('facebook/deauth', '\IdnoPlugins\Facebook\Pages\Deauth');
// Register the callback URL
Idno::site()->addPageHandler('facebook/callback', '\IdnoPlugins\Facebook\Pages\Callback');
// Register admin settings
Idno::site()->addPageHandler('admin/facebook', '\IdnoPlugins\Facebook\Pages\Admin');
// Register settings page
Idno::site()->addPageHandler('account/facebook', '\IdnoPlugins\Facebook\Pages\Account');
/** Template extensions */
// Add menu items to account & administration screens
Idno::site()->template()->extendTemplate('admin/menu/items', 'admin/facebook/menu');
Idno::site()->template()->extendTemplate('account/menu/items', 'account/facebook/menu');
Idno::site()->template()->extendTemplate('onboarding/connect/networks', 'onboarding/connect/facebook');
}
function registerEventHooks()
{
Idno::site()->syndication()->registerService('facebook', function () {
return $this->hasFacebook();
}, array('note', 'article', 'image', 'media','rsvp', 'bookmark'));
Idno::site()->addEventHook('user/auth/success', function (\Idno\Core\Event $event) {
if ($this->hasFacebook()) {
if (is_array(Idno::site()->session()->currentUser()->facebook)) {
foreach(Idno::site()->session()->currentUser()->facebook as $username => $details) {
if ($username != 'access_token') {
if (empty($details['expiry']) || ($details['expiry'] > time())) {
Idno::site()->syndication()->registerServiceAccount('facebook', $username, $details['name']);
}
} else {
Idno::site()->syndication()->registerServiceAccount('facebook', $username, 'Facebook');
}
}
}
}
});
$notes_function = function (\Idno\Core\Event $event) {
$eventdata = $event->data();
if ($this->hasFacebook()) {
$object = $eventdata['object'];
$facebookAPI = $this->getFacebookAPI($eventdata['syndication_account']);
$name = $this->getDisplayName($eventdata['syndication_account']);
if (!empty($facebookAPI)) {
$message = preg_replace('/<[^\>]*>/', '', $object->getDescription()); //strip_tags($object->getDescription());
$message = html_entity_decode($message);
// Obey the IndieWeb reference setting
if (!substr_count($message, Idno::site()->config()->host) && Idno::site()->config()->indieweb_reference) {
$message .= "\n\n(" . $object->getShortURL(true, false) . ")";
}
if (!empty($message) && substr($message, 0, 1) != '@') {
$params = array(
'message' => $message,
);
if (preg_match('/(?<!=)(?<!["\'])((ht|f)tps?:\/\/[^\s\r\n\t<>"\'\(\)]+)/i', $message, $matches)) {
$params['link'] = $matches[0]; // Set the first discovered link as the match
$params['message'] = str_replace($params['link'],'',$params['message']);
foreach(['youtube.com','youtu.be','vimeo.com'] as $video_domain) {
if (substr_count(strtolower($params['link']), $video_domain)) {
unset($params['actions']); // Facebook doesn't like "actions" to co-exist with video links
}
}
}
try {
$this->warmFacebookCache($object->getURL());
$result = $facebookAPI->api('/'.$this->endpoint.'/feed', 'POST', $params);
if (!empty($result['id'])) {
$result['id'] = str_replace('_', '/posts/', $result['id']);
$object->setPosseLink('facebook', 'https://facebook.com/' . $result['id'], $name, "", $name);
$object->save();
} else {
\Idno\Core\Idno::site()->logging()->error("Nothing was posted to Facebook: " . var_export($result,true));
}
} catch (\Exception $e) {
\Idno\Core\Idno::site()->logging()->error('There was a problem posting to Facebook: ' . $e->getMessage());
Idno::site()->session()->addMessage('There was a problem posting to Facebook: ' . $e->getMessage());
}
}
}
}
};
// Push "notes" to Facebook
Idno::site()->addEventHook('post/note/facebook', $notes_function);
Idno::site()->addEventHook('post/bookmark/facebook', $notes_function);
// Push "articles" to Facebook
Idno::site()->addEventHook('post/article/facebook', function (\Idno\Core\Event $event) {
$eventdata = $event->data();
if ($this->hasFacebook()) {
$object = $eventdata['object'];
$facebookAPI = $this->getFacebookAPI($eventdata['syndication_account']);
$name = $this->getDisplayName($eventdata['syndication_account']);
if (!empty($facebookAPI)) {
try {
$this->warmFacebookCache($object->getURL());
$result = $facebookAPI->api('/'.$this->endpoint.'/feed', 'POST',
array(
'link' => $object->getURL(),
'message' => $message = html_entity_decode($object->getTitle()),
));
if (!empty($result['id'])) {
$result['id'] = str_replace('_', '/posts/', $result['id']);
$object->setPosseLink('facebook', 'https://facebook.com/' . $result['id'], $name, "", $name);
$object->save();
} else {
\Idno\Core\Idno::site()->logging()->error("Nothing was posted to Facebook: " . var_export($result,true));
}
} catch (\Exception $e) {
\Idno\Core\Idno::site()->logging()->error('There was a problem posting to Facebook: ' . $e->getMessage());
Idno::site()->session()->addMessage('There was a problem posting to Facebook: ' . $e->getMessage());
}
}
}
});
// Push RSVPs to Facebook
Idno::site()->addEventHook('post/rsvp/facebook', function (\Idno\Core\Event $event) {
Idno::site()->logging()->debug("publishing RSVP to Facebook");
$eventdata = $event->data();
$object = $eventdata['object'];
$facebookAPI = $this->getFacebookAPI($eventdata['syndication_account']);
$name = $this->getDisplayName($eventdata['syndication_account']);
if ($facebookAPI) {
$eventRegex = '#https?://(?:www\.|m\.)?facebook.com/events/(\d+)/?#';
$eventUrl = $this->possePostDiscovery($object->inreplyto, $eventRegex);
if ($eventUrl && preg_match($eventRegex, $eventUrl, $matches)) {
$eventId = $matches[1];
$endpoint = false;
if ($object->rsvp === 'yes') {
$endpoint = "/$eventId/attending";
} else if ($object->rsvp === 'no') {
$endpoint = "/$eventId/declined";
} else if ($object->rsvp === 'maybe') {
$endpoint = "/$eventId/maybe";
}
if ($endpoint) {
Idno::site()->logging()->debug("publishing rsvp to Facebook on $endpoint");
$response = $facebookAPI->api($endpoint, 'POST');
Idno::site()->logging()->debug("publish response from Facebook", ['response' => $response]);
}
}
}
});
// Push "media" to Facebook
Idno::site()->addEventHook('post/media/facebook', function (\Idno\Core\Event $event) {
$eventdata = $event->data();
if ($this->hasFacebook()) {
$object = $eventdata['object'];
$facebookAPI = $this->getFacebookAPI($eventdata['syndication_account']);
$name = $this->getDisplayName($eventdata['syndication_account']);
if (!empty($facebookAPI)) {
try {
$result = $facebookAPI->api('/'.$this->endpoint.'/feed', 'POST',
array(
'link' => $object->getURL(),
'message' => $message = html_entity_decode($object->getTitle()),
));
if (!empty($result['id'])) {
$result['id'] = str_replace('_', '/posts/', $result['id']);
$object->setPosseLink('facebook', 'https://facebook.com/' . $result['id'], $name, "", $name);
$object->save();
} else {
\Idno\Core\Idno::site()->logging()->error("Nothing was posted to Facebook: " . var_export($result,true));
}
} catch (\Exception $e) {
\Idno\Core\Idno::site()->logging()->error('There was a problem posting to Facebook: ' . $e->getMessage());
Idno::site()->session()->addMessage('There was a problem posting to Facebook: ' . $e->getMessage());
}
}
}
});
// Push "images" to Facebook
Idno::site()->addEventHook('post/image/facebook', function (\Idno\Core\Event $event) {
$eventdata = $event->data();
$object = $eventdata['object'];
if ($attachments = $object->getAttachments()) {
foreach ($attachments as $attachment) {
if ($this->hasFacebook()) {
$facebookAPI = $this->getFacebookAPI($eventdata['syndication_account']);
$name = $this->getDisplayName($eventdata['syndication_account']);
if (!empty($facebookAPI)) {
$message = strip_tags($object->getTitle()) . "\n\n" . strip_tags($object->getDescription());
$message = html_entity_decode($message);
// Strip out "Untitled"
$message = str_replace("Untitled\n\n",'',$message);
$message .= "\n\nOriginal: " . $object->getURL();
try {
//$facebookAPI->setFileUploadSupport(true);
$response = $facebookAPI->api(
'/'.$this->endpoint.'/photos/',
'post',
array(
'message' => $message,
'url' => $attachment['url'],
)
);
if (!empty($response['id'])) {
$result['id'] = str_replace('_', '/photos/', $response['id']);
$object->setPosseLink('facebook', 'https://facebook.com/' . $response['id'], $name, "", $name);
$object->save();
} else {
\Idno\Core\Idno::site()->logging()->error("Nothing was posted to Facebook: " . var_export($result,true));
}
} catch (\FacebookApiException $e) {
\Idno\Core\Idno::site()->logging()->error('Could not post image to Facebook: ' . $e->getMessage());
}
}
}
}
}
});
}
private function getFacebookAPI($syndicationAccount)
{
if ($this->hasFacebook()) {
if (!empty($syndicationAccount)) {
return $this->connect($syndicationAccount);
}
return $this->connect();
}
return false;
}
private function getDisplayName($syndicationAccount)
{
if (!empty($syndicationAccount)
&& !empty(Idno::site()->session()->currentUser()->facebook[$syndicationAccount]['name'])) {
return Idno::site()->session()->currentUser()->facebook[$syndicationAccount]['name'];
}
return 'Facebook';
}
/**
* Given an array of original URLs and a permalink regex,
* looks for silo-specific syndication URLs. If the
* original is a silo url, that url is returned; otherwise
* we fetch the source and attempt to look for
* rel-syndication and u-syndication URLs.
*
* TODO merge this with Webmention::addSyndicatedReplyTargets?
*
* @param array $originals the original URLs to fetch and
* search for syndication links
* @param string $regex the regex that matches syndication
* links for this source
* @return a string matching the regex or false
*/
private function possePostDiscovery($originals, $regex)
{
$originals = (array) $originals;
foreach ($originals as $original) {
if (preg_match($regex, $original)) {
Idno::site()->logging()->debug("Found a matching url in the original list: $original");
return $original;
}
}
foreach ($originals as $original) {
Idno::site()->logging()->debug("Fetching $original to look for syndication links");
$resp = Webservice::get($original);
if ($resp['response'] >= 200 && $resp['response'] < 300) {
$d = (new \Mf2\Parser($resp['content'], $original))->parse();
$urls = [];
if (!empty($d['rels']['syndication'])) {
$urls = array_merge($urls, $d['rels']['syndication']);
}
if (!empty($d['items'])) {
foreach ($d['items'] as $item) {
if ((in_array('h-entry', $item['type']) || in_array('h-event', $item['type']))
&& !empty($item['properties']['syndication'])) {
$urls = array_merge($urls, $item['properties']['syndication']);
}
}
}
foreach ($urls as $url) {
if (preg_match($regex, $url)) {
Idno::site()->logging()->debug("Discovered a matching url in the syndication links $url");
return $url;
}
}
} else {
Idno::site()->logging()->warning("Failed to fetch $original: status={$resp['response']}, error={$resp['error']}");
}
}
return false;
}
/**
* Retrieve the URL to authenticate with Facebook
* @return string
*/
function getAuthURL()
{
$facebook = $this;
if ($facebookAPI = $facebook->connect()) {
return $facebookAPI->getLoginUrl();
}
return '';
}
/**
* Connect to Facebook
* @return bool|FacebookAPI
*/
function connect($account_id = '')
{
if (!empty(Idno::site()->config()->facebook)) {
require_once(dirname(__FILE__) . '/external/facebook-sdk/src/Facebook/autoload.php');
$facebookAPI = new FacebookAPI();
if (!empty($account_id)) {
if (!empty(Idno::site()->session()->currentUser()->facebook[$account_id])) {
if ($account_id == 'Facebook' || $account_id == 'access_token') {
$facebookAPI->setAccessToken(Idno::site()->session()->currentUser()->facebook['access_token']);
} else {
$facebookAPI->setAccessToken(Idno::site()->session()->currentUser()->facebook[$account_id]['access_token']);
$this->endpoint = $account_id;
}
return $facebookAPI;
} else {
if ($account_id == 'Facebook' && !empty(Idno::site()->session()->currentUser()->facebook['access_token'])) {
$facebookAPI->setAccessToken(Idno::site()->session()->currentUser()->facebook['access_token']);
}
}
} else {
if (!empty(Idno::site()->session()->currentUser()->facebook['access_token'])) {
$facebookAPI->setAccessToken(Idno::site()->session()->currentUser()->facebook['access_token']);
}
return $facebookAPI; // This needs to return even if we haven't set the user token yet, for the auth callback
}
}
return false;
}
/**
* Facebook is silly and needs to have its cache warmed up before you post.
* @param $url
*/
function warmFacebookCache($url)
{
$client = new Webservice();
$result = $client->post('https://graph.facebook.com/',['id' => $url, 'scrape' => 'true']);
error_log('Facebook cache result: ' . json_encode($result));
}
/**
* Can the current user use Twitter?
* @return bool
*/
function hasFacebook()
{
if (!Idno::site()->session()->currentUser()) {
return false;
}
return Idno::site()->session()->currentUser()->facebook;
}
}
}