6 * PubSub is a protocol which fasten up RSS fetching:
7 * - Every time a new link is posted, Shaarli notify the hub.
8 * - The hub notify all feed subscribers that a new link has been posted.
9 * - Subscribers retrieve the new link.
12 use pubsubhubbub\publisher\Publisher
;
13 use Shaarli\Config\ConfigManager
;
16 * Plugin init function - set the hub to the default appspot one.
18 * @param ConfigManager $conf instance.
20 function pubsubhubbub_init($conf)
22 $hub = $conf->get('plugins.PUBSUBHUB_URL');
25 $conf->set('plugins.PUBSUBHUB_URL', 'https://pubsubhubbub.appspot.com/');
32 * Adds the hub URL in ATOM and RSS feed.
34 * @param array $data Template data.
35 * @param ConfigManager $conf instance.
37 * @return array updated template data.
39 function hook_pubsubhubbub_render_feed($data, $conf)
41 $feedType = $data['_PAGE_'] == Router
::$PAGE_FEED_RSS ? FeedBuilder
::$FEED_RSS : FeedBuilder
::$FEED_ATOM;
42 $template = file_get_contents(PluginManager
::$PLUGINS_PATH . '/pubsubhubbub/hub.'. $feedType .'.xml');
43 $data['feed_plugins_header'][] = sprintf($template, $conf->get('plugins.PUBSUBHUB_URL'));
50 * Publish to the hub when a link is saved.
52 * @param array $data Template data.
53 * @param ConfigManager $conf instance.
55 * @return array unaltered data.
57 function hook_pubsubhubbub_save_link($data, $conf)
60 index_url($_SERVER) .'?do=atom',
61 index_url($_SERVER) .'?do=rss',
64 $httpPost = function_exists('curl_version') ? false : 'nocurl_http_post';
66 $p = new Publisher($conf->get('plugins.PUBSUBHUB_URL'));
67 $p->publish_update($feeds, $httpPost);
68 } catch (Exception
$e) {
69 error_log(sprintf(t('Could not publish to PubSubHubbub: %s'), $e->getMessage()));
76 * Http function used to post to the hub endpoint without cURL extension.
78 * @param string $url Hub endpoint.
79 * @param string $postString String to POST.
83 * @throws Exception An error occurred.
85 function nocurl_http_post($url, $postString) {
86 $params = array('http' => array(
88 'content' => $postString,
89 'user_agent' => 'PubSubHubbub-Publisher-PHP/1.0',
92 $context = stream_context_create($params);
93 $fp = @fopen($url, 'rb', false, $context);
95 throw new Exception(sprintf(t('Could not post to %s'), $url));
97 $response = @stream_get_contents($fp);
98 if ($response === false) {
99 throw new Exception(sprintf(t('Bad response from the hub %s'), $url));
105 * This function is never called, but contains translation calls for GNU gettext extraction.
107 function pubsubhubbub_dummy_translation()
110 t('Enable PubSubHubbub feed publishing.');