3 * StatusNet, the distributed open-source microblogging tool
5 * Plugin to do linkbacks for notices containing links
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.
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.
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/>.
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/
30 if (!defined('STATUSNET')) {
34 require_once(__DIR__ . '/lib/util.php');
36 define('LINKBACKPLUGIN_VERSION', '0.2');
39 * Plugin to do linkbacks for notices containing URLs
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
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/
52 class LinkbackPlugin extends Plugin
56 function __construct()
58 parent::__construct();
61 function onHandleQueuedNotice(Notice $notice)
63 if (!$notice->isLocal() || !$notice->isPublic()) {
67 // Try to avoid actually mucking with the
69 $c = $notice->content;
70 $this->notice = $notice;
72 if (!$notice->getProfile()->getPref('linkbackplugin', 'disable_linkbacks')) {
74 common_replace_urls_callback($c, array($this, 'linkbackUrl'));
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());
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
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);
104 function unparse_url($parsed_url)
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";
118 function linkbackUrl($url)
120 common_log(LOG_DEBUG,"Attempting linkback for " . $url);
123 $url = htmlspecialchars_decode($orig);
124 $base = parse_url($url);
125 if (!in_array($base['scheme'], array('http', 'https'))) {
129 // XXX: Do a HEAD first to save some time/bandwidth
131 $httpclient = new HTTPClient();
132 $response = $httpclient->get($url, ["User-Agent: {$this->userAgent()}",
133 "Accept: application/html+xml,text/html"]);
135 if (!in_array($response->getStatus(), array(200, 206))) {
136 throw new Exception('Invalid response code for GET request');
138 } catch (Exception $e) {
139 // something didn't work out in our GET request
143 $wm = $this->getWebmention($response);
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'];
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));
155 $pb = $this->getPingback($response);
157 // Pingback still looks for exact URL in our source, so we
158 // must send what we have
159 $this->pingback($url, $pb);
161 $tb = $this->getTrackback($response);
163 $this->trackback($response->getEffectiveUrl(), $tb);
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)) {
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)) {
192 function webmention($url, $endpoint) {
193 $source = $this->notice->getUrl();
195 common_log(LOG_DEBUG,"Attempting webmention to $endpoint for $url from $source");
202 $request = HTTPClient::start();
204 $response = $request->post($endpoint,
206 'Content-type: application/x-www-form-urlencoded',
207 'Accept: application/json'
212 if(!in_array($response->getStatus(), array(200,201,202))) {
213 common_log(LOG_WARNING,
214 "Webmention request failed for '$url' ($endpoint)");
216 } catch (Exception $e) {
217 common_log(LOG_WARNING, "Webmention request failed for '{$url}' ({$endpoint}): {$e->getMessage()}");
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)) {
230 function pingback($url, $endpoint)
232 $args = array($this->notice->getUrl(), $url);
234 if (!extension_loaded('xmlrpc')) {
235 if (!dl('xmlrpc.so')) {
236 common_log(LOG_ERR, "Can't pingback; xmlrpc extension not available.");
241 $request = HTTPClient::start();
243 $request->setBody(xmlrpc_encode_request('pingback.ping', $args));
244 $response = $request->post($endpoint,
245 array('Content-Type: text/xml'),
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])");
254 "Pingback success for '$url' ($endpoint): ".
257 } catch (Exception $e) {
258 common_log(LOG_WARNING, "Pingback request failed for '{$url}' ({$endpoint}): {$e->getMessage()}");
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)
267 $text = $response->getBody();
268 $url = $response->getEffectiveUrl();
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]);
277 // Loop through the RDFs array and extract trackback URIs
279 $tb_array = array(); // <- holds list of trackback URIs
281 if (!empty($rdf_array)) {
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]);
293 if (empty($tb_array)) {
300 if (preg_match_all('/(<a[^>]*?rel=[\'"]trackback[\'"][^>]*?>)/', $text, $match)) {
301 foreach ($match[1] as $atag) {
302 if (preg_match('/href=[\'"]([^\'"]*?)[\'"]/', $atag, $url)) {
312 function trackback($url, $endpoint)
314 $profile = $this->notice->getProfile();
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());
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()));
331 common_log(LOG_WARNING, "Trackback error for '$url' ($endpoint): "._ve($response->getBody()));
333 } catch (Exception $e) {
334 common_log(LOG_INFO, "Trackback error for '$url' ($endpoint): "._ve($e->getMessage()));
339 public function onRouterInitialized(URLMapper $m)
341 $m->connect('main/linkback/webmention', array('action' => 'webmention'));
342 $m->connect('main/linkback/pingback', array('action' => 'pingback'));
345 public function onStartShowHTML($action)
347 header('Link: <' . common_local_url('webmention') . '>; rel="webmention"', false);
348 header('X-Pingback: ' . common_local_url('pingback'));
351 public function version()
353 return LINKBACKPLUGIN_VERSION;
356 function onPluginVersion(array &$versions)
358 $versions[] = array('name' => 'Linkback',
359 'version' => LINKBACKPLUGIN_VERSION,
360 'author' => 'Evan Prodromou',
361 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Linkback',
363 // TRANS: Plugin description.
364 _m('Notify blog authors when their posts have been linked in '.
365 'microblog notices using '.
366 '<a href="http://www.hixie.ch/specs/pingback/pingback">Pingback</a> '.
367 'or <a href="http://www.movabletype.org/docs/mttrackback.html">Trackback</a> protocols.'));
371 public function onStartInitializeRouter(URLMapper $m)
373 $m->connect('settings/linkback', array('action' => 'linkbacksettings'));
377 function onEndAccountSettingsNav($action)
379 $action_name = $action->trimmed('action');
381 $action->menuItem(common_local_url('linkbacksettings'),
382 // TRANS: OpenID plugin menu item on user settings page.
383 _m('MENU', 'Send Linkbacks'),
384 // TRANS: OpenID plugin tooltip for user settings menu item.
385 _m('Opt-out of sending linkbacks.'),
386 $action_name === 'linkbacksettings');
390 function onStartNoticeSourceLink($notice, &$name, &$url, &$title)
392 // If we don't handle this, keep the event handler going
393 if (!in_array($notice->source, array('linkback'))) {
398 $url = $notice->getUrl();
399 // If getUrl() throws exception, $url is never set
401 $bits = parse_url($url);
402 $domain = $bits['host'];
403 if (substr($domain, 0, 4) == 'www.') {
404 $name = substr($domain, 4);
409 // TRANS: Title. %s is a domain name.
410 $title = sprintf(_m('Sent from %s via Linkback'), $domain);
412 // Abort event handler, we have a name and URL!
414 } catch (InvalidUrlException $e) {
415 // This just means we don't have the notice source data