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