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('Auth/Yadis/Yadis.php');
36 define('LINKBACKPLUGIN_VERSION', '0.1');
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)
63 if ($notice->is_local == 1) {
64 // Try to avoid actually mucking with the
66 $c = $notice->content;
67 $this->notice = $notice;
69 common_replace_urls_callback($c,
70 array($this, 'linkbackUrl'));
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());
83 function linkbackUrl($url)
85 common_log(LOG_DEBUG,"Attempting linkback for " . $url);
88 $url = htmlspecialchars_decode($orig);
89 $scheme = parse_url($url, PHP_URL_SCHEME);
90 if (!in_array($scheme, array('http', 'https'))) {
94 // XXX: Do a HEAD first to save some time/bandwidth
96 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
98 $result = $fetcher->get($url,
99 array('User-Agent: ' . $this->userAgent(),
100 'Accept: application/html+xml,text/html'));
102 if (!in_array($result->status, array('200', '206'))) {
106 // XXX: Should handle relative-URI resolution in these detections
108 $wm = $this->getWebmention($result);
110 $this->webmention($result->final_url, $wm);
112 $pb = $this->getPingback($result);
114 $this->pingback($result->final_url, $pb);
116 $tb = $this->getTrackback($result);
118 $this->trackback($result->final_url, $tb);
126 // Based on https://github.com/indieweb/mention-client-php
127 // which is licensed Apache 2.0
128 function getWebmention($result) {
129 // XXX: the fetcher only gives back one of each header, so this may fail on multiple Link headers
130 if(preg_match('~<((?:https?://)?[^>]+)>; rel="webmention"~', $result->headers['Link'], $match)) {
132 } elseif(preg_match('~<((?:https?://)?[^>]+)>; rel="http://webmention.org/?"~', $result->headers['Link'], $match)) {
136 if(preg_match('/<(?:link|a)[ ]+href="([^"]+)"[ ]+rel="[^" ]* ?webmention ?[^" ]*"[ ]*\/?>/i', $result->body, $match)
137 || preg_match('/<(?:link|a)[ ]+rel="[^" ]* ?webmention ?[^" ]*"[ ]+href="([^"]+)"[ ]*\/?>/i', $result->body, $match)) {
139 } elseif(preg_match('/<(?:link|a)[ ]+href="([^"]+)"[ ]+rel="http:\/\/webmention\.org\/?"[ ]*\/?>/i', $result->body, $match)
140 || preg_match('/<(?:link|a)[ ]+rel="http:\/\/webmention\.org\/?"[ ]+href="([^"]+)"[ ]*\/?>/i', $result->body, $match)) {
145 function webmention($url, $endpoint) {
146 $source = $this->notice->getUrl();
153 $request = HTTPClient::start();
155 $response = $request->post($endpoint,
157 'Content-type: application/x-www-form-urlencoded',
158 'Accept: application/json'
163 if(!in_array($response->getStatus(), array(200,202))) {
164 common_log(LOG_WARNING,
165 "Webmention request failed for '$url' ($endpoint)");
167 } catch (HTTP_Request2_Exception $e) {
168 common_log(LOG_WARNING,
169 "Webmention request failed for '$url' ($endpoint)");
173 function getPingback($result) {
174 if (array_key_exists('X-Pingback', $result->headers)) {
175 return $result->headers['X-Pingback'];
176 } else if(preg_match('/<(?:link|a)[ ]+href="([^"]+)"[ ]+rel="[^" ]* ?pingback ?[^" ]*"[ ]*\/?>/i', $result->body, $match)
177 || preg_match('/<(?:link|a)[ ]+rel="[^" ]* ?pingback ?[^" ]*"[ ]+href="([^"]+)"[ ]*\/?>/i', $result->body, $match)) {
182 function pingback($url, $endpoint)
184 $args = array($this->notice->getUrl(), $url);
186 if (!extension_loaded('xmlrpc')) {
187 if (!dl('xmlrpc.so')) {
188 common_log(LOG_ERR, "Can't pingback; xmlrpc extension not available.");
193 $request = HTTPClient::start();
195 $response = $request->post($endpoint,
196 array('Content-Type: text/xml'),
197 xmlrpc_encode_request('pingback.ping', $args));
198 $response = xmlrpc_decode($response->getBody());
199 if (xmlrpc_is_fault($response)) {
200 common_log(LOG_WARNING,
201 "Pingback error for '$url' ($endpoint): ".
202 "$response[faultString] ($response[faultCode])");
205 "Pingback success for '$url' ($endpoint): ".
208 } catch (HTTP_Request2_Exception $e) {
209 common_log(LOG_WARNING,
210 "Pingback request failed for '$url' ($endpoint)");
214 // Largely cadged from trackback_cls.php by
215 // Ran Aroussi <ran@blogish.org>, GPL2 or any later version
216 // http://phptrackback.sourceforge.net/
217 function getTrackback($result)
219 $text = $result->body;
220 $url = $result->final_url;
222 if (preg_match_all('/(<rdf:RDF.*?<\/rdf:RDF>)/sm', $text, $match, PREG_SET_ORDER)) {
223 for ($i = 0; $i < count($match); $i++) {
224 if (preg_match('|dc:identifier="' . preg_quote($url) . '"|ms', $match[$i][1])) {
225 $rdf_array[] = trim($match[$i][1]);
229 // Loop through the RDFs array and extract trackback URIs
231 $tb_array = array(); // <- holds list of trackback URIs
233 if (!empty($rdf_array)) {
235 for ($i = 0; $i < count($rdf_array); $i++) {
236 if (preg_match('/trackback:ping="([^"]+)"/', $rdf_array[$i], $array)) {
237 $tb_array[] = trim($array[1]);
245 if (empty($tb_array)) {
252 if (preg_match_all('/(<a[^>]*?rel=[\'"]trackback[\'"][^>]*?>)/', $text, $match)) {
253 foreach ($match[1] as $atag) {
254 if (preg_match('/href=[\'"]([^\'"]*?)[\'"]/', $atag, $url)) {
264 function trackback($url, $endpoint)
266 $profile = $this->notice->getProfile();
268 // TRANS: Trackback title.
269 // TRANS: %1$s is a profile nickname, %2$s is a timestamp.
270 $args = array('title' => sprintf(_m('%1$s\'s status on %2$s'),
272 common_exact_date($this->notice->created)),
273 'excerpt' => $this->notice->content,
274 'url' => $this->notice->getUrl(),
275 'blog_name' => $profile->nickname);
277 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
279 $result = $fetcher->post($endpoint,
280 http_build_query($args),
281 array('User-Agent: ' . $this->userAgent()));
283 if ($result->status != '200') {
284 common_log(LOG_WARNING,
285 "Trackback error for '$url' ($endpoint): ".
289 "Trackback success for '$url' ($endpoint): ".
294 public function version()
296 return LINKBACKPLUGIN_VERSION;
299 function onPluginVersion(array &$versions)
301 $versions[] = array('name' => 'Linkback',
302 'version' => LINKBACKPLUGIN_VERSION,
303 'author' => 'Evan Prodromou',
304 'homepage' => 'http://status.net/wiki/Plugin:Linkback',
306 // TRANS: Plugin description.
307 _m('Notify blog authors when their posts have been linked in '.
308 'microblog notices using '.
309 '<a href="http://www.hixie.ch/specs/pingback/pingback">Pingback</a> '.
310 'or <a href="http://www.movabletype.org/docs/mttrackback.html">Trackback</a> protocols.'));