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');
20 $quiet = ((x($_GET,'quiet')) ? true : false);
25 if(strpos($dfrn_id,':') == 1) {
26 $direction = intval(substr($dfrn_id,0,1));
27 $dfrn_id = substr($dfrn_id,2);
30 if(($dfrn_id === '') && (! x($_POST,'dfrn_id'))) {
31 if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
32 http_status_exit(403);
37 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
41 http_status_exit(404);
42 if(($r[0]['hidewall']) && (! local_user()))
43 http_status_exit(403);
44 $user = $r[0]['nickname'];
47 logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
48 header("Content-type: application/atom+xml");
49 echo get_feed_for($a, '', $user,$last_update);
53 if(($type === 'profile') && (! strlen($sec))) {
58 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
62 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
63 $my_id = '1:' . $dfrn_id;
66 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
67 $my_id = '0:' . $dfrn_id;
74 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
75 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
76 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
77 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
83 $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
85 logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
89 $xml = parse_xml_string($s);
91 if((int) $xml->status == 1) {
92 $_SESSION['authenticated'] = 1;
93 if(! x($_SESSION,'remote'))
94 $_SESSION['remote'] = array();
96 $_SESSION['remote'][] = array('cid' => $r[0]['id'],'uid' => $r[0]['uid'],'url' => $r[0]['url']);
98 $_SESSION['visitor_id'] = $r[0]['id'];
99 $_SESSION['visitor_home'] = $r[0]['url'];
100 $_SESSION['visitor_handle'] = $r[0]['addr'];
101 $_SESSION['visitor_visiting'] = $r[0]['uid'];
103 info( sprintf(t('%1$s welcomes %2$s'), $r[0]['username'] , $r[0]['name']) . EOL);
104 // Visitors get 1 day session.
105 $session_id = session_id();
106 $expire = time() + 86400;
107 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
113 $profile = $r[0]['nickname'];
114 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
120 if($type === 'profile-check' && $dfrn_version < 2.2 ) {
122 if((strlen($challenge)) && (strlen($sec))) {
124 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
125 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
129 xml_status(3, 'No ticket');
132 $orig_id = $r[0]['dfrn_id'];
133 if(strpos($orig_id, ':'))
134 $orig_id = substr($orig_id,2);
136 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
140 xml_status(3, 'No profile');
144 $sent_dfrn_id = hex2bin($dfrn_id);
145 $challenge = hex2bin($challenge);
149 if(($contact['duplex']) && strlen($contact['prvkey'])) {
150 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
151 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
154 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
155 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
158 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
160 if(strpos($final_dfrn_id,':') == 1)
161 $final_dfrn_id = substr($final_dfrn_id,2);
163 if($final_dfrn_id != $orig_id) {
164 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
165 // did not decode properly - cannot trust this site
166 xml_status(3, 'Bad decryption');
169 header("Content-type: text/xml");
170 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
179 $dfrn_id = '0:' . $dfrn_id;
182 $dfrn_id = '1:' . $dfrn_id;
189 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
190 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
194 return; // NOTREACHED
197 return; // NOTREACHED
205 function dfrn_poll_post(&$a) {
207 $dfrn_id = ((x($_POST,'dfrn_id')) ? $_POST['dfrn_id'] : '');
208 $challenge = ((x($_POST,'challenge')) ? $_POST['challenge'] : '');
209 $url = ((x($_POST,'url')) ? $_POST['url'] : '');
210 $sec = ((x($_POST,'sec')) ? $_POST['sec'] : '');
211 $ptype = ((x($_POST,'type')) ? $_POST['type'] : '');
212 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
213 $perm = ((x($_POST,'perm')) ? $_POST['perm'] : 'r');
215 if($ptype === 'profile-check') {
217 if((strlen($challenge)) && (strlen($sec))) {
219 logger('dfrn_poll: POST: profile-check');
221 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
222 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
226 xml_status(3, 'No ticket');
229 $orig_id = $r[0]['dfrn_id'];
230 if(strpos($orig_id, ':'))
231 $orig_id = substr($orig_id,2);
233 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
237 xml_status(3, 'No profile');
241 $sent_dfrn_id = hex2bin($dfrn_id);
242 $challenge = hex2bin($challenge);
246 if(($contact['duplex']) && strlen($contact['prvkey'])) {
247 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
248 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
251 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
252 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
255 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
257 if(strpos($final_dfrn_id,':') == 1)
258 $final_dfrn_id = substr($final_dfrn_id,2);
260 if($final_dfrn_id != $orig_id) {
261 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
262 // did not decode properly - cannot trust this site
263 xml_status(3, 'Bad decryption');
266 header("Content-type: text/xml");
267 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
275 if(strpos($dfrn_id,':') == 1) {
276 $direction = intval(substr($dfrn_id,0,1));
277 $dfrn_id = substr($dfrn_id,2);
281 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
289 $type = $r[0]['type'];
290 $last_update = $r[0]['last_update'];
292 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'",
301 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
305 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
306 $my_id = '1:' . $dfrn_id;
309 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
310 $my_id = '0:' . $dfrn_id;
318 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
325 $owner_uid = $r[0]['uid'];
326 $contact_id = $r[0]['id'];
329 if($type === 'reputation' && strlen($url)) {
330 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
338 $reputation = $r[0]['rating'];
339 $text = $r[0]['reason'];
341 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
347 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
350 <rating>$reputation</rating>
351 <description>$text</description>
359 // Update the writable flag if it changed
360 logger('dfrn_poll: post request feed: ' . print_r($_POST,true),LOGGER_DATA);
361 if($dfrn_version >= 2.21) {
367 if($writable != $contact['writable']) {
368 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
375 header("Content-type: application/atom+xml");
376 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
383 function dfrn_poll_content(&$a) {
385 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
386 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
387 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
388 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
389 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
390 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
391 $perm = ((x($_GET,'perm')) ? $_GET['perm'] : 'r');
392 $quiet = ((x($_GET,'quiet')) ? true : false);
395 if(strpos($dfrn_id,':') == 1) {
396 $direction = intval(substr($dfrn_id,0,1));
397 $dfrn_id = substr($dfrn_id,2);
402 // initial communication from external contact
403 $hash = random_string();
407 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
409 if($type !== 'profile') {
410 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
411 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
414 intval(time() + 60 ),
422 if($type === 'profile')
423 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
425 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
429 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
430 $my_id = '1:' . $dfrn_id;
433 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
434 $my_id = '0:' . $dfrn_id;
441 $nickname = $a->argv[1];
443 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
444 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
445 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
446 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
454 $id_str = $my_id . '.' . mt_rand(1000,9999);
456 if(($r[0]['duplex'] && strlen($r[0]['pubkey'])) || (! strlen($r[0]['prvkey']))) {
457 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
458 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
461 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
462 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
465 $challenge = bin2hex($challenge);
466 $encrypted_id = bin2hex($encrypted_id);
474 if(($type === 'profile') && (strlen($sec))) {
478 if($dfrn_version < 2.2) {
479 $s = fetch_url($r[0]['poll']
480 . '?dfrn_id=' . $encrypted_id
481 . '&type=profile-check'
482 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
483 . '&challenge=' . $challenge
488 $s = post_url($r[0]['poll'], array(
489 'dfrn_id' => $encrypted_id,
490 'type' => 'profile-check',
491 'dfrn_version' => DFRN_PROTOCOL_VERSION,
492 'challenge' => $challenge,
497 $profile = ((count($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
499 switch($destination_url) {
501 $dest = $a->get_baseurl() . '/profile/' . $profile . '?f=&tab=profile';
504 $dest = $a->get_baseurl() . '/photos/' . $profile;
508 $dest = $a->get_baseurl() . '/profile/' . $profile;
511 $dest = $destination_url . '?f=&redir=1';
515 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
517 if(strlen($s) && strstr($s,'<?xml')) {
519 $xml = parse_xml_string($s);
521 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
523 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
524 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
527 if(((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
528 $_SESSION['authenticated'] = 1;
529 if(! x($_SESSION,'remote'))
530 $_SESSION['remote'] = array();
531 $_SESSION['remote'][] = array('cid' => $r[0]['id'],'uid' => $r[0]['uid'],'url' => $r[0]['url']);
532 $_SESSION['visitor_id'] = $r[0]['id'];
533 $_SESSION['visitor_home'] = $r[0]['url'];
534 $_SESSION['visitor_visiting'] = $r[0]['uid'];
536 info( sprintf(t('%1$s welcomes %2$s'), $r[0]['username'] , $r[0]['name']) . EOL);
537 // Visitors get 1 day session.
538 $session_id = session_id();
539 $expire = time() + 86400;
540 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
554 header("Content-type: text/xml");
555 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
556 . '<dfrn_poll>' . "\r\n"
557 . "\t" . '<status>' . $status . '</status>' . "\r\n"
558 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
559 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
560 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
561 . '</dfrn_poll>' . "\r\n" ;