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