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