5 require_once('include/items.php');
6 require_once('include/auth.php');
9 function dfrn_poll_init(&$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');
24 if(strpos($dfrn_id,':') == 1) {
25 $direction = intval(substr($dfrn_id,0,1));
26 $dfrn_id = substr($dfrn_id,2);
29 if(($dfrn_id === '') && (! x($_POST,'dfrn_id')) && ($a->argc > 1)) {
30 if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
34 logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] );
35 header("Content-type: application/atom+xml");
36 $o = get_feed_for($a, '', $a->argv[1],$last_update);
41 if(($type === 'profile') && (! strlen($sec))) {
46 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
50 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
51 $my_id = '1:' . $dfrn_id;
54 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
55 $my_id = '0:' . $dfrn_id;
58 goaway($a->get_baseurl());
62 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
63 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
64 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
65 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
71 $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
73 logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
77 $xml = parse_xml_string($s);
79 if((int) $xml->status == 1) {
80 $_SESSION['authenticated'] = 1;
81 $_SESSION['visitor_id'] = $r[0]['id'];
82 $_SESSION['visitor_home'] = $r[0]['url'];
83 $_SESSION['visitor_visiting'] = $r[0]['uid'];
84 notice( sprintf(t('%s welcomes %s'), $r[0]['username'] , $r[0]['name']) . EOL);
85 // Visitors get 1 day session.
86 $session_id = session_id();
87 $expire = time() + 86400;
88 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
94 $profile = $r[0]['nickname'];
95 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
97 goaway($a->get_baseurl());
101 if($type === 'profile-check' && $dfrn_version < 2.2 ) {
103 if((strlen($challenge)) && (strlen($sec))) {
105 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
106 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
110 xml_status(3, 'No ticket');
113 $orig_id = $r[0]['dfrn_id'];
114 if(strpos($orig_id, ':'))
115 $orig_id = substr($orig_id,2);
117 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
121 xml_status(3, 'No profile');
125 $sent_dfrn_id = hex2bin($dfrn_id);
126 $challenge = hex2bin($challenge);
130 if(($contact['duplex']) && strlen($contact['prvkey'])) {
131 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
132 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
135 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
136 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
139 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
141 if(strpos($final_dfrn_id,':') == 1)
142 $final_dfrn_id = substr($final_dfrn_id,2);
144 if($final_dfrn_id != $orig_id) {
145 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
146 // did not decode properly - cannot trust this site
147 xml_status(3, 'Bad decryption');
150 header("Content-type: text/xml");
151 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
160 $dfrn_id = '0:' . $dfrn_id;
163 $dfrn_id = '1:' . $dfrn_id;
170 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
171 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
175 return; // NOTREACHED
178 return; // NOTREACHED
186 function dfrn_poll_post(&$a) {
188 $dfrn_id = ((x($_POST,'dfrn_id')) ? $_POST['dfrn_id'] : '');
189 $challenge = ((x($_POST,'challenge')) ? $_POST['challenge'] : '');
190 $url = ((x($_POST,'url')) ? $_POST['url'] : '');
191 $sec = ((x($_POST,'sec')) ? $_POST['sec'] : '');
192 $ptype = ((x($_POST,'type')) ? $_POST['type'] : '');
193 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
194 $perm = ((x($_POST,'perm')) ? $_POST['perm'] : 'r');
196 if($ptype === 'profile-check') {
198 if((strlen($challenge)) && (strlen($sec))) {
200 logger('dfrn_poll: POST: profile-check');
202 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
203 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
207 xml_status(3, 'No ticket');
210 $orig_id = $r[0]['dfrn_id'];
211 if(strpos($orig_id, ':'))
212 $orig_id = substr($orig_id,2);
214 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
218 xml_status(3, 'No profile');
222 $sent_dfrn_id = hex2bin($dfrn_id);
223 $challenge = hex2bin($challenge);
227 if(($contact['duplex']) && strlen($contact['prvkey'])) {
228 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
229 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
232 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
233 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
236 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
238 if(strpos($final_dfrn_id,':') == 1)
239 $final_dfrn_id = substr($final_dfrn_id,2);
241 if($final_dfrn_id != $orig_id) {
242 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
243 // did not decode properly - cannot trust this site
244 xml_status(3, 'Bad decryption');
247 header("Content-type: text/xml");
248 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
256 if(strpos($dfrn_id,':') == 1) {
257 $direction = intval(substr($dfrn_id,0,1));
258 $dfrn_id = substr($dfrn_id,2);
262 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
270 $type = $r[0]['type'];
271 $last_update = $r[0]['last_update'];
273 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
282 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
286 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
287 $my_id = '1:' . $dfrn_id;
290 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
291 $my_id = '0:' . $dfrn_id;
294 goaway($a->get_baseurl());
299 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
306 $owner_uid = $r[0]['uid'];
307 $contact_id = $r[0]['id'];
310 if($type === 'reputation' && strlen($url)) {
311 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
319 $reputation = $r[0]['rating'];
320 $text = $r[0]['reason'];
322 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
328 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
331 <rating>$reputation</rating>
332 <description>$text</description>
340 // Update the writable flag if it changed
341 logger('dfrn_poll: post request feed: ' . print_r($_POST,true),LOGGER_DATA);
342 if($dfrn_version >= 2.21) {
348 if($writable != $contact['writable']) {
349 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d LIMIT 1",
356 header("Content-type: application/atom+xml");
357 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
364 function dfrn_poll_content(&$a) {
366 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
367 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
368 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
369 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
370 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
371 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
372 $perm = ((x($_GET,'perm')) ? $_GET['perm'] : 'r');
375 if(strpos($dfrn_id,':') == 1) {
376 $direction = intval(substr($dfrn_id,0,1));
377 $dfrn_id = substr($dfrn_id,2);
382 // initial communication from external contact
383 $hash = random_string();
387 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
389 if($type !== 'profile') {
390 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
391 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
394 intval(time() + 60 ),
402 if($type === 'profile')
403 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
405 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
409 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
410 $my_id = '1:' . $dfrn_id;
413 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
414 $my_id = '0:' . $dfrn_id;
417 goaway($a->get_baseurl());
421 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
422 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
423 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
424 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
432 $id_str = $my_id . '.' . mt_rand(1000,9999);
434 if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
435 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
436 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
439 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
440 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
443 $challenge = bin2hex($challenge);
444 $encrypted_id = bin2hex($encrypted_id);
452 if(($type === 'profile') && (strlen($sec))) {
456 if($dfrn_version < 2.2) {
457 $s = fetch_url($r[0]['poll']
458 . '?dfrn_id=' . $encrypted_id
459 . '&type=profile-check'
460 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
461 . '&challenge=' . $challenge
466 $s = post_url($r[0]['poll'], array(
467 'dfrn_id' => $encrypted_id,
468 'type' => 'profile-check',
469 'dfrn_version' => DFRN_PROTOCOL_VERSION,
470 'challenge' => $challenge,
475 $profile = $r[0]['nickname'];
477 switch($destination_url) {
479 $dest = $a->get_baseurl() . '/profile/' . $profile . '?tab=profile';
482 $dest = $a->get_baseurl() . '/photos/' . $profile;
486 $dest = $a->get_baseurl() . '/profile/' . $profile;
489 $dest = $destination_url;
493 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
495 if(strlen($s) && strstr($s,'<?xml')) {
497 $xml = parse_xml_string($s);
499 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
501 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
502 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
505 if(((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
506 $_SESSION['authenticated'] = 1;
507 $_SESSION['visitor_id'] = $r[0]['id'];
508 $_SESSION['visitor_home'] = $r[0]['url'];
509 $_SESSION['visitor_visiting'] = $r[0]['uid'];
510 notice( sprintf(t('%s welcomes %s'), $r[0]['username'] , $r[0]['name']) . EOL);
511 // Visitors get 1 day session.
512 $session_id = session_id();
513 $expire = time() + 86400;
514 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
528 header("Content-type: text/xml");
529 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
530 . '<dfrn_poll>' . "\r\n"
531 . "\t" . '<status>' . $status . '</status>' . "\r\n"
532 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
533 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
534 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
535 . '</dfrn_poll>' . "\r\n" ;