4 use Friendica\Core\Config;
5 use Friendica\Core\System;
6 use Friendica\Database\DBM;
7 use Friendica\Util\Network;
9 function post_var($name) {
10 return (x($_POST, $name)) ? notags(trim($_POST[$name])) : '';
13 function pubsubhubbub_init(App $a) {
14 // PuSH subscription must be considered "public" so just block it
15 // if public access isn't enabled.
16 if (Config::get('system', 'block_public')) {
17 System::httpExit(403);
20 // Subscription request from subscriber
21 // https://pubsubhubbub.googlecode.com/git/pubsubhubbub-core-0.4.html#anchor4
22 // Example from GNU Social:
23 // [hub_mode] => subscribe
24 // [hub_callback] => http://status.local/main/push/callback/1
25 // [hub_verify] => sync
26 // [hub_verify_token] => af11...
27 // [hub_secret] => af11...
28 // [hub_topic] => http://friendica.local/dfrn_poll/sazius
30 if ($_SERVER['REQUEST_METHOD'] === 'POST') {
31 $hub_mode = post_var('hub_mode');
32 $hub_callback = post_var('hub_callback');
33 $hub_verify = post_var('hub_verify');
34 $hub_verify_token = post_var('hub_verify_token');
35 $hub_secret = post_var('hub_secret');
36 $hub_topic = post_var('hub_topic');
38 // check for valid hub_mode
39 if ($hub_mode === 'subscribe') {
41 } elseif ($hub_mode === 'unsubscribe') {
44 logger("pubsubhubbub: invalid hub_mode=$hub_mode, ignoring.");
45 System::httpExit(404);
48 logger("pubsubhubbub: $hub_mode request from " .
49 $_SERVER['REMOTE_ADDR']);
51 // get the nick name from the topic, a bit hacky but needed as a fallback
52 $nick = substr(strrchr($hub_topic, "/"), 1);
54 // Normally the url should now contain the nick name as last part of the url
60 logger('pubsubhubbub: bad hub_topic=$hub_topic, ignoring.');
61 System::httpExit(404);
64 // fetch user from database given the nickname
65 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s'" .
66 " AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
69 if (!DBM::is_result($r)) {
70 logger('pubsubhubbub: local account not found: ' . $nick);
71 System::httpExit(404);
76 // abort if user's wall is supposed to be private
77 if ($r[0]['hidewall']) {
78 logger('pubsubhubbub: local user ' . $nick .
79 'has chosen to hide wall, ignoring.');
80 System::httpExit(403);
83 // get corresponding row from contact table
84 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked`".
85 " AND NOT `pending` AND `self` LIMIT 1",
86 intval($owner['uid']));
87 if (!DBM::is_result($r)) {
88 logger('pubsubhubbub: contact not found.');
89 System::httpExit(404);
94 // sanity check that topic URLs are the same
95 if (!link_compare($hub_topic, $contact['poll'])) {
96 logger('pubsubhubbub: hub topic ' . $hub_topic . ' != ' .
98 System::httpExit(404);
101 // do subscriber verification according to the PuSH protocol
102 $hub_challenge = random_string(40);
103 $params = 'hub.mode=' .
104 ($subscribe == 1 ? 'subscribe' : 'unsubscribe') .
105 '&hub.topic=' . urlencode($hub_topic) .
106 '&hub.challenge=' . $hub_challenge .
107 '&hub.lease_seconds=604800' .
108 '&hub.verify_token=' . $hub_verify_token;
110 // lease time is hard coded to one week (in seconds)
111 // we don't actually enforce the lease time because GNU
112 // Social/StatusNet doesn't honour it (yet)
114 $body = Network::fetchUrl($hub_callback . "?" . $params);
115 $ret = $a->get_curl_code();
117 // give up if the HTTP return code wasn't a success (2xx)
118 if ($ret < 200 || $ret > 299) {
119 logger("pubsubhubbub: subscriber verification at $hub_callback ".
120 "returned $ret, ignoring.");
121 System::httpExit(404);
124 // check that the correct hub_challenge code was echoed back
125 if (trim($body) !== $hub_challenge) {
126 logger("pubsubhubbub: subscriber did not echo back ".
127 "hub.challenge, ignoring.");
128 logger("\"$hub_challenge\" != \"".trim($body)."\"");
129 System::httpExit(404);
132 // fetch the old subscription if it exists
133 $r = q("SELECT * FROM `push_subscriber` WHERE `callback_url` = '%s'",
134 dbesc($hub_callback));
136 // delete old subscription if it exists
137 q("DELETE FROM `push_subscriber` WHERE `callback_url` = '%s'",
138 dbesc($hub_callback));
141 $last_update = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
144 // if we are just updating an old subscription, keep the
145 // old values for push and last_update
146 if (DBM::is_result($r)) {
147 $last_update = $r[0]['last_update'];
148 $push_flag = $r[0]['push'];
151 // subscribe means adding the row to the table
152 q("INSERT INTO `push_subscriber` (`uid`, `callback_url`, " .
153 "`topic`, `nickname`, `push`, `last_update`, `secret`) values " .
154 "(%d, '%s', '%s', '%s', %d, '%s', '%s')",
155 intval($owner['uid']),
156 dbesc($hub_callback),
162 logger("pubsubhubbub: successfully subscribed [$hub_callback].");
164 logger("pubsubhubbub: successfully unsubscribed [$hub_callback].");
165 // we do nothing here, since the row was already deleted
167 System::httpExit(202);