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