4 * @file mod/dfrn_poll.php
7 use Friendica\Core\Config;
8 use Friendica\Core\L10n;
9 use Friendica\Core\System;
10 use Friendica\Database\DBM;
11 use Friendica\Module\Login;
12 use Friendica\Protocol\DFRN;
13 use Friendica\Protocol\OStatus;
14 use Friendica\Util\Network;
15 use Friendica\Util\XML;
17 require_once 'include/items.php';
19 function dfrn_poll_init(App $a)
23 $dfrn_id = defaults($_GET, 'dfrn_id' , '');
24 $type = defaults($_GET, 'type' , 'data');
25 $last_update = defaults($_GET, 'last_update' , '');
26 $destination_url = defaults($_GET, 'destination_url', '');
27 $challenge = defaults($_GET, 'challenge' , '');
28 $sec = defaults($_GET, 'sec' , '');
29 $dfrn_version = (float) defaults($_GET, 'dfrn_version' , 2.0);
30 $perm = defaults($_GET, 'perm' , 'r');
31 $quiet = x($_GET, 'quiet');
33 // Possibly it is an OStatus compatible server that requests a user feed
34 if (($a->argc > 1) && ($dfrn_id == '') && !strstr($_SERVER["HTTP_USER_AGENT"], 'Friendica')) {
35 $nickname = $a->argv[1];
36 header("Content-type: application/atom+xml");
37 echo OStatus::feed($nickname, $last_update, 10);
43 if (strpos($dfrn_id, ':') == 1) {
44 $direction = intval(substr($dfrn_id, 0, 1));
45 $dfrn_id = substr($dfrn_id, 2);
50 if (($dfrn_id === '') && (!x($_POST, 'dfrn_id'))) {
51 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
52 System::httpExit(403);
57 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
61 System::httpExit(404);
64 $hidewall = ($r[0]['hidewall'] && !local_user());
66 $user = $r[0]['nickname'];
69 logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
70 header("Content-type: application/atom+xml");
71 echo DFRN::feed('', $user, $last_update, 0, $hidewall);
75 if (($type === 'profile') && (!strlen($sec))) {
79 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
83 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
84 $my_id = '1:' . $dfrn_id;
87 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
88 $my_id = '0:' . $dfrn_id;
91 goaway(System::baseUrl());
95 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
96 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
97 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
98 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
102 if (DBM::is_result($r)) {
103 $s = Network::fetchUrl($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
105 logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
108 $xml = XML::parseString($s);
110 if ((int) $xml->status === 1) {
111 $_SESSION['authenticated'] = 1;
112 if (!x($_SESSION, 'remote')) {
113 $_SESSION['remote'] = [];
116 $_SESSION['remote'][] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']];
118 $_SESSION['visitor_id'] = $r[0]['id'];
119 $_SESSION['visitor_home'] = $r[0]['url'];
120 $_SESSION['visitor_handle'] = $r[0]['addr'];
121 $_SESSION['visitor_visiting'] = $r[0]['uid'];
123 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
126 // Visitors get 1 day session.
127 $session_id = session_id();
128 $expire = time() + 86400;
129 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
135 $profile = $r[0]['nickname'];
136 goaway((strlen($destination_url)) ? $destination_url : System::baseUrl() . '/profile/' . $profile);
138 goaway(System::baseUrl());
141 if ($type === 'profile-check' && $dfrn_version < 2.2) {
142 if ((strlen($challenge)) && (strlen($sec))) {
143 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
144 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
147 if (!DBM::is_result($r)) {
148 System::xmlExit(3, 'No ticket');
152 $orig_id = $r[0]['dfrn_id'];
153 if (strpos($orig_id, ':')) {
154 $orig_id = substr($orig_id, 2);
157 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
160 if (!DBM::is_result($c)) {
161 System::xmlExit(3, 'No profile');
166 $sent_dfrn_id = hex2bin($dfrn_id);
167 $challenge = hex2bin($challenge);
171 if (($contact['duplex']) && strlen($contact['prvkey'])) {
172 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
173 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
175 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
176 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
179 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
181 if (strpos($final_dfrn_id, ':') == 1) {
182 $final_dfrn_id = substr($final_dfrn_id, 2);
185 if ($final_dfrn_id != $orig_id) {
186 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
187 // did not decode properly - cannot trust this site
188 System::xmlExit(3, 'Bad decryption');
191 header("Content-type: text/xml");
192 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
197 switch ($direction) {
199 $dfrn_id = '0:' . $dfrn_id;
202 $dfrn_id = '1:' . $dfrn_id;
208 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
209 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
211 if (DBM::is_result($r)) {
213 return; // NOTREACHED
216 return; // NOTREACHED
221 function dfrn_poll_post(App $a)
223 $dfrn_id = x($_POST,'dfrn_id') ? $_POST['dfrn_id'] : '';
224 $challenge = x($_POST,'challenge') ? $_POST['challenge'] : '';
225 $url = x($_POST,'url') ? $_POST['url'] : '';
226 $sec = x($_POST,'sec') ? $_POST['sec'] : '';
227 $ptype = x($_POST,'type') ? $_POST['type'] : '';
228 $dfrn_version = x($_POST,'dfrn_version') ? (float) $_POST['dfrn_version'] : 2.0;
229 $perm = x($_POST,'perm') ? $_POST['perm'] : 'r';
231 if ($ptype === 'profile-check') {
232 if (strlen($challenge) && strlen($sec)) {
233 logger('dfrn_poll: POST: profile-check');
235 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
236 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
239 if (!DBM::is_result($r)) {
240 System::xmlExit(3, 'No ticket');
244 $orig_id = $r[0]['dfrn_id'];
245 if (strpos($orig_id, ':')) {
246 $orig_id = substr($orig_id, 2);
249 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
252 if (!DBM::is_result($c)) {
253 System::xmlExit(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']);
267 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
268 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
271 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
273 if (strpos($final_dfrn_id, ':') == 1) {
274 $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 System::xmlExit(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>";
291 if (strpos($dfrn_id, ':') == 1) {
292 $direction = intval(substr($dfrn_id, 0, 1));
293 $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'",
315 switch ($direction) {
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());
333 $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'];
342 if ($type === 'reputation' && strlen($url)) {
343 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
350 if (DBM::is_result($r)) {
351 $reputation = $r[0]['rating'];
352 $text = $r[0]['reason'];
354 if ($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
360 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
363 <rating>$reputation</rating>
364 <description>$text</description>
370 // Update the writable flag if it changed
371 logger('dfrn_poll: post request feed: ' . print_r($_POST, true), LOGGER_DATA);
372 if ($dfrn_version >= 2.21) {
373 if ($perm === 'rw') {
379 if ($writable != $contact['writable']) {
380 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
387 header("Content-type: application/atom+xml");
388 $o = DFRN::feed($dfrn_id, $a->argv[1], $last_update, $direction);
394 function dfrn_poll_content(App $a)
396 $dfrn_id = x($_GET,'dfrn_id') ? $_GET['dfrn_id'] : '';
397 $type = x($_GET,'type') ? $_GET['type'] : 'data';
398 $last_update = x($_GET,'last_update') ? $_GET['last_update'] : '';
399 $destination_url = x($_GET,'destination_url') ? $_GET['destination_url'] : '';
400 $sec = x($_GET,'sec') ? $_GET['sec'] : '';
401 $dfrn_version = x($_GET,'dfrn_version') ? (float) $_GET['dfrn_version'] : 2.0;
402 $perm = x($_GET,'perm') ? $_GET['perm'] : 'r';
403 $quiet = x($_GET,'quiet') ? true : false;
406 if (strpos($dfrn_id, ':') == 1) {
407 $direction = intval(substr($dfrn_id, 0, 1));
408 $dfrn_id = substr($dfrn_id, 2);
411 if ($dfrn_id != '') {
412 // initial communication from external contact
413 $hash = random_string();
417 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
419 if ($type !== 'profile') {
420 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
421 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
424 intval(time() + 60 ),
431 switch ($direction) {
433 if ($type === 'profile') {
434 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
436 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
442 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
443 $my_id = '1:' . $dfrn_id;
446 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
447 $my_id = '0:' . $dfrn_id;
450 goaway(System::baseUrl());
454 $nickname = $a->argv[1];
456 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
457 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
458 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
459 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
462 if (DBM::is_result($r)) {
465 $id_str = $my_id . '.' . mt_rand(1000, 9999);
467 if (($r[0]['duplex'] && strlen($r[0]['pubkey'])) || !strlen($r[0]['prvkey'])) {
468 openssl_public_encrypt($hash, $challenge, $r[0]['pubkey']);
469 openssl_public_encrypt($id_str, $encrypted_id, $r[0]['pubkey']);
471 openssl_private_encrypt($hash, $challenge, $r[0]['prvkey']);
472 openssl_private_encrypt($id_str, $encrypted_id, $r[0]['prvkey']);
475 $challenge = bin2hex($challenge);
476 $encrypted_id = bin2hex($encrypted_id);
483 if (($type === 'profile') && (strlen($sec))) {
485 if ($dfrn_version < 2.2) {
486 $s = Network::fetchUrl($r[0]['poll']
487 . '?dfrn_id=' . $encrypted_id
488 . '&type=profile-check'
489 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
490 . '&challenge=' . $challenge
494 $s = Network::post($r[0]['poll'], [
495 'dfrn_id' => $encrypted_id,
496 'type' => 'profile-check',
497 'dfrn_version' => DFRN_PROTOCOL_VERSION,
498 'challenge' => $challenge,
503 $profile = ((DBM::is_result($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
505 switch ($destination_url) {
507 $dest = System::baseUrl() . '/profile/' . $profile . '?f=&tab=profile';
510 $dest = System::baseUrl() . '/photos/' . $profile;
514 $dest = System::baseUrl() . '/profile/' . $profile;
517 $appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
518 $dest = $destination_url . $appendix;
522 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
524 if (strlen($s) && strstr($s, '<?xml')) {
525 $xml = XML::parseString($s);
527 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml, true), LOGGER_DATA);
529 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
530 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
532 if (((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
533 $_SESSION['authenticated'] = 1;
534 if (!x($_SESSION, 'remote')) {
535 $_SESSION['remote'] = [];
538 $_SESSION['remote'][] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']];
539 $_SESSION['visitor_id'] = $r[0]['id'];
540 $_SESSION['visitor_home'] = $r[0]['url'];
541 $_SESSION['visitor_visiting'] = $r[0]['uid'];
543 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
546 // Visitors get 1 day session.
547 $session_id = session_id();
548 $expire = time() + 86400;
549 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
561 header("Content-type: text/xml");
562 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
563 . '<dfrn_poll>' . "\r\n"
564 . "\t" . '<status>' . $status . '</status>' . "\r\n"
565 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
566 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
567 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
568 . '</dfrn_poll>' . "\r\n";