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