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 require_once 'include/items.php';
22 function dfrn_poll_init(App $a)
26 $dfrn_id = defaults($_GET, 'dfrn_id' , '');
27 $type = defaults($_GET, 'type' , 'data');
28 $last_update = defaults($_GET, 'last_update' , '');
29 $destination_url = defaults($_GET, 'destination_url', '');
30 $challenge = defaults($_GET, 'challenge' , '');
31 $sec = defaults($_GET, 'sec' , '');
32 $dfrn_version = (float) defaults($_GET, 'dfrn_version' , 2.0);
33 $perm = defaults($_GET, 'perm' , 'r');
34 $quiet = x($_GET, 'quiet');
36 // Possibly it is an OStatus compatible server that requests a user feed
37 $user_agent = defaults($_SERVER, 'HTTP_USER_AGENT', '');
38 if (($a->argc > 1) && ($dfrn_id == '') && !strstr($user_agent, 'Friendica')) {
39 $nickname = $a->argv[1];
40 header("Content-type: application/atom+xml");
41 echo OStatus::feed($nickname, $last_update, 10);
47 if (strpos($dfrn_id, ':') == 1) {
48 $direction = intval(substr($dfrn_id, 0, 1));
49 $dfrn_id = substr($dfrn_id, 2);
54 if (($dfrn_id === '') && (!x($_POST, 'dfrn_id'))) {
55 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
56 System::httpExit(403);
61 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
62 DBA::escape($a->argv[1])
65 System::httpExit(404);
68 $hidewall = ($r[0]['hidewall'] && !local_user());
70 $user = $r[0]['nickname'];
73 Logger::log('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
74 header("Content-type: application/atom+xml");
75 echo DFRN::feed('', $user, $last_update, 0, $hidewall);
79 if (($type === 'profile') && (!strlen($sec))) {
83 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", DBA::escape($dfrn_id), DBA::escape($dfrn_id));
87 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
88 $my_id = '1:' . $dfrn_id;
91 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
92 $my_id = '0:' . $dfrn_id;
95 $a->internalRedirect();
99 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
100 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
101 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
102 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
103 DBA::escape($a->argv[1])
106 if (DBA::isResult($r)) {
107 $s = Network::fetchUrl($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
109 Logger::log("dfrn_poll: old profile returns " . $s, Logger::DATA);
112 $xml = XML::parseString($s);
114 if ((int)$xml->status === 1) {
115 $_SESSION['authenticated'] = 1;
116 if (!x($_SESSION, 'remote')) {
117 $_SESSION['remote'] = [];
120 $_SESSION['remote'][] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']];
122 $_SESSION['visitor_id'] = $r[0]['id'];
123 $_SESSION['visitor_home'] = $r[0]['url'];
124 $_SESSION['visitor_handle'] = $r[0]['addr'];
125 $_SESSION['visitor_visiting'] = $r[0]['uid'];
126 $_SESSION['my_url'] = $r[0]['url'];
128 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
131 // Visitors get 1 day session.
132 $session_id = session_id();
133 $expire = time() + 86400;
134 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
135 DBA::escape($expire),
136 DBA::escape($session_id)
141 $profile = (count($r) > 0 && isset($r[0]['nickname']) ? $r[0]['nickname'] : '');
142 if (!empty($destination_url)) {
143 System::externalRedirect($destination_url);
145 $a->internalRedirect('profile/' . $profile);
148 $a->internalRedirect();
151 if ($type === 'profile-check' && $dfrn_version < 2.2) {
152 if ((strlen($challenge)) && (strlen($sec))) {
153 DBA::delete('profile_check', ["`expire` < ?", time()]);
154 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
157 if (!DBA::isResult($r)) {
158 System::xmlExit(3, 'No ticket');
162 $orig_id = $r[0]['dfrn_id'];
163 if (strpos($orig_id, ':')) {
164 $orig_id = substr($orig_id, 2);
167 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
170 if (!DBA::isResult($c)) {
171 System::xmlExit(3, 'No profile');
176 $sent_dfrn_id = hex2bin($dfrn_id);
177 $challenge = hex2bin($challenge);
181 if (($contact['duplex']) && strlen($contact['prvkey'])) {
182 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
183 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
185 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
186 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
189 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
191 if (strpos($final_dfrn_id, ':') == 1) {
192 $final_dfrn_id = substr($final_dfrn_id, 2);
195 if ($final_dfrn_id != $orig_id) {
196 Logger::log('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, Logger::DEBUG);
197 // did not decode properly - cannot trust this site
198 System::xmlExit(3, 'Bad decryption');
201 header("Content-type: text/xml");
202 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
207 switch ($direction) {
209 $dfrn_id = '0:' . $dfrn_id;
212 $dfrn_id = '1:' . $dfrn_id;
218 DBA::delete('profile_check', ["`expire` < ?", time()]);
219 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
220 DBA::escape($dfrn_id));
221 if (DBA::isResult($r)) {
223 return; // NOTREACHED
226 return; // NOTREACHED
231 function dfrn_poll_post(App $a)
233 $dfrn_id = x($_POST,'dfrn_id') ? $_POST['dfrn_id'] : '';
234 $challenge = x($_POST,'challenge') ? $_POST['challenge'] : '';
235 $url = x($_POST,'url') ? $_POST['url'] : '';
236 $sec = x($_POST,'sec') ? $_POST['sec'] : '';
237 $ptype = x($_POST,'type') ? $_POST['type'] : '';
238 $dfrn_version = x($_POST,'dfrn_version') ? (float) $_POST['dfrn_version'] : 2.0;
239 $perm = x($_POST,'perm') ? $_POST['perm'] : 'r';
241 if ($ptype === 'profile-check') {
242 if (strlen($challenge) && strlen($sec)) {
243 Logger::log('dfrn_poll: POST: profile-check');
245 DBA::delete('profile_check', ["`expire` < ?", time()]);
246 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
249 if (!DBA::isResult($r)) {
250 System::xmlExit(3, 'No ticket');
254 $orig_id = $r[0]['dfrn_id'];
255 if (strpos($orig_id, ':')) {
256 $orig_id = substr($orig_id, 2);
259 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
262 if (!DBA::isResult($c)) {
263 System::xmlExit(3, 'No profile');
268 $sent_dfrn_id = hex2bin($dfrn_id);
269 $challenge = hex2bin($challenge);
273 if ($contact['duplex'] && strlen($contact['prvkey'])) {
274 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
275 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
277 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
278 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
281 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
283 if (strpos($final_dfrn_id, ':') == 1) {
284 $final_dfrn_id = substr($final_dfrn_id, 2);
287 if ($final_dfrn_id != $orig_id) {
288 Logger::log('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, Logger::DEBUG);
289 // did not decode properly - cannot trust this site
290 System::xmlExit(3, 'Bad decryption');
293 header("Content-type: text/xml");
294 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
301 if (strpos($dfrn_id, ':') == 1) {
302 $direction = intval(substr($dfrn_id, 0, 1));
303 $dfrn_id = substr($dfrn_id, 2);
306 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
307 DBA::escape($dfrn_id),
308 DBA::escape($challenge)
311 if (!DBA::isResult($r)) {
315 $type = $r[0]['type'];
316 $last_update = $r[0]['last_update'];
318 DBA::delete('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge]);
321 switch ($direction) {
323 $sql_extra = sprintf(" AND `issued-id` = '%s' ", DBA::escape($dfrn_id));
327 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
328 $my_id = '1:' . $dfrn_id;
331 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
332 $my_id = '0:' . $dfrn_id;
335 $a->internalRedirect();
339 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
340 if (!DBA::isResult($r)) {
345 $owner_uid = $r[0]['uid'];
346 $contact_id = $r[0]['id'];
348 if ($type === 'reputation' && strlen($url)) {
349 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
356 if (DBA::isResult($r)) {
357 $reputation = $r[0]['rating'];
358 $text = $r[0]['reason'];
360 if ($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
366 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
369 <rating>$reputation</rating>
370 <description>$text</description>
376 // Update the writable flag if it changed
377 Logger::log('dfrn_poll: post request feed: ' . print_r($_POST, true), Logger::DATA);
378 if ($dfrn_version >= 2.21) {
379 if ($perm === 'rw') {
385 if ($writable != $contact['writable']) {
386 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
393 header("Content-type: application/atom+xml");
394 $o = DFRN::feed($dfrn_id, $a->argv[1], $last_update, $direction);
400 function dfrn_poll_content(App $a)
402 $dfrn_id = x($_GET,'dfrn_id') ? $_GET['dfrn_id'] : '';
403 $type = x($_GET,'type') ? $_GET['type'] : 'data';
404 $last_update = x($_GET,'last_update') ? $_GET['last_update'] : '';
405 $destination_url = x($_GET,'destination_url') ? $_GET['destination_url'] : '';
406 $sec = x($_GET,'sec') ? $_GET['sec'] : '';
407 $dfrn_version = x($_GET,'dfrn_version') ? (float) $_GET['dfrn_version'] : 2.0;
408 $perm = x($_GET,'perm') ? $_GET['perm'] : 'r';
409 $quiet = x($_GET,'quiet') ? true : false;
412 if (strpos($dfrn_id, ':') == 1) {
413 $direction = intval(substr($dfrn_id, 0, 1));
414 $dfrn_id = substr($dfrn_id, 2);
417 if ($dfrn_id != '') {
418 // initial communication from external contact
419 $hash = Strings::getRandomHex();
423 DBA::delete('challenge', ["`expire` < ?", time()]);
425 if ($type !== 'profile') {
426 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
427 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
429 DBA::escape($dfrn_id),
430 intval(time() + 60 ),
432 DBA::escape($last_update)
437 switch ($direction) {
439 if ($type === 'profile') {
440 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", DBA::escape($dfrn_id), DBA::escape($dfrn_id));
442 $sql_extra = sprintf(" AND `issued-id` = '%s' ", DBA::escape($dfrn_id));
448 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
449 $my_id = '1:' . $dfrn_id;
452 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", DBA::escape($dfrn_id));
453 $my_id = '0:' . $dfrn_id;
456 $a->internalRedirect();
460 $nickname = $a->argv[1];
462 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
463 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
464 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
465 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
466 DBA::escape($nickname)
468 if (DBA::isResult($r)) {
471 $id_str = $my_id . '.' . mt_rand(1000, 9999);
473 if (($r[0]['duplex'] && strlen($r[0]['pubkey'])) || !strlen($r[0]['prvkey'])) {
474 openssl_public_encrypt($hash, $challenge, $r[0]['pubkey']);
475 openssl_public_encrypt($id_str, $encrypted_id, $r[0]['pubkey']);
477 openssl_private_encrypt($hash, $challenge, $r[0]['prvkey']);
478 openssl_private_encrypt($id_str, $encrypted_id, $r[0]['prvkey']);
481 $challenge = bin2hex($challenge);
482 $encrypted_id = bin2hex($encrypted_id);
489 if (($type === 'profile') && (strlen($sec))) {
490 // heluecht: I don't know why we don't fail immediately when the user or contact hadn't been found.
491 // Since it doesn't make sense to continue from this point on, we now fail here. This should be safe.
492 if (!DBA::isResult($r)) {
493 System::httpExit(404, ["title" => L10n::t('Page not found.')]);
497 if ($dfrn_version < 2.2) {
498 $s = Network::fetchUrl($r[0]['poll']
499 . '?dfrn_id=' . $encrypted_id
500 . '&type=profile-check'
501 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
502 . '&challenge=' . $challenge
506 $s = Network::post($r[0]['poll'], [
507 'dfrn_id' => $encrypted_id,
508 'type' => 'profile-check',
509 'dfrn_version' => DFRN_PROTOCOL_VERSION,
510 'challenge' => $challenge,
515 Logger::log("dfrn_poll: sec profile: " . $s, Logger::DATA);
517 if (strlen($s) && strstr($s, '<?xml')) {
518 $xml = XML::parseString($s);
520 Logger::log('dfrn_poll: profile: parsed xml: ' . print_r($xml, true), Logger::DATA);
522 Logger::log('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
523 Logger::log('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
525 if (((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
526 $_SESSION['authenticated'] = 1;
527 if (!x($_SESSION, 'remote')) {
528 $_SESSION['remote'] = [];
531 $_SESSION['remote'][] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']];
532 $_SESSION['visitor_id'] = $r[0]['id'];
533 $_SESSION['visitor_home'] = $r[0]['url'];
534 $_SESSION['visitor_visiting'] = $r[0]['uid'];
535 $_SESSION['my_url'] = $r[0]['url'];
537 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
540 // Visitors get 1 day session.
541 $session_id = session_id();
542 $expire = time() + 86400;
543 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
544 DBA::escape($expire),
545 DBA::escape($session_id)
550 $profile = ((DBA::isResult($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
552 switch ($destination_url) {
554 $a->internalRedirect('profile/' . $profile . '?f=&tab=profile');
557 $a->internalRedirect('photos/' . $profile);
561 $a->internalRedirect('profile/' . $profile);
564 $appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
565 System::externalRedirect($destination_url . $appendix);
571 header("Content-type: text/xml");
572 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
573 . '<dfrn_poll>' . "\r\n"
574 . "\t" . '<status>' . $status . '</status>' . "\r\n"
575 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
576 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
577 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
578 . '</dfrn_poll>' . "\r\n";