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\Database\DBA;
13 use Friendica\Module\Login;
14 use Friendica\Protocol\DFRN;
15 use Friendica\Protocol\OStatus;
16 use Friendica\Util\Network;
17 use Friendica\Util\Strings;
18 use Friendica\Util\XML;
20 function dfrn_poll_init(App $a)
24 $dfrn_id = defaults($_GET, 'dfrn_id' , '');
25 $type = defaults($_GET, 'type' , 'data');
26 $last_update = defaults($_GET, 'last_update' , '');
27 $destination_url = defaults($_GET, 'destination_url', '');
28 $challenge = defaults($_GET, 'challenge' , '');
29 $sec = defaults($_GET, 'sec' , '');
30 $dfrn_version = (float) defaults($_GET, 'dfrn_version' , 2.0);
31 $perm = defaults($_GET, 'perm' , 'r');
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') && !local_user() && !remote_user()) {
54 System::httpExit(403);
59 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
60 DBA::escape($a->argv[1])
63 System::httpExit(404);
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'][] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']];
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));
325 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
326 $my_id = '1:' . $dfrn_id;
329 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
330 $my_id = '0:' . $dfrn_id;
333 $a->internalRedirect();
337 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
338 if (!DBA::isResult($r)) {
343 $owner_uid = $r[0]['uid'];
344 $contact_id = $r[0]['id'];
346 if ($type === 'reputation' && strlen($url)) {
347 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
354 if (DBA::isResult($r)) {
355 $reputation = $r[0]['rating'];
356 $text = $r[0]['reason'];
358 if ($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
364 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
367 <rating>$reputation</rating>
368 <description>$text</description>
374 // Update the writable flag if it changed
375 Logger::log('dfrn_poll: post request feed: ' . print_r($_POST, true), Logger::DATA);
376 if ($dfrn_version >= 2.21) {
377 if ($perm === 'rw') {
383 if ($writable != $contact['writable']) {
384 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
391 header("Content-type: application/atom+xml");
392 $o = DFRN::feed($dfrn_id, $a->argv[1], $last_update, $direction);
398 function dfrn_poll_content(App $a)
400 $dfrn_id = defaults($_GET, 'dfrn_id' , '');
401 $type = defaults($_GET, 'type' , 'data');
402 $last_update = defaults($_GET, 'last_update' , '');
403 $destination_url = defaults($_GET, 'destination_url', '');
404 $sec = defaults($_GET, 'sec' , '');
405 $dfrn_version = !empty($_GET['dfrn_version']) ? (float) $_GET['dfrn_version'] : 2.0;
406 $quiet = !empty($_GET['quiet']);
409 if (strpos($dfrn_id, ':') == 1) {
410 $direction = intval(substr($dfrn_id, 0, 1));
411 $dfrn_id = substr($dfrn_id, 2);
414 if ($dfrn_id != '') {
415 // initial communication from external contact
416 $hash = Strings::getRandomHex();
420 DBA::delete('challenge', ["`expire` < ?", time()]);
422 if ($type !== 'profile') {
423 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
424 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
426 DBA::escape($dfrn_id),
427 intval(time() + 60 ),
429 DBA::escape($last_update)
434 switch ($direction) {
436 if ($type === 'profile') {
437 $sql_extra = sprintf(" AND (`dfrn-id` = '%s' OR `issued-id` = '%s') ", DBA::escape($dfrn_id), DBA::escape($dfrn_id));
439 $sql_extra = sprintf(" AND `issued-id` = '%s' ", DBA::escape($dfrn_id));
445 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
446 $my_id = '1:' . $dfrn_id;
449 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
450 $my_id = '0:' . $dfrn_id;
453 $a->internalRedirect();
457 $nickname = $a->argv[1];
459 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
460 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
461 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
462 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
463 DBA::escape($nickname)
465 if (DBA::isResult($r)) {
468 $id_str = $my_id . '.' . mt_rand(1000, 9999);
470 if (($r[0]['duplex'] && strlen($r[0]['pubkey'])) || !strlen($r[0]['prvkey'])) {
471 openssl_public_encrypt($hash, $challenge, $r[0]['pubkey']);
472 openssl_public_encrypt($id_str, $encrypted_id, $r[0]['pubkey']);
474 openssl_private_encrypt($hash, $challenge, $r[0]['prvkey']);
475 openssl_private_encrypt($id_str, $encrypted_id, $r[0]['prvkey']);
478 $challenge = bin2hex($challenge);
479 $encrypted_id = bin2hex($encrypted_id);
486 if (($type === 'profile') && (strlen($sec))) {
487 // heluecht: I don't know why we don't fail immediately when the user or contact hadn't been found.
488 // Since it doesn't make sense to continue from this point on, we now fail here. This should be safe.
489 if (!DBA::isResult($r)) {
490 System::httpExit(404, ["title" => L10n::t('Page not found.')]);
494 if ($dfrn_version < 2.2) {
495 $s = Network::fetchUrl($r[0]['poll']
496 . '?dfrn_id=' . $encrypted_id
497 . '&type=profile-check'
498 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
499 . '&challenge=' . $challenge
503 $s = Network::post($r[0]['poll'], [
504 'dfrn_id' => $encrypted_id,
505 'type' => 'profile-check',
506 'dfrn_version' => DFRN_PROTOCOL_VERSION,
507 'challenge' => $challenge,
512 Logger::log("dfrn_poll: sec profile: " . $s, Logger::DATA);
514 if (strlen($s) && strstr($s, '<?xml')) {
515 $xml = XML::parseString($s);
517 Logger::log('dfrn_poll: profile: parsed xml: ' . print_r($xml, true), Logger::DATA);
519 Logger::log('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
520 Logger::log('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
522 if (((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
523 $_SESSION['authenticated'] = 1;
524 if (empty($_SESSION['remote'])) {
525 $_SESSION['remote'] = [];
528 $_SESSION['remote'][] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']];
529 $_SESSION['visitor_id'] = $r[0]['id'];
530 $_SESSION['visitor_home'] = $r[0]['url'];
531 $_SESSION['visitor_visiting'] = $r[0]['uid'];
532 $_SESSION['my_url'] = $r[0]['url'];
534 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
537 // Visitors get 1 day session.
538 $session_id = session_id();
539 $expire = time() + 86400;
540 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
541 DBA::escape($expire),
542 DBA::escape($session_id)
547 $profile = ((DBA::isResult($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
549 switch ($destination_url) {
551 $a->internalRedirect('profile/' . $profile . '?f=&tab=profile');
554 $a->internalRedirect('photos/' . $profile);
558 $a->internalRedirect('profile/' . $profile);
561 $appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
562 $a->redirect($destination_url . $appendix);
568 header("Content-type: text/xml");
569 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
570 . '<dfrn_poll>' . "\r\n"
571 . "\t" . '<status>' . $status . '</status>' . "\r\n"
572 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
573 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
574 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
575 . '</dfrn_poll>' . "\r\n";