]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Linkback/LinkbackPlugin.php
Fix Pingback
[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
36 define('LINKBACKPLUGIN_VERSION', '0.1');
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)
62     {
63         if ($notice->is_local == 1) {
64             // Try to avoid actually mucking with the
65             // notice content
66             $c = $notice->content;
67             $this->notice = $notice;
68             // Ignoring results
69             common_replace_urls_callback($c,
70                                          array($this, 'linkbackUrl'));
71
72             if($notice->isRepeat()) {
73                 $repeat = Notice::getByID($notice->repeat_of);
74                 $this->linkbackUrl($repeat->getUrl());
75             } else if(!empty($notice->reply_to)) {
76                 $parent = $notice->getParent();
77                 $this->linkbackUrl($parent->getUrl());
78             }
79         }
80         return true;
81     }
82
83     function linkbackUrl($url)
84     {
85         common_log(LOG_DEBUG,"Attempting linkback for " . $url);
86
87         $orig = $url;
88         $url = htmlspecialchars_decode($orig);
89         $scheme = parse_url($url, PHP_URL_SCHEME);
90         if (!in_array($scheme, array('http', 'https'))) {
91             return $orig;
92         }
93
94         // XXX: Do a HEAD first to save some time/bandwidth
95
96         $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
97
98         $result = $fetcher->get($url,
99                                 array('User-Agent: ' . $this->userAgent(),
100                                       'Accept: application/html+xml,text/html'));
101
102         if (!in_array($result->status, array('200', '206'))) {
103             return $orig;
104         }
105
106         // XXX: Should handle relative-URI resolution in these detections
107
108         $wm = $this->getWebmention($result);
109         if(!empty($wm)) {
110             // It is the webmention receiver's job to resolve source
111             // Ref: https://github.com/converspace/webmention/issues/43
112             $this->webmention($url, $wm);
113         } else {
114             $pb = $this->getPingback($result);
115             if (!empty($pb)) {
116                 // Pingback still looks for exact URL in our source, so we
117                 // must send what we have
118                 $this->pingback($url, $pb);
119             } else {
120                 $tb = $this->getTrackback($result);
121                 if (!empty($tb)) {
122                     $this->trackback($result->final_url, $tb);
123                 }
124             }
125         }
126
127         return $orig;
128     }
129
130     // Based on https://github.com/indieweb/mention-client-php
131     // which is licensed Apache 2.0
132     function getWebmention($result) {
133         // XXX: the fetcher only gives back one of each header, so this may fail on multiple Link headers
134         if(preg_match('~<((?:https?://)?[^>]+)>; rel="webmention"~', $result->headers['Link'], $match)) {
135             return $match[1];
136         } elseif(preg_match('~<((?:https?://)?[^>]+)>; rel="http://webmention.org/?"~', $result->headers['Link'], $match)) {
137             return $match[1];
138         }
139
140         if(preg_match('/<(?:link|a)[ ]+href="([^"]+)"[ ]+rel="[^" ]* ?webmention ?[^" ]*"[ ]*\/?>/i', $result->body, $match)
141            || preg_match('/<(?:link|a)[ ]+rel="[^" ]* ?webmention ?[^" ]*"[ ]+href="([^"]+)"[ ]*\/?>/i', $result->body, $match)) {
142             return $match[1];
143         } elseif(preg_match('/<(?:link|a)[ ]+href="([^"]+)"[ ]+rel="http:\/\/webmention\.org\/?"[ ]*\/?>/i', $result->body, $match)
144                  || preg_match('/<(?:link|a)[ ]+rel="http:\/\/webmention\.org\/?"[ ]+href="([^"]+)"[ ]*\/?>/i', $result->body, $match)) {
145             return $match[1];
146         }
147     }
148
149     function webmention($url, $endpoint) {
150         $source = $this->notice->getUrl();
151
152         $payload = array(
153             'source' => $source,
154             'target' => $url
155         );
156
157         $request = HTTPClient::start();
158         try {
159             $response = $request->post($endpoint,
160                 array(
161                     'Content-type: application/x-www-form-urlencoded',
162                     'Accept: application/json'
163                 ),
164                 $payload
165             );
166
167             if(!in_array($response->getStatus(), array(200,202))) {
168                 common_log(LOG_WARNING,
169                            "Webmention request failed for '$url' ($endpoint)");
170             }
171         } catch (HTTP_Request2_Exception $e) {
172             common_log(LOG_WARNING,
173                        "Webmention request failed for '$url' ($endpoint)");
174         }
175     }
176
177     function getPingback($result) {
178         if (array_key_exists('X-Pingback', $result->headers)) {
179             return $result->headers['X-Pingback'];
180         } else if(preg_match('/<(?:link|a)[ ]+href="([^"]+)"[ ]+rel="[^" ]* ?pingback ?[^" ]*"[ ]*\/?>/i', $result->body, $match)
181                   || preg_match('/<(?:link|a)[ ]+rel="[^" ]* ?pingback ?[^" ]*"[ ]+href="([^"]+)"[ ]*\/?>/i', $result->body, $match)) {
182             return $match[1];
183         }
184     }
185
186     function pingback($url, $endpoint)
187     {
188         $args = array($this->notice->getUrl(), $url);
189
190         if (!extension_loaded('xmlrpc')) {
191             if (!dl('xmlrpc.so')) {
192                 common_log(LOG_ERR, "Can't pingback; xmlrpc extension not available.");
193                 return;
194             }
195         }
196
197         $request = HTTPClient::start();
198         try {
199             $request->setBody(xmlrpc_encode_request('pingback.ping', $args));
200             $response = $request->post($endpoint,
201                 array('Content-Type: text/xml'),
202                 false);
203             $response = xmlrpc_decode($response->getBody());
204             if (xmlrpc_is_fault($response)) {
205                 common_log(LOG_WARNING,
206                        "Pingback error for '$url' ($endpoint): ".
207                        "$response[faultString] ($response[faultCode])");
208             } else {
209                 common_log(LOG_INFO,
210                        "Pingback success for '$url' ($endpoint): ".
211                        "'$response'");
212             }
213         } catch (HTTP_Request2_Exception $e) {
214             common_log(LOG_WARNING,
215                    "Pingback request failed for '$url' ($endpoint)");
216         }
217     }
218
219     // Largely cadged from trackback_cls.php by
220     // Ran Aroussi <ran@blogish.org>, GPL2 or any later version
221     // http://phptrackback.sourceforge.net/
222     function getTrackback($result)
223     {
224         $text = $result->body;
225         $url = $result->final_url;
226
227         if (preg_match_all('/(<rdf:RDF.*?<\/rdf:RDF>)/sm', $text, $match, PREG_SET_ORDER)) {
228             for ($i = 0; $i < count($match); $i++) {
229                 if (preg_match('|dc:identifier="' . preg_quote($url) . '"|ms', $match[$i][1])) {
230                     $rdf_array[] = trim($match[$i][1]);
231                 }
232             }
233
234             // Loop through the RDFs array and extract trackback URIs
235
236             $tb_array = array(); // <- holds list of trackback URIs
237
238             if (!empty($rdf_array)) {
239
240                 for ($i = 0; $i < count($rdf_array); $i++) {
241                     if (preg_match('/trackback:ping="([^"]+)"/', $rdf_array[$i], $array)) {
242                         $tb_array[] = trim($array[1]);
243                         break;
244                     }
245                 }
246             }
247
248             // Return Trackbacks
249
250             if (empty($tb_array)) {
251                 return null;
252             } else {
253                 return $tb_array[0];
254             }
255         }
256
257         if (preg_match_all('/(<a[^>]*?rel=[\'"]trackback[\'"][^>]*?>)/', $text, $match)) {
258             foreach ($match[1] as $atag) {
259                 if (preg_match('/href=[\'"]([^\'"]*?)[\'"]/', $atag, $url)) {
260                     return $url[1];
261                 }
262             }
263         }
264
265         return null;
266
267     }
268
269     function trackback($url, $endpoint)
270     {
271         $profile = $this->notice->getProfile();
272
273         // TRANS: Trackback title.
274         // TRANS: %1$s is a profile nickname, %2$s is a timestamp.
275         $args = array('title' => sprintf(_m('%1$s\'s status on %2$s'),
276                                          $profile->nickname,
277                                          common_exact_date($this->notice->created)),
278                       'excerpt' => $this->notice->content,
279                       'url' => $this->notice->getUrl(),
280                       'blog_name' => $profile->nickname);
281
282         $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
283
284         $result = $fetcher->post($endpoint,
285                                  http_build_query($args),
286                                  array('User-Agent: ' . $this->userAgent()));
287
288         if ($result->status != '200') {
289             common_log(LOG_WARNING,
290                        "Trackback error for '$url' ($endpoint): ".
291                        "$result->body");
292         } else {
293             common_log(LOG_INFO,
294                        "Trackback success for '$url' ($endpoint): ".
295                        "'$result->body'");
296         }
297     }
298
299     public function version()
300     {
301         return LINKBACKPLUGIN_VERSION;
302     }
303
304     function onPluginVersion(array &$versions)
305     {
306         $versions[] = array('name' => 'Linkback',
307                             'version' => LINKBACKPLUGIN_VERSION,
308                             'author' => 'Evan Prodromou',
309                             'homepage' => 'http://status.net/wiki/Plugin:Linkback',
310                             'rawdescription' =>
311                             // TRANS: Plugin description.
312                             _m('Notify blog authors when their posts have been linked in '.
313                                'microblog notices using '.
314                                '<a href="http://www.hixie.ch/specs/pingback/pingback">Pingback</a> '.
315                                'or <a href="http://www.movabletype.org/docs/mttrackback.html">Trackback</a> protocols.'));
316         return true;
317     }
318 }