5 require_once('include/items.php');
6 require_once('include/auth.php');
7 require_once('include/dfrn.php');
8 require_once('include/ostatus.php');
10 function dfrn_poll_init(App $a) {
11 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
12 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
13 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
14 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
15 $challenge = ((x($_GET,'challenge')) ? $_GET['challenge'] : '');
16 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
17 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
18 $perm = ((x($_GET,'perm')) ? $_GET['perm'] : 'r');
19 $quiet = ((x($_GET,'quiet')) ? true : false);
21 // Possibly it is an OStatus compatible server that requests a user feed
22 if (($a->argc > 1) && ($dfrn_id == '') && !strstr($_SERVER["HTTP_USER_AGENT"], 'Friendica')) {
23 $nickname = $a->argv[1];
24 header("Content-type: application/atom+xml");
25 echo ostatus::feed($a, $nickname, $last_update);
32 if(strpos($dfrn_id,':') == 1) {
33 $direction = intval(substr($dfrn_id,0,1));
34 $dfrn_id = substr($dfrn_id,2);
39 if(($dfrn_id === '') && (! x($_POST,'dfrn_id'))) {
40 if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
41 http_status_exit(403);
46 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
50 http_status_exit(404);
52 $hidewall = ($r[0]['hidewall'] && !local_user());
54 $user = $r[0]['nickname'];
57 logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
58 header("Content-type: application/atom+xml");
59 echo dfrn::feed('', $user,$last_update, 0, $hidewall);
63 if(($type === 'profile') && (! strlen($sec))) {
68 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
72 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
73 $my_id = '1:' . $dfrn_id;
76 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
77 $my_id = '0:' . $dfrn_id;
84 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
85 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
86 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
87 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
91 if (dbm::is_result($r)) {
93 $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
95 logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
99 $xml = parse_xml_string($s);
101 if((int) $xml->status == 1) {
102 $_SESSION['authenticated'] = 1;
103 if(! x($_SESSION,'remote'))
104 $_SESSION['remote'] = array();
106 $_SESSION['remote'][] = array('cid' => $r[0]['id'],'uid' => $r[0]['uid'],'url' => $r[0]['url']);
108 $_SESSION['visitor_id'] = $r[0]['id'];
109 $_SESSION['visitor_home'] = $r[0]['url'];
110 $_SESSION['visitor_handle'] = $r[0]['addr'];
111 $_SESSION['visitor_visiting'] = $r[0]['uid'];
113 info( sprintf(t('%1$s welcomes %2$s'), $r[0]['username'] , $r[0]['name']) . EOL);
114 // Visitors get 1 day session.
115 $session_id = session_id();
116 $expire = time() + 86400;
117 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
123 $profile = $r[0]['nickname'];
124 goaway((strlen($destination_url)) ? $destination_url : App::get_baseurl() . '/profile/' . $profile);
130 if($type === 'profile-check' && $dfrn_version < 2.2 ) {
132 if((strlen($challenge)) && (strlen($sec))) {
134 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
135 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
138 if (! dbm::is_result($r)) {
139 xml_status(3, 'No ticket');
142 $orig_id = $r[0]['dfrn_id'];
143 if(strpos($orig_id, ':'))
144 $orig_id = substr($orig_id,2);
146 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
149 if (! dbm::is_result($c)) {
150 xml_status(3, 'No profile');
154 $sent_dfrn_id = hex2bin($dfrn_id);
155 $challenge = hex2bin($challenge);
159 if(($contact['duplex']) && strlen($contact['prvkey'])) {
160 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
161 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
164 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
165 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
168 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
170 if(strpos($final_dfrn_id,':') == 1)
171 $final_dfrn_id = substr($final_dfrn_id,2);
173 if($final_dfrn_id != $orig_id) {
174 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
175 // did not decode properly - cannot trust this site
176 xml_status(3, 'Bad decryption');
179 header("Content-type: text/xml");
180 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
189 $dfrn_id = '0:' . $dfrn_id;
192 $dfrn_id = '1:' . $dfrn_id;
199 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
200 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
202 if (dbm::is_result($r)) {
204 return; // NOTREACHED
207 return; // NOTREACHED
215 function dfrn_poll_post(App $a) {
217 $dfrn_id = ((x($_POST,'dfrn_id')) ? $_POST['dfrn_id'] : '');
218 $challenge = ((x($_POST,'challenge')) ? $_POST['challenge'] : '');
219 $url = ((x($_POST,'url')) ? $_POST['url'] : '');
220 $sec = ((x($_POST,'sec')) ? $_POST['sec'] : '');
221 $ptype = ((x($_POST,'type')) ? $_POST['type'] : '');
222 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
223 $perm = ((x($_POST,'perm')) ? $_POST['perm'] : 'r');
225 if($ptype === 'profile-check') {
227 if((strlen($challenge)) && (strlen($sec))) {
229 logger('dfrn_poll: POST: profile-check');
231 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
232 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
235 if (! dbm::is_result($r)) {
236 xml_status(3, 'No ticket');
239 $orig_id = $r[0]['dfrn_id'];
240 if(strpos($orig_id, ':'))
241 $orig_id = substr($orig_id,2);
243 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
246 if (! dbm::is_result($c)) {
247 xml_status(3, 'No profile');
251 $sent_dfrn_id = hex2bin($dfrn_id);
252 $challenge = hex2bin($challenge);
256 if(($contact['duplex']) && strlen($contact['prvkey'])) {
257 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
258 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
261 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
262 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
265 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
267 if(strpos($final_dfrn_id,':') == 1)
268 $final_dfrn_id = substr($final_dfrn_id,2);
270 if($final_dfrn_id != $orig_id) {
271 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
272 // did not decode properly - cannot trust this site
273 xml_status(3, 'Bad decryption');
276 header("Content-type: text/xml");
277 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
285 if(strpos($dfrn_id,':') == 1) {
286 $direction = intval(substr($dfrn_id,0,1));
287 $dfrn_id = substr($dfrn_id,2);
291 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
296 if (! dbm::is_result($r)) {
300 $type = $r[0]['type'];
301 $last_update = $r[0]['last_update'];
303 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'",
312 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
316 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
317 $my_id = '1:' . $dfrn_id;
320 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
321 $my_id = '0:' . $dfrn_id;
329 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
332 if (! dbm::is_result($r)) {
337 $owner_uid = $r[0]['uid'];
338 $contact_id = $r[0]['id'];
341 if($type === 'reputation' && strlen($url)) {
342 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
349 if (dbm::is_result($r)) {
350 $reputation = $r[0]['rating'];
351 $text = $r[0]['reason'];
353 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
359 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
362 <rating>$reputation</rating>
363 <description>$text</description>
371 // Update the writable flag if it changed
372 logger('dfrn_poll: post request feed: ' . print_r($_POST,true),LOGGER_DATA);
373 if($dfrn_version >= 2.21) {
379 if($writable != $contact['writable']) {
380 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
387 header("Content-type: application/atom+xml");
388 $o = dfrn::feed($dfrn_id, $a->argv[1], $last_update, $direction);
395 function dfrn_poll_content(App $a) {
397 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
398 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
399 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
400 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
401 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
402 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
403 $perm = ((x($_GET,'perm')) ? $_GET['perm'] : 'r');
404 $quiet = ((x($_GET,'quiet')) ? true : false);
407 if(strpos($dfrn_id,':') == 1) {
408 $direction = intval(substr($dfrn_id,0,1));
409 $dfrn_id = substr($dfrn_id,2);
414 // initial communication from external contact
415 $hash = random_string();
419 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
421 if($type !== 'profile') {
422 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
423 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
426 intval(time() + 60 ),
434 if($type === 'profile')
435 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
437 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
441 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
442 $my_id = '1:' . $dfrn_id;
445 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
446 $my_id = '0:' . $dfrn_id;
453 $nickname = $a->argv[1];
455 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
456 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
457 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
458 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
462 if (dbm::is_result($r)) {
466 $id_str = $my_id . '.' . mt_rand(1000,9999);
468 if(($r[0]['duplex'] && strlen($r[0]['pubkey'])) || (! strlen($r[0]['prvkey']))) {
469 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
470 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
473 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
474 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
477 $challenge = bin2hex($challenge);
478 $encrypted_id = bin2hex($encrypted_id);
486 if(($type === 'profile') && (strlen($sec))) {
490 if($dfrn_version < 2.2) {
491 $s = fetch_url($r[0]['poll']
492 . '?dfrn_id=' . $encrypted_id
493 . '&type=profile-check'
494 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
495 . '&challenge=' . $challenge
500 $s = post_url($r[0]['poll'], array(
501 'dfrn_id' => $encrypted_id,
502 'type' => 'profile-check',
503 'dfrn_version' => DFRN_PROTOCOL_VERSION,
504 'challenge' => $challenge,
509 $profile = ((dbm::is_result($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
511 switch($destination_url) {
513 $dest = App::get_baseurl() . '/profile/' . $profile . '?f=&tab=profile';
516 $dest = App::get_baseurl() . '/photos/' . $profile;
520 $dest = App::get_baseurl() . '/profile/' . $profile;
523 $appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
524 $dest = $destination_url . $appendix;
528 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
530 if(strlen($s) && strstr($s,'<?xml')) {
532 $xml = parse_xml_string($s);
534 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
536 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
537 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
540 if(((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
541 $_SESSION['authenticated'] = 1;
542 if(! x($_SESSION,'remote'))
543 $_SESSION['remote'] = array();
544 $_SESSION['remote'][] = array('cid' => $r[0]['id'],'uid' => $r[0]['uid'],'url' => $r[0]['url']);
545 $_SESSION['visitor_id'] = $r[0]['id'];
546 $_SESSION['visitor_home'] = $r[0]['url'];
547 $_SESSION['visitor_visiting'] = $r[0]['uid'];
549 info( sprintf(t('%1$s welcomes %2$s'), $r[0]['username'] , $r[0]['name']) . EOL);
550 // Visitors get 1 day session.
551 $session_id = session_id();
552 $expire = time() + 86400;
553 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
567 header("Content-type: text/xml");
568 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
569 . '<dfrn_poll>' . "\r\n"
570 . "\t" . '<status>' . $status . '</status>' . "\r\n"
571 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
572 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
573 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
574 . '</dfrn_poll>' . "\r\n" ;