4 use Friendica\Core\Config;
5 use Friendica\Core\System;
7 require_once('include/items.php');
8 require_once('include/auth.php');
9 require_once('include/dfrn.php');
10 require_once('include/ostatus.php');
12 function dfrn_poll_init(App $a) {
13 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
14 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
15 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
16 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
17 $challenge = ((x($_GET,'challenge')) ? $_GET['challenge'] : '');
18 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
19 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
20 $perm = ((x($_GET,'perm')) ? $_GET['perm'] : 'r');
21 $quiet = ((x($_GET,'quiet')) ? true : false);
23 // Possibly it is an OStatus compatible server that requests a user feed
24 if (($a->argc > 1) && ($dfrn_id == '') && !strstr($_SERVER["HTTP_USER_AGENT"], 'Friendica')) {
25 $nickname = $a->argv[1];
26 header("Content-type: application/atom+xml");
27 echo ostatus::feed($a, $nickname, $last_update, 10);
34 if(strpos($dfrn_id,':') == 1) {
35 $direction = intval(substr($dfrn_id,0,1));
36 $dfrn_id = substr($dfrn_id,2);
41 if(($dfrn_id === '') && (! x($_POST,'dfrn_id'))) {
42 if((Config::get('system','block_public')) && (! local_user()) && (! remote_user())) {
43 http_status_exit(403);
48 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
52 http_status_exit(404);
54 $hidewall = ($r[0]['hidewall'] && !local_user());
56 $user = $r[0]['nickname'];
59 logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
60 header("Content-type: application/atom+xml");
61 echo dfrn::feed('', $user,$last_update, 0, $hidewall);
65 if(($type === 'profile') && (! strlen($sec))) {
70 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
74 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
75 $my_id = '1:' . $dfrn_id;
78 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
79 $my_id = '0:' . $dfrn_id;
82 goaway(System::baseUrl());
86 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
87 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
88 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
89 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
93 if (dbm::is_result($r)) {
95 $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
97 logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
101 $xml = parse_xml_string($s);
103 if((int) $xml->status == 1) {
104 $_SESSION['authenticated'] = 1;
105 if(! x($_SESSION,'remote'))
106 $_SESSION['remote'] = array();
108 $_SESSION['remote'][] = array('cid' => $r[0]['id'],'uid' => $r[0]['uid'],'url' => $r[0]['url']);
110 $_SESSION['visitor_id'] = $r[0]['id'];
111 $_SESSION['visitor_home'] = $r[0]['url'];
112 $_SESSION['visitor_handle'] = $r[0]['addr'];
113 $_SESSION['visitor_visiting'] = $r[0]['uid'];
115 info( sprintf(t('%1$s welcomes %2$s'), $r[0]['username'] , $r[0]['name']) . EOL);
116 // Visitors get 1 day session.
117 $session_id = session_id();
118 $expire = time() + 86400;
119 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
125 $profile = $r[0]['nickname'];
126 goaway((strlen($destination_url)) ? $destination_url : System::baseUrl() . '/profile/' . $profile);
128 goaway(System::baseUrl());
132 if($type === 'profile-check' && $dfrn_version < 2.2 ) {
134 if((strlen($challenge)) && (strlen($sec))) {
136 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
137 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
140 if (! dbm::is_result($r)) {
141 xml_status(3, 'No ticket');
144 $orig_id = $r[0]['dfrn_id'];
145 if(strpos($orig_id, ':'))
146 $orig_id = substr($orig_id,2);
148 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
151 if (! dbm::is_result($c)) {
152 xml_status(3, 'No profile');
156 $sent_dfrn_id = hex2bin($dfrn_id);
157 $challenge = hex2bin($challenge);
161 if(($contact['duplex']) && strlen($contact['prvkey'])) {
162 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
163 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
166 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
167 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
170 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
172 if(strpos($final_dfrn_id,':') == 1)
173 $final_dfrn_id = substr($final_dfrn_id,2);
175 if($final_dfrn_id != $orig_id) {
176 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
177 // did not decode properly - cannot trust this site
178 xml_status(3, 'Bad decryption');
181 header("Content-type: text/xml");
182 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
191 $dfrn_id = '0:' . $dfrn_id;
194 $dfrn_id = '1:' . $dfrn_id;
201 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
202 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
204 if (dbm::is_result($r)) {
206 return; // NOTREACHED
209 return; // NOTREACHED
217 function dfrn_poll_post(App $a) {
219 $dfrn_id = ((x($_POST,'dfrn_id')) ? $_POST['dfrn_id'] : '');
220 $challenge = ((x($_POST,'challenge')) ? $_POST['challenge'] : '');
221 $url = ((x($_POST,'url')) ? $_POST['url'] : '');
222 $sec = ((x($_POST,'sec')) ? $_POST['sec'] : '');
223 $ptype = ((x($_POST,'type')) ? $_POST['type'] : '');
224 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
225 $perm = ((x($_POST,'perm')) ? $_POST['perm'] : 'r');
227 if($ptype === 'profile-check') {
229 if((strlen($challenge)) && (strlen($sec))) {
231 logger('dfrn_poll: POST: profile-check');
233 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
234 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
237 if (! dbm::is_result($r)) {
238 xml_status(3, 'No ticket');
241 $orig_id = $r[0]['dfrn_id'];
242 if(strpos($orig_id, ':'))
243 $orig_id = substr($orig_id,2);
245 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
248 if (! dbm::is_result($c)) {
249 xml_status(3, 'No profile');
253 $sent_dfrn_id = hex2bin($dfrn_id);
254 $challenge = hex2bin($challenge);
258 if(($contact['duplex']) && strlen($contact['prvkey'])) {
259 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
260 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
263 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
264 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
267 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
269 if(strpos($final_dfrn_id,':') == 1)
270 $final_dfrn_id = substr($final_dfrn_id,2);
272 if($final_dfrn_id != $orig_id) {
273 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
274 // did not decode properly - cannot trust this site
275 xml_status(3, 'Bad decryption');
278 header("Content-type: text/xml");
279 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
287 if(strpos($dfrn_id,':') == 1) {
288 $direction = intval(substr($dfrn_id,0,1));
289 $dfrn_id = substr($dfrn_id,2);
293 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
298 if (! dbm::is_result($r)) {
302 $type = $r[0]['type'];
303 $last_update = $r[0]['last_update'];
305 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'",
314 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
318 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
319 $my_id = '1:' . $dfrn_id;
322 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
323 $my_id = '0:' . $dfrn_id;
326 goaway(System::baseUrl());
331 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
334 if (! dbm::is_result($r)) {
339 $owner_uid = $r[0]['uid'];
340 $contact_id = $r[0]['id'];
343 if($type === 'reputation' && strlen($url)) {
344 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
351 if (dbm::is_result($r)) {
352 $reputation = $r[0]['rating'];
353 $text = $r[0]['reason'];
355 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
361 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
364 <rating>$reputation</rating>
365 <description>$text</description>
373 // Update the writable flag if it changed
374 logger('dfrn_poll: post request feed: ' . print_r($_POST,true),LOGGER_DATA);
375 if($dfrn_version >= 2.21) {
381 if($writable != $contact['writable']) {
382 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
389 header("Content-type: application/atom+xml");
390 $o = dfrn::feed($dfrn_id, $a->argv[1], $last_update, $direction);
397 function dfrn_poll_content(App $a) {
399 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
400 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
401 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
402 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
403 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
404 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
405 $perm = ((x($_GET,'perm')) ? $_GET['perm'] : 'r');
406 $quiet = ((x($_GET,'quiet')) ? true : false);
409 if(strpos($dfrn_id,':') == 1) {
410 $direction = intval(substr($dfrn_id,0,1));
411 $dfrn_id = substr($dfrn_id,2);
416 // initial communication from external contact
417 $hash = random_string();
421 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
423 if($type !== 'profile') {
424 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
425 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
428 intval(time() + 60 ),
436 if($type === 'profile')
437 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
439 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
443 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
444 $my_id = '1:' . $dfrn_id;
447 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
448 $my_id = '0:' . $dfrn_id;
451 goaway(System::baseUrl());
455 $nickname = $a->argv[1];
457 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
458 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
459 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
460 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
464 if (dbm::is_result($r)) {
468 $id_str = $my_id . '.' . mt_rand(1000,9999);
470 if(($r[0]['duplex'] && strlen($r[0]['pubkey'])) || (! strlen($r[0]['prvkey']))) {
471 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
472 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
475 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
476 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
479 $challenge = bin2hex($challenge);
480 $encrypted_id = bin2hex($encrypted_id);
488 if(($type === 'profile') && (strlen($sec))) {
492 if($dfrn_version < 2.2) {
493 $s = fetch_url($r[0]['poll']
494 . '?dfrn_id=' . $encrypted_id
495 . '&type=profile-check'
496 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
497 . '&challenge=' . $challenge
502 $s = post_url($r[0]['poll'], array(
503 'dfrn_id' => $encrypted_id,
504 'type' => 'profile-check',
505 'dfrn_version' => DFRN_PROTOCOL_VERSION,
506 'challenge' => $challenge,
511 $profile = ((dbm::is_result($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
513 switch($destination_url) {
515 $dest = System::baseUrl() . '/profile/' . $profile . '?f=&tab=profile';
518 $dest = System::baseUrl() . '/photos/' . $profile;
522 $dest = System::baseUrl() . '/profile/' . $profile;
525 $appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
526 $dest = $destination_url . $appendix;
530 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
532 if(strlen($s) && strstr($s,'<?xml')) {
534 $xml = parse_xml_string($s);
536 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
538 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
539 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
542 if(((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
543 $_SESSION['authenticated'] = 1;
544 if(! x($_SESSION,'remote'))
545 $_SESSION['remote'] = array();
546 $_SESSION['remote'][] = array('cid' => $r[0]['id'],'uid' => $r[0]['uid'],'url' => $r[0]['url']);
547 $_SESSION['visitor_id'] = $r[0]['id'];
548 $_SESSION['visitor_home'] = $r[0]['url'];
549 $_SESSION['visitor_visiting'] = $r[0]['uid'];
551 info( sprintf(t('%1$s welcomes %2$s'), $r[0]['username'] , $r[0]['name']) . EOL);
552 // Visitors get 1 day session.
553 $session_id = session_id();
554 $expire = time() + 86400;
555 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
569 header("Content-type: text/xml");
570 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
571 . '<dfrn_poll>' . "\r\n"
572 . "\t" . '<status>' . $status . '</status>' . "\r\n"
573 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
574 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
575 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
576 . '</dfrn_poll>' . "\r\n" ;