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