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);
23 if(strpos($dfrn_id,':') == 1) {
24 $direction = intval(substr($dfrn_id,0,1));
25 $dfrn_id = substr($dfrn_id,2);
28 if(($dfrn_id === '') && (! x($_POST,'dfrn_id')) && ($a->argc > 1)) {
29 logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] );
30 header("Content-type: application/atom+xml");
31 $o = get_feed_for($a, '', $a->argv[1],$last_update);
36 if(($type === 'profile') && (! strlen($sec))) {
41 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
45 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
46 $my_id = '1:' . $dfrn_id;
49 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
50 $my_id = '0:' . $dfrn_id;
53 goaway($a->get_baseurl());
57 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
58 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
59 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
60 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
66 $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
68 logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
72 $xml = parse_xml_string($s);
74 if((int) $xml->status == 1) {
75 $_SESSION['authenticated'] = 1;
76 $_SESSION['visitor_id'] = $r[0]['id'];
77 $_SESSION['visitor_home'] = $r[0]['url'];
78 notice( sprintf(t('%s welcomes %s'), $r[0]['username'] , $r[0]['name']) . EOL);
79 // Visitors get 1 day session.
80 $session_id = session_id();
81 $expire = time() + 86400;
82 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
88 $profile = $r[0]['nickname'];
89 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
91 goaway($a->get_baseurl());
95 if($type === 'profile-check' && $dfrn_version < 2.2 ) {
97 if((strlen($challenge)) && (strlen($sec))) {
99 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
100 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
104 xml_status(3, 'No ticket');
107 $orig_id = $r[0]['dfrn_id'];
108 if(strpos($orig_id, ':'))
109 $orig_id = substr($orig_id,2);
111 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
115 xml_status(3, 'No profile');
119 $sent_dfrn_id = hex2bin($dfrn_id);
120 $challenge = hex2bin($challenge);
124 if(($contact['duplex']) && strlen($contact['prvkey'])) {
125 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
126 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
129 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
130 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
133 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
135 if(strpos($final_dfrn_id,':') == 1)
136 $final_dfrn_id = substr($final_dfrn_id,2);
138 if($final_dfrn_id != $orig_id) {
139 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
140 // did not decode properly - cannot trust this site
141 xml_status(3, 'Bad decryption');
144 header("Content-type: text/xml");
145 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
154 $dfrn_id = '0:' . $dfrn_id;
157 $dfrn_id = '1:' . $dfrn_id;
164 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
165 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
169 return; // NOTREACHED
172 return; // NOTREACHED
180 function dfrn_poll_post(&$a) {
182 $dfrn_id = ((x($_POST,'dfrn_id')) ? $_POST['dfrn_id'] : '');
183 $challenge = ((x($_POST,'challenge')) ? $_POST['challenge'] : '');
184 $url = ((x($_POST,'url')) ? $_POST['url'] : '');
185 $sec = ((x($_POST,'sec')) ? $_POST['sec'] : '');
186 $ptype = ((x($_POST,'type')) ? $_POST['type'] : '');
187 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
189 if($ptype === 'profile-check') {
191 if((strlen($challenge)) && (strlen($sec))) {
193 logger('dfrn_poll: POST: profile-check');
195 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
196 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
200 xml_status(3, 'No ticket');
203 $orig_id = $r[0]['dfrn_id'];
204 if(strpos($orig_id, ':'))
205 $orig_id = substr($orig_id,2);
207 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
211 xml_status(3, 'No profile');
215 $sent_dfrn_id = hex2bin($dfrn_id);
216 $challenge = hex2bin($challenge);
220 if(($contact['duplex']) && strlen($contact['prvkey'])) {
221 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
222 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
225 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
226 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
229 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
231 if(strpos($final_dfrn_id,':') == 1)
232 $final_dfrn_id = substr($final_dfrn_id,2);
234 if($final_dfrn_id != $orig_id) {
235 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
236 // did not decode properly - cannot trust this site
237 xml_status(3, 'Bad decryption');
240 header("Content-type: text/xml");
241 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
249 if(strpos($dfrn_id,':') == 1) {
250 $direction = intval(substr($dfrn_id,0,1));
251 $dfrn_id = substr($dfrn_id,2);
255 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
263 $type = $r[0]['type'];
264 $last_update = $r[0]['last_update'];
266 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
275 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
279 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
280 $my_id = '1:' . $dfrn_id;
283 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
284 $my_id = '0:' . $dfrn_id;
287 goaway($a->get_baseurl());
292 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
298 $owner_uid = $r[0]['uid'];
299 $contact_id = $r[0]['id'];
302 if($type === 'reputation' && strlen($url)) {
303 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
311 $reputation = $r[0]['rating'];
312 $text = $r[0]['reason'];
314 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
320 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
323 <rating>$reputation</rating>
324 <description>$text</description>
331 header("Content-type: application/atom+xml");
332 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
339 function dfrn_poll_content(&$a) {
341 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
342 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
343 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
344 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
345 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
346 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
349 if(strpos($dfrn_id,':') == 1) {
350 $direction = intval(substr($dfrn_id,0,1));
351 $dfrn_id = substr($dfrn_id,2);
356 // initial communication from external contact
357 $hash = random_string();
361 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
363 if($type !== 'profile') {
364 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
365 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
368 intval(time() + 60 ),
376 if($type === 'profile')
377 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
379 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
383 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
384 $my_id = '1:' . $dfrn_id;
387 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
388 $my_id = '0:' . $dfrn_id;
391 goaway($a->get_baseurl());
395 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
396 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
397 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
398 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
406 $id_str = $my_id . '.' . mt_rand(1000,9999);
408 if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
409 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
410 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
413 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
414 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
417 $challenge = bin2hex($challenge);
418 $encrypted_id = bin2hex($encrypted_id);
426 if(($type === 'profile') && (strlen($sec))) {
430 if($dfrn_version < 2.2) {
431 $s = fetch_url($r[0]['poll']
432 . '?dfrn_id=' . $encrypted_id
433 . '&type=profile-check'
434 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
435 . '&challenge=' . $challenge
440 $s = post_url($r[0]['poll'], array(
441 'dfrn_id' => $encrypted_id,
442 'type' => 'profile-check',
443 'dfrn_version' => DFRN_PROTOCOL_VERSION,
444 'challenge' => $challenge,
449 $profile = $r[0]['nickname'];
451 switch($destination_url) {
453 $dest = $a->get_baseurl() . '/profile/' . $profile . '?tab=profile';
456 $dest = $a->get_baseurl() . '/photos/' . $profile;
460 $dest = $a->get_baseurl() . '/profile/' . $profile;
463 $dest = $destination_url;
467 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
469 if(strlen($s) && strstr($s,'<?xml')) {
471 $xml = parse_xml_string($s);
473 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
475 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
476 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
479 if(((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
480 $_SESSION['authenticated'] = 1;
481 $_SESSION['visitor_id'] = $r[0]['id'];
482 $_SESSION['visitor_home'] = $r[0]['url'];
483 notice( sprintf(t('%s welcomes %s'), $r[0]['username'] , $r[0]['name']) . EOL);
484 // Visitors get 1 day session.
485 $session_id = session_id();
486 $expire = time() + 86400;
487 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
501 header("Content-type: text/xml");
502 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
503 . '<dfrn_poll>' . "\r\n"
504 . "\t" . '<status>' . $status . '</status>' . "\r\n"
505 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
506 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
507 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
508 . '</dfrn_poll>' . "\r\n" ;