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