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\XML;
19 require_once 'include/items.php';
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 $perm = defaults($_GET, 'perm' , 'r');
33 $quiet = x($_GET, 'quiet');
35 // Possibly it is an OStatus compatible server that requests a user feed
36 $user_agent = defaults($_SERVER, 'HTTP_USER_AGENT', '');
37 if (($a->argc > 1) && ($dfrn_id == '') && !strstr($user_agent, 'Friendica')) {
38 $nickname = $a->argv[1];
39 header("Content-type: application/atom+xml");
40 echo OStatus::feed($nickname, $last_update, 10);
46 if (strpos($dfrn_id, ':') == 1) {
47 $direction = intval(substr($dfrn_id, 0, 1));
48 $dfrn_id = substr($dfrn_id, 2);
53 if (($dfrn_id === '') && (!x($_POST, 'dfrn_id'))) {
54 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
55 System::httpExit(403);
60 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
61 DBA::escape($a->argv[1])
64 System::httpExit(404);
67 $hidewall = ($r[0]['hidewall'] && !local_user());
69 $user = $r[0]['nickname'];
72 Logger::log('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
73 header("Content-type: application/atom+xml");
74 echo DFRN::feed('', $user, $last_update, 0, $hidewall);
78 if (($type === 'profile') && (!strlen($sec))) {
82 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", DBA::escape($dfrn_id), DBA::escape($dfrn_id));
86 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
87 $my_id = '1:' . $dfrn_id;
90 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
91 $my_id = '0:' . $dfrn_id;
94 $a->internalRedirect();
98 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
99 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
100 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
101 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
102 DBA::escape($a->argv[1])
105 if (DBA::isResult($r)) {
106 $s = Network::fetchUrl($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
108 Logger::log("dfrn_poll: old profile returns " . $s, Logger::DATA);
111 $xml = XML::parseString($s);
113 if ((int)$xml->status === 1) {
114 $_SESSION['authenticated'] = 1;
115 if (!x($_SESSION, 'remote')) {
116 $_SESSION['remote'] = [];
119 $_SESSION['remote'][] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']];
121 $_SESSION['visitor_id'] = $r[0]['id'];
122 $_SESSION['visitor_home'] = $r[0]['url'];
123 $_SESSION['visitor_handle'] = $r[0]['addr'];
124 $_SESSION['visitor_visiting'] = $r[0]['uid'];
125 $_SESSION['my_url'] = $r[0]['url'];
127 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
130 // Visitors get 1 day session.
131 $session_id = session_id();
132 $expire = time() + 86400;
133 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
134 DBA::escape($expire),
135 DBA::escape($session_id)
140 $profile = (count($r) > 0 && isset($r[0]['nickname']) ? $r[0]['nickname'] : '');
141 if (!empty($destination_url)) {
142 System::externalRedirect($destination_url);
144 $a->internalRedirect('profile/' . $profile);
147 $a->internalRedirect();
150 if ($type === 'profile-check' && $dfrn_version < 2.2) {
151 if ((strlen($challenge)) && (strlen($sec))) {
152 DBA::delete('profile_check', ["`expire` < ?", time()]);
153 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
156 if (!DBA::isResult($r)) {
157 System::xmlExit(3, 'No ticket');
161 $orig_id = $r[0]['dfrn_id'];
162 if (strpos($orig_id, ':')) {
163 $orig_id = substr($orig_id, 2);
166 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
169 if (!DBA::isResult($c)) {
170 System::xmlExit(3, 'No profile');
175 $sent_dfrn_id = hex2bin($dfrn_id);
176 $challenge = hex2bin($challenge);
180 if (($contact['duplex']) && strlen($contact['prvkey'])) {
181 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
182 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
184 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
185 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
188 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
190 if (strpos($final_dfrn_id, ':') == 1) {
191 $final_dfrn_id = substr($final_dfrn_id, 2);
194 if ($final_dfrn_id != $orig_id) {
195 Logger::log('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, Logger::DEBUG);
196 // did not decode properly - cannot trust this site
197 System::xmlExit(3, 'Bad decryption');
200 header("Content-type: text/xml");
201 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
206 switch ($direction) {
208 $dfrn_id = '0:' . $dfrn_id;
211 $dfrn_id = '1:' . $dfrn_id;
217 DBA::delete('profile_check', ["`expire` < ?", time()]);
218 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
219 DBA::escape($dfrn_id));
220 if (DBA::isResult($r)) {
222 return; // NOTREACHED
225 return; // NOTREACHED
230 function dfrn_poll_post(App $a)
232 $dfrn_id = x($_POST,'dfrn_id') ? $_POST['dfrn_id'] : '';
233 $challenge = x($_POST,'challenge') ? $_POST['challenge'] : '';
234 $url = x($_POST,'url') ? $_POST['url'] : '';
235 $sec = x($_POST,'sec') ? $_POST['sec'] : '';
236 $ptype = x($_POST,'type') ? $_POST['type'] : '';
237 $dfrn_version = x($_POST,'dfrn_version') ? (float) $_POST['dfrn_version'] : 2.0;
238 $perm = x($_POST,'perm') ? $_POST['perm'] : 'r';
240 if ($ptype === 'profile-check') {
241 if (strlen($challenge) && strlen($sec)) {
242 Logger::log('dfrn_poll: POST: profile-check');
244 DBA::delete('profile_check', ["`expire` < ?", time()]);
245 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
248 if (!DBA::isResult($r)) {
249 System::xmlExit(3, 'No ticket');
253 $orig_id = $r[0]['dfrn_id'];
254 if (strpos($orig_id, ':')) {
255 $orig_id = substr($orig_id, 2);
258 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
261 if (!DBA::isResult($c)) {
262 System::xmlExit(3, 'No profile');
267 $sent_dfrn_id = hex2bin($dfrn_id);
268 $challenge = hex2bin($challenge);
272 if ($contact['duplex'] && strlen($contact['prvkey'])) {
273 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
274 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
276 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
277 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
280 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
282 if (strpos($final_dfrn_id, ':') == 1) {
283 $final_dfrn_id = substr($final_dfrn_id, 2);
286 if ($final_dfrn_id != $orig_id) {
287 Logger::log('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, Logger::DEBUG);
288 // did not decode properly - cannot trust this site
289 System::xmlExit(3, 'Bad decryption');
292 header("Content-type: text/xml");
293 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
300 if (strpos($dfrn_id, ':') == 1) {
301 $direction = intval(substr($dfrn_id, 0, 1));
302 $dfrn_id = substr($dfrn_id, 2);
305 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
306 DBA::escape($dfrn_id),
307 DBA::escape($challenge)
310 if (!DBA::isResult($r)) {
314 $type = $r[0]['type'];
315 $last_update = $r[0]['last_update'];
317 DBA::delete('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge]);
320 switch ($direction) {
322 $sql_extra = sprintf(" AND `issued-id` = '%s' ", DBA::escape($dfrn_id));
326 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
327 $my_id = '1:' . $dfrn_id;
330 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
331 $my_id = '0:' . $dfrn_id;
334 $a->internalRedirect();
338 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
339 if (!DBA::isResult($r)) {
344 $owner_uid = $r[0]['uid'];
345 $contact_id = $r[0]['id'];
347 if ($type === 'reputation' && strlen($url)) {
348 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
355 if (DBA::isResult($r)) {
356 $reputation = $r[0]['rating'];
357 $text = $r[0]['reason'];
359 if ($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
365 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
368 <rating>$reputation</rating>
369 <description>$text</description>
375 // Update the writable flag if it changed
376 Logger::log('dfrn_poll: post request feed: ' . print_r($_POST, true), Logger::DATA);
377 if ($dfrn_version >= 2.21) {
378 if ($perm === 'rw') {
384 if ($writable != $contact['writable']) {
385 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
392 header("Content-type: application/atom+xml");
393 $o = DFRN::feed($dfrn_id, $a->argv[1], $last_update, $direction);
399 function dfrn_poll_content(App $a)
401 $dfrn_id = x($_GET,'dfrn_id') ? $_GET['dfrn_id'] : '';
402 $type = x($_GET,'type') ? $_GET['type'] : 'data';
403 $last_update = x($_GET,'last_update') ? $_GET['last_update'] : '';
404 $destination_url = x($_GET,'destination_url') ? $_GET['destination_url'] : '';
405 $sec = x($_GET,'sec') ? $_GET['sec'] : '';
406 $dfrn_version = x($_GET,'dfrn_version') ? (float) $_GET['dfrn_version'] : 2.0;
407 $perm = x($_GET,'perm') ? $_GET['perm'] : 'r';
408 $quiet = x($_GET,'quiet') ? true : false;
411 if (strpos($dfrn_id, ':') == 1) {
412 $direction = intval(substr($dfrn_id, 0, 1));
413 $dfrn_id = substr($dfrn_id, 2);
416 if ($dfrn_id != '') {
417 // initial communication from external contact
418 $hash = random_string();
422 DBA::delete('challenge', ["`expire` < ?", time()]);
424 if ($type !== 'profile') {
425 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
426 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
428 DBA::escape($dfrn_id),
429 intval(time() + 60 ),
431 DBA::escape($last_update)
436 switch ($direction) {
438 if ($type === 'profile') {
439 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", DBA::escape($dfrn_id), DBA::escape($dfrn_id));
441 $sql_extra = sprintf(" AND `issued-id` = '%s' ", DBA::escape($dfrn_id));
447 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
448 $my_id = '1:' . $dfrn_id;
451 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
452 $my_id = '0:' . $dfrn_id;
455 $a->internalRedirect();
459 $nickname = $a->argv[1];
461 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
462 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
463 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
464 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
465 DBA::escape($nickname)
467 if (DBA::isResult($r)) {
470 $id_str = $my_id . '.' . mt_rand(1000, 9999);
472 if (($r[0]['duplex'] && strlen($r[0]['pubkey'])) || !strlen($r[0]['prvkey'])) {
473 openssl_public_encrypt($hash, $challenge, $r[0]['pubkey']);
474 openssl_public_encrypt($id_str, $encrypted_id, $r[0]['pubkey']);
476 openssl_private_encrypt($hash, $challenge, $r[0]['prvkey']);
477 openssl_private_encrypt($id_str, $encrypted_id, $r[0]['prvkey']);
480 $challenge = bin2hex($challenge);
481 $encrypted_id = bin2hex($encrypted_id);
488 if (($type === 'profile') && (strlen($sec))) {
489 // heluecht: I don't know why we don't fail immediately when the user or contact hadn't been found.
490 // Since it doesn't make sense to continue from this point on, we now fail here. This should be safe.
491 if (!DBA::isResult($r)) {
492 System::httpExit(404, ["title" => L10n::t('Page not found.')]);
496 if ($dfrn_version < 2.2) {
497 $s = Network::fetchUrl($r[0]['poll']
498 . '?dfrn_id=' . $encrypted_id
499 . '&type=profile-check'
500 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
501 . '&challenge=' . $challenge
505 $s = Network::post($r[0]['poll'], [
506 'dfrn_id' => $encrypted_id,
507 'type' => 'profile-check',
508 'dfrn_version' => DFRN_PROTOCOL_VERSION,
509 'challenge' => $challenge,
514 Logger::log("dfrn_poll: sec profile: " . $s, Logger::DATA);
516 if (strlen($s) && strstr($s, '<?xml')) {
517 $xml = XML::parseString($s);
519 Logger::log('dfrn_poll: profile: parsed xml: ' . print_r($xml, true), Logger::DATA);
521 Logger::log('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
522 Logger::log('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
524 if (((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
525 $_SESSION['authenticated'] = 1;
526 if (!x($_SESSION, 'remote')) {
527 $_SESSION['remote'] = [];
530 $_SESSION['remote'][] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']];
531 $_SESSION['visitor_id'] = $r[0]['id'];
532 $_SESSION['visitor_home'] = $r[0]['url'];
533 $_SESSION['visitor_visiting'] = $r[0]['uid'];
534 $_SESSION['my_url'] = $r[0]['url'];
536 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
539 // Visitors get 1 day session.
540 $session_id = session_id();
541 $expire = time() + 86400;
542 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
543 DBA::escape($expire),
544 DBA::escape($session_id)
549 $profile = ((DBA::isResult($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
551 switch ($destination_url) {
553 $a->internalRedirect('profile/' . $profile . '?f=&tab=profile');
556 $a->internalRedirect('photos/' . $profile);
560 $a->internalRedirect('profile/' . $profile);
563 $appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
564 System::externalRedirect($destination_url . $appendix);
570 header("Content-type: text/xml");
571 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
572 . '<dfrn_poll>' . "\r\n"
573 . "\t" . '<status>' . $status . '</status>' . "\r\n"
574 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
575 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
576 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
577 . '</dfrn_poll>' . "\r\n";