LinkbackPlugin.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. require_once('Auth/Yadis/Yadis.php');
  33. require_once(__DIR__ . '/lib/util.php');
  34. define('LINKBACKPLUGIN_VERSION', '0.1');
  35. /**
  36. * Plugin to do linkbacks for notices containing URLs
  37. *
  38. * After new notices are saved, we check their text for URLs. If there
  39. * are URLs, we test each URL to see if it supports any
  40. *
  41. * @category Plugin
  42. * @package StatusNet
  43. * @author Evan Prodromou <evan@status.net>
  44. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  45. * @link http://status.net/
  46. *
  47. * @see Event
  48. */
  49. class LinkbackPlugin extends Plugin
  50. {
  51. var $notice = null;
  52. function __construct()
  53. {
  54. parent::__construct();
  55. }
  56. function onHandleQueuedNotice($notice)
  57. {
  58. if (intval($notice->is_local) === Notice::LOCAL_PUBLIC) {
  59. // Try to avoid actually mucking with the
  60. // notice content
  61. $c = $notice->content;
  62. $this->notice = $notice;
  63. if(!$notice->getProfile()->
  64. getPref("linkbackplugin", "disable_linkbacks")
  65. ) {
  66. // Ignoring results
  67. common_replace_urls_callback($c,
  68. array($this, 'linkbackUrl'));
  69. }
  70. if($notice->isRepeat()) {
  71. $repeat = Notice::getByID($notice->repeat_of);
  72. $this->linkbackUrl($repeat->getUrl());
  73. } else if(!empty($notice->reply_to)) {
  74. try {
  75. $parent = $notice->getParent();
  76. $this->linkbackUrl($parent->getUrl());
  77. } catch (NoParentNoticeException $e) {
  78. // can't link back to what we don't know (apparently parent notice disappeared from our db)
  79. return true;
  80. }
  81. }
  82. // doubling up getReplies and getAttentionProfileIDs because we're not entirely migrated yet
  83. $replyProfiles = Profile::multiGet('id', array_unique(array_merge($notice->getReplies(), $notice->getAttentionProfileIDs())));
  84. foreach($replyProfiles->fetchAll('profileurl') as $profileurl) {
  85. $this->linkbackUrl($profileurl);
  86. }
  87. }
  88. return true;
  89. }
  90. function linkbackUrl($url)
  91. {
  92. common_log(LOG_DEBUG,"Attempting linkback for " . $url);
  93. $orig = $url;
  94. $url = htmlspecialchars_decode($orig);
  95. $scheme = parse_url($url, PHP_URL_SCHEME);
  96. if (!in_array($scheme, array('http', 'https'))) {
  97. return $orig;
  98. }
  99. // XXX: Do a HEAD first to save some time/bandwidth
  100. $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
  101. $result = $fetcher->get($url,
  102. array('User-Agent: ' . $this->userAgent(),
  103. 'Accept: application/html+xml,text/html'));
  104. if (!in_array($result->status, array('200', '206'))) {
  105. return $orig;
  106. }
  107. // XXX: Should handle relative-URI resolution in these detections
  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. return $orig;
  127. }
  128. // Based on https://github.com/indieweb/mention-client-php
  129. // which is licensed Apache 2.0
  130. function getWebmention($result) {
  131. if (isset($result->headers['Link'])) {
  132. // XXX: the fetcher only gives back one of each header, so this may fail on multiple Link headers
  133. if(preg_match('~<((?:https?://)?[^>]+)>; rel="webmention"~', $result->headers['Link'], $match)) {
  134. return $match[1];
  135. } elseif(preg_match('~<((?:https?://)?[^>]+)>; rel="http://webmention.org/?"~', $result->headers['Link'], $match)) {
  136. return $match[1];
  137. }
  138. }
  139. // FIXME: Do proper DOM traversal
  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. function webmention($url, $endpoint) {
  149. $source = $this->notice->getUrl();
  150. $payload = array(
  151. 'source' => $source,
  152. 'target' => $url
  153. );
  154. $request = HTTPClient::start();
  155. try {
  156. $response = $request->post($endpoint,
  157. array(
  158. 'Content-type: application/x-www-form-urlencoded',
  159. 'Accept: application/json'
  160. ),
  161. $payload
  162. );
  163. if(!in_array($response->getStatus(), array(200,202))) {
  164. common_log(LOG_WARNING,
  165. "Webmention request failed for '$url' ($endpoint)");
  166. }
  167. } catch (Exception $e) {
  168. common_log(LOG_WARNING, "Webmention request failed for '{$url}' ({$endpoint}): {$e->getMessage()}");
  169. }
  170. }
  171. function getPingback($result) {
  172. if (array_key_exists('X-Pingback', $result->headers)) {
  173. return $result->headers['X-Pingback'];
  174. } else if(preg_match('/<(?:link|a)[ ]+href="([^"]+)"[ ]+rel="[^" ]* ?pingback ?[^" ]*"[ ]*\/?>/i', $result->body, $match)
  175. || preg_match('/<(?:link|a)[ ]+rel="[^" ]* ?pingback ?[^" ]*"[ ]+href="([^"]+)"[ ]*\/?>/i', $result->body, $match)) {
  176. return $match[1];
  177. }
  178. }
  179. function pingback($url, $endpoint)
  180. {
  181. $args = array($this->notice->getUrl(), $url);
  182. if (!extension_loaded('xmlrpc')) {
  183. if (!dl('xmlrpc.so')) {
  184. common_log(LOG_ERR, "Can't pingback; xmlrpc extension not available.");
  185. return;
  186. }
  187. }
  188. $request = HTTPClient::start();
  189. try {
  190. $request->setBody(xmlrpc_encode_request('pingback.ping', $args));
  191. $response = $request->post($endpoint,
  192. array('Content-Type: text/xml'),
  193. false);
  194. $response = xmlrpc_decode($response->getBody());
  195. if (xmlrpc_is_fault($response)) {
  196. common_log(LOG_WARNING,
  197. "Pingback error for '$url' ($endpoint): ".
  198. "$response[faultString] ($response[faultCode])");
  199. } else {
  200. common_log(LOG_INFO,
  201. "Pingback success for '$url' ($endpoint): ".
  202. "'$response'");
  203. }
  204. } catch (Exception $e) {
  205. common_log(LOG_WARNING, "Pingback request failed for '{$url}' ({$endpoint}): {$e->getMessage()}");
  206. }
  207. }
  208. // Largely cadged from trackback_cls.php by
  209. // Ran Aroussi <ran@blogish.org>, GPL2 or any later version
  210. // http://phptrackback.sourceforge.net/
  211. function getTrackback($result)
  212. {
  213. $text = $result->body;
  214. $url = $result->final_url;
  215. if (preg_match_all('/(<rdf:RDF.*?<\/rdf:RDF>)/sm', $text, $match, PREG_SET_ORDER)) {
  216. for ($i = 0; $i < count($match); $i++) {
  217. if (preg_match('|dc:identifier="' . preg_quote($url) . '"|ms', $match[$i][1])) {
  218. $rdf_array[] = trim($match[$i][1]);
  219. }
  220. }
  221. // Loop through the RDFs array and extract trackback URIs
  222. $tb_array = array(); // <- holds list of trackback URIs
  223. if (!empty($rdf_array)) {
  224. for ($i = 0; $i < count($rdf_array); $i++) {
  225. if (preg_match('/trackback:ping="([^"]+)"/', $rdf_array[$i], $array)) {
  226. $tb_array[] = trim($array[1]);
  227. break;
  228. }
  229. }
  230. }
  231. // Return Trackbacks
  232. if (empty($tb_array)) {
  233. return null;
  234. } else {
  235. return $tb_array[0];
  236. }
  237. }
  238. if (preg_match_all('/(<a[^>]*?rel=[\'"]trackback[\'"][^>]*?>)/', $text, $match)) {
  239. foreach ($match[1] as $atag) {
  240. if (preg_match('/href=[\'"]([^\'"]*?)[\'"]/', $atag, $url)) {
  241. return $url[1];
  242. }
  243. }
  244. }
  245. return null;
  246. }
  247. function trackback($url, $endpoint)
  248. {
  249. $profile = $this->notice->getProfile();
  250. // TRANS: Trackback title.
  251. // TRANS: %1$s is a profile nickname, %2$s is a timestamp.
  252. $args = array('title' => sprintf(_m('%1$s\'s status on %2$s'),
  253. $profile->nickname,
  254. common_exact_date($this->notice->created)),
  255. 'excerpt' => $this->notice->content,
  256. 'url' => $this->notice->getUrl(),
  257. 'blog_name' => $profile->nickname);
  258. $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
  259. $result = $fetcher->post($endpoint,
  260. http_build_query($args),
  261. array('User-Agent: ' . $this->userAgent()));
  262. if ($result->status != '200') {
  263. common_log(LOG_WARNING,
  264. "Trackback error for '$url' ($endpoint): ".
  265. "$result->body");
  266. } else {
  267. common_log(LOG_INFO,
  268. "Trackback success for '$url' ($endpoint): ".
  269. "'$result->body'");
  270. }
  271. }
  272. public function onRouterInitialized(URLMapper $m)
  273. {
  274. $m->connect('main/linkback/webmention', array('action' => 'webmention'));
  275. $m->connect('main/linkback/pingback', array('action' => 'pingback'));
  276. }
  277. public function onStartShowHTML($action)
  278. {
  279. header('Link: <' . common_local_url('webmention') . '>; rel="webmention"', false);
  280. header('X-Pingback: ' . common_local_url('pingback'));
  281. }
  282. public function version()
  283. {
  284. return LINKBACKPLUGIN_VERSION;
  285. }
  286. function onPluginVersion(array &$versions)
  287. {
  288. $versions[] = array('name' => 'Linkback',
  289. 'version' => LINKBACKPLUGIN_VERSION,
  290. 'author' => 'Evan Prodromou',
  291. 'homepage' => 'http://status.net/wiki/Plugin:Linkback',
  292. 'rawdescription' =>
  293. // TRANS: Plugin description.
  294. _m('Notify blog authors when their posts have been linked in '.
  295. 'microblog notices using '.
  296. '<a href="http://www.hixie.ch/specs/pingback/pingback">Pingback</a> '.
  297. 'or <a href="http://www.movabletype.org/docs/mttrackback.html">Trackback</a> protocols.'));
  298. return true;
  299. }
  300. public function onStartInitializeRouter(URLMapper $m)
  301. {
  302. $m->connect('settings/linkback', array('action' => 'linkbacksettings'));
  303. return true;
  304. }
  305. function onEndAccountSettingsNav($action)
  306. {
  307. $action_name = $action->trimmed('action');
  308. $action->menuItem(common_local_url('linkbacksettings'),
  309. // TRANS: OpenID plugin menu item on user settings page.
  310. _m('MENU', 'Send Linkbacks'),
  311. // TRANS: OpenID plugin tooltip for user settings menu item.
  312. _m('Opt-out of sending linkbacks.'),
  313. $action_name === 'linkbacksettings');
  314. return true;
  315. }
  316. function onStartNoticeSourceLink($notice, &$name, &$url, &$title)
  317. {
  318. // If we don't handle this, keep the event handler going
  319. if (!in_array($notice->source, array('linkback'))) {
  320. return true;
  321. }
  322. try {
  323. $url = $notice->getUrl();
  324. // If getUrl() throws exception, $url is never set
  325. $bits = parse_url($url);
  326. $domain = $bits['host'];
  327. if (substr($domain, 0, 4) == 'www.') {
  328. $name = substr($domain, 4);
  329. } else {
  330. $name = $domain;
  331. }
  332. // TRANS: Title. %s is a domain name.
  333. $title = sprintf(_m('Sent from %s via Linkback'), $domain);
  334. // Abort event handler, we have a name and URL!
  335. return false;
  336. } catch (InvalidUrlException $e) {
  337. // This just means we don't have the notice source data
  338. return true;
  339. }
  340. }
  341. }