4 use Friendica\Core\Config;
5 use Friendica\Core\System;
6 use Friendica\Database\DBM;
7 use Friendica\Protocol\DFRN;
9 require_once('include/items.php');
10 require_once('include/auth.php');
11 require_once('include/ostatus.php');
13 function dfrn_poll_init(App $a) {
14 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
15 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
16 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
17 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
18 $challenge = ((x($_GET,'challenge')) ? $_GET['challenge'] : '');
19 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
20 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
21 $perm = ((x($_GET,'perm')) ? $_GET['perm'] : 'r');
22 $quiet = ((x($_GET,'quiet')) ? true : false);
24 // Possibly it is an OStatus compatible server that requests a user feed
25 if (($a->argc > 1) && ($dfrn_id == '') && !strstr($_SERVER["HTTP_USER_AGENT"], 'Friendica')) {
26 $nickname = $a->argv[1];
27 header("Content-type: application/atom+xml");
28 echo ostatus::feed($a, $nickname, $last_update, 10);
35 if(strpos($dfrn_id,':') == 1) {
36 $direction = intval(substr($dfrn_id,0,1));
37 $dfrn_id = substr($dfrn_id,2);
42 if(($dfrn_id === '') && (! x($_POST,'dfrn_id'))) {
43 if((Config::get('system','block_public')) && (! local_user()) && (! remote_user())) {
44 http_status_exit(403);
49 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
53 http_status_exit(404);
55 $hidewall = ($r[0]['hidewall'] && !local_user());
57 $user = $r[0]['nickname'];
60 logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
61 header("Content-type: application/atom+xml");
62 echo DFRN::feed('', $user,$last_update, 0, $hidewall);
66 if(($type === 'profile') && (! strlen($sec))) {
71 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
75 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
76 $my_id = '1:' . $dfrn_id;
79 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
80 $my_id = '0:' . $dfrn_id;
83 goaway(System::baseUrl());
87 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
88 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
89 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
90 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
94 if (DBM::is_result($r)) {
96 $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
98 logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
102 $xml = parse_xml_string($s);
104 if((int) $xml->status == 1) {
105 $_SESSION['authenticated'] = 1;
106 if(! x($_SESSION,'remote'))
107 $_SESSION['remote'] = array();
109 $_SESSION['remote'][] = array('cid' => $r[0]['id'],'uid' => $r[0]['uid'],'url' => $r[0]['url']);
111 $_SESSION['visitor_id'] = $r[0]['id'];
112 $_SESSION['visitor_home'] = $r[0]['url'];
113 $_SESSION['visitor_handle'] = $r[0]['addr'];
114 $_SESSION['visitor_visiting'] = $r[0]['uid'];
116 info( sprintf(t('%1$s welcomes %2$s'), $r[0]['username'] , $r[0]['name']) . EOL);
117 // Visitors get 1 day session.
118 $session_id = session_id();
119 $expire = time() + 86400;
120 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
126 $profile = $r[0]['nickname'];
127 goaway((strlen($destination_url)) ? $destination_url : System::baseUrl() . '/profile/' . $profile);
129 goaway(System::baseUrl());
133 if($type === 'profile-check' && $dfrn_version < 2.2 ) {
135 if((strlen($challenge)) && (strlen($sec))) {
137 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
138 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
141 if (! DBM::is_result($r)) {
142 xml_status(3, 'No ticket');
145 $orig_id = $r[0]['dfrn_id'];
146 if(strpos($orig_id, ':'))
147 $orig_id = substr($orig_id,2);
149 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
152 if (! DBM::is_result($c)) {
153 xml_status(3, 'No profile');
157 $sent_dfrn_id = hex2bin($dfrn_id);
158 $challenge = hex2bin($challenge);
162 if(($contact['duplex']) && strlen($contact['prvkey'])) {
163 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
164 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
167 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
168 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
171 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
173 if(strpos($final_dfrn_id,':') == 1)
174 $final_dfrn_id = substr($final_dfrn_id,2);
176 if($final_dfrn_id != $orig_id) {
177 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
178 // did not decode properly - cannot trust this site
179 xml_status(3, 'Bad decryption');
182 header("Content-type: text/xml");
183 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
192 $dfrn_id = '0:' . $dfrn_id;
195 $dfrn_id = '1:' . $dfrn_id;
202 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
203 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
205 if (DBM::is_result($r)) {
207 return; // NOTREACHED
210 return; // NOTREACHED
218 function dfrn_poll_post(App $a) {
220 $dfrn_id = ((x($_POST,'dfrn_id')) ? $_POST['dfrn_id'] : '');
221 $challenge = ((x($_POST,'challenge')) ? $_POST['challenge'] : '');
222 $url = ((x($_POST,'url')) ? $_POST['url'] : '');
223 $sec = ((x($_POST,'sec')) ? $_POST['sec'] : '');
224 $ptype = ((x($_POST,'type')) ? $_POST['type'] : '');
225 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
226 $perm = ((x($_POST,'perm')) ? $_POST['perm'] : 'r');
228 if($ptype === 'profile-check') {
230 if((strlen($challenge)) && (strlen($sec))) {
232 logger('dfrn_poll: POST: profile-check');
234 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
235 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
238 if (! DBM::is_result($r)) {
239 xml_status(3, 'No ticket');
242 $orig_id = $r[0]['dfrn_id'];
243 if(strpos($orig_id, ':'))
244 $orig_id = substr($orig_id,2);
246 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
249 if (! DBM::is_result($c)) {
250 xml_status(3, 'No profile');
254 $sent_dfrn_id = hex2bin($dfrn_id);
255 $challenge = hex2bin($challenge);
259 if(($contact['duplex']) && strlen($contact['prvkey'])) {
260 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
261 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
264 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
265 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
268 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
270 if(strpos($final_dfrn_id,':') == 1)
271 $final_dfrn_id = substr($final_dfrn_id,2);
273 if($final_dfrn_id != $orig_id) {
274 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
275 // did not decode properly - cannot trust this site
276 xml_status(3, 'Bad decryption');
279 header("Content-type: text/xml");
280 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
288 if(strpos($dfrn_id,':') == 1) {
289 $direction = intval(substr($dfrn_id,0,1));
290 $dfrn_id = substr($dfrn_id,2);
294 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
299 if (! DBM::is_result($r)) {
303 $type = $r[0]['type'];
304 $last_update = $r[0]['last_update'];
306 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'",
315 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
319 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
320 $my_id = '1:' . $dfrn_id;
323 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
324 $my_id = '0:' . $dfrn_id;
327 goaway(System::baseUrl());
332 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
335 if (! DBM::is_result($r)) {
340 $owner_uid = $r[0]['uid'];
341 $contact_id = $r[0]['id'];
344 if($type === 'reputation' && strlen($url)) {
345 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
352 if (DBM::is_result($r)) {
353 $reputation = $r[0]['rating'];
354 $text = $r[0]['reason'];
356 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
362 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
365 <rating>$reputation</rating>
366 <description>$text</description>
374 // Update the writable flag if it changed
375 logger('dfrn_poll: post request feed: ' . print_r($_POST,true),LOGGER_DATA);
376 if($dfrn_version >= 2.21) {
382 if($writable != $contact['writable']) {
383 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
390 header("Content-type: application/atom+xml");
391 $o = DFRN::feed($dfrn_id, $a->argv[1], $last_update, $direction);
398 function dfrn_poll_content(App $a) {
400 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
401 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
402 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
403 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
404 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
405 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
406 $perm = ((x($_GET,'perm')) ? $_GET['perm'] : 'r');
407 $quiet = ((x($_GET,'quiet')) ? true : false);
410 if(strpos($dfrn_id,':') == 1) {
411 $direction = intval(substr($dfrn_id,0,1));
412 $dfrn_id = substr($dfrn_id,2);
417 // initial communication from external contact
418 $hash = random_string();
422 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
424 if($type !== 'profile') {
425 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
426 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
429 intval(time() + 60 ),
437 if($type === 'profile')
438 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
440 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
444 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
445 $my_id = '1:' . $dfrn_id;
448 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
449 $my_id = '0:' . $dfrn_id;
452 goaway(System::baseUrl());
456 $nickname = $a->argv[1];
458 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
459 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
460 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
461 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
465 if (DBM::is_result($r)) {
469 $id_str = $my_id . '.' . mt_rand(1000,9999);
471 if(($r[0]['duplex'] && strlen($r[0]['pubkey'])) || (! strlen($r[0]['prvkey']))) {
472 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
473 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
476 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
477 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
480 $challenge = bin2hex($challenge);
481 $encrypted_id = bin2hex($encrypted_id);
489 if(($type === 'profile') && (strlen($sec))) {
493 if($dfrn_version < 2.2) {
494 $s = fetch_url($r[0]['poll']
495 . '?dfrn_id=' . $encrypted_id
496 . '&type=profile-check'
497 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
498 . '&challenge=' . $challenge
503 $s = post_url($r[0]['poll'], array(
504 'dfrn_id' => $encrypted_id,
505 'type' => 'profile-check',
506 'dfrn_version' => DFRN_PROTOCOL_VERSION,
507 'challenge' => $challenge,
512 $profile = ((DBM::is_result($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
514 switch($destination_url) {
516 $dest = System::baseUrl() . '/profile/' . $profile . '?f=&tab=profile';
519 $dest = System::baseUrl() . '/photos/' . $profile;
523 $dest = System::baseUrl() . '/profile/' . $profile;
526 $appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
527 $dest = $destination_url . $appendix;
531 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
533 if(strlen($s) && strstr($s,'<?xml')) {
535 $xml = parse_xml_string($s);
537 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
539 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
540 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
543 if(((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
544 $_SESSION['authenticated'] = 1;
545 if(! x($_SESSION,'remote'))
546 $_SESSION['remote'] = array();
547 $_SESSION['remote'][] = array('cid' => $r[0]['id'],'uid' => $r[0]['uid'],'url' => $r[0]['url']);
548 $_SESSION['visitor_id'] = $r[0]['id'];
549 $_SESSION['visitor_home'] = $r[0]['url'];
550 $_SESSION['visitor_visiting'] = $r[0]['uid'];
552 info( sprintf(t('%1$s welcomes %2$s'), $r[0]['username'] , $r[0]['name']) . EOL);
553 // Visitors get 1 day session.
554 $session_id = session_id();
555 $expire = time() + 86400;
556 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
570 header("Content-type: text/xml");
571 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
572 . '<dfrn_poll>' . "\r\n"
573 . "\t" . '<status>' . $status . '</status>' . "\r\n"
574 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
575 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
576 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
577 . '</dfrn_poll>' . "\r\n" ;