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