]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Linkback/LinkbackPlugin.php
Catch some exceptions in Linkback
[quix0rs-gnu-social.git] / plugins / Linkback / LinkbackPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin to do linkbacks for notices containing links
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Plugin
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once('Auth/Yadis/Yadis.php');
35 require_once(__DIR__ . '/lib/util.php');
36
37 define('LINKBACKPLUGIN_VERSION', '0.1');
38
39 /**
40  * Plugin to do linkbacks for notices containing URLs
41  *
42  * After new notices are saved, we check their text for URLs. If there
43  * are URLs, we test each URL to see if it supports any
44  *
45  * @category Plugin
46  * @package  StatusNet
47  * @author   Evan Prodromou <evan@status.net>
48  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
49  * @link     http://status.net/
50  *
51  * @see      Event
52  */
53 class LinkbackPlugin extends Plugin
54 {
55     var $notice = null;
56
57     function __construct()
58     {
59         parent::__construct();
60     }
61
62     function onHandleQueuedNotice(Notice $notice)
63     {
64         if (!$notice->isLocal() || !$notice->isPublic()) {
65             return true;
66         }
67
68         // Try to avoid actually mucking with the
69         // notice content
70         $c = $notice->content;
71         $this->notice = $notice;
72
73         if (!$notice->getProfile()->getPref('linkbackplugin', 'disable_linkbacks')) {
74             // Ignoring results
75             common_replace_urls_callback($c, array($this, 'linkbackUrl'));
76         }
77
78         try {
79             if ($notice->isRepeat()) {
80                 $repeat = Notice::getByID($notice->repeat_of);
81                 $this->linkbackUrl($repeat->getUrl());
82             } elseif (!empty($notice->reply_to)) {
83                 $parent = $notice->getParent();
84                 $this->linkbackUrl($parent->getUrl());
85             }
86         } catch (InvalidUrlException $e) {
87             // can't send linkback to notice if we don't have a remote HTTP(S) URL
88             // but we can still ping the attention-receivers below
89         } catch (NoParentNoticeException $e) {
90             // can't send linkback to non-existing parent URL
91             return true;
92         }
93
94         // doubling up getReplies and getAttentionProfileIDs because we're not entirely migrated yet
95         $replyProfiles = Profile::multiGet('id', array_unique(array_merge($notice->getReplies(), $notice->getAttentionProfileIDs())));
96         foreach ($replyProfiles->fetchAll('profileurl') as $profileurl) {
97             if (common_valid_http_url($profileurl)) {
98                 $this->linkbackUrl($profileurl);
99             }
100         }
101
102         return true;
103     }
104
105     function linkbackUrl($url)
106     {
107         common_log(LOG_DEBUG,"Attempting linkback for " . $url);
108
109         $orig = $url;
110         $url = htmlspecialchars_decode($orig);
111         $scheme = parse_url($url, PHP_URL_SCHEME);
112         if (!in_array($scheme, array('http', 'https'))) {
113             return $orig;
114         }
115
116         // XXX: Do a HEAD first to save some time/bandwidth
117
118         $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
119
120         $result = $fetcher->get($url,
121                                 array('User-Agent: ' . $this->userAgent(),
122                                       'Accept: application/html+xml,text/html'));
123
124         if (!in_array($result->status, array('200', '206'))) {
125             return $orig;
126         }
127
128         // XXX: Should handle relative-URI resolution in these detections
129
130         $wm = $this->getWebmention($result);
131         if(!empty($wm)) {
132             // It is the webmention receiver's job to resolve source
133             // Ref: https://github.com/converspace/webmention/issues/43
134             $this->webmention($url, $wm);
135         } else {
136             $pb = $this->getPingback($result);
137             if (!empty($pb)) {
138                 // Pingback still looks for exact URL in our source, so we
139                 // must send what we have
140                 $this->pingback($url, $pb);
141             } else {
142                 $tb = $this->getTrackback($result);
143                 if (!empty($tb)) {
144                     $this->trackback($result->final_url, $tb);
145                 }
146             }
147         }
148
149         return $orig;
150     }
151
152     // Based on https://github.com/indieweb/mention-client-php
153     // which is licensed Apache 2.0
154     function getWebmention($result) {
155         if (isset($result->headers['Link'])) {
156             // XXX: the fetcher only gives back one of each header, so this may fail on multiple Link headers
157             if(preg_match('~<((?:https?://)?[^>]+)>; rel="webmention"~', $result->headers['Link'], $match)) {
158                 return $match[1];
159             } elseif(preg_match('~<((?:https?://)?[^>]+)>; rel="http://webmention.org/?"~', $result->headers['Link'], $match)) {
160                 return $match[1];
161             }
162         }
163
164         // FIXME: Do proper DOM traversal
165         if(preg_match('/<(?:link|a)[ ]+href="([^"]+)"[ ]+rel="[^" ]* ?webmention ?[^" ]*"[ ]*\/?>/i', $result->body, $match)
166            || preg_match('/<(?:link|a)[ ]+rel="[^" ]* ?webmention ?[^" ]*"[ ]+href="([^"]+)"[ ]*\/?>/i', $result->body, $match)) {
167             return $match[1];
168         } elseif(preg_match('/<(?:link|a)[ ]+href="([^"]+)"[ ]+rel="http:\/\/webmention\.org\/?"[ ]*\/?>/i', $result->body, $match)
169                  || preg_match('/<(?:link|a)[ ]+rel="http:\/\/webmention\.org\/?"[ ]+href="([^"]+)"[ ]*\/?>/i', $result->body, $match)) {
170             return $match[1];
171         }
172     }
173
174     function webmention($url, $endpoint) {
175         $source = $this->notice->getUrl();
176
177         $payload = array(
178             'source' => $source,
179             'target' => $url
180         );
181
182         $request = HTTPClient::start();
183         try {
184             $response = $request->post($endpoint,
185                 array(
186                     'Content-type: application/x-www-form-urlencoded',
187                     'Accept: application/json'
188                 ),
189                 $payload
190             );
191
192             if(!in_array($response->getStatus(), array(200,202))) {
193                 common_log(LOG_WARNING,
194                            "Webmention request failed for '$url' ($endpoint)");
195             }
196         } catch (Exception $e) {
197             common_log(LOG_WARNING, "Webmention request failed for '{$url}' ({$endpoint}): {$e->getMessage()}");
198         }
199     }
200
201     function getPingback($result) {
202         if (array_key_exists('X-Pingback', $result->headers)) {
203             return $result->headers['X-Pingback'];
204         } else if(preg_match('/<(?:link|a)[ ]+href="([^"]+)"[ ]+rel="[^" ]* ?pingback ?[^" ]*"[ ]*\/?>/i', $result->body, $match)
205                   || preg_match('/<(?:link|a)[ ]+rel="[^" ]* ?pingback ?[^" ]*"[ ]+href="([^"]+)"[ ]*\/?>/i', $result->body, $match)) {
206             return $match[1];
207         }
208     }
209
210     function pingback($url, $endpoint)
211     {
212         $args = array($this->notice->getUrl(), $url);
213
214         if (!extension_loaded('xmlrpc')) {
215             if (!dl('xmlrpc.so')) {
216                 common_log(LOG_ERR, "Can't pingback; xmlrpc extension not available.");
217                 return;
218             }
219         }
220
221         $request = HTTPClient::start();
222         try {
223             $request->setBody(xmlrpc_encode_request('pingback.ping', $args));
224             $response = $request->post($endpoint,
225                 array('Content-Type: text/xml'),
226                 false);
227             $response = xmlrpc_decode($response->getBody());
228             if (xmlrpc_is_fault($response)) {
229                 common_log(LOG_WARNING,
230                        "Pingback error for '$url' ($endpoint): ".
231                        "$response[faultString] ($response[faultCode])");
232             } else {
233                 common_log(LOG_INFO,
234                        "Pingback success for '$url' ($endpoint): ".
235                        "'$response'");
236             }
237         } catch (Exception $e) {
238             common_log(LOG_WARNING, "Pingback request failed for '{$url}' ({$endpoint}): {$e->getMessage()}");
239         }
240     }
241
242     // Largely cadged from trackback_cls.php by
243     // Ran Aroussi <ran@blogish.org>, GPL2 or any later version
244     // http://phptrackback.sourceforge.net/
245     function getTrackback($result)
246     {
247         $text = $result->body;
248         $url = $result->final_url;
249
250         if (preg_match_all('/(<rdf:RDF.*?<\/rdf:RDF>)/sm', $text, $match, PREG_SET_ORDER)) {
251             for ($i = 0; $i < count($match); $i++) {
252                 if (preg_match('|dc:identifier="' . preg_quote($url) . '"|ms', $match[$i][1])) {
253                     $rdf_array[] = trim($match[$i][1]);
254                 }
255             }
256
257             // Loop through the RDFs array and extract trackback URIs
258
259             $tb_array = array(); // <- holds list of trackback URIs
260
261             if (!empty($rdf_array)) {
262
263                 for ($i = 0; $i < count($rdf_array); $i++) {
264                     if (preg_match('/trackback:ping="([^"]+)"/', $rdf_array[$i], $array)) {
265                         $tb_array[] = trim($array[1]);
266                         break;
267                     }
268                 }
269             }
270
271             // Return Trackbacks
272
273             if (empty($tb_array)) {
274                 return null;
275             } else {
276                 return $tb_array[0];
277             }
278         }
279
280         if (preg_match_all('/(<a[^>]*?rel=[\'"]trackback[\'"][^>]*?>)/', $text, $match)) {
281             foreach ($match[1] as $atag) {
282                 if (preg_match('/href=[\'"]([^\'"]*?)[\'"]/', $atag, $url)) {
283                     return $url[1];
284                 }
285             }
286         }
287
288         return null;
289
290     }
291
292     function trackback($url, $endpoint)
293     {
294         $profile = $this->notice->getProfile();
295
296         // TRANS: Trackback title.
297         // TRANS: %1$s is a profile nickname, %2$s is a timestamp.
298         $args = array('title' => sprintf(_m('%1$s\'s status on %2$s'),
299                                          $profile->nickname,
300                                          common_exact_date($this->notice->created)),
301                       'excerpt' => $this->notice->content,
302                       'url' => $this->notice->getUrl(),
303                       'blog_name' => $profile->nickname);
304
305         $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
306
307         $result = $fetcher->post($endpoint,
308                                  http_build_query($args),
309                                  array('User-Agent: ' . $this->userAgent()));
310
311         if ($result->status != '200') {
312             common_log(LOG_WARNING,
313                        "Trackback error for '$url' ($endpoint): ".
314                        "$result->body");
315         } else {
316             common_log(LOG_INFO,
317                        "Trackback success for '$url' ($endpoint): ".
318                        "'$result->body'");
319         }
320     }
321
322
323     public function onRouterInitialized(URLMapper $m)
324     {
325         $m->connect('main/linkback/webmention', array('action' => 'webmention'));
326         $m->connect('main/linkback/pingback', array('action' => 'pingback'));
327     }
328
329     public function onStartShowHTML($action)
330     {
331         header('Link: <' . common_local_url('webmention') . '>; rel="webmention"', false);
332         header('X-Pingback: ' . common_local_url('pingback'));
333     }
334
335     public function version()
336     {
337         return LINKBACKPLUGIN_VERSION;
338     }
339
340     function onPluginVersion(array &$versions)
341     {
342         $versions[] = array('name' => 'Linkback',
343                             'version' => LINKBACKPLUGIN_VERSION,
344                             'author' => 'Evan Prodromou',
345                             'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Linkback',
346                             'rawdescription' =>
347                             // TRANS: Plugin description.
348                             _m('Notify blog authors when their posts have been linked in '.
349                                'microblog notices using '.
350                                '<a href="http://www.hixie.ch/specs/pingback/pingback">Pingback</a> '.
351                                'or <a href="http://www.movabletype.org/docs/mttrackback.html">Trackback</a> protocols.'));
352         return true;
353     }
354
355     public function onStartInitializeRouter(URLMapper $m)
356     {
357         $m->connect('settings/linkback', array('action' => 'linkbacksettings'));
358         return true;
359     }
360
361     function onEndAccountSettingsNav($action)
362     {
363         $action_name = $action->trimmed('action');
364
365         $action->menuItem(common_local_url('linkbacksettings'),
366                           // TRANS: OpenID plugin menu item on user settings page.
367                           _m('MENU', 'Send Linkbacks'),
368                           // TRANS: OpenID plugin tooltip for user settings menu item.
369                           _m('Opt-out of sending linkbacks.'),
370                           $action_name === 'linkbacksettings');
371         return true;
372     }
373
374     function onStartNoticeSourceLink($notice, &$name, &$url, &$title)
375     {
376         // If we don't handle this, keep the event handler going
377         if (!in_array($notice->source, array('linkback'))) {
378             return true;
379         }
380
381         try {
382             $url = $notice->getUrl();
383             // If getUrl() throws exception, $url is never set
384
385             $bits = parse_url($url);
386             $domain = $bits['host'];
387             if (substr($domain, 0, 4) == 'www.') {
388                 $name = substr($domain, 4);
389             } else {
390                 $name = $domain;
391             }
392
393             // TRANS: Title. %s is a domain name.
394             $title = sprintf(_m('Sent from %s via Linkback'), $domain);
395
396             // Abort event handler, we have a name and URL!
397             return false;
398         } catch (InvalidUrlException $e) {
399             // This just means we don't have the notice source data
400             return true;
401         }
402     }
403 }