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