3 * @file mod/dfrn_poll.php
6 use Friendica\Core\Config;
7 use Friendica\Core\System;
8 use Friendica\Database\DBM;
9 use Friendica\Protocol\DFRN;
10 use Friendica\Protocol\OStatus;
12 require_once 'include/items.php';
13 require_once 'include/auth.php';
15 function dfrn_poll_init(App $a) {
16 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
17 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
18 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
19 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
20 $challenge = ((x($_GET,'challenge')) ? $_GET['challenge'] : '');
21 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
22 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
23 $perm = ((x($_GET,'perm')) ? $_GET['perm'] : 'r');
24 $quiet = ((x($_GET,'quiet')) ? true : false);
26 // Possibly it is an OStatus compatible server that requests a user feed
27 if (($a->argc > 1) && ($dfrn_id == '') && !strstr($_SERVER["HTTP_USER_AGENT"], 'Friendica')) {
28 $nickname = $a->argv[1];
29 header("Content-type: application/atom+xml");
30 echo OStatus::feed($a, $nickname, $last_update, 10);
37 if(strpos($dfrn_id,':') == 1) {
38 $direction = intval(substr($dfrn_id,0,1));
39 $dfrn_id = substr($dfrn_id,2);
44 if(($dfrn_id === '') && (! x($_POST,'dfrn_id'))) {
45 if((Config::get('system','block_public')) && (! local_user()) && (! remote_user())) {
46 http_status_exit(403);
51 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
55 http_status_exit(404);
57 $hidewall = ($r[0]['hidewall'] && !local_user());
59 $user = $r[0]['nickname'];
62 logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
63 header("Content-type: application/atom+xml");
64 echo DFRN::feed('', $user,$last_update, 0, $hidewall);
68 if(($type === 'profile') && (! strlen($sec))) {
73 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
77 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
78 $my_id = '1:' . $dfrn_id;
81 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
82 $my_id = '0:' . $dfrn_id;
85 goaway(System::baseUrl());
89 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
90 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
91 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
92 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
96 if (DBM::is_result($r)) {
98 $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
100 logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
104 $xml = parse_xml_string($s);
106 if((int) $xml->status == 1) {
107 $_SESSION['authenticated'] = 1;
108 if(! x($_SESSION,'remote'))
109 $_SESSION['remote'] = array();
111 $_SESSION['remote'][] = array('cid' => $r[0]['id'],'uid' => $r[0]['uid'],'url' => $r[0]['url']);
113 $_SESSION['visitor_id'] = $r[0]['id'];
114 $_SESSION['visitor_home'] = $r[0]['url'];
115 $_SESSION['visitor_handle'] = $r[0]['addr'];
116 $_SESSION['visitor_visiting'] = $r[0]['uid'];
118 info( sprintf(t('%1$s welcomes %2$s'), $r[0]['username'] , $r[0]['name']) . EOL);
119 // Visitors get 1 day session.
120 $session_id = session_id();
121 $expire = time() + 86400;
122 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
128 $profile = $r[0]['nickname'];
129 goaway((strlen($destination_url)) ? $destination_url : System::baseUrl() . '/profile/' . $profile);
131 goaway(System::baseUrl());
135 if($type === 'profile-check' && $dfrn_version < 2.2 ) {
137 if((strlen($challenge)) && (strlen($sec))) {
139 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
140 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
143 if (! DBM::is_result($r)) {
144 xml_status(3, 'No ticket');
147 $orig_id = $r[0]['dfrn_id'];
148 if(strpos($orig_id, ':'))
149 $orig_id = substr($orig_id,2);
151 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
154 if (! DBM::is_result($c)) {
155 xml_status(3, 'No profile');
159 $sent_dfrn_id = hex2bin($dfrn_id);
160 $challenge = hex2bin($challenge);
164 if(($contact['duplex']) && strlen($contact['prvkey'])) {
165 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
166 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
169 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
170 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
173 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
175 if(strpos($final_dfrn_id,':') == 1)
176 $final_dfrn_id = substr($final_dfrn_id,2);
178 if($final_dfrn_id != $orig_id) {
179 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
180 // did not decode properly - cannot trust this site
181 xml_status(3, 'Bad decryption');
184 header("Content-type: text/xml");
185 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
194 $dfrn_id = '0:' . $dfrn_id;
197 $dfrn_id = '1:' . $dfrn_id;
204 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
205 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
207 if (DBM::is_result($r)) {
209 return; // NOTREACHED
212 return; // NOTREACHED
220 function dfrn_poll_post(App $a) {
222 $dfrn_id = ((x($_POST,'dfrn_id')) ? $_POST['dfrn_id'] : '');
223 $challenge = ((x($_POST,'challenge')) ? $_POST['challenge'] : '');
224 $url = ((x($_POST,'url')) ? $_POST['url'] : '');
225 $sec = ((x($_POST,'sec')) ? $_POST['sec'] : '');
226 $ptype = ((x($_POST,'type')) ? $_POST['type'] : '');
227 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
228 $perm = ((x($_POST,'perm')) ? $_POST['perm'] : 'r');
230 if($ptype === 'profile-check') {
232 if((strlen($challenge)) && (strlen($sec))) {
234 logger('dfrn_poll: POST: profile-check');
236 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
237 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
240 if (! DBM::is_result($r)) {
241 xml_status(3, 'No ticket');
244 $orig_id = $r[0]['dfrn_id'];
245 if(strpos($orig_id, ':'))
246 $orig_id = substr($orig_id,2);
248 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
251 if (! DBM::is_result($c)) {
252 xml_status(3, 'No profile');
256 $sent_dfrn_id = hex2bin($dfrn_id);
257 $challenge = hex2bin($challenge);
261 if(($contact['duplex']) && strlen($contact['prvkey'])) {
262 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
263 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
266 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
267 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
270 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
272 if(strpos($final_dfrn_id,':') == 1)
273 $final_dfrn_id = substr($final_dfrn_id,2);
275 if($final_dfrn_id != $orig_id) {
276 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
277 // did not decode properly - cannot trust this site
278 xml_status(3, 'Bad decryption');
281 header("Content-type: text/xml");
282 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
290 if(strpos($dfrn_id,':') == 1) {
291 $direction = intval(substr($dfrn_id,0,1));
292 $dfrn_id = substr($dfrn_id,2);
296 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
301 if (! DBM::is_result($r)) {
305 $type = $r[0]['type'];
306 $last_update = $r[0]['last_update'];
308 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'",
317 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
321 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
322 $my_id = '1:' . $dfrn_id;
325 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
326 $my_id = '0:' . $dfrn_id;
329 goaway(System::baseUrl());
334 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
337 if (! DBM::is_result($r)) {
342 $owner_uid = $r[0]['uid'];
343 $contact_id = $r[0]['id'];
346 if($type === 'reputation' && strlen($url)) {
347 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
354 if (DBM::is_result($r)) {
355 $reputation = $r[0]['rating'];
356 $text = $r[0]['reason'];
358 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
364 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
367 <rating>$reputation</rating>
368 <description>$text</description>
376 // Update the writable flag if it changed
377 logger('dfrn_poll: post request feed: ' . print_r($_POST,true),LOGGER_DATA);
378 if($dfrn_version >= 2.21) {
384 if($writable != $contact['writable']) {
385 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
392 header("Content-type: application/atom+xml");
393 $o = DFRN::feed($dfrn_id, $a->argv[1], $last_update, $direction);
400 function dfrn_poll_content(App $a) {
402 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
403 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
404 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
405 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
406 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
407 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
408 $perm = ((x($_GET,'perm')) ? $_GET['perm'] : 'r');
409 $quiet = ((x($_GET,'quiet')) ? true : false);
412 if(strpos($dfrn_id,':') == 1) {
413 $direction = intval(substr($dfrn_id,0,1));
414 $dfrn_id = substr($dfrn_id,2);
419 // initial communication from external contact
420 $hash = random_string();
424 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
426 if($type !== 'profile') {
427 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
428 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
431 intval(time() + 60 ),
439 if($type === 'profile')
440 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
442 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
446 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
447 $my_id = '1:' . $dfrn_id;
450 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
451 $my_id = '0:' . $dfrn_id;
454 goaway(System::baseUrl());
458 $nickname = $a->argv[1];
460 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
461 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
462 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
463 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
467 if (DBM::is_result($r)) {
471 $id_str = $my_id . '.' . mt_rand(1000,9999);
473 if(($r[0]['duplex'] && strlen($r[0]['pubkey'])) || (! strlen($r[0]['prvkey']))) {
474 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
475 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
478 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
479 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
482 $challenge = bin2hex($challenge);
483 $encrypted_id = bin2hex($encrypted_id);
491 if(($type === 'profile') && (strlen($sec))) {
495 if($dfrn_version < 2.2) {
496 $s = fetch_url($r[0]['poll']
497 . '?dfrn_id=' . $encrypted_id
498 . '&type=profile-check'
499 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
500 . '&challenge=' . $challenge
505 $s = post_url($r[0]['poll'], array(
506 'dfrn_id' => $encrypted_id,
507 'type' => 'profile-check',
508 'dfrn_version' => DFRN_PROTOCOL_VERSION,
509 'challenge' => $challenge,
514 $profile = ((DBM::is_result($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
516 switch($destination_url) {
518 $dest = System::baseUrl() . '/profile/' . $profile . '?f=&tab=profile';
521 $dest = System::baseUrl() . '/photos/' . $profile;
525 $dest = System::baseUrl() . '/profile/' . $profile;
528 $appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
529 $dest = $destination_url . $appendix;
533 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
535 if(strlen($s) && strstr($s,'<?xml')) {
537 $xml = parse_xml_string($s);
539 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
541 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
542 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
545 if(((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
546 $_SESSION['authenticated'] = 1;
547 if(! x($_SESSION,'remote'))
548 $_SESSION['remote'] = array();
549 $_SESSION['remote'][] = array('cid' => $r[0]['id'],'uid' => $r[0]['uid'],'url' => $r[0]['url']);
550 $_SESSION['visitor_id'] = $r[0]['id'];
551 $_SESSION['visitor_home'] = $r[0]['url'];
552 $_SESSION['visitor_visiting'] = $r[0]['uid'];
554 info( sprintf(t('%1$s welcomes %2$s'), $r[0]['username'] , $r[0]['name']) . EOL);
555 // Visitors get 1 day session.
556 $session_id = session_id();
557 $expire = time() + 86400;
558 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
572 header("Content-type: text/xml");
573 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
574 . '<dfrn_poll>' . "\r\n"
575 . "\t" . '<status>' . $status . '</status>' . "\r\n"
576 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
577 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
578 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
579 . '</dfrn_poll>' . "\r\n" ;