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 goaway(System::baseUrl());
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)
138 $profile = $r[0]['nickname'];
139 goaway((strlen($destination_url)) ? $destination_url : System::baseUrl() . '/profile/' . $profile);
141 goaway(System::baseUrl());
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('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 = x($_POST,'dfrn_id') ? $_POST['dfrn_id'] : '';
227 $challenge = x($_POST,'challenge') ? $_POST['challenge'] : '';
228 $url = x($_POST,'url') ? $_POST['url'] : '';
229 $sec = x($_POST,'sec') ? $_POST['sec'] : '';
230 $ptype = x($_POST,'type') ? $_POST['type'] : '';
231 $dfrn_version = x($_POST,'dfrn_version') ? (float) $_POST['dfrn_version'] : 2.0;
232 $perm = x($_POST,'perm') ? $_POST['perm'] : 'r';
234 if ($ptype === 'profile-check') {
235 if (strlen($challenge) && strlen($sec)) {
236 logger('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('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));
320 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
321 $my_id = '1:' . $dfrn_id;
324 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
325 $my_id = '0:' . $dfrn_id;
328 goaway(System::baseUrl());
332 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
333 if (!DBA::isResult($r)) {
338 $owner_uid = $r[0]['uid'];
339 $contact_id = $r[0]['id'];
341 if ($type === 'reputation' && strlen($url)) {
342 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
349 if (DBA::isResult($r)) {
350 $reputation = $r[0]['rating'];
351 $text = $r[0]['reason'];
353 if ($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
359 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
362 <rating>$reputation</rating>
363 <description>$text</description>
369 // Update the writable flag if it changed
370 logger('dfrn_poll: post request feed: ' . print_r($_POST, true), LOGGER_DATA);
371 if ($dfrn_version >= 2.21) {
372 if ($perm === 'rw') {
378 if ($writable != $contact['writable']) {
379 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
386 header("Content-type: application/atom+xml");
387 $o = DFRN::feed($dfrn_id, $a->argv[1], $last_update, $direction);
393 function dfrn_poll_content(App $a)
395 $dfrn_id = x($_GET,'dfrn_id') ? $_GET['dfrn_id'] : '';
396 $type = x($_GET,'type') ? $_GET['type'] : 'data';
397 $last_update = x($_GET,'last_update') ? $_GET['last_update'] : '';
398 $destination_url = x($_GET,'destination_url') ? $_GET['destination_url'] : '';
399 $sec = x($_GET,'sec') ? $_GET['sec'] : '';
400 $dfrn_version = x($_GET,'dfrn_version') ? (float) $_GET['dfrn_version'] : 2.0;
401 $perm = x($_GET,'perm') ? $_GET['perm'] : 'r';
402 $quiet = x($_GET,'quiet') ? true : false;
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 = random_string();
416 DBA::delete('challenge', ["`expire` < ?", time()]);
418 if ($type !== 'profile') {
419 $r = 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 goaway(System::baseUrl());
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 $profile = ((DBA::isResult($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
510 switch ($destination_url) {
512 $dest = System::baseUrl() . '/profile/' . $profile . '?f=&tab=profile';
515 $dest = System::baseUrl() . '/photos/' . $profile;
519 $dest = System::baseUrl() . '/profile/' . $profile;
522 $appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
523 $dest = $destination_url . $appendix;
527 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
529 if (strlen($s) && strstr($s, '<?xml')) {
530 $xml = XML::parseString($s);
532 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml, true), LOGGER_DATA);
534 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
535 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
537 if (((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
538 $_SESSION['authenticated'] = 1;
539 if (!x($_SESSION, 'remote')) {
540 $_SESSION['remote'] = [];
543 $_SESSION['remote'][] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']];
544 $_SESSION['visitor_id'] = $r[0]['id'];
545 $_SESSION['visitor_home'] = $r[0]['url'];
546 $_SESSION['visitor_visiting'] = $r[0]['uid'];
547 $_SESSION['my_url'] = $r[0]['url'];
549 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
552 // Visitors get 1 day session.
553 $session_id = session_id();
554 $expire = time() + 86400;
555 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
556 DBA::escape($expire),
557 DBA::escape($session_id)
567 header("Content-type: text/xml");
568 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
569 . '<dfrn_poll>' . "\r\n"
570 . "\t" . '<status>' . $status . '</status>' . "\r\n"
571 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
572 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
573 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
574 . '</dfrn_poll>' . "\r\n";