2 require_once('include/items.php');
3 require_once('include/auth.php');
4 require_once('include/dfrn.php');
7 function dfrn_poll_init(App &$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);
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);
43 $hidewall = ($r[0]['hidewall'] && !local_user());
45 $user = $r[0]['nickname'];
48 logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
49 header("Content-type: application/atom+xml");
50 echo dfrn::feed('', $user,$last_update, 0, $hidewall);
54 if(($type === 'profile') && (! strlen($sec))) {
59 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
63 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
64 $my_id = '1:' . $dfrn_id;
67 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
68 $my_id = '0:' . $dfrn_id;
75 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
76 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
77 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
78 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
82 if (dbm::is_result($r)) {
84 $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
86 logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
90 $xml = parse_xml_string($s);
92 if((int) $xml->status == 1) {
93 $_SESSION['authenticated'] = 1;
94 if(! x($_SESSION,'remote'))
95 $_SESSION['remote'] = array();
97 $_SESSION['remote'][] = array('cid' => $r[0]['id'],'uid' => $r[0]['uid'],'url' => $r[0]['url']);
99 $_SESSION['visitor_id'] = $r[0]['id'];
100 $_SESSION['visitor_home'] = $r[0]['url'];
101 $_SESSION['visitor_handle'] = $r[0]['addr'];
102 $_SESSION['visitor_visiting'] = $r[0]['uid'];
104 info( sprintf(t('%1$s welcomes %2$s'), $r[0]['username'] , $r[0]['name']) . EOL);
105 // Visitors get 1 day session.
106 $session_id = session_id();
107 $expire = time() + 86400;
108 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
114 $profile = $r[0]['nickname'];
115 goaway((strlen($destination_url)) ? $destination_url : App::get_baseurl() . '/profile/' . $profile);
121 if($type === 'profile-check' && $dfrn_version < 2.2 ) {
123 if((strlen($challenge)) && (strlen($sec))) {
125 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
126 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
129 if (! dbm::is_result($r)) {
130 xml_status(3, 'No ticket');
133 $orig_id = $r[0]['dfrn_id'];
134 if(strpos($orig_id, ':'))
135 $orig_id = substr($orig_id,2);
137 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
140 if (! dbm::is_result($c)) {
141 xml_status(3, 'No profile');
145 $sent_dfrn_id = hex2bin($dfrn_id);
146 $challenge = hex2bin($challenge);
150 if(($contact['duplex']) && strlen($contact['prvkey'])) {
151 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
152 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
155 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
156 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
159 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
161 if(strpos($final_dfrn_id,':') == 1)
162 $final_dfrn_id = substr($final_dfrn_id,2);
164 if($final_dfrn_id != $orig_id) {
165 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
166 // did not decode properly - cannot trust this site
167 xml_status(3, 'Bad decryption');
170 header("Content-type: text/xml");
171 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
180 $dfrn_id = '0:' . $dfrn_id;
183 $dfrn_id = '1:' . $dfrn_id;
190 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
191 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
193 if (dbm::is_result($r)) {
195 return; // NOTREACHED
198 return; // NOTREACHED
206 function dfrn_poll_post(App &$a) {
208 $dfrn_id = ((x($_POST,'dfrn_id')) ? $_POST['dfrn_id'] : '');
209 $challenge = ((x($_POST,'challenge')) ? $_POST['challenge'] : '');
210 $url = ((x($_POST,'url')) ? $_POST['url'] : '');
211 $sec = ((x($_POST,'sec')) ? $_POST['sec'] : '');
212 $ptype = ((x($_POST,'type')) ? $_POST['type'] : '');
213 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
214 $perm = ((x($_POST,'perm')) ? $_POST['perm'] : 'r');
216 if($ptype === 'profile-check') {
218 if((strlen($challenge)) && (strlen($sec))) {
220 logger('dfrn_poll: POST: profile-check');
222 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
223 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
226 if (! dbm::is_result($r)) {
227 xml_status(3, 'No ticket');
230 $orig_id = $r[0]['dfrn_id'];
231 if(strpos($orig_id, ':'))
232 $orig_id = substr($orig_id,2);
234 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
237 if (! dbm::is_result($c)) {
238 xml_status(3, 'No profile');
242 $sent_dfrn_id = hex2bin($dfrn_id);
243 $challenge = hex2bin($challenge);
247 if(($contact['duplex']) && strlen($contact['prvkey'])) {
248 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
249 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
252 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
253 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
256 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
258 if(strpos($final_dfrn_id,':') == 1)
259 $final_dfrn_id = substr($final_dfrn_id,2);
261 if($final_dfrn_id != $orig_id) {
262 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
263 // did not decode properly - cannot trust this site
264 xml_status(3, 'Bad decryption');
267 header("Content-type: text/xml");
268 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
276 if(strpos($dfrn_id,':') == 1) {
277 $direction = intval(substr($dfrn_id,0,1));
278 $dfrn_id = substr($dfrn_id,2);
282 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
287 if (! dbm::is_result($r)) {
291 $type = $r[0]['type'];
292 $last_update = $r[0]['last_update'];
294 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'",
303 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
307 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
308 $my_id = '1:' . $dfrn_id;
311 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
312 $my_id = '0:' . $dfrn_id;
320 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
323 if (! dbm::is_result($r)) {
328 $owner_uid = $r[0]['uid'];
329 $contact_id = $r[0]['id'];
332 if($type === 'reputation' && strlen($url)) {
333 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
340 if (dbm::is_result($r)) {
341 $reputation = $r[0]['rating'];
342 $text = $r[0]['reason'];
344 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
350 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
353 <rating>$reputation</rating>
354 <description>$text</description>
362 // Update the writable flag if it changed
363 logger('dfrn_poll: post request feed: ' . print_r($_POST,true),LOGGER_DATA);
364 if($dfrn_version >= 2.21) {
370 if($writable != $contact['writable']) {
371 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
378 header("Content-type: application/atom+xml");
379 $o = dfrn::feed($dfrn_id, $a->argv[1], $last_update, $direction);
386 function dfrn_poll_content(App &$a) {
388 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
389 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
390 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
391 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
392 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
393 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
394 $perm = ((x($_GET,'perm')) ? $_GET['perm'] : 'r');
395 $quiet = ((x($_GET,'quiet')) ? true : false);
398 if(strpos($dfrn_id,':') == 1) {
399 $direction = intval(substr($dfrn_id,0,1));
400 $dfrn_id = substr($dfrn_id,2);
405 // initial communication from external contact
406 $hash = random_string();
410 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
412 if($type !== 'profile') {
413 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
414 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
417 intval(time() + 60 ),
425 if($type === 'profile')
426 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
428 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
432 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
433 $my_id = '1:' . $dfrn_id;
436 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
437 $my_id = '0:' . $dfrn_id;
444 $nickname = $a->argv[1];
446 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
447 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
448 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
449 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
453 if (dbm::is_result($r)) {
457 $id_str = $my_id . '.' . mt_rand(1000,9999);
459 if(($r[0]['duplex'] && strlen($r[0]['pubkey'])) || (! strlen($r[0]['prvkey']))) {
460 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
461 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
464 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
465 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
468 $challenge = bin2hex($challenge);
469 $encrypted_id = bin2hex($encrypted_id);
477 if(($type === 'profile') && (strlen($sec))) {
481 if($dfrn_version < 2.2) {
482 $s = fetch_url($r[0]['poll']
483 . '?dfrn_id=' . $encrypted_id
484 . '&type=profile-check'
485 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
486 . '&challenge=' . $challenge
491 $s = post_url($r[0]['poll'], array(
492 'dfrn_id' => $encrypted_id,
493 'type' => 'profile-check',
494 'dfrn_version' => DFRN_PROTOCOL_VERSION,
495 'challenge' => $challenge,
500 $profile = ((dbm::is_result($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
502 switch($destination_url) {
504 $dest = App::get_baseurl() . '/profile/' . $profile . '?f=&tab=profile';
507 $dest = App::get_baseurl() . '/photos/' . $profile;
511 $dest = App::get_baseurl() . '/profile/' . $profile;
514 $dest = $destination_url . '?f=&redir=1';
518 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
520 if(strlen($s) && strstr($s,'<?xml')) {
522 $xml = parse_xml_string($s);
524 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
526 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
527 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
530 if(((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
531 $_SESSION['authenticated'] = 1;
532 if(! x($_SESSION,'remote'))
533 $_SESSION['remote'] = array();
534 $_SESSION['remote'][] = array('cid' => $r[0]['id'],'uid' => $r[0]['uid'],'url' => $r[0]['url']);
535 $_SESSION['visitor_id'] = $r[0]['id'];
536 $_SESSION['visitor_home'] = $r[0]['url'];
537 $_SESSION['visitor_visiting'] = $r[0]['uid'];
539 info( sprintf(t('%1$s welcomes %2$s'), $r[0]['username'] , $r[0]['name']) . EOL);
540 // Visitors get 1 day session.
541 $session_id = session_id();
542 $expire = time() + 86400;
543 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
557 header("Content-type: text/xml");
558 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
559 . '<dfrn_poll>' . "\r\n"
560 . "\t" . '<status>' . $status . '</status>' . "\r\n"
561 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
562 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
563 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
564 . '</dfrn_poll>' . "\r\n" ;