4 * @file mod/dfrn_poll.php
8 use Friendica\BaseObject;
9 use Friendica\App\Authentication;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\Logger;
13 use Friendica\Core\System;
14 use Friendica\Core\Session;
15 use Friendica\Database\DBA;
16 use Friendica\Module\Security\Login;
17 use Friendica\Protocol\DFRN;
18 use Friendica\Protocol\OStatus;
19 use Friendica\Util\Network;
20 use Friendica\Util\Strings;
21 use Friendica\Util\XML;
23 function dfrn_poll_init(App $a)
25 /** @var Authentication $authentication */
26 $authentication = BaseObject::getClass(Authentication::class);
27 $authentication->withSession($a);
29 $dfrn_id = $_GET['dfrn_id'] ?? '';
30 $type = ($_GET['type'] ?? '') ?: 'data';
31 $last_update = $_GET['last_update'] ?? '';
32 $destination_url = $_GET['destination_url'] ?? '';
33 $challenge = $_GET['challenge'] ?? '';
34 $sec = $_GET['sec'] ?? '';
35 $dfrn_version = floatval(($_GET['dfrn_version'] ?? 0.0) ?: 2.0);
36 $quiet = !empty($_GET['quiet']);
38 // Possibly it is an OStatus compatible server that requests a user feed
39 $user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
40 if (($a->argc > 1) && ($dfrn_id == '') && !strstr($user_agent, 'Friendica')) {
41 $nickname = $a->argv[1];
42 header("Content-type: application/atom+xml");
43 echo OStatus::feed($nickname, $last_update, 10);
49 if (strpos($dfrn_id, ':') == 1) {
50 $direction = intval(substr($dfrn_id, 0, 1));
51 $dfrn_id = substr($dfrn_id, 2);
56 if (($dfrn_id === '') && empty($_POST['dfrn_id'])) {
57 if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
58 throw new \Friendica\Network\HTTPException\ForbiddenException();
63 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
64 DBA::escape($a->argv[1])
67 throw new \Friendica\Network\HTTPException\NotFoundException();
70 $hidewall = ($r[0]['hidewall'] && !local_user());
72 $user = $r[0]['nickname'];
75 Logger::log('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
76 header("Content-type: application/atom+xml");
77 echo DFRN::feed('', $user, $last_update, 0, $hidewall);
81 if (($type === 'profile') && (!strlen($sec))) {
85 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", DBA::escape($dfrn_id), DBA::escape($dfrn_id));
89 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
90 $my_id = '1:' . $dfrn_id;
93 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
94 $my_id = '0:' . $dfrn_id;
97 $a->internalRedirect();
101 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
102 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
103 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
104 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
105 DBA::escape($a->argv[1])
108 if (DBA::isResult($r)) {
109 $s = Network::fetchUrl($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
111 Logger::log("dfrn_poll: old profile returns " . $s, Logger::DATA);
114 $xml = XML::parseString($s);
116 if ((int)$xml->status === 1) {
117 $_SESSION['authenticated'] = 1;
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'];
122 $_SESSION['my_url'] = $r[0]['url'];
124 Session::setVisitorsContacts();
127 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
130 // Visitors get 1 day session.
131 $session_id = session_id();
132 $expire = time() + 86400;
133 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
134 DBA::escape($expire),
135 DBA::escape($session_id)
140 $profile = (count($r) > 0 && isset($r[0]['nickname']) ? $r[0]['nickname'] : '');
141 if (!empty($destination_url)) {
142 System::externalRedirect($destination_url);
144 $a->internalRedirect('profile/' . $profile);
147 $a->internalRedirect();
150 if ($type === 'profile-check' && $dfrn_version < 2.2) {
151 if ((strlen($challenge)) && (strlen($sec))) {
152 DBA::delete('profile_check', ["`expire` < ?", time()]);
153 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
156 if (!DBA::isResult($r)) {
157 System::xmlExit(3, 'No ticket');
161 $orig_id = $r[0]['dfrn_id'];
162 if (strpos($orig_id, ':')) {
163 $orig_id = substr($orig_id, 2);
166 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
169 if (!DBA::isResult($c)) {
170 System::xmlExit(3, 'No profile');
175 $sent_dfrn_id = hex2bin($dfrn_id);
176 $challenge = hex2bin($challenge);
180 if (($contact['duplex']) && strlen($contact['prvkey'])) {
181 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
182 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
184 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
185 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
188 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
190 if (strpos($final_dfrn_id, ':') == 1) {
191 $final_dfrn_id = substr($final_dfrn_id, 2);
194 if ($final_dfrn_id != $orig_id) {
195 Logger::log('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, Logger::DEBUG);
196 // did not decode properly - cannot trust this site
197 System::xmlExit(3, 'Bad decryption');
200 header("Content-type: text/xml");
201 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
206 switch ($direction) {
208 $dfrn_id = '0:' . $dfrn_id;
211 $dfrn_id = '1:' . $dfrn_id;
217 DBA::delete('profile_check', ["`expire` < ?", time()]);
218 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
219 DBA::escape($dfrn_id));
220 if (DBA::isResult($r)) {
222 return; // NOTREACHED
225 return; // NOTREACHED
230 function dfrn_poll_post(App $a)
232 $dfrn_id = $_POST['dfrn_id'] ?? '';
233 $challenge = $_POST['challenge'] ?? '';
234 $url = $_POST['url'] ?? '';
235 $sec = $_POST['sec'] ?? '';
236 $ptype = $_POST['type'] ?? '';
237 $perm = ($_POST['perm'] ?? '') ?: 'r';
238 $dfrn_version = floatval(($_GET['dfrn_version'] ?? 0.0) ?: 2.0);
240 if ($ptype === 'profile-check') {
241 if (strlen($challenge) && strlen($sec)) {
242 Logger::log('dfrn_poll: POST: profile-check');
244 DBA::delete('profile_check', ["`expire` < ?", time()]);
245 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
248 if (!DBA::isResult($r)) {
249 System::xmlExit(3, 'No ticket');
253 $orig_id = $r[0]['dfrn_id'];
254 if (strpos($orig_id, ':')) {
255 $orig_id = substr($orig_id, 2);
258 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
261 if (!DBA::isResult($c)) {
262 System::xmlExit(3, 'No profile');
267 $sent_dfrn_id = hex2bin($dfrn_id);
268 $challenge = hex2bin($challenge);
272 if ($contact['duplex'] && strlen($contact['prvkey'])) {
273 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
274 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
276 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
277 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
280 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
282 if (strpos($final_dfrn_id, ':') == 1) {
283 $final_dfrn_id = substr($final_dfrn_id, 2);
286 if ($final_dfrn_id != $orig_id) {
287 Logger::log('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, Logger::DEBUG);
288 // did not decode properly - cannot trust this site
289 System::xmlExit(3, 'Bad decryption');
292 header("Content-type: text/xml");
293 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
300 if (strpos($dfrn_id, ':') == 1) {
301 $direction = intval(substr($dfrn_id, 0, 1));
302 $dfrn_id = substr($dfrn_id, 2);
305 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
306 DBA::escape($dfrn_id),
307 DBA::escape($challenge)
310 if (!DBA::isResult($r)) {
314 $type = $r[0]['type'];
315 $last_update = $r[0]['last_update'];
317 DBA::delete('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge]);
320 switch ($direction) {
322 $sql_extra = sprintf(" AND `issued-id` = '%s' ", DBA::escape($dfrn_id));
325 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
328 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
331 $a->internalRedirect();
335 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
336 if (!DBA::isResult($r)) {
341 $owner_uid = $r[0]['uid'];
342 $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 (DBA::isResult($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>
372 // Update the writable flag if it changed
373 Logger::log('dfrn_poll: post request feed: ' . print_r($_POST, true), Logger::DATA);
374 if ($dfrn_version >= 2.21) {
375 if ($perm === 'rw') {
381 if ($writable != $contact['writable']) {
382 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
389 header("Content-type: application/atom+xml");
390 $o = DFRN::feed($dfrn_id, $a->argv[1], $last_update, $direction);
396 function dfrn_poll_content(App $a)
398 $dfrn_id = $_GET['dfrn_id'] ?? '';
399 $type = ($_GET['type'] ?? '') ?: 'data';
400 $last_update = $_GET['last_update'] ?? '';
401 $destination_url = $_GET['destination_url'] ?? '';
402 $sec = $_GET['sec'] ?? '';
403 $dfrn_version = floatval(($_GET['dfrn_version'] ?? 0.0) ?: 2.0);
404 $quiet = !empty($_GET['quiet']);
407 if (strpos($dfrn_id, ':') == 1) {
408 $direction = intval(substr($dfrn_id, 0, 1));
409 $dfrn_id = substr($dfrn_id, 2);
412 if ($dfrn_id != '') {
413 // initial communication from external contact
414 $hash = Strings::getRandomHex();
418 DBA::delete('challenge', ["`expire` < ?", time()]);
420 if ($type !== 'profile') {
421 q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
422 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
424 DBA::escape($dfrn_id),
425 intval(time() + 60 ),
427 DBA::escape($last_update)
432 switch ($direction) {
434 if ($type === 'profile') {
435 $sql_extra = sprintf(" AND (`dfrn-id` = '%s' OR `issued-id` = '%s') ", DBA::escape($dfrn_id), DBA::escape($dfrn_id));
437 $sql_extra = sprintf(" AND `issued-id` = '%s' ", DBA::escape($dfrn_id));
443 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
444 $my_id = '1:' . $dfrn_id;
447 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
448 $my_id = '0:' . $dfrn_id;
451 $a->internalRedirect();
455 $nickname = $a->argv[1];
457 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
458 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
459 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
460 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
461 DBA::escape($nickname)
463 if (DBA::isResult($r)) {
466 $id_str = $my_id . '.' . mt_rand(1000, 9999);
468 if (($r[0]['duplex'] && strlen($r[0]['pubkey'])) || !strlen($r[0]['prvkey'])) {
469 openssl_public_encrypt($hash, $challenge, $r[0]['pubkey']);
470 openssl_public_encrypt($id_str, $encrypted_id, $r[0]['pubkey']);
472 openssl_private_encrypt($hash, $challenge, $r[0]['prvkey']);
473 openssl_private_encrypt($id_str, $encrypted_id, $r[0]['prvkey']);
476 $challenge = bin2hex($challenge);
477 $encrypted_id = bin2hex($encrypted_id);
484 if (($type === 'profile') && (strlen($sec))) {
485 // heluecht: I don't know why we don't fail immediately when the user or contact hadn't been found.
486 // Since it doesn't make sense to continue from this point on, we now fail here. This should be safe.
487 if (!DBA::isResult($r)) {
488 throw new \Friendica\Network\HTTPException\NotFoundException();
492 if ($dfrn_version < 2.2) {
493 $s = Network::fetchUrl($r[0]['poll']
494 . '?dfrn_id=' . $encrypted_id
495 . '&type=profile-check'
496 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
497 . '&challenge=' . $challenge
501 $s = Network::post($r[0]['poll'], [
502 'dfrn_id' => $encrypted_id,
503 'type' => 'profile-check',
504 'dfrn_version' => DFRN_PROTOCOL_VERSION,
505 'challenge' => $challenge,
510 Logger::log("dfrn_poll: sec profile: " . $s, Logger::DATA);
512 if (strlen($s) && strstr($s, '<?xml')) {
513 $xml = XML::parseString($s);
515 Logger::log('dfrn_poll: profile: parsed xml: ' . print_r($xml, true), Logger::DATA);
517 Logger::log('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
518 Logger::log('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
520 if (((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
521 $_SESSION['authenticated'] = 1;
522 $_SESSION['visitor_id'] = $r[0]['id'];
523 $_SESSION['visitor_home'] = $r[0]['url'];
524 $_SESSION['visitor_visiting'] = $r[0]['uid'];
525 $_SESSION['my_url'] = $r[0]['url'];
527 Session::setVisitorsContacts();
530 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
533 // Visitors get 1 day session.
534 $session_id = session_id();
535 $expire = time() + 86400;
536 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
537 DBA::escape($expire),
538 DBA::escape($session_id)
543 $profile = ((DBA::isResult($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
545 switch ($destination_url) {
547 $a->internalRedirect('profile/' . $profile . '?f=&tab=profile');
550 $a->internalRedirect('photos/' . $profile);
554 $a->internalRedirect('profile/' . $profile);
557 $appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
558 $a->redirect($destination_url . $appendix);
564 header("Content-type: text/xml");
565 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
566 . '<dfrn_poll>' . "\r\n"
567 . "\t" . '<status>' . $status . '</status>' . "\r\n"
568 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
569 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
570 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
571 . '</dfrn_poll>' . "\r\n";