aboutsummaryrefslogtreecommitdiffhomepage
path: root/plugins/pubsubhubbub/pubsubhubbub.php
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/pubsubhubbub/pubsubhubbub.php')
-rw-r--r--plugins/pubsubhubbub/pubsubhubbub.php101
1 files changed, 101 insertions, 0 deletions
diff --git a/plugins/pubsubhubbub/pubsubhubbub.php b/plugins/pubsubhubbub/pubsubhubbub.php
new file mode 100644
index 00000000..03b6757b
--- /dev/null
+++ b/plugins/pubsubhubbub/pubsubhubbub.php
@@ -0,0 +1,101 @@
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.
9 * - Subscribers retrieve the new link.
10 */
11
12use pubsubhubbub\publisher\Publisher;
13
14/**
15 * Plugin init function - set the hub to the default appspot one.
16 *
17 * @param ConfigManager $conf instance.
18 */
19function pubsubhubbub_init($conf)
20{
21 $hub = $conf->get('plugins.PUBSUBHUB_URL');
22 if (empty($hub)) {
23 // Default hub.
24 $conf->set('plugins.PUBSUBHUB_URL', 'https://pubsubhubbub.appspot.com/');
25 }
26}
27
28
29/**
30 * Render feed hook.
31 * Adds the hub URL in ATOM and RSS feed.
32 *
33 * @param array $data Template data.
34 * @param ConfigManager $conf instance.
35 *
36 * @return array updated template data.
37 */
38function hook_pubsubhubbub_render_feed($data, $conf)
39{
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'));
43
44 return $data;
45}
46
47/**
48 * Save link hook.
49 * Publish to the hub when a link is saved.
50 *
51 * @param array $data Template data.
52 * @param ConfigManager $conf instance.
53 *
54 * @return array unaltered data.
55 */
56function hook_pubsubhubbub_save_link($data, $conf)
57{
58 $feeds = array(
59 index_url($_SERVER) .'?do=atom',
60 index_url($_SERVER) .'?do=rss',
61 );
62
63 $httpPost = function_exists('curl_version') ? false : 'nocurl_http_post';
64 try {
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());
69 }
70
71 return $data;
72}
73
74/**
75 * Http function used to post to the hub endpoint without cURL extension.
76 *
77 * @param string $url Hub endpoint.
78 * @param string $postString String to POST.
79 *
80 * @return bool
81 *
82 * @throws Exception An error occurred.
83 */
84function nocurl_http_post($url, $postString) {
85 $params = array('http' => array(
86 'method' => 'POST',
87 'content' => $postString,
88 'user_agent' => 'PubSubHubbub-Publisher-PHP/1.0',
89 ));
90
91 $context = stream_context_create($params);
92 $fp = @fopen($url, 'rb', false, $context);
93 if (!$fp) {
94 throw new Exception('Could not post to '. $url);
95 }
96 $response = @stream_get_contents($fp);
97 if ($response === false) {
98 throw new Exception('Bad response from the hub '. $url);
99 }
100 return $response;
101}