4 * @file mod/dfrn_poll.php
8 use Friendica\Core\Config;
9 use Friendica\Core\L10n;
10 use Friendica\Core\System;
11 use Friendica\Database\DBA;
12 use Friendica\Module\Login;
13 use Friendica\Protocol\DFRN;
14 use Friendica\Protocol\OStatus;
15 use Friendica\Util\Network;
16 use Friendica\Util\XML;
18 require_once 'include/items.php';
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 = x($_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 === '') && (!x($_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('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("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 (!x($_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('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 = x($_POST,'dfrn_id') ? $_POST['dfrn_id'] : '';
232 $challenge = x($_POST,'challenge') ? $_POST['challenge'] : '';
233 $url = x($_POST,'url') ? $_POST['url'] : '';
234 $sec = x($_POST,'sec') ? $_POST['sec'] : '';
235 $ptype = x($_POST,'type') ? $_POST['type'] : '';
236 $dfrn_version = x($_POST,'dfrn_version') ? (float) $_POST['dfrn_version'] : 2.0;
237 $perm = x($_POST,'perm') ? $_POST['perm'] : 'r';
239 if ($ptype === 'profile-check') {
240 if (strlen($challenge) && strlen($sec)) {
241 logger('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('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('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 = x($_GET,'dfrn_id') ? $_GET['dfrn_id'] : '';
401 $type = x($_GET,'type') ? $_GET['type'] : 'data';
402 $last_update = x($_GET,'last_update') ? $_GET['last_update'] : '';
403 $destination_url = x($_GET,'destination_url') ? $_GET['destination_url'] : '';
404 $sec = x($_GET,'sec') ? $_GET['sec'] : '';
405 $dfrn_version = x($_GET,'dfrn_version') ? (float) $_GET['dfrn_version'] : 2.0;
406 $perm = x($_GET,'perm') ? $_GET['perm'] : 'r';
407 $quiet = x($_GET,'quiet') ? true : false;
410 if (strpos($dfrn_id, ':') == 1) {
411 $direction = intval(substr($dfrn_id, 0, 1));
412 $dfrn_id = substr($dfrn_id, 2);
415 if ($dfrn_id != '') {
416 // initial communication from external contact
417 $hash = random_string();
421 DBA::delete('challenge', ["`expire` < ?", time()]);
423 if ($type !== 'profile') {
424 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
425 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
427 DBA::escape($dfrn_id),
428 intval(time() + 60 ),
430 DBA::escape($last_update)
435 switch ($direction) {
437 if ($type === 'profile') {
438 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", DBA::escape($dfrn_id), DBA::escape($dfrn_id));
440 $sql_extra = sprintf(" AND `issued-id` = '%s' ", DBA::escape($dfrn_id));
446 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
447 $my_id = '1:' . $dfrn_id;
450 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
451 $my_id = '0:' . $dfrn_id;
454 $a->internalRedirect();
458 $nickname = $a->argv[1];
460 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
461 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
462 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
463 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
464 DBA::escape($nickname)
466 if (DBA::isResult($r)) {
469 $id_str = $my_id . '.' . mt_rand(1000, 9999);
471 if (($r[0]['duplex'] && strlen($r[0]['pubkey'])) || !strlen($r[0]['prvkey'])) {
472 openssl_public_encrypt($hash, $challenge, $r[0]['pubkey']);
473 openssl_public_encrypt($id_str, $encrypted_id, $r[0]['pubkey']);
475 openssl_private_encrypt($hash, $challenge, $r[0]['prvkey']);
476 openssl_private_encrypt($id_str, $encrypted_id, $r[0]['prvkey']);
479 $challenge = bin2hex($challenge);
480 $encrypted_id = bin2hex($encrypted_id);
487 if (($type === 'profile') && (strlen($sec))) {
488 // heluecht: I don't know why we don't fail immediately when the user or contact hadn't been found.
489 // Since it doesn't make sense to continue from this point on, we now fail here. This should be safe.
490 if (!DBA::isResult($r)) {
491 System::httpExit(404, ["title" => L10n::t('Page not found.')]);
495 if ($dfrn_version < 2.2) {
496 $s = Network::fetchUrl($r[0]['poll']
497 . '?dfrn_id=' . $encrypted_id
498 . '&type=profile-check'
499 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
500 . '&challenge=' . $challenge
504 $s = Network::post($r[0]['poll'], [
505 'dfrn_id' => $encrypted_id,
506 'type' => 'profile-check',
507 'dfrn_version' => DFRN_PROTOCOL_VERSION,
508 'challenge' => $challenge,
513 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
515 if (strlen($s) && strstr($s, '<?xml')) {
516 $xml = XML::parseString($s);
518 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml, true), LOGGER_DATA);
520 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
521 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
523 if (((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
524 $_SESSION['authenticated'] = 1;
525 if (!x($_SESSION, 'remote')) {
526 $_SESSION['remote'] = [];
529 $_SESSION['remote'][] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']];
530 $_SESSION['visitor_id'] = $r[0]['id'];
531 $_SESSION['visitor_home'] = $r[0]['url'];
532 $_SESSION['visitor_visiting'] = $r[0]['uid'];
533 $_SESSION['my_url'] = $r[0]['url'];
535 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
538 // Visitors get 1 day session.
539 $session_id = session_id();
540 $expire = time() + 86400;
541 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
542 DBA::escape($expire),
543 DBA::escape($session_id)
548 $profile = ((DBA::isResult($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
550 switch ($destination_url) {
552 $a->internalRedirect('profile/' . $profile . '?f=&tab=profile');
555 $a->internalRedirect('photos/' . $profile);
559 $a->internalRedirect('profile/' . $profile);
562 $appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
563 System::externalRedirect($destination_url . $appendix);
569 header("Content-type: text/xml");
570 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
571 . '<dfrn_poll>' . "\r\n"
572 . "\t" . '<status>' . $status . '</status>' . "\r\n"
573 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
574 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
575 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
576 . '</dfrn_poll>' . "\r\n";