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