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
;
15 * Plugin init function - set the hub to the default appspot one.
17 * @param ConfigManager $conf instance.
19 function pubsubhubbub_init($conf)
21 $hub = $conf->get('plugins.PUBSUBHUB_URL');
24 $conf->set('plugins.PUBSUBHUB_URL', 'https://pubsubhubbub.appspot.com/');
31 * Adds the hub URL in ATOM and RSS feed.
33 * @param array $data Template data.
34 * @param ConfigManager $conf instance.
36 * @return array updated template data.
38 function hook_pubsubhubbub_render_feed($data, $conf)
40 $feedType = $data['_PAGE_'] == Router
::$PAGE_FEED_RSS ? FeedBuilder
::$FEED_RSS : FeedBuilder
::$FEED_ATOM;
41 $template = file_get_contents(PluginManager
::$PLUGINS_PATH . '/pubsubhubbub/hub.'. $feedType .'.xml');
42 $data['feed_plugins_header'][] = sprintf($template, $conf->get('plugins.PUBSUBHUB_URL'));
49 * Publish to the hub when a link is saved.
51 * @param array $data Template data.
52 * @param ConfigManager $conf instance.
54 * @return array unaltered data.
56 function hook_pubsubhubbub_save_link($data, $conf)
59 index_url($_SERVER) .'?do=atom',
60 index_url($_SERVER) .'?do=rss',
63 $httpPost = function_exists('curl_version') ? false : 'nocurl_http_post';
65 $p = new Publisher($conf->get('plugins.PUBSUBHUB_URL'));
66 $p->publish_update($feeds, $httpPost);
67 } catch (Exception
$e) {
68 error_log('Could not publish to PubSubHubbub: ' . $e->getMessage());
75 * Http function used to post to the hub endpoint without cURL extension.
77 * @param string $url Hub endpoint.
78 * @param string $postString String to POST.
82 * @throws Exception An error occurred.
84 function nocurl_http_post($url, $postString) {
85 $params = array('http' => array(
87 'content' => $postString,
88 'user_agent' => 'PubSubHubbub-Publisher-PHP/1.0',
91 $context = stream_context_create($params);
92 $fp = @fopen($url, 'rb', false, $context);
94 throw new Exception('Could not post to '. $url);
96 $response = @stream_get_contents($fp);
97 if ($response === false) {
98 throw new Exception('Bad response from the hub '. $url);