pubsubhubbub.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. use Shaarli\Config\ConfigManager;
  12. /**
  13. * Plugin init function - set the hub to the default appspot one.
  14. *
  15. * @param ConfigManager $conf instance.
  16. */
  17. function pubsubhubbub_init($conf)
  18. {
  19. $hub = $conf->get('plugins.PUBSUBHUB_URL');
  20. if (empty($hub)) {
  21. // Default hub.
  22. $conf->set('plugins.PUBSUBHUB_URL', 'https://pubsubhubbub.appspot.com/');
  23. }
  24. }
  25. /**
  26. * Render feed hook.
  27. * Adds the hub URL in ATOM and RSS feed.
  28. *
  29. * @param array $data Template data.
  30. * @param ConfigManager $conf instance.
  31. *
  32. * @return array updated template data.
  33. */
  34. function hook_pubsubhubbub_render_feed($data, $conf)
  35. {
  36. $feedType = $data['_PAGE_'] == Router::$PAGE_FEED_RSS ? FeedBuilder::$FEED_RSS : FeedBuilder::$FEED_ATOM;
  37. $template = file_get_contents(PluginManager::$PLUGINS_PATH . '/pubsubhubbub/hub.'. $feedType .'.xml');
  38. $data['feed_plugins_header'][] = sprintf($template, $conf->get('plugins.PUBSUBHUB_URL'));
  39. return $data;
  40. }
  41. /**
  42. * Save link hook.
  43. * Publish to the hub when a link is saved.
  44. *
  45. * @param array $data Template data.
  46. * @param ConfigManager $conf instance.
  47. *
  48. * @return array unaltered data.
  49. */
  50. function hook_pubsubhubbub_save_link($data, $conf)
  51. {
  52. $feeds = array(
  53. index_url($_SERVER) .'?do=atom',
  54. index_url($_SERVER) .'?do=rss',
  55. );
  56. $httpPost = function_exists('curl_version') ? false : 'nocurl_http_post';
  57. try {
  58. $p = new Publisher($conf->get('plugins.PUBSUBHUB_URL'));
  59. $p->publish_update($feeds, $httpPost);
  60. } catch (Exception $e) {
  61. error_log(sprintf(t('Could not publish to PubSubHubbub: %s'), $e->getMessage()));
  62. }
  63. return $data;
  64. }
  65. /**
  66. * Http function used to post to the hub endpoint without cURL extension.
  67. *
  68. * @param string $url Hub endpoint.
  69. * @param string $postString String to POST.
  70. *
  71. * @return bool
  72. *
  73. * @throws Exception An error occurred.
  74. */
  75. function nocurl_http_post($url, $postString) {
  76. $params = array('http' => array(
  77. 'method' => 'POST',
  78. 'content' => $postString,
  79. 'user_agent' => 'PubSubHubbub-Publisher-PHP/1.0',
  80. ));
  81. $context = stream_context_create($params);
  82. $fp = @fopen($url, 'rb', false, $context);
  83. if (!$fp) {
  84. throw new Exception(sprintf(t('Could not post to %s'), $url));
  85. }
  86. $response = @stream_get_contents($fp);
  87. if ($response === false) {
  88. throw new Exception(sprintf(t('Bad response from the hub %s'), $url));
  89. }
  90. return $response;
  91. }
  92. /**
  93. * This function is never called, but contains translation calls for GNU gettext extraction.
  94. */
  95. function pubsubhubbub_dummy_translation()
  96. {
  97. // meta
  98. t('Enable PubSubHubbub feed publishing.');
  99. }