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 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);
37 if(($type === 'profile') && (! strlen($sec))) {
42 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
46 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
47 $my_id = '1:' . $dfrn_id;
50 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
51 $my_id = '0:' . $dfrn_id;
54 goaway($a->get_baseurl());
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",
67 $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
69 logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
73 $xml = parse_xml_string($s);
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 $_SESSION['visitor_visiting'] = $r[0]['uid'];
80 notice( sprintf(t('%s welcomes %s'), $r[0]['username'] , $r[0]['name']) . EOL);
81 // Visitors get 1 day session.
82 $session_id = session_id();
83 $expire = time() + 86400;
84 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
90 $profile = $r[0]['nickname'];
91 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
93 goaway($a->get_baseurl());
97 if($type === 'profile-check' && $dfrn_version < 2.2 ) {
99 if((strlen($challenge)) && (strlen($sec))) {
101 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
102 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
106 xml_status(3, 'No ticket');
109 $orig_id = $r[0]['dfrn_id'];
110 if(strpos($orig_id, ':'))
111 $orig_id = substr($orig_id,2);
113 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
117 xml_status(3, 'No profile');
121 $sent_dfrn_id = hex2bin($dfrn_id);
122 $challenge = hex2bin($challenge);
126 if(($contact['duplex']) && strlen($contact['prvkey'])) {
127 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
128 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
131 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
132 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
135 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
137 if(strpos($final_dfrn_id,':') == 1)
138 $final_dfrn_id = substr($final_dfrn_id,2);
140 if($final_dfrn_id != $orig_id) {
141 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
142 // did not decode properly - cannot trust this site
143 xml_status(3, 'Bad decryption');
146 header("Content-type: text/xml");
147 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
156 $dfrn_id = '0:' . $dfrn_id;
159 $dfrn_id = '1:' . $dfrn_id;
166 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
167 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
171 return; // NOTREACHED
174 return; // NOTREACHED
182 function dfrn_poll_post(&$a) {
184 $dfrn_id = ((x($_POST,'dfrn_id')) ? $_POST['dfrn_id'] : '');
185 $challenge = ((x($_POST,'challenge')) ? $_POST['challenge'] : '');
186 $url = ((x($_POST,'url')) ? $_POST['url'] : '');
187 $sec = ((x($_POST,'sec')) ? $_POST['sec'] : '');
188 $ptype = ((x($_POST,'type')) ? $_POST['type'] : '');
189 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
190 $perm = ((x($_POST,'perm')) ? $_POST['perm'] : 'r');
192 if($ptype === 'profile-check') {
194 if((strlen($challenge)) && (strlen($sec))) {
196 logger('dfrn_poll: POST: profile-check');
198 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
199 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
203 xml_status(3, 'No ticket');
206 $orig_id = $r[0]['dfrn_id'];
207 if(strpos($orig_id, ':'))
208 $orig_id = substr($orig_id,2);
210 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
214 xml_status(3, 'No profile');
218 $sent_dfrn_id = hex2bin($dfrn_id);
219 $challenge = hex2bin($challenge);
223 if(($contact['duplex']) && strlen($contact['prvkey'])) {
224 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
225 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
228 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
229 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
232 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
234 if(strpos($final_dfrn_id,':') == 1)
235 $final_dfrn_id = substr($final_dfrn_id,2);
237 if($final_dfrn_id != $orig_id) {
238 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
239 // did not decode properly - cannot trust this site
240 xml_status(3, 'Bad decryption');
243 header("Content-type: text/xml");
244 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
252 if(strpos($dfrn_id,':') == 1) {
253 $direction = intval(substr($dfrn_id,0,1));
254 $dfrn_id = substr($dfrn_id,2);
258 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
266 $type = $r[0]['type'];
267 $last_update = $r[0]['last_update'];
269 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
278 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
282 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
283 $my_id = '1:' . $dfrn_id;
286 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
287 $my_id = '0:' . $dfrn_id;
290 goaway($a->get_baseurl());
295 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
302 $owner_uid = $r[0]['uid'];
303 $contact_id = $r[0]['id'];
306 if($type === 'reputation' && strlen($url)) {
307 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
315 $reputation = $r[0]['rating'];
316 $text = $r[0]['reason'];
318 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
324 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
327 <rating>$reputation</rating>
328 <description>$text</description>
336 // Update the writable flag if it changed
337 logger('dfrn_poll: post request feed: ' . print_r($_POST,true),LOGGER_DATA);
338 if($dfrn_version >= 2.21) {
344 if($writable != $contact['writable']) {
345 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d LIMIT 1",
352 header("Content-type: application/atom+xml");
353 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
360 function dfrn_poll_content(&$a) {
362 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
363 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
364 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
365 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
366 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
367 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
368 $perm = ((x($_GET,'perm')) ? $_GET['perm'] : 'r');
371 if(strpos($dfrn_id,':') == 1) {
372 $direction = intval(substr($dfrn_id,0,1));
373 $dfrn_id = substr($dfrn_id,2);
378 // initial communication from external contact
379 $hash = random_string();
383 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
385 if($type !== 'profile') {
386 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
387 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
390 intval(time() + 60 ),
398 if($type === 'profile')
399 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
401 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
405 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
406 $my_id = '1:' . $dfrn_id;
409 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
410 $my_id = '0:' . $dfrn_id;
413 goaway($a->get_baseurl());
417 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
418 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
419 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
420 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
428 $id_str = $my_id . '.' . mt_rand(1000,9999);
430 if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
431 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
432 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
435 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
436 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
439 $challenge = bin2hex($challenge);
440 $encrypted_id = bin2hex($encrypted_id);
448 if(($type === 'profile') && (strlen($sec))) {
452 if($dfrn_version < 2.2) {
453 $s = fetch_url($r[0]['poll']
454 . '?dfrn_id=' . $encrypted_id
455 . '&type=profile-check'
456 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
457 . '&challenge=' . $challenge
462 $s = post_url($r[0]['poll'], array(
463 'dfrn_id' => $encrypted_id,
464 'type' => 'profile-check',
465 'dfrn_version' => DFRN_PROTOCOL_VERSION,
466 'challenge' => $challenge,
471 $profile = $r[0]['nickname'];
473 switch($destination_url) {
475 $dest = $a->get_baseurl() . '/profile/' . $profile . '?tab=profile';
478 $dest = $a->get_baseurl() . '/photos/' . $profile;
482 $dest = $a->get_baseurl() . '/profile/' . $profile;
485 $dest = $destination_url;
489 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
491 if(strlen($s) && strstr($s,'<?xml')) {
493 $xml = parse_xml_string($s);
495 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
497 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
498 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
501 if(((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
502 $_SESSION['authenticated'] = 1;
503 $_SESSION['visitor_id'] = $r[0]['id'];
504 $_SESSION['visitor_home'] = $r[0]['url'];
505 $_SESSION['visitor_visiting'] = $r[0]['uid'];
506 notice( sprintf(t('%s welcomes %s'), $r[0]['username'] , $r[0]['name']) . EOL);
507 // Visitors get 1 day session.
508 $session_id = session_id();
509 $expire = time() + 86400;
510 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
524 header("Content-type: text/xml");
525 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
526 . '<dfrn_poll>' . "\r\n"
527 . "\t" . '<status>' . $status . '</status>' . "\r\n"
528 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
529 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
530 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
531 . '</dfrn_poll>' . "\r\n" ;