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