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'))) {
30 if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
36 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
39 if((! count($r)) || (count($r) && $r[0]['hidewall']))
41 $user = $r[0]['nickname'];
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);
50 if(($type === 'profile') && (! strlen($sec))) {
55 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
59 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
60 $my_id = '1:' . $dfrn_id;
63 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
64 $my_id = '0:' . $dfrn_id;
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",
80 $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
82 logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
86 $xml = parse_xml_string($s);
88 if((int) $xml->status == 1) {
89 $_SESSION['authenticated'] = 1;
90 if(! x($_SESSION,'remote'))
91 $_SESSION['remote'] = array();
93 $_SESSION['remote'][] = array('cid' => $r[0]['id'],'uid' => $r[0]['uid'],'url' => $r[0]['url']);
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('%s welcomes %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",
109 $profile = $r[0]['nickname'];
110 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
116 if($type === 'profile-check' && $dfrn_version < 2.2 ) {
118 if((strlen($challenge)) && (strlen($sec))) {
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",
125 xml_status(3, 'No ticket');
128 $orig_id = $r[0]['dfrn_id'];
129 if(strpos($orig_id, ':'))
130 $orig_id = substr($orig_id,2);
132 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
136 xml_status(3, 'No profile');
140 $sent_dfrn_id = hex2bin($dfrn_id);
141 $challenge = hex2bin($challenge);
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']);
150 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
151 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
154 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
156 if(strpos($final_dfrn_id,':') == 1)
157 $final_dfrn_id = substr($final_dfrn_id,2);
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');
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>";
175 $dfrn_id = '0:' . $dfrn_id;
178 $dfrn_id = '1:' . $dfrn_id;
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",
190 return; // NOTREACHED
193 return; // NOTREACHED
201 function dfrn_poll_post(&$a) {
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');
211 if($ptype === 'profile-check') {
213 if((strlen($challenge)) && (strlen($sec))) {
215 logger('dfrn_poll: POST: profile-check');
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",
222 xml_status(3, 'No ticket');
225 $orig_id = $r[0]['dfrn_id'];
226 if(strpos($orig_id, ':'))
227 $orig_id = substr($orig_id,2);
229 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
233 xml_status(3, 'No profile');
237 $sent_dfrn_id = hex2bin($dfrn_id);
238 $challenge = hex2bin($challenge);
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']);
247 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
248 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
251 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
253 if(strpos($final_dfrn_id,':') == 1)
254 $final_dfrn_id = substr($final_dfrn_id,2);
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');
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>";
271 if(strpos($dfrn_id,':') == 1) {
272 $direction = intval(substr($dfrn_id,0,1));
273 $dfrn_id = substr($dfrn_id,2);
277 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
285 $type = $r[0]['type'];
286 $last_update = $r[0]['last_update'];
288 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
297 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
301 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
302 $my_id = '1:' . $dfrn_id;
305 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
306 $my_id = '0:' . $dfrn_id;
314 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
321 $owner_uid = $r[0]['uid'];
322 $contact_id = $r[0]['id'];
325 if($type === 'reputation' && strlen($url)) {
326 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
334 $reputation = $r[0]['rating'];
335 $text = $r[0]['reason'];
337 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
343 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
346 <rating>$reputation</rating>
347 <description>$text</description>
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) {
363 if($writable != $contact['writable']) {
364 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d LIMIT 1",
371 header("Content-type: application/atom+xml");
372 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
379 function dfrn_poll_content(&$a) {
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');
390 if(strpos($dfrn_id,':') == 1) {
391 $direction = intval(substr($dfrn_id,0,1));
392 $dfrn_id = substr($dfrn_id,2);
397 // initial communication from external contact
398 $hash = random_string();
402 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
404 if($type !== 'profile') {
405 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
406 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
409 intval(time() + 60 ),
417 if($type === 'profile')
418 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
420 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
424 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
425 $my_id = '1:' . $dfrn_id;
428 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
429 $my_id = '0:' . $dfrn_id;
436 $nickname = $a->argv[1];
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",
449 $id_str = $my_id . '.' . mt_rand(1000,9999);
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']);
456 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
457 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
460 $challenge = bin2hex($challenge);
461 $encrypted_id = bin2hex($encrypted_id);
469 if(($type === 'profile') && (strlen($sec))) {
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
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,
492 $profile = ((count($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
494 switch($destination_url) {
496 $dest = $a->get_baseurl() . '/profile/' . $profile . '?tab=profile';
499 $dest = $a->get_baseurl() . '/photos/' . $profile;
503 $dest = $a->get_baseurl() . '/profile/' . $profile;
506 $dest = $destination_url;
510 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
512 if(strlen($s) && strstr($s,'<?xml')) {
514 $xml = parse_xml_string($s);
516 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
518 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
519 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
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('%s welcomes %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",
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" ;