]>
Commit | Line | Data |
---|---|---|
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 | ||
12 | use pubsubhubbub\publisher\Publisher; | |
13 | use Shaarli\Config\ConfigManager; | |
14 | ||
15 | /** | |
16 | * Plugin init function - set the hub to the default appspot one. | |
17 | * | |
18 | * @param ConfigManager $conf instance. | |
19 | */ | |
20 | function 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 | */ | |
39 | function 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 | */ | |
57 | function 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 | */ | |
85 | function nocurl_http_post($url, $postString) | |
86 | { | |
87 | $params = array('http' => array( | |
88 | 'method' => 'POST', | |
89 | 'content' => $postString, | |
90 | 'user_agent' => 'PubSubHubbub-Publisher-PHP/1.0', | |
91 | )); | |
92 | ||
93 | $context = stream_context_create($params); | |
94 | $fp = @fopen($url, 'rb', false, $context); | |
95 | if (!$fp) { | |
96 | throw new Exception(sprintf(t('Could not post to %s'), $url)); | |
97 | } | |
98 | $response = @stream_get_contents($fp); | |
99 | if ($response === false) { | |
100 | throw new Exception(sprintf(t('Bad response from the hub %s'), $url)); | |
101 | } | |
102 | return $response; | |
103 | } | |
104 | ||
105 | /** | |
106 | * This function is never called, but contains translation calls for GNU gettext extraction. | |
107 | */ | |
108 | function pubsubhubbub_dummy_translation() | |
109 | { | |
110 | // meta | |
111 | t('Enable PubSubHubbub feed publishing.'); | |
112 | } |