pubsubhubbub.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * PubSubHubbub plugin.
  4. *
  5. * PubSub is a protocol which fasten up RSS fetching:
  6. * - Every time a new link is posted, Shaarli notify the hub.
  7. * - The hub notify all feed subscribers that a new link has been posted.
  8. * - Subscribers retrieve the new link.
  9. */
  10. use pubsubhubbub\publisher\Publisher;
  11. /**
  12. * Plugin init function - set the hub to the default appspot one.
  13. *
  14. * @param ConfigManager $conf instance.
  15. */
  16. function pubsubhubbub_init($conf)
  17. {
  18. $hub = $conf->get('plugins.PUBSUBHUB_URL');
  19. if (empty($hub)) {
  20. // Default hub.
  21. $conf->set('plugins.PUBSUBHUB_URL', 'https://pubsubhubbub.appspot.com/');
  22. }
  23. }
  24. /**
  25. * Render feed hook.
  26. * Adds the hub URL in ATOM and RSS feed.
  27. *
  28. * @param array $data Template data.
  29. * @param ConfigManager $conf instance.
  30. *
  31. * @return array updated template data.
  32. */
  33. function hook_pubsubhubbub_render_feed($data, $conf)
  34. {
  35. $feedType = $data['_PAGE_'] == Router::$PAGE_FEED_RSS ? FeedBuilder::$FEED_RSS : FeedBuilder::$FEED_ATOM;
  36. $template = file_get_contents(PluginManager::$PLUGINS_PATH . '/pubsubhubbub/hub.'. $feedType .'.xml');
  37. $data['feed_plugins_header'][] = sprintf($template, $conf->get('plugins.PUBSUBHUB_URL'));
  38. return $data;
  39. }
  40. /**
  41. * Save link hook.
  42. * Publish to the hub when a link is saved.
  43. *
  44. * @param array $data Template data.
  45. * @param ConfigManager $conf instance.
  46. *
  47. * @return array unaltered data.
  48. */
  49. function hook_pubsubhubbub_save_link($data, $conf)
  50. {
  51. $feeds = array(
  52. index_url($_SERVER) .'?do=atom',
  53. index_url($_SERVER) .'?do=rss',
  54. );
  55. $httpPost = function_exists('curl_version') ? false : 'nocurl_http_post';
  56. try {
  57. $p = new Publisher($conf->get('plugins.PUBSUBHUB_URL'));
  58. $p->publish_update($feeds, $httpPost);
  59. } catch (Exception $e) {
  60. error_log('Could not publish to PubSubHubbub: ' . $e->getMessage());
  61. }
  62. return $data;
  63. }
  64. /**
  65. * Http function used to post to the hub endpoint without cURL extension.
  66. *
  67. * @param string $url Hub endpoint.
  68. * @param string $postString String to POST.
  69. *
  70. * @return bool
  71. *
  72. * @throws Exception An error occurred.
  73. */
  74. function nocurl_http_post($url, $postString) {
  75. $params = array('http' => array(
  76. 'method' => 'POST',
  77. 'content' => $postString,
  78. 'user_agent' => 'PubSubHubbub-Publisher-PHP/1.0',
  79. ));
  80. $context = stream_context_create($params);
  81. $fp = @fopen($url, 'rb', false, $context);
  82. if (!$fp) {
  83. throw new Exception('Could not post to '. $url);
  84. }
  85. $response = @stream_get_contents($fp);
  86. if ($response === false) {
  87. throw new Exception('Bad response from the hub '. $url);
  88. }
  89. return $response;
  90. }