3 * @file mod/dfrn_poll.php
6 use Friendica\Core\Config;
7 use Friendica\Core\System;
8 use Friendica\Database\DBM;
9 use Friendica\Module\Login;
10 use Friendica\Protocol\DFRN;
11 use Friendica\Protocol\OStatus;
13 require_once 'include/items.php';
15 function dfrn_poll_init(App $a) {
18 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
19 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
20 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
21 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
22 $challenge = ((x($_GET,'challenge')) ? $_GET['challenge'] : '');
23 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
24 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
25 $perm = ((x($_GET,'perm')) ? $_GET['perm'] : 'r');
26 $quiet = ((x($_GET,'quiet')) ? true : false);
28 // Possibly it is an OStatus compatible server that requests a user feed
29 if (($a->argc > 1) && ($dfrn_id == '') && !strstr($_SERVER["HTTP_USER_AGENT"], 'Friendica')) {
30 $nickname = $a->argv[1];
31 header("Content-type: application/atom+xml");
32 echo OStatus::feed($a, $nickname, $last_update, 10);
39 if(strpos($dfrn_id,':') == 1) {
40 $direction = intval(substr($dfrn_id,0,1));
41 $dfrn_id = substr($dfrn_id,2);
46 if(($dfrn_id === '') && (! x($_POST,'dfrn_id'))) {
47 if((Config::get('system','block_public')) && (! local_user()) && (! remote_user())) {
48 http_status_exit(403);
53 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
57 http_status_exit(404);
59 $hidewall = ($r[0]['hidewall'] && !local_user());
61 $user = $r[0]['nickname'];
64 logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
65 header("Content-type: application/atom+xml");
66 echo DFRN::feed('', $user,$last_update, 0, $hidewall);
70 if(($type === 'profile') && (! strlen($sec))) {
75 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
79 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
80 $my_id = '1:' . $dfrn_id;
83 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
84 $my_id = '0:' . $dfrn_id;
87 goaway(System::baseUrl());
91 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
92 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
93 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
94 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
98 if (DBM::is_result($r)) {
100 $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
102 logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
106 $xml = parse_xml_string($s);
108 if((int) $xml->status == 1) {
109 $_SESSION['authenticated'] = 1;
110 if(! x($_SESSION,'remote'))
111 $_SESSION['remote'] = array();
113 $_SESSION['remote'][] = array('cid' => $r[0]['id'],'uid' => $r[0]['uid'],'url' => $r[0]['url']);
115 $_SESSION['visitor_id'] = $r[0]['id'];
116 $_SESSION['visitor_home'] = $r[0]['url'];
117 $_SESSION['visitor_handle'] = $r[0]['addr'];
118 $_SESSION['visitor_visiting'] = $r[0]['uid'];
120 info( sprintf(t('%1$s welcomes %2$s'), $r[0]['username'] , $r[0]['name']) . EOL);
121 // Visitors get 1 day session.
122 $session_id = session_id();
123 $expire = time() + 86400;
124 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
130 $profile = $r[0]['nickname'];
131 goaway((strlen($destination_url)) ? $destination_url : System::baseUrl() . '/profile/' . $profile);
133 goaway(System::baseUrl());
137 if($type === 'profile-check' && $dfrn_version < 2.2 ) {
139 if((strlen($challenge)) && (strlen($sec))) {
141 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
142 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
145 if (! DBM::is_result($r)) {
146 xml_status(3, 'No ticket');
149 $orig_id = $r[0]['dfrn_id'];
150 if(strpos($orig_id, ':'))
151 $orig_id = substr($orig_id,2);
153 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
156 if (! DBM::is_result($c)) {
157 xml_status(3, 'No profile');
161 $sent_dfrn_id = hex2bin($dfrn_id);
162 $challenge = hex2bin($challenge);
166 if(($contact['duplex']) && strlen($contact['prvkey'])) {
167 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
168 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
171 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
172 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
175 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
177 if(strpos($final_dfrn_id,':') == 1)
178 $final_dfrn_id = substr($final_dfrn_id,2);
180 if($final_dfrn_id != $orig_id) {
181 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
182 // did not decode properly - cannot trust this site
183 xml_status(3, 'Bad decryption');
186 header("Content-type: text/xml");
187 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
196 $dfrn_id = '0:' . $dfrn_id;
199 $dfrn_id = '1:' . $dfrn_id;
206 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
207 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
209 if (DBM::is_result($r)) {
211 return; // NOTREACHED
214 return; // NOTREACHED
222 function dfrn_poll_post(App $a) {
224 $dfrn_id = ((x($_POST,'dfrn_id')) ? $_POST['dfrn_id'] : '');
225 $challenge = ((x($_POST,'challenge')) ? $_POST['challenge'] : '');
226 $url = ((x($_POST,'url')) ? $_POST['url'] : '');
227 $sec = ((x($_POST,'sec')) ? $_POST['sec'] : '');
228 $ptype = ((x($_POST,'type')) ? $_POST['type'] : '');
229 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
230 $perm = ((x($_POST,'perm')) ? $_POST['perm'] : 'r');
232 if($ptype === 'profile-check') {
234 if((strlen($challenge)) && (strlen($sec))) {
236 logger('dfrn_poll: POST: profile-check');
238 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
239 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
242 if (! DBM::is_result($r)) {
243 xml_status(3, 'No ticket');
246 $orig_id = $r[0]['dfrn_id'];
247 if(strpos($orig_id, ':'))
248 $orig_id = substr($orig_id,2);
250 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
253 if (! DBM::is_result($c)) {
254 xml_status(3, 'No profile');
258 $sent_dfrn_id = hex2bin($dfrn_id);
259 $challenge = hex2bin($challenge);
263 if(($contact['duplex']) && strlen($contact['prvkey'])) {
264 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
265 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
268 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
269 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
272 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
274 if(strpos($final_dfrn_id,':') == 1)
275 $final_dfrn_id = substr($final_dfrn_id,2);
277 if($final_dfrn_id != $orig_id) {
278 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
279 // did not decode properly - cannot trust this site
280 xml_status(3, 'Bad decryption');
283 header("Content-type: text/xml");
284 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
292 if(strpos($dfrn_id,':') == 1) {
293 $direction = intval(substr($dfrn_id,0,1));
294 $dfrn_id = substr($dfrn_id,2);
298 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
303 if (! DBM::is_result($r)) {
307 $type = $r[0]['type'];
308 $last_update = $r[0]['last_update'];
310 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'",
319 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
323 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
324 $my_id = '1:' . $dfrn_id;
327 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
328 $my_id = '0:' . $dfrn_id;
331 goaway(System::baseUrl());
336 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
339 if (! DBM::is_result($r)) {
344 $owner_uid = $r[0]['uid'];
345 $contact_id = $r[0]['id'];
348 if($type === 'reputation' && strlen($url)) {
349 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
356 if (DBM::is_result($r)) {
357 $reputation = $r[0]['rating'];
358 $text = $r[0]['reason'];
360 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
366 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
369 <rating>$reputation</rating>
370 <description>$text</description>
378 // Update the writable flag if it changed
379 logger('dfrn_poll: post request feed: ' . print_r($_POST,true),LOGGER_DATA);
380 if($dfrn_version >= 2.21) {
386 if($writable != $contact['writable']) {
387 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
394 header("Content-type: application/atom+xml");
395 $o = DFRN::feed($dfrn_id, $a->argv[1], $last_update, $direction);
402 function dfrn_poll_content(App $a) {
404 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
405 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
406 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
407 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
408 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
409 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
410 $perm = ((x($_GET,'perm')) ? $_GET['perm'] : 'r');
411 $quiet = ((x($_GET,'quiet')) ? true : false);
414 if(strpos($dfrn_id,':') == 1) {
415 $direction = intval(substr($dfrn_id,0,1));
416 $dfrn_id = substr($dfrn_id,2);
421 // initial communication from external contact
422 $hash = random_string();
426 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
428 if($type !== 'profile') {
429 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
430 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
433 intval(time() + 60 ),
441 if($type === 'profile')
442 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
444 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
448 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
449 $my_id = '1:' . $dfrn_id;
452 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
453 $my_id = '0:' . $dfrn_id;
456 goaway(System::baseUrl());
460 $nickname = $a->argv[1];
462 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
463 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
464 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
465 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
469 if (DBM::is_result($r)) {
473 $id_str = $my_id . '.' . mt_rand(1000,9999);
475 if(($r[0]['duplex'] && strlen($r[0]['pubkey'])) || (! strlen($r[0]['prvkey']))) {
476 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
477 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
480 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
481 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
484 $challenge = bin2hex($challenge);
485 $encrypted_id = bin2hex($encrypted_id);
493 if(($type === 'profile') && (strlen($sec))) {
497 if($dfrn_version < 2.2) {
498 $s = fetch_url($r[0]['poll']
499 . '?dfrn_id=' . $encrypted_id
500 . '&type=profile-check'
501 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
502 . '&challenge=' . $challenge
507 $s = post_url($r[0]['poll'], array(
508 'dfrn_id' => $encrypted_id,
509 'type' => 'profile-check',
510 'dfrn_version' => DFRN_PROTOCOL_VERSION,
511 'challenge' => $challenge,
516 $profile = ((DBM::is_result($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
518 switch($destination_url) {
520 $dest = System::baseUrl() . '/profile/' . $profile . '?f=&tab=profile';
523 $dest = System::baseUrl() . '/photos/' . $profile;
527 $dest = System::baseUrl() . '/profile/' . $profile;
530 $appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
531 $dest = $destination_url . $appendix;
535 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
537 if(strlen($s) && strstr($s,'<?xml')) {
539 $xml = parse_xml_string($s);
541 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
543 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
544 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
547 if(((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
548 $_SESSION['authenticated'] = 1;
549 if(! x($_SESSION,'remote'))
550 $_SESSION['remote'] = array();
551 $_SESSION['remote'][] = array('cid' => $r[0]['id'],'uid' => $r[0]['uid'],'url' => $r[0]['url']);
552 $_SESSION['visitor_id'] = $r[0]['id'];
553 $_SESSION['visitor_home'] = $r[0]['url'];
554 $_SESSION['visitor_visiting'] = $r[0]['uid'];
556 info( sprintf(t('%1$s welcomes %2$s'), $r[0]['username'] , $r[0]['name']) . EOL);
557 // Visitors get 1 day session.
558 $session_id = session_id();
559 $expire = time() + 86400;
560 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
574 header("Content-type: text/xml");
575 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
576 . '<dfrn_poll>' . "\r\n"
577 . "\t" . '<status>' . $status . '</status>' . "\r\n"
578 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
579 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
580 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
581 . '</dfrn_poll>' . "\r\n" ;