]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_poll.php
Don't show the local community page on single user instances
[friendica.git] / mod / dfrn_poll.php
1 <?php
2
3 /**
4  * @file mod/dfrn_poll.php
5  */
6 use Friendica\App;
7 use Friendica\Core\Config;
8 use Friendica\Core\System;
9 use Friendica\Database\DBM;
10 use Friendica\Module\Login;
11 use Friendica\Protocol\DFRN;
12 use Friendica\Protocol\OStatus;
13
14 require_once 'include/items.php';
15
16 function dfrn_poll_init(App $a)
17 {
18         Login::sessionAuth();
19
20         $dfrn_id         = defaults($_GET, 'dfrn_id'        , '');
21         $type            = defaults($_GET, 'type'           , 'data');
22         $last_update     = defaults($_GET, 'last_update'    , '');
23         $destination_url = defaults($_GET, 'destination_url', '');
24         $challenge       = defaults($_GET, 'challenge'      , '');
25         $sec             = defaults($_GET, 'sec'            , '');
26         $dfrn_version    = (float) defaults($_GET, 'dfrn_version'   , 2.0);
27         $perm            = defaults($_GET, 'perm'           , 'r');
28         $quiet                   = x($_GET, 'quiet');
29
30         // Possibly it is an OStatus compatible server that requests a user feed
31         if (($a->argc > 1) && ($dfrn_id == '') && !strstr($_SERVER["HTTP_USER_AGENT"], 'Friendica')) {
32                 $nickname = $a->argv[1];
33                 header("Content-type: application/atom+xml");
34                 echo OStatus::feed($nickname, $last_update, 10);
35                 killme();
36         }
37
38         $direction = -1;
39
40         if (strpos($dfrn_id, ':') == 1) {
41                 $direction = intval(substr($dfrn_id, 0, 1));
42                 $dfrn_id = substr($dfrn_id, 2);
43         }
44
45         $hidewall = false;
46
47         if (($dfrn_id === '') && (!x($_POST, 'dfrn_id'))) {
48                 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
49                         http_status_exit(403);
50                 }
51
52                 $user = '';
53                 if ($a->argc > 1) {
54                         $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
55                                 dbesc($a->argv[1])
56                         );
57                         if (!$r) {
58                                 http_status_exit(404);
59                         }
60
61                         $hidewall = ($r[0]['hidewall'] && !local_user());
62
63                         $user = $r[0]['nickname'];
64                 }
65
66                 logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
67                 header("Content-type: application/atom+xml");
68                 echo DFRN::feed('', $user, $last_update, 0, $hidewall);
69                 killme();
70         }
71
72         if (($type === 'profile') && (!strlen($sec))) {
73                 $sql_extra = '';
74                 switch ($direction) {
75                         case -1:
76                                 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
77                                 $my_id = $dfrn_id;
78                                 break;
79                         case 0:
80                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
81                                 $my_id = '1:' . $dfrn_id;
82                                 break;
83                         case 1:
84                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
85                                 $my_id = '0:' . $dfrn_id;
86                                 break;
87                         default:
88                                 goaway(System::baseUrl());
89                                 break; // NOTREACHED
90                 }
91
92                 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
93                         FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
94                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
95                         AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
96                         dbesc($a->argv[1])
97                 );
98
99                 if (DBM::is_result($r)) {
100                         $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
101
102                         logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
103
104                         if (strlen($s)) {
105                                 $xml = parse_xml_string($s);
106
107                                 if ((int) $xml->status === 1) {
108                                         $_SESSION['authenticated'] = 1;
109                                         if (!x($_SESSION, 'remote')) {
110                                                 $_SESSION['remote'] = array();
111                                         }
112
113                                         $_SESSION['remote'][] = array('cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']);
114
115                                         $_SESSION['visitor_id'] = $r[0]['id'];
116                                         $_SESSION['visitor_home'] = $r[0]['url'];
117                                         $_SESSION['visitor_handle'] = $r[0]['addr'];
118                                         $_SESSION['visitor_visiting'] = $r[0]['uid'];
119                                         if (!$quiet) {
120                                                 info(sprintf(t('%1$s welcomes %2$s'), $r[0]['username'], $r[0]['name']) . EOL);
121                                         }
122
123                                         // Visitors get 1 day session.
124                                         $session_id = session_id();
125                                         $expire = time() + 86400;
126                                         q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
127                                                 dbesc($expire),
128                                                 dbesc($session_id)
129                                         );
130                                 }
131                         }
132                         $profile = $r[0]['nickname'];
133                         goaway((strlen($destination_url)) ? $destination_url : System::baseUrl() . '/profile/' . $profile);
134                 }
135                 goaway(System::baseUrl());
136         }
137
138         if ($type === 'profile-check' && $dfrn_version < 2.2) {
139                 if ((strlen($challenge)) && (strlen($sec))) {
140                         q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
141                         $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
142                                 dbesc($sec)
143                         );
144                         if (!DBM::is_result($r)) {
145                                 xml_status(3, 'No ticket');
146                                 // NOTREACHED
147                         }
148
149                         $orig_id = $r[0]['dfrn_id'];
150                         if (strpos($orig_id, ':')) {
151                                 $orig_id = substr($orig_id, 2);
152                         }
153
154                         $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
155                                 intval($r[0]['cid'])
156                         );
157                         if (!DBM::is_result($c)) {
158                                 xml_status(3, 'No profile');
159                         }
160
161                         $contact = $c[0];
162
163                         $sent_dfrn_id = hex2bin($dfrn_id);
164                         $challenge = hex2bin($challenge);
165
166                         $final_dfrn_id = '';
167
168                         if (($contact['duplex']) && strlen($contact['prvkey'])) {
169                                 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
170                                 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
171                         } else {
172                                 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
173                                 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
174                         }
175
176                         $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
177
178                         if (strpos($final_dfrn_id, ':') == 1) {
179                                 $final_dfrn_id = substr($final_dfrn_id, 2);
180                         }
181
182                         if ($final_dfrn_id != $orig_id) {
183                                 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
184                                 // did not decode properly - cannot trust this site
185                                 xml_status(3, 'Bad decryption');
186                         }
187
188                         header("Content-type: text/xml");
189                         echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
190                         killme();
191                         // NOTREACHED
192                 } else {
193                         // old protocol
194                         switch ($direction) {
195                                 case 1:
196                                         $dfrn_id = '0:' . $dfrn_id;
197                                         break;
198                                 case 0:
199                                         $dfrn_id = '1:' . $dfrn_id;
200                                         break;
201                                 default:
202                                         break;
203                         }
204
205                         q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
206                         $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
207                                 dbesc($dfrn_id));
208                         if (DBM::is_result($r)) {
209                                 xml_status(1);
210                                 return; // NOTREACHED
211                         }
212                         xml_status(0);
213                         return; // NOTREACHED
214                 }
215         }
216 }
217
218 function dfrn_poll_post(App $a)
219 {
220         $dfrn_id      = x($_POST,'dfrn_id')      ? $_POST['dfrn_id']              : '';
221         $challenge    = x($_POST,'challenge')    ? $_POST['challenge']            : '';
222         $url          = x($_POST,'url')          ? $_POST['url']                  : '';
223         $sec          = x($_POST,'sec')          ? $_POST['sec']                  : '';
224         $ptype        = x($_POST,'type')         ? $_POST['type']                 : '';
225         $dfrn_version = x($_POST,'dfrn_version') ? (float) $_POST['dfrn_version'] : 2.0;
226         $perm         = x($_POST,'perm')         ? $_POST['perm']                 : 'r';
227
228         if ($ptype === 'profile-check') {
229                 if (strlen($challenge) && strlen($sec)) {
230                         logger('dfrn_poll: POST: profile-check');
231
232                         q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
233                         $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
234                                 dbesc($sec)
235                         );
236                         if (!DBM::is_result($r)) {
237                                 xml_status(3, 'No ticket');
238                                 // NOTREACHED
239                         }
240
241                         $orig_id = $r[0]['dfrn_id'];
242                         if (strpos($orig_id, ':')) {
243                                 $orig_id = substr($orig_id, 2);
244                         }
245
246                         $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
247                                 intval($r[0]['cid'])
248                         );
249                         if (!DBM::is_result($c)) {
250                                 xml_status(3, 'No profile');
251                         }
252
253                         $contact = $c[0];
254
255                         $sent_dfrn_id = hex2bin($dfrn_id);
256                         $challenge = hex2bin($challenge);
257
258                         $final_dfrn_id = '';
259
260                         if ($contact['duplex'] && strlen($contact['prvkey'])) {
261                                 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
262                                 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
263                         } else {
264                                 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
265                                 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
266                         }
267
268                         $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
269
270                         if (strpos($final_dfrn_id, ':') == 1) {
271                                 $final_dfrn_id = substr($final_dfrn_id, 2);
272                         }
273
274                         if ($final_dfrn_id != $orig_id) {
275                                 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
276                                 // did not decode properly - cannot trust this site
277                                 xml_status(3, 'Bad decryption');
278                         }
279
280                         header("Content-type: text/xml");
281                         echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
282                         killme();
283                         // NOTREACHED
284                 }
285         }
286
287         $direction = -1;
288         if (strpos($dfrn_id, ':') == 1) {
289                 $direction = intval(substr($dfrn_id, 0, 1));
290                 $dfrn_id = substr($dfrn_id, 2);
291         }
292
293         $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
294                 dbesc($dfrn_id),
295                 dbesc($challenge)
296         );
297
298         if (!DBM::is_result($r)) {
299                 killme();
300         }
301
302         $type = $r[0]['type'];
303         $last_update = $r[0]['last_update'];
304
305         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'",
306                 dbesc($dfrn_id),
307                 dbesc($challenge)
308         );
309
310
311         $sql_extra = '';
312         switch ($direction) {
313                 case -1:
314                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
315                         $my_id = $dfrn_id;
316                         break;
317                 case 0:
318                         $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
319                         $my_id = '1:' . $dfrn_id;
320                         break;
321                 case 1:
322                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
323                         $my_id = '0:' . $dfrn_id;
324                         break;
325                 default:
326                         goaway(System::baseUrl());
327                         break; // NOTREACHED
328         }
329
330         $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
331         if (!DBM::is_result($r)) {
332                 killme();
333         }
334
335         $contact = $r[0];
336         $owner_uid = $r[0]['uid'];
337         $contact_id = $r[0]['id'];
338
339         if ($type === 'reputation' && strlen($url)) {
340                 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
341                         dbesc($url),
342                         intval($owner_uid)
343                 );
344                 $reputation = 0;
345                 $text = '';
346
347                 if (DBM::is_result($r)) {
348                         $reputation = $r[0]['rating'];
349                         $text = $r[0]['reason'];
350
351                         if ($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
352                                 $reputation = 0;
353                                 $text = '';
354                         }
355                 }
356
357                 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
358                 <reputation>
359                         <url>$url</url>
360                         <rating>$reputation</rating>
361                         <description>$text</description>
362                 </reputation>
363                 ";
364                 killme();
365                 // NOTREACHED
366         } else {
367                 // Update the writable flag if it changed
368                 logger('dfrn_poll: post request feed: ' . print_r($_POST, true), LOGGER_DATA);
369                 if ($dfrn_version >= 2.21) {
370                         if ($perm === 'rw') {
371                                 $writable = 1;
372                         } else {
373                                 $writable = 0;
374                         }
375
376                         if ($writable != $contact['writable']) {
377                                 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
378                                         intval($writable),
379                                         intval($contact_id)
380                                 );
381                         }
382                 }
383
384                 header("Content-type: application/atom+xml");
385                 $o = DFRN::feed($dfrn_id, $a->argv[1], $last_update, $direction);
386                 echo $o;
387                 killme();
388         }
389 }
390
391 function dfrn_poll_content(App $a)
392 {
393         $dfrn_id         = x($_GET,'dfrn_id')         ? $_GET['dfrn_id']              : '';
394         $type            = x($_GET,'type')            ? $_GET['type']                 : 'data';
395         $last_update     = x($_GET,'last_update')     ? $_GET['last_update']          : '';
396         $destination_url = x($_GET,'destination_url') ? $_GET['destination_url']      : '';
397         $sec             = x($_GET,'sec')             ? $_GET['sec']                  : '';
398         $dfrn_version    = x($_GET,'dfrn_version')    ? (float) $_GET['dfrn_version'] : 2.0;
399         $perm            = x($_GET,'perm')            ? $_GET['perm']                 : 'r';
400         $quiet           = x($_GET,'quiet')           ? true                          : false;
401
402         $direction = -1;
403         if (strpos($dfrn_id, ':') == 1) {
404                 $direction = intval(substr($dfrn_id, 0, 1));
405                 $dfrn_id = substr($dfrn_id, 2);
406         }
407
408         if ($dfrn_id != '') {
409                 // initial communication from external contact
410                 $hash = random_string();
411
412                 $status = 0;
413
414                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
415
416                 if ($type !== 'profile') {
417                         $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
418                                 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
419                                 dbesc($hash),
420                                 dbesc($dfrn_id),
421                                 intval(time() + 60 ),
422                                 dbesc($type),
423                                 dbesc($last_update)
424                         );
425                 }
426
427                 $sql_extra = '';
428                 switch ($direction) {
429                         case -1:
430                                 if ($type === 'profile') {
431                                         $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
432                                 } else {
433                                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
434                                 }
435
436                                 $my_id = $dfrn_id;
437                                 break;
438                         case 0:
439                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
440                                 $my_id = '1:' . $dfrn_id;
441                                 break;
442                         case 1:
443                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
444                                 $my_id = '0:' . $dfrn_id;
445                                 break;
446                         default:
447                                 goaway(System::baseUrl());
448                                 break; // NOTREACHED
449                 }
450
451                 $nickname = $a->argv[1];
452
453                 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
454                         FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
455                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
456                         AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
457                         dbesc($nickname)
458                 );
459                 if (DBM::is_result($r)) {
460                         $challenge = '';
461                         $encrypted_id = '';
462                         $id_str = $my_id . '.' . mt_rand(1000, 9999);
463
464                         if (($r[0]['duplex'] && strlen($r[0]['pubkey'])) || !strlen($r[0]['prvkey'])) {
465                                 openssl_public_encrypt($hash, $challenge, $r[0]['pubkey']);
466                                 openssl_public_encrypt($id_str, $encrypted_id, $r[0]['pubkey']);
467                         } else {
468                                 openssl_private_encrypt($hash, $challenge, $r[0]['prvkey']);
469                                 openssl_private_encrypt($id_str, $encrypted_id, $r[0]['prvkey']);
470                         }
471
472                         $challenge = bin2hex($challenge);
473                         $encrypted_id = bin2hex($encrypted_id);
474                 } else {
475                         $status = 1;
476                         $challenge = '';
477                         $encrypted_id = '';
478                 }
479
480                 if (($type === 'profile') && (strlen($sec))) {
481                         // URL reply
482                         if ($dfrn_version < 2.2) {
483                                 $s = fetch_url($r[0]['poll']
484                                         . '?dfrn_id=' . $encrypted_id
485                                         . '&type=profile-check'
486                                         . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
487                                         . '&challenge=' . $challenge
488                                         . '&sec=' . $sec
489                                 );
490                         } else {
491                                 $s = post_url($r[0]['poll'], array(
492                                         'dfrn_id' => $encrypted_id,
493                                         'type' => 'profile-check',
494                                         'dfrn_version' => DFRN_PROTOCOL_VERSION,
495                                         'challenge' => $challenge,
496                                         'sec' => $sec
497                                 ));
498                         }
499
500                         $profile = ((DBM::is_result($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
501
502                         switch ($destination_url) {
503                                 case 'profile':
504                                         $dest = System::baseUrl() . '/profile/' . $profile . '?f=&tab=profile';
505                                         break;
506                                 case 'photos':
507                                         $dest = System::baseUrl() . '/photos/' . $profile;
508                                         break;
509                                 case 'status':
510                                 case '':
511                                         $dest = System::baseUrl() . '/profile/' . $profile;
512                                         break;
513                                 default:
514                                         $appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
515                                         $dest = $destination_url . $appendix;
516                                         break;
517                         }
518
519                         logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
520
521                         if (strlen($s) && strstr($s, '<?xml')) {
522                                 $xml = parse_xml_string($s);
523
524                                 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml, true), LOGGER_DATA);
525
526                                 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
527                                 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
528
529                                 if (((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
530                                         $_SESSION['authenticated'] = 1;
531                                         if (!x($_SESSION, 'remote')) {
532                                                 $_SESSION['remote'] = array();
533                                         }
534
535                                         $_SESSION['remote'][] = array('cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']);
536                                         $_SESSION['visitor_id'] = $r[0]['id'];
537                                         $_SESSION['visitor_home'] = $r[0]['url'];
538                                         $_SESSION['visitor_visiting'] = $r[0]['uid'];
539                                         if (!$quiet) {
540                                                 info(sprintf(t('%1$s welcomes %2$s'), $r[0]['username'], $r[0]['name']) . EOL);
541                                         }
542
543                                         // Visitors get 1 day session.
544                                         $session_id = session_id();
545                                         $expire = time() + 86400;
546                                         q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
547                                                 dbesc($expire),
548                                                 dbesc($session_id)
549                                         );
550                                 }
551
552                                 goaway($dest);
553                         }
554                         goaway($dest);
555                         // NOTREACHED
556                 } else {
557                         // XML reply
558                         header("Content-type: text/xml");
559                         echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
560                                 . '<dfrn_poll>' . "\r\n"
561                                 . "\t" . '<status>' . $status . '</status>' . "\r\n"
562                                 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
563                                 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
564                                 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
565                                 . '</dfrn_poll>' . "\r\n";
566                         killme();
567                         // NOTREACHED
568                 }
569         }
570 }