4 * @file mod/dfrn_poll.php
7 use Friendica\Core\Config;
8 use Friendica\Core\System;
9 use Friendica\Database\DBM;
10 use Friendica\Protocol\DFRN;
11 use Friendica\Protocol\OStatus;
13 require_once 'include/items.php';
14 require_once 'include/auth.php';
16 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($nickname, $last_update, 10);
38 if (strpos($dfrn_id, ':') == 1) {
39 $direction = intval(substr($dfrn_id, 0, 1));
40 $dfrn_id = substr($dfrn_id, 2);
45 if (($dfrn_id === '') && (!x($_POST, 'dfrn_id'))) {
46 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
47 http_status_exit(403);
52 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
56 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))) {
74 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
78 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
79 $my_id = '1:' . $dfrn_id;
82 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
83 $my_id = '0:' . $dfrn_id;
86 goaway(System::baseUrl());
90 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
91 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
92 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
93 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
97 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);
103 $xml = parse_xml_string($s);
105 if ((int) $xml->status === 1) {
106 $_SESSION['authenticated'] = 1;
107 if (!x($_SESSION, 'remote')) {
108 $_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);
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());
136 if ($type === 'profile-check' && $dfrn_version < 2.2) {
137 if ((strlen($challenge)) && (strlen($sec))) {
138 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
139 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
142 if (!DBM::is_result($r)) {
143 xml_status(3, 'No ticket');
147 $orig_id = $r[0]['dfrn_id'];
148 if (strpos($orig_id, ':')) {
149 $orig_id = substr($orig_id, 2);
152 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
155 if (!DBM::is_result($c)) {
156 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']);
170 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
171 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
174 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
176 if (strpos($final_dfrn_id, ':') == 1) {
177 $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>";
192 switch ($direction) {
194 $dfrn_id = '0:' . $dfrn_id;
197 $dfrn_id = '1:' . $dfrn_id;
203 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
204 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
206 if (DBM::is_result($r)) {
208 return; // NOTREACHED
211 return; // NOTREACHED
216 function dfrn_poll_post(App $a)
218 $dfrn_id = x($_POST,'dfrn_id') ? $_POST['dfrn_id'] : '';
219 $challenge = x($_POST,'challenge') ? $_POST['challenge'] : '';
220 $url = x($_POST,'url') ? $_POST['url'] : '';
221 $sec = x($_POST,'sec') ? $_POST['sec'] : '';
222 $ptype = x($_POST,'type') ? $_POST['type'] : '';
223 $dfrn_version = x($_POST,'dfrn_version') ? (float) $_POST['dfrn_version'] : 2.0;
224 $perm = x($_POST,'perm') ? $_POST['perm'] : 'r';
226 if ($ptype === 'profile-check') {
227 if (strlen($challenge) && strlen($sec)) {
228 logger('dfrn_poll: POST: profile-check');
230 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
231 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
234 if (!DBM::is_result($r)) {
235 xml_status(3, 'No ticket');
239 $orig_id = $r[0]['dfrn_id'];
240 if (strpos($orig_id, ':')) {
241 $orig_id = substr($orig_id, 2);
244 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
247 if (!DBM::is_result($c)) {
248 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']);
262 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
263 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
266 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
268 if (strpos($final_dfrn_id, ':') == 1) {
269 $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>";
286 if (strpos($dfrn_id, ':') == 1) {
287 $direction = intval(substr($dfrn_id, 0, 1));
288 $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'",
310 switch ($direction) {
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;
324 goaway(System::baseUrl());
328 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
329 if (!DBM::is_result($r)) {
334 $owner_uid = $r[0]['uid'];
335 $contact_id = $r[0]['id'];
337 if ($type === 'reputation' && strlen($url)) {
338 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
345 if (DBM::is_result($r)) {
346 $reputation = $r[0]['rating'];
347 $text = $r[0]['reason'];
349 if ($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
355 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
358 <rating>$reputation</rating>
359 <description>$text</description>
365 // Update the writable flag if it changed
366 logger('dfrn_poll: post request feed: ' . print_r($_POST, true), LOGGER_DATA);
367 if ($dfrn_version >= 2.21) {
368 if ($perm === 'rw') {
374 if ($writable != $contact['writable']) {
375 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
382 header("Content-type: application/atom+xml");
383 $o = DFRN::feed($dfrn_id, $a->argv[1], $last_update, $direction);
389 function dfrn_poll_content(App $a)
391 $dfrn_id = x($_GET,'dfrn_id') ? $_GET['dfrn_id'] : '';
392 $type = x($_GET,'type') ? $_GET['type'] : 'data';
393 $last_update = x($_GET,'last_update') ? $_GET['last_update'] : '';
394 $destination_url = x($_GET,'destination_url') ? $_GET['destination_url'] : '';
395 $sec = x($_GET,'sec') ? $_GET['sec'] : '';
396 $dfrn_version = x($_GET,'dfrn_version') ? (float) $_GET['dfrn_version'] : 2.0;
397 $perm = x($_GET,'perm') ? $_GET['perm'] : 'r';
398 $quiet = x($_GET,'quiet') ? true : false;
401 if (strpos($dfrn_id, ':') == 1) {
402 $direction = intval(substr($dfrn_id, 0, 1));
403 $dfrn_id = substr($dfrn_id, 2);
406 if ($dfrn_id != '') {
407 // initial communication from external contact
408 $hash = random_string();
412 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
414 if ($type !== 'profile') {
415 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
416 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
419 intval(time() + 60 ),
426 switch ($direction) {
428 if ($type === 'profile') {
429 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
431 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
437 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
438 $my_id = '1:' . $dfrn_id;
441 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
442 $my_id = '0:' . $dfrn_id;
445 goaway(System::baseUrl());
449 $nickname = $a->argv[1];
451 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
452 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
453 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
454 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
457 if (DBM::is_result($r)) {
460 $id_str = $my_id . '.' . mt_rand(1000, 9999);
462 if (($r[0]['duplex'] && strlen($r[0]['pubkey'])) || !strlen($r[0]['prvkey'])) {
463 openssl_public_encrypt($hash, $challenge, $r[0]['pubkey']);
464 openssl_public_encrypt($id_str, $encrypted_id, $r[0]['pubkey']);
466 openssl_private_encrypt($hash, $challenge, $r[0]['prvkey']);
467 openssl_private_encrypt($id_str, $encrypted_id, $r[0]['prvkey']);
470 $challenge = bin2hex($challenge);
471 $encrypted_id = bin2hex($encrypted_id);
478 if (($type === 'profile') && (strlen($sec))) {
480 if ($dfrn_version < 2.2) {
481 $s = fetch_url($r[0]['poll']
482 . '?dfrn_id=' . $encrypted_id
483 . '&type=profile-check'
484 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
485 . '&challenge=' . $challenge
489 $s = post_url($r[0]['poll'], array(
490 'dfrn_id' => $encrypted_id,
491 'type' => 'profile-check',
492 'dfrn_version' => DFRN_PROTOCOL_VERSION,
493 'challenge' => $challenge,
498 $profile = ((DBM::is_result($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
500 switch ($destination_url) {
502 $dest = System::baseUrl() . '/profile/' . $profile . '?f=&tab=profile';
505 $dest = System::baseUrl() . '/photos/' . $profile;
509 $dest = System::baseUrl() . '/profile/' . $profile;
512 $appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
513 $dest = $destination_url . $appendix;
517 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
519 if (strlen($s) && strstr($s, '<?xml')) {
520 $xml = parse_xml_string($s);
522 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml, true), LOGGER_DATA);
524 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
525 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
527 if (((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
528 $_SESSION['authenticated'] = 1;
529 if (!x($_SESSION, 'remote')) {
530 $_SESSION['remote'] = array();
533 $_SESSION['remote'][] = array('cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']);
534 $_SESSION['visitor_id'] = $r[0]['id'];
535 $_SESSION['visitor_home'] = $r[0]['url'];
536 $_SESSION['visitor_visiting'] = $r[0]['uid'];
538 info(sprintf(t('%1$s welcomes %2$s'), $r[0]['username'], $r[0]['name']) . EOL);
541 // Visitors get 1 day session.
542 $session_id = session_id();
543 $expire = time() + 86400;
544 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
556 header("Content-type: text/xml");
557 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
558 . '<dfrn_poll>' . "\r\n"
559 . "\t" . '<status>' . $status . '</status>' . "\r\n"
560 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
561 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
562 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
563 . '</dfrn_poll>' . "\r\n";