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