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;
14 use Friendica\Module\Login;
15 use Friendica\Protocol\DFRN;
16 use Friendica\Protocol\OStatus;
17 use Friendica\Util\Network;
18 use Friendica\Util\Strings;
19 use Friendica\Util\XML;
21 function dfrn_poll_init(App $a)
25 $dfrn_id = defaults($_GET, 'dfrn_id' , '');
26 $type = defaults($_GET, 'type' , 'data');
27 $last_update = defaults($_GET, 'last_update' , '');
28 $destination_url = defaults($_GET, 'destination_url', '');
29 $challenge = defaults($_GET, 'challenge' , '');
30 $sec = defaults($_GET, 'sec' , '');
31 $dfrn_version = (float) defaults($_GET, 'dfrn_version' , 2.0);
32 $quiet = !empty($_GET['quiet']);
34 // Possibly it is an OStatus compatible server that requests a user feed
35 $user_agent = defaults($_SERVER, 'HTTP_USER_AGENT', '');
36 if (($a->argc > 1) && ($dfrn_id == '') && !strstr($user_agent, 'Friendica')) {
37 $nickname = $a->argv[1];
38 header("Content-type: application/atom+xml");
39 echo OStatus::feed($nickname, $last_update, 10);
45 if (strpos($dfrn_id, ':') == 1) {
46 $direction = intval(substr($dfrn_id, 0, 1));
47 $dfrn_id = substr($dfrn_id, 2);
52 if (($dfrn_id === '') && empty($_POST['dfrn_id'])) {
53 if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
54 throw new \Friendica\Network\HTTPException\ForbiddenException();
59 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
60 DBA::escape($a->argv[1])
63 throw new \Friendica\Network\HTTPException\NotFoundException();
66 $hidewall = ($r[0]['hidewall'] && !local_user());
68 $user = $r[0]['nickname'];
71 Logger::log('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
72 header("Content-type: application/atom+xml");
73 echo DFRN::feed('', $user, $last_update, 0, $hidewall);
77 if (($type === 'profile') && (!strlen($sec))) {
81 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", DBA::escape($dfrn_id), DBA::escape($dfrn_id));
85 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
86 $my_id = '1:' . $dfrn_id;
89 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
90 $my_id = '0:' . $dfrn_id;
93 $a->internalRedirect();
97 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
98 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
99 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
100 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
101 DBA::escape($a->argv[1])
104 if (DBA::isResult($r)) {
105 $s = Network::fetchUrl($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
107 Logger::log("dfrn_poll: old profile returns " . $s, Logger::DATA);
110 $xml = XML::parseString($s);
112 if ((int)$xml->status === 1) {
113 $_SESSION['authenticated'] = 1;
114 if (empty($_SESSION['remote'])) {
115 $_SESSION['remote'] = [];
118 $_SESSION['remote'][$r[0]['uid']] = $r[0]['id'];
120 $_SESSION['visitor_id'] = $r[0]['id'];
121 $_SESSION['visitor_home'] = $r[0]['url'];
122 $_SESSION['visitor_handle'] = $r[0]['addr'];
123 $_SESSION['visitor_visiting'] = $r[0]['uid'];
124 $_SESSION['my_url'] = $r[0]['url'];
126 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
129 // Visitors get 1 day session.
130 $session_id = session_id();
131 $expire = time() + 86400;
132 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
133 DBA::escape($expire),
134 DBA::escape($session_id)
139 $profile = (count($r) > 0 && isset($r[0]['nickname']) ? $r[0]['nickname'] : '');
140 if (!empty($destination_url)) {
141 System::externalRedirect($destination_url);
143 $a->internalRedirect('profile/' . $profile);
146 $a->internalRedirect();
149 if ($type === 'profile-check' && $dfrn_version < 2.2) {
150 if ((strlen($challenge)) && (strlen($sec))) {
151 DBA::delete('profile_check', ["`expire` < ?", time()]);
152 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
155 if (!DBA::isResult($r)) {
156 System::xmlExit(3, 'No ticket');
160 $orig_id = $r[0]['dfrn_id'];
161 if (strpos($orig_id, ':')) {
162 $orig_id = substr($orig_id, 2);
165 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
168 if (!DBA::isResult($c)) {
169 System::xmlExit(3, 'No profile');
174 $sent_dfrn_id = hex2bin($dfrn_id);
175 $challenge = hex2bin($challenge);
179 if (($contact['duplex']) && strlen($contact['prvkey'])) {
180 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
181 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
183 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
184 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
187 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
189 if (strpos($final_dfrn_id, ':') == 1) {
190 $final_dfrn_id = substr($final_dfrn_id, 2);
193 if ($final_dfrn_id != $orig_id) {
194 Logger::log('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, Logger::DEBUG);
195 // did not decode properly - cannot trust this site
196 System::xmlExit(3, 'Bad decryption');
199 header("Content-type: text/xml");
200 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
205 switch ($direction) {
207 $dfrn_id = '0:' . $dfrn_id;
210 $dfrn_id = '1:' . $dfrn_id;
216 DBA::delete('profile_check', ["`expire` < ?", time()]);
217 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
218 DBA::escape($dfrn_id));
219 if (DBA::isResult($r)) {
221 return; // NOTREACHED
224 return; // NOTREACHED
229 function dfrn_poll_post(App $a)
231 $dfrn_id = defaults($_POST, 'dfrn_id' , '');
232 $challenge = defaults($_POST, 'challenge', '');
233 $url = defaults($_POST, 'url' , '');
234 $sec = defaults($_POST, 'sec' , '');
235 $ptype = defaults($_POST, 'type' , '');
236 $perm = defaults($_POST, 'perm' , 'r');
237 $dfrn_version = !empty($_POST['dfrn_version']) ? (float) $_POST['dfrn_version'] : 2.0;
239 if ($ptype === 'profile-check') {
240 if (strlen($challenge) && strlen($sec)) {
241 Logger::log('dfrn_poll: POST: profile-check');
243 DBA::delete('profile_check', ["`expire` < ?", time()]);
244 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
247 if (!DBA::isResult($r)) {
248 System::xmlExit(3, 'No ticket');
252 $orig_id = $r[0]['dfrn_id'];
253 if (strpos($orig_id, ':')) {
254 $orig_id = substr($orig_id, 2);
257 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
260 if (!DBA::isResult($c)) {
261 System::xmlExit(3, 'No profile');
266 $sent_dfrn_id = hex2bin($dfrn_id);
267 $challenge = hex2bin($challenge);
271 if ($contact['duplex'] && strlen($contact['prvkey'])) {
272 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
273 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
275 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
276 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
279 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
281 if (strpos($final_dfrn_id, ':') == 1) {
282 $final_dfrn_id = substr($final_dfrn_id, 2);
285 if ($final_dfrn_id != $orig_id) {
286 Logger::log('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, Logger::DEBUG);
287 // did not decode properly - cannot trust this site
288 System::xmlExit(3, 'Bad decryption');
291 header("Content-type: text/xml");
292 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
299 if (strpos($dfrn_id, ':') == 1) {
300 $direction = intval(substr($dfrn_id, 0, 1));
301 $dfrn_id = substr($dfrn_id, 2);
304 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
305 DBA::escape($dfrn_id),
306 DBA::escape($challenge)
309 if (!DBA::isResult($r)) {
313 $type = $r[0]['type'];
314 $last_update = $r[0]['last_update'];
316 DBA::delete('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge]);
319 switch ($direction) {
321 $sql_extra = sprintf(" AND `issued-id` = '%s' ", DBA::escape($dfrn_id));
324 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
327 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
330 $a->internalRedirect();
334 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
335 if (!DBA::isResult($r)) {
340 $owner_uid = $r[0]['uid'];
341 $contact_id = $r[0]['id'];
343 if ($type === 'reputation' && strlen($url)) {
344 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
351 if (DBA::isResult($r)) {
352 $reputation = $r[0]['rating'];
353 $text = $r[0]['reason'];
355 if ($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
361 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
364 <rating>$reputation</rating>
365 <description>$text</description>
371 // Update the writable flag if it changed
372 Logger::log('dfrn_poll: post request feed: ' . print_r($_POST, true), Logger::DATA);
373 if ($dfrn_version >= 2.21) {
374 if ($perm === 'rw') {
380 if ($writable != $contact['writable']) {
381 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
388 header("Content-type: application/atom+xml");
389 $o = DFRN::feed($dfrn_id, $a->argv[1], $last_update, $direction);
395 function dfrn_poll_content(App $a)
397 $dfrn_id = defaults($_GET, 'dfrn_id' , '');
398 $type = defaults($_GET, 'type' , 'data');
399 $last_update = defaults($_GET, 'last_update' , '');
400 $destination_url = defaults($_GET, 'destination_url', '');
401 $sec = defaults($_GET, 'sec' , '');
402 $dfrn_version = !empty($_GET['dfrn_version']) ? (float) $_GET['dfrn_version'] : 2.0;
403 $quiet = !empty($_GET['quiet']);
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 = Strings::getRandomHex();
417 DBA::delete('challenge', ["`expire` < ?", time()]);
419 if ($type !== 'profile') {
420 q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
421 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
423 DBA::escape($dfrn_id),
424 intval(time() + 60 ),
426 DBA::escape($last_update)
431 switch ($direction) {
433 if ($type === 'profile') {
434 $sql_extra = sprintf(" AND (`dfrn-id` = '%s' OR `issued-id` = '%s') ", DBA::escape($dfrn_id), DBA::escape($dfrn_id));
436 $sql_extra = sprintf(" AND `issued-id` = '%s' ", DBA::escape($dfrn_id));
442 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
443 $my_id = '1:' . $dfrn_id;
446 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
447 $my_id = '0:' . $dfrn_id;
450 $a->internalRedirect();
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",
460 DBA::escape($nickname)
462 if (DBA::isResult($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))) {
484 // heluecht: I don't know why we don't fail immediately when the user or contact hadn't been found.
485 // Since it doesn't make sense to continue from this point on, we now fail here. This should be safe.
486 if (!DBA::isResult($r)) {
487 throw new \Friendica\Network\HTTPException\NotFoundException();
491 if ($dfrn_version < 2.2) {
492 $s = Network::fetchUrl($r[0]['poll']
493 . '?dfrn_id=' . $encrypted_id
494 . '&type=profile-check'
495 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
496 . '&challenge=' . $challenge
500 $s = Network::post($r[0]['poll'], [
501 'dfrn_id' => $encrypted_id,
502 'type' => 'profile-check',
503 'dfrn_version' => DFRN_PROTOCOL_VERSION,
504 'challenge' => $challenge,
509 Logger::log("dfrn_poll: sec profile: " . $s, Logger::DATA);
511 if (strlen($s) && strstr($s, '<?xml')) {
512 $xml = XML::parseString($s);
514 Logger::log('dfrn_poll: profile: parsed xml: ' . print_r($xml, true), Logger::DATA);
516 Logger::log('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
517 Logger::log('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
519 if (((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
520 $_SESSION['authenticated'] = 1;
521 if (empty($_SESSION['remote'])) {
522 $_SESSION['remote'] = [];
525 $_SESSION['remote'][$r[0]['uid']] = $r[0]['id'];
527 $_SESSION['visitor_id'] = $r[0]['id'];
528 $_SESSION['visitor_home'] = $r[0]['url'];
529 $_SESSION['visitor_visiting'] = $r[0]['uid'];
530 $_SESSION['my_url'] = $r[0]['url'];
532 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
535 // Visitors get 1 day session.
536 $session_id = session_id();
537 $expire = time() + 86400;
538 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
539 DBA::escape($expire),
540 DBA::escape($session_id)
545 $profile = ((DBA::isResult($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
547 switch ($destination_url) {
549 $a->internalRedirect('profile/' . $profile . '?f=&tab=profile');
552 $a->internalRedirect('photos/' . $profile);
556 $a->internalRedirect('profile/' . $profile);
559 $appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
560 $a->redirect($destination_url . $appendix);
566 header("Content-type: text/xml");
567 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
568 . '<dfrn_poll>' . "\r\n"
569 . "\t" . '<status>' . $status . '</status>' . "\r\n"
570 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
571 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
572 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
573 . '</dfrn_poll>' . "\r\n";