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 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",
89 $profile = $r[0]['nickname'];
90 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
92 goaway($a->get_baseurl());
96 if($type === 'profile-check' && $dfrn_version < 2.2 ) {
98 if((strlen($challenge)) && (strlen($sec))) {
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",
105 xml_status(3, 'No ticket');
108 $orig_id = $r[0]['dfrn_id'];
109 if(strpos($orig_id, ':'))
110 $orig_id = substr($orig_id,2);
112 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
116 xml_status(3, 'No profile');
120 $sent_dfrn_id = hex2bin($dfrn_id);
121 $challenge = hex2bin($challenge);
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']);
130 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
131 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
134 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
136 if(strpos($final_dfrn_id,':') == 1)
137 $final_dfrn_id = substr($final_dfrn_id,2);
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');
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>";
155 $dfrn_id = '0:' . $dfrn_id;
158 $dfrn_id = '1:' . $dfrn_id;
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",
170 return; // NOTREACHED
173 return; // NOTREACHED
181 function dfrn_poll_post(&$a) {
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');
191 if($ptype === 'profile-check') {
193 if((strlen($challenge)) && (strlen($sec))) {
195 logger('dfrn_poll: POST: profile-check');
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",
202 xml_status(3, 'No ticket');
205 $orig_id = $r[0]['dfrn_id'];
206 if(strpos($orig_id, ':'))
207 $orig_id = substr($orig_id,2);
209 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
213 xml_status(3, 'No profile');
217 $sent_dfrn_id = hex2bin($dfrn_id);
218 $challenge = hex2bin($challenge);
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']);
227 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
228 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
231 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
233 if(strpos($final_dfrn_id,':') == 1)
234 $final_dfrn_id = substr($final_dfrn_id,2);
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');
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>";
251 if(strpos($dfrn_id,':') == 1) {
252 $direction = intval(substr($dfrn_id,0,1));
253 $dfrn_id = substr($dfrn_id,2);
257 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
265 $type = $r[0]['type'];
266 $last_update = $r[0]['last_update'];
268 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
277 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
281 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
282 $my_id = '1:' . $dfrn_id;
285 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
286 $my_id = '0:' . $dfrn_id;
289 goaway($a->get_baseurl());
294 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
300 $owner_uid = $r[0]['uid'];
301 $contact_id = $r[0]['id'];
304 if($type === 'reputation' && strlen($url)) {
305 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
313 $reputation = $r[0]['rating'];
314 $text = $r[0]['reason'];
316 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
322 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
325 <rating>$reputation</rating>
326 <description>$text</description>
333 header("Content-type: application/atom+xml");
334 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
341 function dfrn_poll_content(&$a) {
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');
352 if(strpos($dfrn_id,':') == 1) {
353 $direction = intval(substr($dfrn_id,0,1));
354 $dfrn_id = substr($dfrn_id,2);
359 // initial communication from external contact
360 $hash = random_string();
364 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
366 if($type !== 'profile') {
367 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
368 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
371 intval(time() + 60 ),
379 if($type === 'profile')
380 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
382 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
386 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
387 $my_id = '1:' . $dfrn_id;
390 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
391 $my_id = '0:' . $dfrn_id;
394 goaway($a->get_baseurl());
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",
409 $id_str = $my_id . '.' . mt_rand(1000,9999);
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']);
416 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
417 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
420 $challenge = bin2hex($challenge);
421 $encrypted_id = bin2hex($encrypted_id);
429 if(($type === 'profile') && (strlen($sec))) {
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
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,
452 $profile = $r[0]['nickname'];
454 switch($destination_url) {
456 $dest = $a->get_baseurl() . '/profile/' . $profile . '?tab=profile';
459 $dest = $a->get_baseurl() . '/photos/' . $profile;
463 $dest = $a->get_baseurl() . '/profile/' . $profile;
466 $dest = $destination_url;
470 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
472 if(strlen($s) && strstr($s,'<?xml')) {
474 $xml = parse_xml_string($s);
476 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
478 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
479 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
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",
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" ;