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 $quiet = !empty($_GET['quiet']);
33 // Possibly it is an OStatus compatible server that requests a user feed
34 $user_agent = defaults($_SERVER, 'HTTP_USER_AGENT', '');
35 if (($a->argc > 1) && ($dfrn_id == '') && !strstr($user_agent, 'Friendica')) {
36 $nickname = $a->argv[1];
37 header("Content-type: application/atom+xml");
38 echo OStatus::feed($nickname, $last_update, 10);
44 if (strpos($dfrn_id, ':') == 1) {
45 $direction = intval(substr($dfrn_id, 0, 1));
46 $dfrn_id = substr($dfrn_id, 2);
51 if (($dfrn_id === '') && empty($_POST['dfrn_id'])) {
52 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
53 System::httpExit(403);
58 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
59 DBA::escape($a->argv[1])
62 System::httpExit(404);
65 $hidewall = ($r[0]['hidewall'] && !local_user());
67 $user = $r[0]['nickname'];
70 Logger::log('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
71 header("Content-type: application/atom+xml");
72 echo DFRN::feed('', $user, $last_update, 0, $hidewall);
76 if (($type === 'profile') && (!strlen($sec))) {
80 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", DBA::escape($dfrn_id), DBA::escape($dfrn_id));
84 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
85 $my_id = '1:' . $dfrn_id;
88 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
89 $my_id = '0:' . $dfrn_id;
92 $a->internalRedirect();
96 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
97 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
98 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
99 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
100 DBA::escape($a->argv[1])
103 if (DBA::isResult($r)) {
104 $s = Network::fetchUrl($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
106 Logger::log("dfrn_poll: old profile returns " . $s, Logger::DATA);
109 $xml = XML::parseString($s);
111 if ((int)$xml->status === 1) {
112 $_SESSION['authenticated'] = 1;
113 if (empty($_SESSION['remote'])) {
114 $_SESSION['remote'] = [];
117 $_SESSION['remote'][] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']];
119 $_SESSION['visitor_id'] = $r[0]['id'];
120 $_SESSION['visitor_home'] = $r[0]['url'];
121 $_SESSION['visitor_handle'] = $r[0]['addr'];
122 $_SESSION['visitor_visiting'] = $r[0]['uid'];
123 $_SESSION['my_url'] = $r[0]['url'];
125 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
128 // Visitors get 1 day session.
129 $session_id = session_id();
130 $expire = time() + 86400;
131 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
132 DBA::escape($expire),
133 DBA::escape($session_id)
138 $profile = (count($r) > 0 && isset($r[0]['nickname']) ? $r[0]['nickname'] : '');
139 if (!empty($destination_url)) {
140 System::externalRedirect($destination_url);
142 $a->internalRedirect('profile/' . $profile);
145 $a->internalRedirect();
148 if ($type === 'profile-check' && $dfrn_version < 2.2) {
149 if ((strlen($challenge)) && (strlen($sec))) {
150 DBA::delete('profile_check', ["`expire` < ?", time()]);
151 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
154 if (!DBA::isResult($r)) {
155 System::xmlExit(3, 'No ticket');
159 $orig_id = $r[0]['dfrn_id'];
160 if (strpos($orig_id, ':')) {
161 $orig_id = substr($orig_id, 2);
164 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
167 if (!DBA::isResult($c)) {
168 System::xmlExit(3, 'No profile');
173 $sent_dfrn_id = hex2bin($dfrn_id);
174 $challenge = hex2bin($challenge);
178 if (($contact['duplex']) && strlen($contact['prvkey'])) {
179 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
180 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
182 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
183 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
186 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
188 if (strpos($final_dfrn_id, ':') == 1) {
189 $final_dfrn_id = substr($final_dfrn_id, 2);
192 if ($final_dfrn_id != $orig_id) {
193 Logger::log('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, Logger::DEBUG);
194 // did not decode properly - cannot trust this site
195 System::xmlExit(3, 'Bad decryption');
198 header("Content-type: text/xml");
199 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
204 switch ($direction) {
206 $dfrn_id = '0:' . $dfrn_id;
209 $dfrn_id = '1:' . $dfrn_id;
215 DBA::delete('profile_check', ["`expire` < ?", time()]);
216 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
217 DBA::escape($dfrn_id));
218 if (DBA::isResult($r)) {
220 return; // NOTREACHED
223 return; // NOTREACHED
228 function dfrn_poll_post(App $a)
230 $dfrn_id = defaults($_POST, 'dfrn_id' , '');
231 $challenge = defaults($_POST, 'challenge', '');
232 $url = defaults($_POST, 'url' , '');
233 $sec = defaults($_POST, 'sec' , '');
234 $ptype = defaults($_POST, 'type' , '');
235 $perm = defaults($_POST, 'perm' , 'r');
236 $dfrn_version = !empty($_POST['dfrn_version']) ? (float) $_POST['dfrn_version'] : 2.0;
238 if ($ptype === 'profile-check') {
239 if (strlen($challenge) && strlen($sec)) {
240 Logger::log('dfrn_poll: POST: profile-check');
242 DBA::delete('profile_check', ["`expire` < ?", time()]);
243 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
246 if (!DBA::isResult($r)) {
247 System::xmlExit(3, 'No ticket');
251 $orig_id = $r[0]['dfrn_id'];
252 if (strpos($orig_id, ':')) {
253 $orig_id = substr($orig_id, 2);
256 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
259 if (!DBA::isResult($c)) {
260 System::xmlExit(3, 'No profile');
265 $sent_dfrn_id = hex2bin($dfrn_id);
266 $challenge = hex2bin($challenge);
270 if ($contact['duplex'] && strlen($contact['prvkey'])) {
271 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
272 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
274 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
275 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
278 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
280 if (strpos($final_dfrn_id, ':') == 1) {
281 $final_dfrn_id = substr($final_dfrn_id, 2);
284 if ($final_dfrn_id != $orig_id) {
285 Logger::log('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, Logger::DEBUG);
286 // did not decode properly - cannot trust this site
287 System::xmlExit(3, 'Bad decryption');
290 header("Content-type: text/xml");
291 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
298 if (strpos($dfrn_id, ':') == 1) {
299 $direction = intval(substr($dfrn_id, 0, 1));
300 $dfrn_id = substr($dfrn_id, 2);
303 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
304 DBA::escape($dfrn_id),
305 DBA::escape($challenge)
308 if (!DBA::isResult($r)) {
312 $type = $r[0]['type'];
313 $last_update = $r[0]['last_update'];
315 DBA::delete('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge]);
318 switch ($direction) {
320 $sql_extra = sprintf(" AND `issued-id` = '%s' ", DBA::escape($dfrn_id));
323 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
326 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
329 $a->internalRedirect();
333 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
334 if (!DBA::isResult($r)) {
339 $owner_uid = $r[0]['uid'];
340 $contact_id = $r[0]['id'];
342 if ($type === 'reputation' && strlen($url)) {
343 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
350 if (DBA::isResult($r)) {
351 $reputation = $r[0]['rating'];
352 $text = $r[0]['reason'];
354 if ($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
360 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
363 <rating>$reputation</rating>
364 <description>$text</description>
370 // Update the writable flag if it changed
371 Logger::log('dfrn_poll: post request feed: ' . print_r($_POST, true), Logger::DATA);
372 if ($dfrn_version >= 2.21) {
373 if ($perm === 'rw') {
379 if ($writable != $contact['writable']) {
380 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
387 header("Content-type: application/atom+xml");
388 $o = DFRN::feed($dfrn_id, $a->argv[1], $last_update, $direction);
394 function dfrn_poll_content(App $a)
396 $dfrn_id = defaults($_GET, 'dfrn_id' , '');
397 $type = defaults($_GET, 'type' , 'data');
398 $last_update = defaults($_GET, 'last_update' , '');
399 $destination_url = defaults($_GET, 'destination_url', '');
400 $sec = defaults($_GET, 'sec' , '');
401 $dfrn_version = !empty($_GET['dfrn_version']) ? (float) $_GET['dfrn_version'] : 2.0;
402 $quiet = !empty($_GET['quiet']);
405 if (strpos($dfrn_id, ':') == 1) {
406 $direction = intval(substr($dfrn_id, 0, 1));
407 $dfrn_id = substr($dfrn_id, 2);
410 if ($dfrn_id != '') {
411 // initial communication from external contact
412 $hash = Strings::getRandomHex();
416 DBA::delete('challenge', ["`expire` < ?", time()]);
418 if ($type !== 'profile') {
419 q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
420 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
422 DBA::escape($dfrn_id),
423 intval(time() + 60 ),
425 DBA::escape($last_update)
430 switch ($direction) {
432 if ($type === 'profile') {
433 $sql_extra = sprintf(" AND (`dfrn-id` = '%s' OR `issued-id` = '%s') ", DBA::escape($dfrn_id), DBA::escape($dfrn_id));
435 $sql_extra = sprintf(" AND `issued-id` = '%s' ", DBA::escape($dfrn_id));
441 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
442 $my_id = '1:' . $dfrn_id;
445 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
446 $my_id = '0:' . $dfrn_id;
449 $a->internalRedirect();
453 $nickname = $a->argv[1];
455 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
456 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
457 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
458 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
459 DBA::escape($nickname)
461 if (DBA::isResult($r)) {
464 $id_str = $my_id . '.' . mt_rand(1000, 9999);
466 if (($r[0]['duplex'] && strlen($r[0]['pubkey'])) || !strlen($r[0]['prvkey'])) {
467 openssl_public_encrypt($hash, $challenge, $r[0]['pubkey']);
468 openssl_public_encrypt($id_str, $encrypted_id, $r[0]['pubkey']);
470 openssl_private_encrypt($hash, $challenge, $r[0]['prvkey']);
471 openssl_private_encrypt($id_str, $encrypted_id, $r[0]['prvkey']);
474 $challenge = bin2hex($challenge);
475 $encrypted_id = bin2hex($encrypted_id);
482 if (($type === 'profile') && (strlen($sec))) {
483 // heluecht: I don't know why we don't fail immediately when the user or contact hadn't been found.
484 // Since it doesn't make sense to continue from this point on, we now fail here. This should be safe.
485 if (!DBA::isResult($r)) {
486 System::httpExit(404, ["title" => L10n::t('Page not found.')]);
490 if ($dfrn_version < 2.2) {
491 $s = Network::fetchUrl($r[0]['poll']
492 . '?dfrn_id=' . $encrypted_id
493 . '&type=profile-check'
494 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
495 . '&challenge=' . $challenge
499 $s = Network::post($r[0]['poll'], [
500 'dfrn_id' => $encrypted_id,
501 'type' => 'profile-check',
502 'dfrn_version' => DFRN_PROTOCOL_VERSION,
503 'challenge' => $challenge,
508 Logger::log("dfrn_poll: sec profile: " . $s, Logger::DATA);
510 if (strlen($s) && strstr($s, '<?xml')) {
511 $xml = XML::parseString($s);
513 Logger::log('dfrn_poll: profile: parsed xml: ' . print_r($xml, true), Logger::DATA);
515 Logger::log('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
516 Logger::log('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
518 if (((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
519 $_SESSION['authenticated'] = 1;
520 if (empty($_SESSION['remote'])) {
521 $_SESSION['remote'] = [];
524 $_SESSION['remote'][] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']];
525 $_SESSION['visitor_id'] = $r[0]['id'];
526 $_SESSION['visitor_home'] = $r[0]['url'];
527 $_SESSION['visitor_visiting'] = $r[0]['uid'];
528 $_SESSION['my_url'] = $r[0]['url'];
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";