5 function post_var($name) {
6 return (x($_POST, $name)) ? notags(trim($_POST[$name])) : '';
9 function pubsubhubbub_init(App $a) {
10 // PuSH subscription must be considered "public" so just block it
11 // if public access isn't enabled.
12 if (get_config('system', 'block_public')) {
13 http_status_exit(403);
16 // Subscription request from subscriber
17 // https://pubsubhubbub.googlecode.com/git/pubsubhubbub-core-0.4.html#anchor4
18 // Example from GNU Social:
19 // [hub_mode] => subscribe
20 // [hub_callback] => http://status.local/main/push/callback/1
21 // [hub_verify] => sync
22 // [hub_verify_token] => af11...
23 // [hub_secret] => af11...
24 // [hub_topic] => http://friendica.local/dfrn_poll/sazius
26 if($_SERVER['REQUEST_METHOD'] === 'POST') {
27 $hub_mode = post_var('hub_mode');
28 $hub_callback = post_var('hub_callback');
29 $hub_verify = post_var('hub_verify');
30 $hub_verify_token = post_var('hub_verify_token');
31 $hub_secret = post_var('hub_secret');
32 $hub_topic = post_var('hub_topic');
34 // check for valid hub_mode
35 if ($hub_mode === 'subscribe') {
37 } else if ($hub_mode === 'unsubscribe') {
40 logger("pubsubhubbub: invalid hub_mode=$hub_mode, ignoring.");
41 http_status_exit(404);
44 logger("pubsubhubbub: $hub_mode request from " .
45 $_SERVER['REMOTE_ADDR']);
47 // get the nick name from the topic, a bit hacky but needed
48 $nick = substr(strrchr($hub_topic, "/"), 1);
51 logger('pubsubhubbub: bad hub_topic=$hub_topic, ignoring.');
52 http_status_exit(404);
55 // fetch user from database given the nickname
56 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s'" .
57 " AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
60 if (!dbm::is_result($r)) {
61 logger('pubsubhubbub: local account not found: ' . $nick);
62 http_status_exit(404);
67 // abort if user's wall is supposed to be private
68 if ($r[0]['hidewall']) {
69 logger('pubsubhubbub: local user ' . $nick .
70 'has chosen to hide wall, ignoring.');
71 http_status_exit(403);
74 // get corresponding row from contact table
75 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked`".
76 " AND NOT `pending` AND `self` LIMIT 1",
77 intval($owner['uid']));
78 if (!dbm::is_result($r)) {
79 logger('pubsubhubbub: contact not found.');
80 http_status_exit(404);
85 // sanity check that topic URLs are the same
86 if(!link_compare($hub_topic, $contact['poll'])) {
87 logger('pubsubhubbub: hub topic ' . $hub_topic . ' != ' .
89 http_status_exit(404);
92 // do subscriber verification according to the PuSH protocol
93 $hub_challenge = random_string(40);
94 $params = 'hub.mode=' .
95 ($subscribe == 1 ? 'subscribe' : 'unsubscribe') .
96 '&hub.topic=' . urlencode($hub_topic) .
97 '&hub.challenge=' . $hub_challenge .
98 '&hub.lease_seconds=604800' .
99 '&hub.verify_token=' . $hub_verify_token;
101 // lease time is hard coded to one week (in seconds)
102 // we don't actually enforce the lease time because GNU
103 // Social/StatusNet doesn't honour it (yet)
105 $body = fetch_url($hub_callback . "?" . $params);
106 $ret = $a->get_curl_code();
108 // give up if the HTTP return code wasn't a success (2xx)
109 if ($ret < 200 || $ret > 299) {
110 logger("pubsubhubbub: subscriber verification at $hub_callback ".
111 "returned $ret, ignoring.");
112 http_status_exit(404);
115 // check that the correct hub_challenge code was echoed back
116 if (trim($body) !== $hub_challenge) {
117 logger("pubsubhubbub: subscriber did not echo back ".
118 "hub.challenge, ignoring.");
119 logger("\"$hub_challenge\" != \"".trim($body)."\"");
120 http_status_exit(404);
123 // fetch the old subscription if it exists
124 $r = q("SELECT * FROM `push_subscriber` WHERE `callback_url` = '%s'",
125 dbesc($hub_callback));
127 // delete old subscription if it exists
128 q("DELETE FROM `push_subscriber` WHERE `callback_url` = '%s'",
129 dbesc($hub_callback));
132 $last_update = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
135 // if we are just updating an old subscription, keep the
136 // old values for push and last_update
137 if (dbm::is_result($r)) {
138 $last_update = $r[0]['last_update'];
139 $push_flag = $r[0]['push'];
142 // subscribe means adding the row to the table
143 q("INSERT INTO `push_subscriber` (`uid`, `callback_url`, " .
144 "`topic`, `nickname`, `push`, `last_update`, `secret`) values " .
145 "(%d, '%s', '%s', '%s', %d, '%s', '%s')",
146 intval($owner['uid']),
147 dbesc($hub_callback),
153 logger("pubsubhubbub: successfully subscribed [$hub_callback].");
155 logger("pubsubhubbub: successfully unsubscribed [$hub_callback].");
156 // we do nothing here, since the row was already deleted
158 http_status_exit(202);