LinkbackPlugin.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. define('LINKBACKPLUGIN_VERSION', '0.1');
  34. /**
  35. * Plugin to do linkbacks for notices containing URLs
  36. *
  37. * After new notices are saved, we check their text for URLs. If there
  38. * are URLs, we test each URL to see if it supports any
  39. *
  40. * @category Plugin
  41. * @package StatusNet
  42. * @author Evan Prodromou <evan@status.net>
  43. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  44. * @link http://status.net/
  45. *
  46. * @see Event
  47. */
  48. class LinkbackPlugin extends Plugin
  49. {
  50. var $notice = null;
  51. function __construct()
  52. {
  53. parent::__construct();
  54. }
  55. function onHandleQueuedNotice($notice)
  56. {
  57. if ($notice->is_local == 1) {
  58. // Try to avoid actually mucking with the
  59. // notice content
  60. $c = $notice->content;
  61. $this->notice = $notice;
  62. // Ignoring results
  63. common_replace_urls_callback($c,
  64. array($this, 'linkbackUrl'));
  65. }
  66. return true;
  67. }
  68. function linkbackUrl($url)
  69. {
  70. common_log(LOG_DEBUG,"Attempting linkback for " . $url);
  71. $orig = $url;
  72. $url = htmlspecialchars_decode($orig);
  73. $scheme = parse_url($url, PHP_URL_SCHEME);
  74. if (!in_array($scheme, array('http', 'https'))) {
  75. return $orig;
  76. }
  77. // XXX: Do a HEAD first to save some time/bandwidth
  78. $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
  79. $result = $fetcher->get($url,
  80. array('User-Agent: ' . $this->userAgent(),
  81. 'Accept: application/html+xml,text/html'));
  82. if (!in_array($result->status, array('200', '206'))) {
  83. return $orig;
  84. }
  85. $pb = null;
  86. $tb = null;
  87. if (array_key_exists('X-Pingback', $result->headers)) {
  88. $pb = $result->headers['X-Pingback'];
  89. } else if (preg_match('/<link rel="pingback" href="([^"]+)" ?\/?>/',
  90. $result->body,
  91. $match)) {
  92. $pb = $match[1];
  93. }
  94. if (!empty($pb)) {
  95. $this->pingback($result->final_url, $pb);
  96. } else {
  97. $tb = $this->getTrackback($result->body, $result->final_url);
  98. if (!empty($tb)) {
  99. $this->trackback($result->final_url, $tb);
  100. }
  101. }
  102. return $orig;
  103. }
  104. function pingback($url, $endpoint)
  105. {
  106. $args = array($this->notice->uri, $url);
  107. if (!extension_loaded('xmlrpc')) {
  108. if (!dl('xmlrpc.so')) {
  109. common_log(LOG_ERR, "Can't pingback; xmlrpc extension not available.");
  110. return;
  111. }
  112. }
  113. $request = HTTPClient::start();
  114. try {
  115. $response = $request->post($endpoint,
  116. array('Content-Type: text/xml'),
  117. xmlrpc_encode_request('pingback.ping', $args));
  118. $response = xmlrpc_decode($response->getBody());
  119. if (xmlrpc_is_fault($response)) {
  120. common_log(LOG_WARNING,
  121. "Pingback error for '$url' ($endpoint): ".
  122. "$response[faultString] ($response[faultCode])");
  123. } else {
  124. common_log(LOG_INFO,
  125. "Pingback success for '$url' ($endpoint): ".
  126. "'$response'");
  127. }
  128. } catch (HTTP_Request2_Exception $e) {
  129. common_log(LOG_WARNING,
  130. "Pingback request failed for '$url' ($endpoint)");
  131. }
  132. }
  133. // Largely cadged from trackback_cls.php by
  134. // Ran Aroussi <ran@blogish.org>, GPL2 or any later version
  135. // http://phptrackback.sourceforge.net/
  136. function getTrackback($text, $url)
  137. {
  138. if (preg_match_all('/(<rdf:RDF.*?<\/rdf:RDF>)/sm', $text, $match, PREG_SET_ORDER)) {
  139. for ($i = 0; $i < count($match); $i++) {
  140. if (preg_match('|dc:identifier="' . preg_quote($url) . '"|ms', $match[$i][1])) {
  141. $rdf_array[] = trim($match[$i][1]);
  142. }
  143. }
  144. // Loop through the RDFs array and extract trackback URIs
  145. $tb_array = array(); // <- holds list of trackback URIs
  146. if (!empty($rdf_array)) {
  147. for ($i = 0; $i < count($rdf_array); $i++) {
  148. if (preg_match('/trackback:ping="([^"]+)"/', $rdf_array[$i], $array)) {
  149. $tb_array[] = trim($array[1]);
  150. break;
  151. }
  152. }
  153. }
  154. // Return Trackbacks
  155. if (empty($tb_array)) {
  156. return null;
  157. } else {
  158. return $tb_array[0];
  159. }
  160. }
  161. if (preg_match_all('/(<a[^>]*?rel=[\'"]trackback[\'"][^>]*?>)/', $text, $match)) {
  162. foreach ($match[1] as $atag) {
  163. if (preg_match('/href=[\'"]([^\'"]*?)[\'"]/', $atag, $url)) {
  164. return $url[1];
  165. }
  166. }
  167. }
  168. return null;
  169. }
  170. function trackback($url, $endpoint)
  171. {
  172. $profile = $this->notice->getProfile();
  173. // TRANS: Trackback title.
  174. // TRANS: %1$s is a profile nickname, %2$s is a timestamp.
  175. $args = array('title' => sprintf(_m('%1$s\'s status on %2$s'),
  176. $profile->nickname,
  177. common_exact_date($this->notice->created)),
  178. 'excerpt' => $this->notice->content,
  179. 'url' => $this->notice->getUrl(),
  180. 'blog_name' => $profile->nickname);
  181. $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
  182. $result = $fetcher->post($endpoint,
  183. http_build_query($args),
  184. array('User-Agent: ' . $this->userAgent()));
  185. if ($result->status != '200') {
  186. common_log(LOG_WARNING,
  187. "Trackback error for '$url' ($endpoint): ".
  188. "$result->body");
  189. } else {
  190. common_log(LOG_INFO,
  191. "Trackback success for '$url' ($endpoint): ".
  192. "'$result->body'");
  193. }
  194. }
  195. public function version()
  196. {
  197. return LINKBACKPLUGIN_VERSION;
  198. }
  199. function onPluginVersion(&$versions)
  200. {
  201. $versions[] = array('name' => 'Linkback',
  202. 'version' => LINKBACKPLUGIN_VERSION,
  203. 'author' => 'Evan Prodromou',
  204. 'homepage' => 'http://status.net/wiki/Plugin:Linkback',
  205. 'rawdescription' =>
  206. // TRANS: Plugin description.
  207. _m('Notify blog authors when their posts have been linked in '.
  208. 'microblog notices using '.
  209. '<a href="http://www.hixie.ch/specs/pingback/pingback">Pingback</a> '.
  210. 'or <a href="http://www.movabletype.org/docs/mttrackback.html">Trackback</a> protocols.'));
  211. return true;
  212. }
  213. }