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");
301 $owner_uid = $r[0]['uid'];
302 $contact_id = $r[0]['id'];
305 if($type === 'reputation' && strlen($url)) {
306 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
314 $reputation = $r[0]['rating'];
315 $text = $r[0]['reason'];
317 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
323 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
326 <rating>$reputation</rating>
327 <description>$text</description>
335 // Update the writable flag if it changed
336 logger('dfrn_poll: post request feed: ' . print_r($_POST,true),LOGGER_DATA);
337 if($dfrn_version >= 2.21) {
343 if($writable != $contact['writable']) {
344 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d LIMIT 1",
351 header("Content-type: application/atom+xml");
352 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
359 function dfrn_poll_content(&$a) {
361 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
362 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
363 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
364 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
365 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
366 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
367 $perm = ((x($_GET,'perm')) ? $_GET['perm'] : 'r');
370 if(strpos($dfrn_id,':') == 1) {
371 $direction = intval(substr($dfrn_id,0,1));
372 $dfrn_id = substr($dfrn_id,2);
377 // initial communication from external contact
378 $hash = random_string();
382 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
384 if($type !== 'profile') {
385 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
386 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
389 intval(time() + 60 ),
397 if($type === 'profile')
398 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
400 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
404 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
405 $my_id = '1:' . $dfrn_id;
408 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
409 $my_id = '0:' . $dfrn_id;
412 goaway($a->get_baseurl());
416 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
417 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
418 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
419 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
427 $id_str = $my_id . '.' . mt_rand(1000,9999);
429 if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
430 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
431 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
434 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
435 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
438 $challenge = bin2hex($challenge);
439 $encrypted_id = bin2hex($encrypted_id);
447 if(($type === 'profile') && (strlen($sec))) {
451 if($dfrn_version < 2.2) {
452 $s = fetch_url($r[0]['poll']
453 . '?dfrn_id=' . $encrypted_id
454 . '&type=profile-check'
455 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
456 . '&challenge=' . $challenge
461 $s = post_url($r[0]['poll'], array(
462 'dfrn_id' => $encrypted_id,
463 'type' => 'profile-check',
464 'dfrn_version' => DFRN_PROTOCOL_VERSION,
465 'challenge' => $challenge,
470 $profile = $r[0]['nickname'];
472 switch($destination_url) {
474 $dest = $a->get_baseurl() . '/profile/' . $profile . '?tab=profile';
477 $dest = $a->get_baseurl() . '/photos/' . $profile;
481 $dest = $a->get_baseurl() . '/profile/' . $profile;
484 $dest = $destination_url;
488 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
490 if(strlen($s) && strstr($s,'<?xml')) {
492 $xml = parse_xml_string($s);
494 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
496 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
497 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
500 if(((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
501 $_SESSION['authenticated'] = 1;
502 $_SESSION['visitor_id'] = $r[0]['id'];
503 $_SESSION['visitor_home'] = $r[0]['url'];
504 notice( sprintf(t('%s welcomes %s'), $r[0]['username'] , $r[0]['name']) . EOL);
505 // Visitors get 1 day session.
506 $session_id = session_id();
507 $expire = time() + 86400;
508 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
522 header("Content-type: text/xml");
523 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
524 . '<dfrn_poll>' . "\r\n"
525 . "\t" . '<status>' . $status . '</status>' . "\r\n"
526 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
527 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
528 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
529 . '</dfrn_poll>' . "\r\n" ;