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