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