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