4 * @file mod/dfrn_poll.php
8 use Friendica\Core\Logger;
9 use Friendica\Core\System;
10 use Friendica\Core\Session;
11 use Friendica\Database\DBA;
13 use Friendica\Protocol\DFRN;
14 use Friendica\Protocol\OStatus;
15 use Friendica\Util\Network;
16 use Friendica\Util\Strings;
17 use Friendica\Util\XML;
19 function dfrn_poll_init(App $a)
21 DI::auth()->withSession($a);
23 $dfrn_id = $_GET['dfrn_id'] ?? '';
24 $type = ($_GET['type'] ?? '') ?: 'data';
25 $last_update = $_GET['last_update'] ?? '';
26 $destination_url = $_GET['destination_url'] ?? '';
27 $challenge = $_GET['challenge'] ?? '';
28 $sec = $_GET['sec'] ?? '';
29 $dfrn_version = floatval(($_GET['dfrn_version'] ?? 0.0) ?: 2.0);
30 $quiet = !empty($_GET['quiet']);
32 // Possibly it is an OStatus compatible server that requests a user feed
33 $user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
34 if (($a->argc > 1) && ($dfrn_id == '') && !strstr($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 === '') && empty($_POST['dfrn_id'])) {
51 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
52 throw new \Friendica\Network\HTTPException\ForbiddenException();
57 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
58 DBA::escape($a->argv[1])
61 throw new \Friendica\Network\HTTPException\NotFoundException();
64 $hidewall = ($r[0]['hidewall'] && !local_user());
66 $user = $r[0]['nickname'];
69 Logger::log('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' ) ", DBA::escape($dfrn_id), DBA::escape($dfrn_id));
83 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
84 $my_id = '1:' . $dfrn_id;
87 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
88 $my_id = '0:' . $dfrn_id;
91 DI::baseUrl()->redirect();
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",
99 DBA::escape($a->argv[1])
102 if (DBA::isResult($r)) {
103 $s = Network::fetchUrl($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
105 Logger::log("dfrn_poll: old profile returns " . $s, Logger::DATA);
108 $xml = XML::parseString($s);
110 if ((int)$xml->status === 1) {
111 $_SESSION['authenticated'] = 1;
112 $_SESSION['visitor_id'] = $r[0]['id'];
113 $_SESSION['visitor_home'] = $r[0]['url'];
114 $_SESSION['visitor_handle'] = $r[0]['addr'];
115 $_SESSION['visitor_visiting'] = $r[0]['uid'];
116 $_SESSION['my_url'] = $r[0]['url'];
118 Session::setVisitorsContacts();
121 info(DI::l10n()->t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
124 // Visitors get 1 day session.
125 $session_id = session_id();
126 $expire = time() + 86400;
127 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
128 DBA::escape($expire),
129 DBA::escape($session_id)
134 $profile = (count($r) > 0 && isset($r[0]['nickname']) ? $r[0]['nickname'] : '');
135 if (!empty($destination_url)) {
136 System::externalRedirect($destination_url);
138 DI::baseUrl()->redirect('profile/' . $profile);
141 DI::baseUrl()->redirect();
144 if ($type === 'profile-check' && $dfrn_version < 2.2) {
145 if ((strlen($challenge)) && (strlen($sec))) {
146 DBA::delete('profile_check', ["`expire` < ?", time()]);
147 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
150 if (!DBA::isResult($r)) {
151 System::xmlExit(3, 'No ticket');
155 $orig_id = $r[0]['dfrn_id'];
156 if (strpos($orig_id, ':')) {
157 $orig_id = substr($orig_id, 2);
160 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
163 if (!DBA::isResult($c)) {
164 System::xmlExit(3, 'No profile');
169 $sent_dfrn_id = hex2bin($dfrn_id);
170 $challenge = hex2bin($challenge);
174 if (($contact['duplex']) && strlen($contact['prvkey'])) {
175 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
176 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
178 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
179 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
182 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
184 if (strpos($final_dfrn_id, ':') == 1) {
185 $final_dfrn_id = substr($final_dfrn_id, 2);
188 if ($final_dfrn_id != $orig_id) {
189 Logger::log('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, Logger::DEBUG);
190 // did not decode properly - cannot trust this site
191 System::xmlExit(3, 'Bad decryption');
194 header("Content-type: text/xml");
195 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
200 switch ($direction) {
202 $dfrn_id = '0:' . $dfrn_id;
205 $dfrn_id = '1:' . $dfrn_id;
211 DBA::delete('profile_check', ["`expire` < ?", time()]);
212 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
213 DBA::escape($dfrn_id));
214 if (DBA::isResult($r)) {
216 return; // NOTREACHED
219 return; // NOTREACHED
224 function dfrn_poll_post(App $a)
226 $dfrn_id = $_POST['dfrn_id'] ?? '';
227 $challenge = $_POST['challenge'] ?? '';
228 $url = $_POST['url'] ?? '';
229 $sec = $_POST['sec'] ?? '';
230 $ptype = $_POST['type'] ?? '';
231 $perm = ($_POST['perm'] ?? '') ?: 'r';
232 $dfrn_version = floatval(($_GET['dfrn_version'] ?? 0.0) ?: 2.0);
234 if ($ptype === 'profile-check') {
235 if (strlen($challenge) && strlen($sec)) {
236 Logger::log('dfrn_poll: POST: profile-check');
238 DBA::delete('profile_check', ["`expire` < ?", time()]);
239 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
242 if (!DBA::isResult($r)) {
243 System::xmlExit(3, 'No ticket');
247 $orig_id = $r[0]['dfrn_id'];
248 if (strpos($orig_id, ':')) {
249 $orig_id = substr($orig_id, 2);
252 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
255 if (!DBA::isResult($c)) {
256 System::xmlExit(3, 'No profile');
261 $sent_dfrn_id = hex2bin($dfrn_id);
262 $challenge = hex2bin($challenge);
266 if ($contact['duplex'] && strlen($contact['prvkey'])) {
267 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
268 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
270 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
271 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
274 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
276 if (strpos($final_dfrn_id, ':') == 1) {
277 $final_dfrn_id = substr($final_dfrn_id, 2);
280 if ($final_dfrn_id != $orig_id) {
281 Logger::log('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, Logger::DEBUG);
282 // did not decode properly - cannot trust this site
283 System::xmlExit(3, 'Bad decryption');
286 header("Content-type: text/xml");
287 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
294 if (strpos($dfrn_id, ':') == 1) {
295 $direction = intval(substr($dfrn_id, 0, 1));
296 $dfrn_id = substr($dfrn_id, 2);
299 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
300 DBA::escape($dfrn_id),
301 DBA::escape($challenge)
304 if (!DBA::isResult($r)) {
308 $type = $r[0]['type'];
309 $last_update = $r[0]['last_update'];
311 DBA::delete('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge]);
314 switch ($direction) {
316 $sql_extra = sprintf(" AND `issued-id` = '%s' ", DBA::escape($dfrn_id));
319 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
322 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
325 DI::baseUrl()->redirect();
329 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
330 if (!DBA::isResult($r)) {
335 $owner_uid = $r[0]['uid'];
336 $contact_id = $r[0]['id'];
338 if ($type === 'reputation' && strlen($url)) {
339 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
346 if (DBA::isResult($r)) {
347 $reputation = $r[0]['rating'];
348 $text = $r[0]['reason'];
350 if ($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
356 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
359 <rating>$reputation</rating>
360 <description>$text</description>
366 // Update the writable flag if it changed
367 Logger::log('dfrn_poll: post request feed: ' . print_r($_POST, true), Logger::DATA);
368 if ($dfrn_version >= 2.21) {
369 if ($perm === 'rw') {
375 if ($writable != $contact['writable']) {
376 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
383 header("Content-type: application/atom+xml");
384 $o = DFRN::feed($dfrn_id, $a->argv[1], $last_update, $direction);
390 function dfrn_poll_content(App $a)
392 $dfrn_id = $_GET['dfrn_id'] ?? '';
393 $type = ($_GET['type'] ?? '') ?: 'data';
394 $last_update = $_GET['last_update'] ?? '';
395 $destination_url = $_GET['destination_url'] ?? '';
396 $sec = $_GET['sec'] ?? '';
397 $dfrn_version = floatval(($_GET['dfrn_version'] ?? 0.0) ?: 2.0);
398 $quiet = !empty($_GET['quiet']);
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 = Strings::getRandomHex();
412 DBA::delete('challenge', ["`expire` < ?", time()]);
414 if ($type !== 'profile') {
415 q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
416 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
418 DBA::escape($dfrn_id),
419 intval(time() + 60 ),
421 DBA::escape($last_update)
426 switch ($direction) {
428 if ($type === 'profile') {
429 $sql_extra = sprintf(" AND (`dfrn-id` = '%s' OR `issued-id` = '%s') ", DBA::escape($dfrn_id), DBA::escape($dfrn_id));
431 $sql_extra = sprintf(" AND `issued-id` = '%s' ", DBA::escape($dfrn_id));
437 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
438 $my_id = '1:' . $dfrn_id;
441 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
442 $my_id = '0:' . $dfrn_id;
445 DI::baseUrl()->redirect();
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",
455 DBA::escape($nickname)
457 if (DBA::isResult($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))) {
479 // heluecht: I don't know why we don't fail immediately when the user or contact hadn't been found.
480 // Since it doesn't make sense to continue from this point on, we now fail here. This should be safe.
481 if (!DBA::isResult($r)) {
482 throw new \Friendica\Network\HTTPException\NotFoundException();
486 if ($dfrn_version < 2.2) {
487 $s = Network::fetchUrl($r[0]['poll']
488 . '?dfrn_id=' . $encrypted_id
489 . '&type=profile-check'
490 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
491 . '&challenge=' . $challenge
495 $s = Network::post($r[0]['poll'], [
496 'dfrn_id' => $encrypted_id,
497 'type' => 'profile-check',
498 'dfrn_version' => DFRN_PROTOCOL_VERSION,
499 'challenge' => $challenge,
504 Logger::log("dfrn_poll: sec profile: " . $s, Logger::DATA);
506 if (strlen($s) && strstr($s, '<?xml')) {
507 $xml = XML::parseString($s);
509 Logger::log('dfrn_poll: profile: parsed xml: ' . print_r($xml, true), Logger::DATA);
511 Logger::log('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
512 Logger::log('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
514 if (((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
515 $_SESSION['authenticated'] = 1;
516 $_SESSION['visitor_id'] = $r[0]['id'];
517 $_SESSION['visitor_home'] = $r[0]['url'];
518 $_SESSION['visitor_visiting'] = $r[0]['uid'];
519 $_SESSION['my_url'] = $r[0]['url'];
521 Session::setVisitorsContacts();
524 info(DI::l10n()->t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
527 // Visitors get 1 day session.
528 $session_id = session_id();
529 $expire = time() + 86400;
530 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
531 DBA::escape($expire),
532 DBA::escape($session_id)
537 $profile = ((DBA::isResult($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
539 switch ($destination_url) {
541 DI::baseUrl()->redirect('profile/' . $profile . '/profile');
544 DI::baseUrl()->redirect('photos/' . $profile);
548 DI::baseUrl()->redirect('profile/' . $profile);
551 $appendix = (strstr($destination_url, '?') ? '&redir=1' : '?redir=1');
552 DI::baseUrl()->redirect($destination_url . $appendix);
558 header("Content-type: text/xml");
559 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
560 . '<dfrn_poll>' . "\r\n"
561 . "\t" . '<status>' . $status . '</status>' . "\r\n"
562 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
563 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
564 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
565 . '</dfrn_poll>' . "\r\n";