From db90dfcbbc406b50381f17a72f24095fee91bb09 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 2 Aug 2016 11:55:49 +0200 Subject: Move PubSubHubbub code as a default plugin --- plugins/pubsubhubbub/README.md | 20 +++++++ plugins/pubsubhubbub/hub.atom.xml | 1 + plugins/pubsubhubbub/hub.rss.xml | 1 + plugins/pubsubhubbub/pubsubhubbub.meta | 2 + plugins/pubsubhubbub/pubsubhubbub.php | 101 +++++++++++++++++++++++++++++++++ 5 files changed, 125 insertions(+) create mode 100644 plugins/pubsubhubbub/README.md create mode 100644 plugins/pubsubhubbub/hub.atom.xml create mode 100644 plugins/pubsubhubbub/hub.rss.xml create mode 100644 plugins/pubsubhubbub/pubsubhubbub.meta create mode 100644 plugins/pubsubhubbub/pubsubhubbub.php (limited to 'plugins/pubsubhubbub') 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 @@ +# PubSubHubbub plugin + +Enable this plugin to notify a Hub everytime you add or edit a link. + +This allow hub subcribers to receive update notifications in real time, +which is useful for feed syndication service which supports PubSubHubbub. + +## Public Hub + +By default, Shaarli will use [Google's public hub](http://pubsubhubbub.appspot.com/). + +[Here](https://github.com/pubsubhubbub/PubSubHubbub/wiki/Hubs) is a list of public hubs. + +You can also host your own PubSubHubbub server implementation, such as [phubb](https://github.com/cweiske/phubb). + +## cURL + +While there is a fallback function to notify the hub, it's recommended that +you have PHP cURL extension enabled to use this plugin. + 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 @@ + \ 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 @@ + \ 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 @@ +description="Enable PubSubHubbub feed publishing." +parameters="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 @@ +get('plugins.PUBSUBHUB_URL'); + if (empty($hub)) { + // Default hub. + $conf->set('plugins.PUBSUBHUB_URL', 'https://pubsubhubbub.appspot.com/'); + } +} + + +/** + * Render feed hook. + * Adds the hub URL in ATOM and RSS feed. + * + * @param array $data Template data. + * @param ConfigManager $conf instance. + * + * @return array updated template data. + */ +function hook_pubsubhubbub_render_feed($data, $conf) +{ + $feedType = $data['_PAGE_'] == Router::$PAGE_FEED_RSS ? FeedBuilder::$FEED_RSS : FeedBuilder::$FEED_ATOM; + $template = file_get_contents(PluginManager::$PLUGINS_PATH . '/pubsubhubbub/hub.'. $feedType .'.xml'); + $data['feed_plugins_header'][] = sprintf($template, $conf->get('plugins.PUBSUBHUB_URL')); + + return $data; +} + +/** + * Save link hook. + * Publish to the hub when a link is saved. + * + * @param array $data Template data. + * @param ConfigManager $conf instance. + * + * @return array unaltered data. + */ +function hook_pubsubhubbub_save_link($data, $conf) +{ + $feeds = array( + index_url($_SERVER) .'?do=atom', + index_url($_SERVER) .'?do=rss', + ); + + $httpPost = function_exists('curl_version') ? false : 'nocurl_http_post'; + try { + $p = new Publisher($conf->get('plugins.PUBSUBHUB_URL')); + $p->publish_update($feeds, $httpPost); + } catch (Exception $e) { + error_log('Could not publish to PubSubHubbub: ' . $e->getMessage()); + } + + return $data; +} + +/** + * Http function used to post to the hub endpoint without cURL extension. + * + * @param string $url Hub endpoint. + * @param string $postString String to POST. + * + * @return bool + * + * @throws Exception An error occurred. + */ +function nocurl_http_post($url, $postString) { + $params = array('http' => array( + 'method' => 'POST', + 'content' => $postString, + 'user_agent' => 'PubSubHubbub-Publisher-PHP/1.0', + )); + + $context = stream_context_create($params); + $fp = @fopen($url, 'rb', false, $context); + if (!$fp) { + throw new Exception('Could not post to '. $url); + } + $response = @stream_get_contents($fp); + if ($response === false) { + throw new Exception('Bad response from the hub '. $url); + } + return $response; +} -- cgit v1.2.3