]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_poll.php
"profile locator/address" is now called "Identity Address" to reduce confusion
[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         $contact = $r[0];
301         $owner_uid = $r[0]['uid'];
302         $contact_id = $r[0]['id']; 
303
304
305         if($type === 'reputation' && strlen($url)) {
306                 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
307                         dbesc($url),
308                         intval($owner_uid)
309                 );
310                 $reputation = 0;
311                 $text = '';
312
313                 if(count($r)) {
314                         $reputation = $r[0]['rating'];
315                         $text = $r[0]['reason'];
316
317                         if($r[0]['id'] == $contact_id) {        // inquiring about own reputation not allowed
318                                 $reputation = 0;
319                                 $text = '';
320                         }
321                 }
322
323                 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
324                 <reputation>
325                         <url>$url</url>
326                         <rating>$reputation</rating>
327                         <description>$text</description>
328                 </reputation>
329                 ";
330                 killme();
331                 // NOTREACHED
332         }
333         else {
334
335                 // Update the writable flag if it changed               
336                 logger('dfrn_poll: post request feed: ' . print_r($_POST,true),LOGGER_DATA);
337                 if($dfrn_version >= 2.21) {
338                         if($perm === 'rw')
339                                 $writable = 1;
340                         else
341                                 $writable = 0;
342
343                         if($writable !=  $contact['writable']) {
344                                 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d LIMIT 1",
345                                         intval($writable),
346                                         intval($contact_id)
347                                 );
348                         }
349                 }                               
350
351                 header("Content-type: application/atom+xml");
352                 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
353                 echo $o;
354                 killme();
355
356         }
357 }
358
359 function dfrn_poll_content(&$a) {
360
361         $dfrn_id         = ((x($_GET,'dfrn_id'))         ? $_GET['dfrn_id']              : '');
362         $type            = ((x($_GET,'type'))            ? $_GET['type']                 : 'data');
363         $last_update     = ((x($_GET,'last_update'))     ? $_GET['last_update']          : '');
364         $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url']      : '');
365         $sec             = ((x($_GET,'sec'))             ? $_GET['sec']                  : '');
366         $dfrn_version    = ((x($_GET,'dfrn_version'))    ? (float) $_GET['dfrn_version'] : 2.0);
367         $perm            = ((x($_GET,'perm'))            ? $_GET['perm']                 : 'r');
368
369         $direction = (-1);
370         if(strpos($dfrn_id,':') == 1) {
371                 $direction = intval(substr($dfrn_id,0,1));
372                 $dfrn_id = substr($dfrn_id,2);
373         }
374
375
376         if($dfrn_id != '') {
377                 // initial communication from external contact
378                 $hash = random_string();
379
380                 $status = 0;
381
382                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
383
384                 if($type !== 'profile') {
385                         $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
386                                 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
387                                 dbesc($hash),
388                                 dbesc($dfrn_id),
389                                 intval(time() + 60 ),
390                                 dbesc($type),
391                                 dbesc($last_update)
392                         );
393                 }
394                 $sql_extra = '';
395                 switch($direction) {
396                         case (-1):
397                                 if($type === 'profile')
398                                         $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
399                                 else
400                                         $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
401                                 $my_id = $dfrn_id;
402                                 break;
403                         case 0:
404                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
405                                 $my_id = '1:' . $dfrn_id;
406                                 break;
407                         case 1:
408                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
409                                 $my_id = '0:' . $dfrn_id;
410                                 break;
411                         default:
412                                 goaway($a->get_baseurl());
413                                 break; // NOTREACHED
414                 }
415
416                 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname` 
417                         FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
418                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
419                         AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
420                         dbesc($a->argv[1])
421                 );
422
423                 if(count($r)) {
424
425                         $challenge = '';
426                         $encrypted_id = '';
427                         $id_str = $my_id . '.' . mt_rand(1000,9999);
428
429                         if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
430                                 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
431                                 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
432                         }
433                         else {
434                                 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
435                                 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
436                         }
437
438                         $challenge = bin2hex($challenge);
439                         $encrypted_id = bin2hex($encrypted_id);
440                 }
441                 else {
442                         $status = 1;
443                         $challenge = '';
444                         $encrypted_id = '';
445                 }
446
447                 if(($type === 'profile') && (strlen($sec))) {
448
449                         // URL reply
450
451                         if($dfrn_version < 2.2) {
452                                 $s = fetch_url($r[0]['poll'] 
453                                         . '?dfrn_id=' . $encrypted_id 
454                                         . '&type=profile-check'
455                                         . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
456                                         . '&challenge=' . $challenge
457                                         . '&sec=' . $sec
458                                 );
459                         }
460                         else {
461                                 $s = post_url($r[0]['poll'], array(
462                                         'dfrn_id' => $encrypted_id,
463                                         'type' => 'profile-check',
464                                         'dfrn_version' => DFRN_PROTOCOL_VERSION,
465                                         'challenge' => $challenge,
466                                         'sec' => $sec
467                                 ));
468                         }
469                         
470                         $profile = $r[0]['nickname'];
471
472                         switch($destination_url) {
473                                 case 'profile':
474                                         $dest = $a->get_baseurl() . '/profile/' . $profile . '?tab=profile';
475                                         break;
476                                 case 'photos':
477                                         $dest = $a->get_baseurl() . '/photos/' . $profile;
478                                         break;
479                                 case 'status':
480                                 case '':
481                                         $dest = $a->get_baseurl() . '/profile/' . $profile;
482                                         break;          
483                                 default:
484                                         $dest = $destination_url;
485                                         break;
486                         }
487
488                         logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
489
490                         if(strlen($s) && strstr($s,'<?xml')) {
491
492                                 $xml = parse_xml_string($s);
493
494                                 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
495
496                                 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
497                                 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
498  
499                                 
500                                 if(((int) $xml->status == 0) && ($xml->challenge == $hash)  && ($xml->sec == $sec)) {
501                                         $_SESSION['authenticated'] = 1;
502                                         $_SESSION['visitor_id'] = $r[0]['id'];
503                                         $_SESSION['visitor_home'] = $r[0]['url'];
504                                         notice( sprintf(t('%s welcomes %s'), $r[0]['username'] , $r[0]['name']) . EOL);
505                                         // Visitors get 1 day session.
506                                         $session_id = session_id();
507                                         $expire = time() + 86400;
508                                         q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
509                                                 dbesc($expire),
510                                                 dbesc($session_id)
511                                         ); 
512                                 }
513                         
514                                 goaway($dest);
515                         }
516                         goaway($dest);
517                         // NOTREACHED
518
519                 }
520                 else {
521                         // XML reply
522                         header("Content-type: text/xml");
523                         echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
524                                 . '<dfrn_poll>' . "\r\n"
525                                 . "\t" . '<status>' . $status . '</status>' . "\r\n"
526                                 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
527                                 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
528                                 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
529                                 . '</dfrn_poll>' . "\r\n" ;
530                         killme();
531                         // NOTREACHED
532                 }
533         }
534 }
535
536