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.php111
1 files changed, 111 insertions, 0 deletions
diff --git a/plugins/pubsubhubbub/pubsubhubbub.php b/plugins/pubsubhubbub/pubsubhubbub.php
new file mode 100644
index 00000000..184b588b
--- /dev/null
+++ b/plugins/pubsubhubbub/pubsubhubbub.php
@@ -0,0 +1,111 @@
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;
13use Shaarli\Config\ConfigManager;
14
15/**
16 * Plugin init function - set the hub to the default appspot one.
17 *
18 * @param ConfigManager $conf instance.
19 */
20function pubsubhubbub_init($conf)
21{
22 $hub = $conf->get('plugins.PUBSUBHUB_URL');
23 if (empty($hub)) {
24 // Default hub.
25 $conf->set('plugins.PUBSUBHUB_URL', 'https://pubsubhubbub.appspot.com/');
26 }
27}
28
29
30/**
31 * Render feed hook.
32 * Adds the hub URL in ATOM and RSS feed.
33 *
34 * @param array $data Template data.
35 * @param ConfigManager $conf instance.
36 *
37 * @return array updated template data.
38 */
39function hook_pubsubhubbub_render_feed($data, $conf)
40{
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'));
44
45 return $data;
46}
47
48/**
49 * Save link hook.
50 * Publish to the hub when a link is saved.
51 *
52 * @param array $data Template data.
53 * @param ConfigManager $conf instance.
54 *
55 * @return array unaltered data.
56 */
57function hook_pubsubhubbub_save_link($data, $conf)
58{
59 $feeds = array(
60 index_url($_SERVER) .'?do=atom',
61 index_url($_SERVER) .'?do=rss',
62 );
63
64 $httpPost = function_exists('curl_version') ? false : 'nocurl_http_post';
65 try {
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()));
70 }
71
72 return $data;
73}
74
75/**
76 * Http function used to post to the hub endpoint without cURL extension.
77 *
78 * @param string $url Hub endpoint.
79 * @param string $postString String to POST.
80 *
81 * @return bool
82 *
83 * @throws Exception An error occurred.
84 */
85function nocurl_http_post($url, $postString) {
86 $params = array('http' => array(
87 'method' => 'POST',
88 'content' => $postString,
89 'user_agent' => 'PubSubHubbub-Publisher-PHP/1.0',
90 ));
91
92 $context = stream_context_create($params);
93 $fp = @fopen($url, 'rb', false, $context);
94 if (!$fp) {
95 throw new Exception(sprintf(t('Could not post to %s'), $url));
96 }
97 $response = @stream_get_contents($fp);
98 if ($response === false) {
99 throw new Exception(sprintf(t('Bad response from the hub %s'), $url));
100 }
101 return $response;
102}
103
104/**
105 * This function is never called, but contains translation calls for GNU gettext extraction.
106 */
107function pubsubhubbub_dummy_translation()
108{
109 // meta
110 t('Enable PubSubHubbub feed publishing.');
111}