4 * @file mod/dfrn_poll.php
8 use Friendica\Core\Config;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Logger;
11 use Friendica\Core\System;
12 use Friendica\Core\Session;
13 use Friendica\Database\DBA;
15 use Friendica\Module\Security\Login;
16 use Friendica\Protocol\DFRN;
17 use Friendica\Protocol\OStatus;
18 use Friendica\Util\Network;
19 use Friendica\Util\Strings;
20 use Friendica\Util\XML;
22 function dfrn_poll_init(App $a)
24 DI::auth()->withSession($a);
26 $dfrn_id = $_GET['dfrn_id'] ?? '';
27 $type = ($_GET['type'] ?? '') ?: 'data';
28 $last_update = $_GET['last_update'] ?? '';
29 $destination_url = $_GET['destination_url'] ?? '';
30 $challenge = $_GET['challenge'] ?? '';
31 $sec = $_GET['sec'] ?? '';
32 $dfrn_version = floatval(($_GET['dfrn_version'] ?? 0.0) ?: 2.0);
33 $quiet = !empty($_GET['quiet']);
35 // Possibly it is an OStatus compatible server that requests a user feed
36 $user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
37 if (($a->argc > 1) && ($dfrn_id == '') && !strstr($user_agent, 'Friendica')) {
38 $nickname = $a->argv[1];
39 header("Content-type: application/atom+xml");
40 echo OStatus::feed($nickname, $last_update, 10);
46 if (strpos($dfrn_id, ':') == 1) {
47 $direction = intval(substr($dfrn_id, 0, 1));
48 $dfrn_id = substr($dfrn_id, 2);
53 if (($dfrn_id === '') && empty($_POST['dfrn_id'])) {
54 if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
55 throw new \Friendica\Network\HTTPException\ForbiddenException();
60 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
61 DBA::escape($a->argv[1])
64 throw new \Friendica\Network\HTTPException\NotFoundException();
67 $hidewall = ($r[0]['hidewall'] && !local_user());
69 $user = $r[0]['nickname'];
72 Logger::log('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
73 header("Content-type: application/atom+xml");
74 echo DFRN::feed('', $user, $last_update, 0, $hidewall);
78 if (($type === 'profile') && (!strlen($sec))) {
82 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", DBA::escape($dfrn_id), DBA::escape($dfrn_id));
86 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
87 $my_id = '1:' . $dfrn_id;
90 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
91 $my_id = '0:' . $dfrn_id;
94 DI::baseUrl()->redirect();
98 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
99 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
100 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
101 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
102 DBA::escape($a->argv[1])
105 if (DBA::isResult($r)) {
106 $s = Network::fetchUrl($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
108 Logger::log("dfrn_poll: old profile returns " . $s, Logger::DATA);
111 $xml = XML::parseString($s);
113 if ((int)$xml->status === 1) {
114 $_SESSION['authenticated'] = 1;
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'];
119 $_SESSION['my_url'] = $r[0]['url'];
121 Session::setVisitorsContacts();
124 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
127 // Visitors get 1 day session.
128 $session_id = session_id();
129 $expire = time() + 86400;
130 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
131 DBA::escape($expire),
132 DBA::escape($session_id)
137 $profile = (count($r) > 0 && isset($r[0]['nickname']) ? $r[0]['nickname'] : '');
138 if (!empty($destination_url)) {
139 System::externalRedirect($destination_url);
141 DI::baseUrl()->redirect('profile/' . $profile);
144 DI::baseUrl()->redirect();
147 if ($type === 'profile-check' && $dfrn_version < 2.2) {
148 if ((strlen($challenge)) && (strlen($sec))) {
149 DBA::delete('profile_check', ["`expire` < ?", time()]);
150 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
153 if (!DBA::isResult($r)) {
154 System::xmlExit(3, 'No ticket');
158 $orig_id = $r[0]['dfrn_id'];
159 if (strpos($orig_id, ':')) {
160 $orig_id = substr($orig_id, 2);
163 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
166 if (!DBA::isResult($c)) {
167 System::xmlExit(3, 'No profile');
172 $sent_dfrn_id = hex2bin($dfrn_id);
173 $challenge = hex2bin($challenge);
177 if (($contact['duplex']) && strlen($contact['prvkey'])) {
178 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
179 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
181 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
182 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
185 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
187 if (strpos($final_dfrn_id, ':') == 1) {
188 $final_dfrn_id = substr($final_dfrn_id, 2);
191 if ($final_dfrn_id != $orig_id) {
192 Logger::log('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, Logger::DEBUG);
193 // did not decode properly - cannot trust this site
194 System::xmlExit(3, 'Bad decryption');
197 header("Content-type: text/xml");
198 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
203 switch ($direction) {
205 $dfrn_id = '0:' . $dfrn_id;
208 $dfrn_id = '1:' . $dfrn_id;
214 DBA::delete('profile_check', ["`expire` < ?", time()]);
215 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
216 DBA::escape($dfrn_id));
217 if (DBA::isResult($r)) {
219 return; // NOTREACHED
222 return; // NOTREACHED
227 function dfrn_poll_post(App $a)
229 $dfrn_id = $_POST['dfrn_id'] ?? '';
230 $challenge = $_POST['challenge'] ?? '';
231 $url = $_POST['url'] ?? '';
232 $sec = $_POST['sec'] ?? '';
233 $ptype = $_POST['type'] ?? '';
234 $perm = ($_POST['perm'] ?? '') ?: 'r';
235 $dfrn_version = floatval(($_GET['dfrn_version'] ?? 0.0) ?: 2.0);
237 if ($ptype === 'profile-check') {
238 if (strlen($challenge) && strlen($sec)) {
239 Logger::log('dfrn_poll: POST: profile-check');
241 DBA::delete('profile_check', ["`expire` < ?", time()]);
242 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
245 if (!DBA::isResult($r)) {
246 System::xmlExit(3, 'No ticket');
250 $orig_id = $r[0]['dfrn_id'];
251 if (strpos($orig_id, ':')) {
252 $orig_id = substr($orig_id, 2);
255 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
258 if (!DBA::isResult($c)) {
259 System::xmlExit(3, 'No profile');
264 $sent_dfrn_id = hex2bin($dfrn_id);
265 $challenge = hex2bin($challenge);
269 if ($contact['duplex'] && strlen($contact['prvkey'])) {
270 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
271 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
273 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
274 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
277 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
279 if (strpos($final_dfrn_id, ':') == 1) {
280 $final_dfrn_id = substr($final_dfrn_id, 2);
283 if ($final_dfrn_id != $orig_id) {
284 Logger::log('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, Logger::DEBUG);
285 // did not decode properly - cannot trust this site
286 System::xmlExit(3, 'Bad decryption');
289 header("Content-type: text/xml");
290 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
297 if (strpos($dfrn_id, ':') == 1) {
298 $direction = intval(substr($dfrn_id, 0, 1));
299 $dfrn_id = substr($dfrn_id, 2);
302 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
303 DBA::escape($dfrn_id),
304 DBA::escape($challenge)
307 if (!DBA::isResult($r)) {
311 $type = $r[0]['type'];
312 $last_update = $r[0]['last_update'];
314 DBA::delete('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge]);
317 switch ($direction) {
319 $sql_extra = sprintf(" AND `issued-id` = '%s' ", DBA::escape($dfrn_id));
322 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
325 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
328 DI::baseUrl()->redirect();
332 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
333 if (!DBA::isResult($r)) {
338 $owner_uid = $r[0]['uid'];
339 $contact_id = $r[0]['id'];
341 if ($type === 'reputation' && strlen($url)) {
342 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
349 if (DBA::isResult($r)) {
350 $reputation = $r[0]['rating'];
351 $text = $r[0]['reason'];
353 if ($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
359 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
362 <rating>$reputation</rating>
363 <description>$text</description>
369 // Update the writable flag if it changed
370 Logger::log('dfrn_poll: post request feed: ' . print_r($_POST, true), Logger::DATA);
371 if ($dfrn_version >= 2.21) {
372 if ($perm === 'rw') {
378 if ($writable != $contact['writable']) {
379 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
386 header("Content-type: application/atom+xml");
387 $o = DFRN::feed($dfrn_id, $a->argv[1], $last_update, $direction);
393 function dfrn_poll_content(App $a)
395 $dfrn_id = $_GET['dfrn_id'] ?? '';
396 $type = ($_GET['type'] ?? '') ?: 'data';
397 $last_update = $_GET['last_update'] ?? '';
398 $destination_url = $_GET['destination_url'] ?? '';
399 $sec = $_GET['sec'] ?? '';
400 $dfrn_version = floatval(($_GET['dfrn_version'] ?? 0.0) ?: 2.0);
401 $quiet = !empty($_GET['quiet']);
404 if (strpos($dfrn_id, ':') == 1) {
405 $direction = intval(substr($dfrn_id, 0, 1));
406 $dfrn_id = substr($dfrn_id, 2);
409 if ($dfrn_id != '') {
410 // initial communication from external contact
411 $hash = Strings::getRandomHex();
415 DBA::delete('challenge', ["`expire` < ?", time()]);
417 if ($type !== 'profile') {
418 q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
419 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
421 DBA::escape($dfrn_id),
422 intval(time() + 60 ),
424 DBA::escape($last_update)
429 switch ($direction) {
431 if ($type === 'profile') {
432 $sql_extra = sprintf(" AND (`dfrn-id` = '%s' OR `issued-id` = '%s') ", DBA::escape($dfrn_id), DBA::escape($dfrn_id));
434 $sql_extra = sprintf(" AND `issued-id` = '%s' ", DBA::escape($dfrn_id));
440 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
441 $my_id = '1:' . $dfrn_id;
444 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
445 $my_id = '0:' . $dfrn_id;
448 DI::baseUrl()->redirect();
452 $nickname = $a->argv[1];
454 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
455 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
456 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
457 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
458 DBA::escape($nickname)
460 if (DBA::isResult($r)) {
463 $id_str = $my_id . '.' . mt_rand(1000, 9999);
465 if (($r[0]['duplex'] && strlen($r[0]['pubkey'])) || !strlen($r[0]['prvkey'])) {
466 openssl_public_encrypt($hash, $challenge, $r[0]['pubkey']);
467 openssl_public_encrypt($id_str, $encrypted_id, $r[0]['pubkey']);
469 openssl_private_encrypt($hash, $challenge, $r[0]['prvkey']);
470 openssl_private_encrypt($id_str, $encrypted_id, $r[0]['prvkey']);
473 $challenge = bin2hex($challenge);
474 $encrypted_id = bin2hex($encrypted_id);
481 if (($type === 'profile') && (strlen($sec))) {
482 // heluecht: I don't know why we don't fail immediately when the user or contact hadn't been found.
483 // Since it doesn't make sense to continue from this point on, we now fail here. This should be safe.
484 if (!DBA::isResult($r)) {
485 throw new \Friendica\Network\HTTPException\NotFoundException();
489 if ($dfrn_version < 2.2) {
490 $s = Network::fetchUrl($r[0]['poll']
491 . '?dfrn_id=' . $encrypted_id
492 . '&type=profile-check'
493 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
494 . '&challenge=' . $challenge
498 $s = Network::post($r[0]['poll'], [
499 'dfrn_id' => $encrypted_id,
500 'type' => 'profile-check',
501 'dfrn_version' => DFRN_PROTOCOL_VERSION,
502 'challenge' => $challenge,
507 Logger::log("dfrn_poll: sec profile: " . $s, Logger::DATA);
509 if (strlen($s) && strstr($s, '<?xml')) {
510 $xml = XML::parseString($s);
512 Logger::log('dfrn_poll: profile: parsed xml: ' . print_r($xml, true), Logger::DATA);
514 Logger::log('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
515 Logger::log('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
517 if (((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
518 $_SESSION['authenticated'] = 1;
519 $_SESSION['visitor_id'] = $r[0]['id'];
520 $_SESSION['visitor_home'] = $r[0]['url'];
521 $_SESSION['visitor_visiting'] = $r[0]['uid'];
522 $_SESSION['my_url'] = $r[0]['url'];
524 Session::setVisitorsContacts();
527 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
530 // Visitors get 1 day session.
531 $session_id = session_id();
532 $expire = time() + 86400;
533 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
534 DBA::escape($expire),
535 DBA::escape($session_id)
540 $profile = ((DBA::isResult($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
542 switch ($destination_url) {
544 DI::baseUrl()->redirect('profile/' . $profile . '?tab=profile');
547 DI::baseUrl()->redirect('photos/' . $profile);
551 DI::baseUrl()->redirect('profile/' . $profile);
554 $appendix = (strstr($destination_url, '?') ? '&redir=1' : '?redir=1');
555 DI::baseUrl()->redirect($destination_url . $appendix);
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";