]> git.mxchange.org Git - friendica.git/blob - mod/pubsub.php
We don't need the contact-id anymore
[friendica.git] / mod / pubsub.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Database\DBM;
5 use Friendica\Protocol\OStatus;
6
7 function hub_return($valid,$body) {
8
9         if ($valid) {
10                 header($_SERVER["SERVER_PROTOCOL"] . ' 200 ' . 'OK');
11                 echo $body;
12                 killme();
13         } else {
14                 header($_SERVER["SERVER_PROTOCOL"] . ' 404 ' . 'Not Found');
15                 killme();
16         }
17
18         // NOTREACHED
19 }
20
21 // when receiving an XML feed, always return OK
22
23 function hub_post_return() {
24
25         header($_SERVER["SERVER_PROTOCOL"] . ' 200 ' . 'OK');
26         killme();
27
28 }
29
30
31
32 function pubsub_init(App $a) {
33
34         $nick       = (($a->argc > 1) ? notags(trim($a->argv[1])) : '');
35         $contact_id = (($a->argc > 2) ? intval($a->argv[2])       : 0 );
36
37         if ($_SERVER['REQUEST_METHOD'] === 'GET') {
38                 $hub_mode      = ((x($_GET,'hub_mode'))          ? notags(trim($_GET['hub_mode']))          : '');
39                 $hub_topic     = ((x($_GET,'hub_topic'))         ? notags(trim($_GET['hub_topic']))         : '');
40                 $hub_challenge = ((x($_GET,'hub_challenge'))     ? notags(trim($_GET['hub_challenge']))     : '');
41                 $hub_lease     = ((x($_GET,'hub_lease_seconds')) ? notags(trim($_GET['hub_lease_seconds'])) : '');
42                 $hub_verify    = ((x($_GET,'hub_verify_token'))  ? notags(trim($_GET['hub_verify_token']))  : '');
43
44                 logger('pubsub: Subscription from ' . $_SERVER['REMOTE_ADDR'] . ' Mode: ' . $hub_mode . ' Nick: ' . $nick);
45                 logger('pubsub: data: ' . print_r($_GET,true), LOGGER_DATA);
46
47                 $subscribe = (($hub_mode === 'subscribe') ? 1 : 0);
48
49                 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
50                         dbesc($nick)
51                 );
52                 if (!DBM::is_result($r)) {
53                         logger('pubsub: local account not found: ' . $nick);
54                         hub_return(false, '');
55                 }
56
57
58                 $owner = $r[0];
59
60                 if (!empty($hub_verify)) {
61                         $sql_extra = sprintf(" AND `hub-verify` = '%s' ", dbesc($hub_verify));
62                         $log_info = 'hub-verify: ' . $hub_verify;
63                 } else {
64                         $sql_extra = sprintf(" AND `id` = %d ", intval($contact_id));
65                         $log_info = 'contact-id: ' . $contact_id;
66                 }
67
68                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d
69                         AND NOT `blocked` AND NOT `pending` $sql_extra LIMIT 1",
70                         intval($owner['uid'])
71                 );
72                 if (!DBM::is_result($r)) {
73                         logger('pubsub: contact not found. ' . $log_info);
74                         hub_return(false, '');
75                 }
76
77                 if (!empty($hub_topic)) {
78                         if (!link_compare($hub_topic,$r[0]['poll'])) {
79                                 logger('pubsub: hub topic ' . $hub_topic . ' != ' . $r[0]['poll']);
80                                 // should abort but let's humour them.
81                         }
82                 }
83
84                 $contact = $r[0];
85
86                 // We must initiate an unsubscribe request with a verify_token.
87                 // Don't allow outsiders to unsubscribe us.
88
89                 if ($hub_mode === 'unsubscribe') {
90                         if (!strlen($hub_verify)) {
91                                 logger('pubsub: bogus unsubscribe');
92                                 hub_return(false, '');
93                         }
94                 }
95
96                 if (!empty($hub_mode)) {
97                         dba::update('contact', ['subhub' => $subscribe], ['id' => $contact['id']]);
98                         logger('pubsub: ' . $hub_mode . ' success');
99                 }
100                 hub_return(true, $hub_challenge);
101         }
102 }
103
104 require_once('include/security.php');
105
106 function pubsub_post(App $a) {
107
108         $xml = file_get_contents('php://input');
109
110         logger('pubsub: feed arrived from ' . $_SERVER['REMOTE_ADDR'] . ' for ' .  $a->cmd );
111         logger('pubsub: user-agent: ' . $_SERVER['HTTP_USER_AGENT'] );
112         logger('pubsub: data: ' . $xml, LOGGER_DATA);
113
114         $nick       = (($a->argc > 1) ? notags(trim($a->argv[1])) : '');
115         $contact_id = (($a->argc > 2) ? intval($a->argv[2])       : 0 );
116
117         $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
118                 dbesc($nick)
119         );
120         if (!DBM::is_result($r)) {
121                 hub_post_return();
122         }
123
124         $importer = $r[0];
125
126         $r = q("SELECT * FROM `contact` WHERE `subhub` AND `id` = %d AND `uid` = %d
127                 AND (`rel` = %d OR `rel` = %d OR network = '%s') AND NOT `blocked` LIMIT 1",
128                 intval($contact_id),
129                 intval($importer['uid']),
130                 intval(CONTACT_IS_SHARING),
131                 intval(CONTACT_IS_FRIEND),
132                 dbesc(NETWORK_FEED)
133         );
134
135         if (!DBM::is_result($r)) {
136                 $author = OStatus::salmonAuthor($xml, $importer);
137                 if (!empty($author['contact-id'])) {
138                         $contact = dba::selectFirst('contact', [], ['id' => $author['contact-id']]);
139                         if (!in_array($contact['rel'], [CONTACT_IS_SHARING, CONTACT_IS_FRIEND]) && ($contact['network'] != NETWORK_FEED)) {
140                                 logger('Contact ' . $author['contact-id'] . ' is not expected to share with us - ignored.');
141                                 hub_post_return();
142                         }
143                         logger('pubsub: no contact record for "'.$nick.' ('.$contact_id.')" - using '.$author['contact-id'].' instead.');
144                 }
145         } else {
146                 $contact = $r[0];
147         }
148
149         // we have no way to match Diaspora guid's with atom post id's and could get duplicates.
150         // we'll assume that direct delivery is robust (and this is a bad assumption, but the duplicates are messy).
151
152         if ($r[0]['network'] === NETWORK_DIASPORA) {
153                 hub_post_return();
154         }
155         $feedhub = '';
156
157         require_once('include/items.php');
158
159         consume_feed($xml,$importer,$contact,$feedhub,1,1);
160
161         // do it a second time so that any children find their parents.
162
163         consume_feed($xml,$importer,$contact,$feedhub,1,2);
164
165         hub_post_return();
166
167 }