2 require_once('include/items.php');
3 require_once('include/auth.php');
4 require_once('include/dfrn.php');
7 function dfrn_poll_init(&$a) {
10 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
11 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
12 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
13 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
14 $challenge = ((x($_GET,'challenge')) ? $_GET['challenge'] : '');
15 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
16 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
17 $perm = ((x($_GET,'perm')) ? $_GET['perm'] : 'r');
18 $quiet = ((x($_GET,'quiet')) ? true : false);
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'))) {
29 if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
30 http_status_exit(403);
35 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
39 http_status_exit(404);
40 if(($r[0]['hidewall']) && (! local_user()))
41 http_status_exit(403);
42 $user = $r[0]['nickname'];
45 logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
46 header("Content-type: application/atom+xml");
47 echo dfrn::feed('', $user,$last_update);
51 if(($type === 'profile') && (! strlen($sec))) {
56 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
60 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
61 $my_id = '1:' . $dfrn_id;
64 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
65 $my_id = '0:' . $dfrn_id;
72 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
73 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
74 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
75 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
81 $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
83 logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
87 $xml = parse_xml_string($s);
89 if((int) $xml->status == 1) {
90 $_SESSION['authenticated'] = 1;
91 if(! x($_SESSION,'remote'))
92 $_SESSION['remote'] = array();
94 $_SESSION['remote'][] = array('cid' => $r[0]['id'],'uid' => $r[0]['uid'],'url' => $r[0]['url']);
96 $_SESSION['visitor_id'] = $r[0]['id'];
97 $_SESSION['visitor_home'] = $r[0]['url'];
98 $_SESSION['visitor_handle'] = $r[0]['addr'];
99 $_SESSION['visitor_visiting'] = $r[0]['uid'];
101 info( sprintf(t('%1$s welcomes %2$s'), $r[0]['username'] , $r[0]['name']) . EOL);
102 // Visitors get 1 day session.
103 $session_id = session_id();
104 $expire = time() + 86400;
105 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
111 $profile = $r[0]['nickname'];
112 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
118 if($type === 'profile-check' && $dfrn_version < 2.2 ) {
120 if((strlen($challenge)) && (strlen($sec))) {
122 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
123 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
127 xml_status(3, 'No ticket');
130 $orig_id = $r[0]['dfrn_id'];
131 if(strpos($orig_id, ':'))
132 $orig_id = substr($orig_id,2);
134 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
138 xml_status(3, 'No profile');
142 $sent_dfrn_id = hex2bin($dfrn_id);
143 $challenge = hex2bin($challenge);
147 if(($contact['duplex']) && strlen($contact['prvkey'])) {
148 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
149 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
152 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
153 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
156 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
158 if(strpos($final_dfrn_id,':') == 1)
159 $final_dfrn_id = substr($final_dfrn_id,2);
161 if($final_dfrn_id != $orig_id) {
162 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
163 // did not decode properly - cannot trust this site
164 xml_status(3, 'Bad decryption');
167 header("Content-type: text/xml");
168 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
177 $dfrn_id = '0:' . $dfrn_id;
180 $dfrn_id = '1:' . $dfrn_id;
187 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
188 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
192 return; // NOTREACHED
195 return; // NOTREACHED
203 function dfrn_poll_post(&$a) {
205 $dfrn_id = ((x($_POST,'dfrn_id')) ? $_POST['dfrn_id'] : '');
206 $challenge = ((x($_POST,'challenge')) ? $_POST['challenge'] : '');
207 $url = ((x($_POST,'url')) ? $_POST['url'] : '');
208 $sec = ((x($_POST,'sec')) ? $_POST['sec'] : '');
209 $ptype = ((x($_POST,'type')) ? $_POST['type'] : '');
210 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
211 $perm = ((x($_POST,'perm')) ? $_POST['perm'] : 'r');
213 if($ptype === 'profile-check') {
215 if((strlen($challenge)) && (strlen($sec))) {
217 logger('dfrn_poll: POST: profile-check');
219 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
220 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
224 xml_status(3, 'No ticket');
227 $orig_id = $r[0]['dfrn_id'];
228 if(strpos($orig_id, ':'))
229 $orig_id = substr($orig_id,2);
231 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
235 xml_status(3, 'No profile');
239 $sent_dfrn_id = hex2bin($dfrn_id);
240 $challenge = hex2bin($challenge);
244 if(($contact['duplex']) && strlen($contact['prvkey'])) {
245 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
246 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
249 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
250 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
253 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
255 if(strpos($final_dfrn_id,':') == 1)
256 $final_dfrn_id = substr($final_dfrn_id,2);
258 if($final_dfrn_id != $orig_id) {
259 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
260 // did not decode properly - cannot trust this site
261 xml_status(3, 'Bad decryption');
264 header("Content-type: text/xml");
265 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
273 if(strpos($dfrn_id,':') == 1) {
274 $direction = intval(substr($dfrn_id,0,1));
275 $dfrn_id = substr($dfrn_id,2);
279 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
287 $type = $r[0]['type'];
288 $last_update = $r[0]['last_update'];
290 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'",
299 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
303 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
304 $my_id = '1:' . $dfrn_id;
307 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
308 $my_id = '0:' . $dfrn_id;
316 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
323 $owner_uid = $r[0]['uid'];
324 $contact_id = $r[0]['id'];
327 if($type === 'reputation' && strlen($url)) {
328 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
336 $reputation = $r[0]['rating'];
337 $text = $r[0]['reason'];
339 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
345 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
348 <rating>$reputation</rating>
349 <description>$text</description>
357 // Update the writable flag if it changed
358 logger('dfrn_poll: post request feed: ' . print_r($_POST,true),LOGGER_DATA);
359 if($dfrn_version >= 2.21) {
365 if($writable != $contact['writable']) {
366 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
373 header("Content-type: application/atom+xml");
374 $o = dfrn::feed($dfrn_id, $a->argv[1], $last_update, $direction);
381 function dfrn_poll_content(&$a) {
383 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
384 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
385 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
386 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
387 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
388 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
389 $perm = ((x($_GET,'perm')) ? $_GET['perm'] : 'r');
390 $quiet = ((x($_GET,'quiet')) ? true : false);
393 if(strpos($dfrn_id,':') == 1) {
394 $direction = intval(substr($dfrn_id,0,1));
395 $dfrn_id = substr($dfrn_id,2);
400 // initial communication from external contact
401 $hash = random_string();
405 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
407 if($type !== 'profile') {
408 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
409 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
412 intval(time() + 60 ),
420 if($type === 'profile')
421 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
423 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
427 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
428 $my_id = '1:' . $dfrn_id;
431 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
432 $my_id = '0:' . $dfrn_id;
439 $nickname = $a->argv[1];
441 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
442 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
443 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
444 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
452 $id_str = $my_id . '.' . mt_rand(1000,9999);
454 if(($r[0]['duplex'] && strlen($r[0]['pubkey'])) || (! strlen($r[0]['prvkey']))) {
455 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
456 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
459 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
460 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
463 $challenge = bin2hex($challenge);
464 $encrypted_id = bin2hex($encrypted_id);
472 if(($type === 'profile') && (strlen($sec))) {
476 if($dfrn_version < 2.2) {
477 $s = fetch_url($r[0]['poll']
478 . '?dfrn_id=' . $encrypted_id
479 . '&type=profile-check'
480 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
481 . '&challenge=' . $challenge
486 $s = post_url($r[0]['poll'], array(
487 'dfrn_id' => $encrypted_id,
488 'type' => 'profile-check',
489 'dfrn_version' => DFRN_PROTOCOL_VERSION,
490 'challenge' => $challenge,
495 $profile = ((count($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
497 switch($destination_url) {
499 $dest = $a->get_baseurl() . '/profile/' . $profile . '?f=&tab=profile';
502 $dest = $a->get_baseurl() . '/photos/' . $profile;
506 $dest = $a->get_baseurl() . '/profile/' . $profile;
509 $dest = $destination_url . '?f=&redir=1';
513 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
515 if(strlen($s) && strstr($s,'<?xml')) {
517 $xml = parse_xml_string($s);
519 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
521 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
522 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
525 if(((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
526 $_SESSION['authenticated'] = 1;
527 if(! x($_SESSION,'remote'))
528 $_SESSION['remote'] = array();
529 $_SESSION['remote'][] = array('cid' => $r[0]['id'],'uid' => $r[0]['uid'],'url' => $r[0]['url']);
530 $_SESSION['visitor_id'] = $r[0]['id'];
531 $_SESSION['visitor_home'] = $r[0]['url'];
532 $_SESSION['visitor_visiting'] = $r[0]['uid'];
534 info( sprintf(t('%1$s welcomes %2$s'), $r[0]['username'] , $r[0]['name']) . EOL);
535 // Visitors get 1 day session.
536 $session_id = session_id();
537 $expire = time() + 86400;
538 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
552 header("Content-type: text/xml");
553 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
554 . '<dfrn_poll>' . "\r\n"
555 . "\t" . '<status>' . $status . '</status>' . "\r\n"
556 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
557 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
558 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
559 . '</dfrn_poll>' . "\r\n" ;