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