2 require_once('include/items.php');
3 require_once('include/auth.php');
4 require_once('include/dfrn.php');
6 function dfrn_poll_init(App $a) {
7 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
8 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
9 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
10 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
11 $challenge = ((x($_GET,'challenge')) ? $_GET['challenge'] : '');
12 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
13 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
14 $perm = ((x($_GET,'perm')) ? $_GET['perm'] : 'r');
15 $quiet = ((x($_GET,'quiet')) ? true : false);
20 if(strpos($dfrn_id,':') == 1) {
21 $direction = intval(substr($dfrn_id,0,1));
22 $dfrn_id = substr($dfrn_id,2);
27 if(($dfrn_id === '') && (! x($_POST,'dfrn_id'))) {
28 if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
29 http_status_exit(403);
34 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
38 http_status_exit(404);
40 $hidewall = ($r[0]['hidewall'] && !local_user());
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, 0, $hidewall);
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",
79 if (dbm::is_result($r)) {
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 : App::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",
126 if (! dbm::is_result($r)) {
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",
137 if (! dbm::is_result($c)) {
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",
190 if (dbm::is_result($r)) {
192 return; // NOTREACHED
195 return; // NOTREACHED
203 function dfrn_poll_post(App $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",
223 if (! dbm::is_result($r)) {
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",
234 if (! dbm::is_result($c)) {
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",
284 if (! dbm::is_result($r)) {
288 $type = $r[0]['type'];
289 $last_update = $r[0]['last_update'];
291 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'",
300 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
304 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
305 $my_id = '1:' . $dfrn_id;
308 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
309 $my_id = '0:' . $dfrn_id;
317 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
320 if (! dbm::is_result($r)) {
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",
337 if (dbm::is_result($r)) {
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 = dfrn::feed($dfrn_id, $a->argv[1], $last_update, $direction);
383 function dfrn_poll_content(App $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",
450 if (dbm::is_result($r)) {
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 = ((dbm::is_result($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
499 switch($destination_url) {
501 $dest = App::get_baseurl() . '/profile/' . $profile . '?f=&tab=profile';
504 $dest = App::get_baseurl() . '/photos/' . $profile;
508 $dest = App::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" ;