aboutsummaryrefslogtreecommitdiffhomepage
path: root/plugins/pubsubhubbub
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2016-08-02 11:55:49 +0200
committerArthurHoaro <arthur@hoa.ro>2016-12-20 11:41:24 +0100
commitdb90dfcbbc406b50381f17a72f24095fee91bb09 (patch)
treeebe39650f12a80c09f8fae1b87a9d5c4c4141466 /plugins/pubsubhubbub
parent085efc33cc0cadaed0c01d926604e219e1d44365 (diff)
downloadShaarli-db90dfcbbc406b50381f17a72f24095fee91bb09.tar.gz
Shaarli-db90dfcbbc406b50381f17a72f24095fee91bb09.tar.zst
Shaarli-db90dfcbbc406b50381f17a72f24095fee91bb09.zip
Move PubSubHubbub code as a default plugin
Diffstat (limited to 'plugins/pubsubhubbub')
-rw-r--r--plugins/pubsubhubbub/README.md20
-rw-r--r--plugins/pubsubhubbub/hub.atom.xml1
-rw-r--r--plugins/pubsubhubbub/hub.rss.xml1
-rw-r--r--plugins/pubsubhubbub/pubsubhubbub.meta2
-rw-r--r--plugins/pubsubhubbub/pubsubhubbub.php101
5 files changed, 125 insertions, 0 deletions
diff --git a/plugins/pubsubhubbub/README.md b/plugins/pubsubhubbub/README.md
new file mode 100644
index 00000000..3a65492a
--- /dev/null
+++ b/plugins/pubsubhubbub/README.md
@@ -0,0 +1,20 @@
1# PubSubHubbub plugin
2
3Enable this plugin to notify a Hub everytime you add or edit a link.
4
5This allow hub subcribers to receive update notifications in real time,
6which is useful for feed syndication service which supports PubSubHubbub.
7
8## Public Hub
9
10By default, Shaarli will use [Google's public hub](http://pubsubhubbub.appspot.com/).
11
12[Here](https://github.com/pubsubhubbub/PubSubHubbub/wiki/Hubs) is a list of public hubs.
13
14You can also host your own PubSubHubbub server implementation, such as [phubb](https://github.com/cweiske/phubb).
15
16## cURL
17
18While there is a fallback function to notify the hub, it's recommended that
19you have PHP cURL extension enabled to use this plugin.
20
diff --git a/plugins/pubsubhubbub/hub.atom.xml b/plugins/pubsubhubbub/hub.atom.xml
new file mode 100644
index 00000000..24d93d3e
--- /dev/null
+++ b/plugins/pubsubhubbub/hub.atom.xml
@@ -0,0 +1 @@
<link rel="hub" href="%s" /> \ No newline at end of file
diff --git a/plugins/pubsubhubbub/hub.rss.xml b/plugins/pubsubhubbub/hub.rss.xml
new file mode 100644
index 00000000..27bf67a6
--- /dev/null
+++ b/plugins/pubsubhubbub/hub.rss.xml
@@ -0,0 +1 @@
<atom:link rel="hub" href="%s" /> \ No newline at end of file
diff --git a/plugins/pubsubhubbub/pubsubhubbub.meta b/plugins/pubsubhubbub/pubsubhubbub.meta
new file mode 100644
index 00000000..289f5cdb
--- /dev/null
+++ b/plugins/pubsubhubbub/pubsubhubbub.meta
@@ -0,0 +1,2 @@
1description="Enable PubSubHubbub feed publishing."
2parameters="PUBSUBHUB_URL"
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}