4 * @file mod/dfrn_poll.php
7 use Friendica\Core\Config;
8 use Friendica\Core\L10n;
9 use Friendica\Core\System;
10 use Friendica\Database\DBM;
11 use Friendica\Module\Login;
12 use Friendica\Protocol\DFRN;
13 use Friendica\Protocol\OStatus;
14 use Friendica\Util\Network;
15 use Friendica\Util\XML;
17 require_once 'include/items.php';
19 function dfrn_poll_init(App $a)
23 $dfrn_id = defaults($_GET, 'dfrn_id' , '');
24 $type = defaults($_GET, 'type' , 'data');
25 $last_update = defaults($_GET, 'last_update' , '');
26 $destination_url = defaults($_GET, 'destination_url', '');
27 $challenge = defaults($_GET, 'challenge' , '');
28 $sec = defaults($_GET, 'sec' , '');
29 $dfrn_version = (float) defaults($_GET, 'dfrn_version' , 2.0);
30 $perm = defaults($_GET, 'perm' , 'r');
31 $quiet = x($_GET, 'quiet');
33 // Possibly it is an OStatus compatible server that requests a user feed
34 if (($a->argc > 1) && ($dfrn_id == '') && !strstr($_SERVER["HTTP_USER_AGENT"], 'Friendica')) {
35 $nickname = $a->argv[1];
36 header("Content-type: application/atom+xml");
37 echo OStatus::feed($nickname, $last_update, 10);
43 if (strpos($dfrn_id, ':') == 1) {
44 $direction = intval(substr($dfrn_id, 0, 1));
45 $dfrn_id = substr($dfrn_id, 2);
50 if (($dfrn_id === '') && (!x($_POST, 'dfrn_id'))) {
51 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
52 System::httpExit(403);
57 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
61 System::httpExit(404);
64 $hidewall = ($r[0]['hidewall'] && !local_user());
66 $user = $r[0]['nickname'];
69 logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
70 header("Content-type: application/atom+xml");
71 echo DFRN::feed('', $user, $last_update, 0, $hidewall);
75 if (($type === 'profile') && (!strlen($sec))) {
79 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
83 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
84 $my_id = '1:' . $dfrn_id;
87 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
88 $my_id = '0:' . $dfrn_id;
91 goaway(System::baseUrl());
95 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
96 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
97 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
98 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
102 if (DBM::is_result($r)) {
103 $s = Network::fetchUrl($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
105 logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
108 $xml = XML::parseString($s);
110 if ((int) $xml->status === 1) {
111 $_SESSION['authenticated'] = 1;
112 if (!x($_SESSION, 'remote')) {
113 $_SESSION['remote'] = [];
116 $_SESSION['remote'][] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']];
118 $_SESSION['visitor_id'] = $r[0]['id'];
119 $_SESSION['visitor_home'] = $r[0]['url'];
120 $_SESSION['visitor_handle'] = $r[0]['addr'];
121 $_SESSION['visitor_visiting'] = $r[0]['uid'];
123 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
126 // Visitors get 1 day session.
127 $session_id = session_id();
128 $expire = time() + 86400;
129 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
135 $profile = $r[0]['nickname'];
136 goaway((strlen($destination_url)) ? $destination_url : System::baseUrl() . '/profile/' . $profile);
138 goaway(System::baseUrl());
141 if ($type === 'profile-check' && $dfrn_version < 2.2) {
142 if ((strlen($challenge)) && (strlen($sec))) {
143 dba::delete('profile_check', ["`expire` < ?", time()]);
144 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
147 if (!DBM::is_result($r)) {
148 System::xmlExit(3, 'No ticket');
152 $orig_id = $r[0]['dfrn_id'];
153 if (strpos($orig_id, ':')) {
154 $orig_id = substr($orig_id, 2);
157 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
160 if (!DBM::is_result($c)) {
161 System::xmlExit(3, 'No profile');
166 $sent_dfrn_id = hex2bin($dfrn_id);
167 $challenge = hex2bin($challenge);
171 if (($contact['duplex']) && strlen($contact['prvkey'])) {
172 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
173 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
175 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
176 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
179 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
181 if (strpos($final_dfrn_id, ':') == 1) {
182 $final_dfrn_id = substr($final_dfrn_id, 2);
185 if ($final_dfrn_id != $orig_id) {
186 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
187 // did not decode properly - cannot trust this site
188 System::xmlExit(3, 'Bad decryption');
191 header("Content-type: text/xml");
192 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
197 switch ($direction) {
199 $dfrn_id = '0:' . $dfrn_id;
202 $dfrn_id = '1:' . $dfrn_id;
208 dba::delete('profile_check', ["`expire` < ?", time()]);
209 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
211 if (DBM::is_result($r)) {
213 return; // NOTREACHED
216 return; // NOTREACHED
221 function dfrn_poll_post(App $a)
223 $dfrn_id = x($_POST,'dfrn_id') ? $_POST['dfrn_id'] : '';
224 $challenge = x($_POST,'challenge') ? $_POST['challenge'] : '';
225 $url = x($_POST,'url') ? $_POST['url'] : '';
226 $sec = x($_POST,'sec') ? $_POST['sec'] : '';
227 $ptype = x($_POST,'type') ? $_POST['type'] : '';
228 $dfrn_version = x($_POST,'dfrn_version') ? (float) $_POST['dfrn_version'] : 2.0;
229 $perm = x($_POST,'perm') ? $_POST['perm'] : 'r';
231 if ($ptype === 'profile-check') {
232 if (strlen($challenge) && strlen($sec)) {
233 logger('dfrn_poll: POST: profile-check');
235 dba::delete('profile_check', ["`expire` < ?", time()]);
236 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
239 if (!DBM::is_result($r)) {
240 System::xmlExit(3, 'No ticket');
244 $orig_id = $r[0]['dfrn_id'];
245 if (strpos($orig_id, ':')) {
246 $orig_id = substr($orig_id, 2);
249 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
252 if (!DBM::is_result($c)) {
253 System::xmlExit(3, 'No profile');
258 $sent_dfrn_id = hex2bin($dfrn_id);
259 $challenge = hex2bin($challenge);
263 if ($contact['duplex'] && strlen($contact['prvkey'])) {
264 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
265 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
267 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
268 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
271 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
273 if (strpos($final_dfrn_id, ':') == 1) {
274 $final_dfrn_id = substr($final_dfrn_id, 2);
277 if ($final_dfrn_id != $orig_id) {
278 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
279 // did not decode properly - cannot trust this site
280 System::xmlExit(3, 'Bad decryption');
283 header("Content-type: text/xml");
284 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
291 if (strpos($dfrn_id, ':') == 1) {
292 $direction = intval(substr($dfrn_id, 0, 1));
293 $dfrn_id = substr($dfrn_id, 2);
296 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
301 if (!DBM::is_result($r)) {
305 $type = $r[0]['type'];
306 $last_update = $r[0]['last_update'];
308 dba::delete('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge]);
311 switch ($direction) {
313 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
317 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
318 $my_id = '1:' . $dfrn_id;
321 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
322 $my_id = '0:' . $dfrn_id;
325 goaway(System::baseUrl());
329 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
330 if (!DBM::is_result($r)) {
335 $owner_uid = $r[0]['uid'];
336 $contact_id = $r[0]['id'];
338 if ($type === 'reputation' && strlen($url)) {
339 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
346 if (DBM::is_result($r)) {
347 $reputation = $r[0]['rating'];
348 $text = $r[0]['reason'];
350 if ($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
356 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
359 <rating>$reputation</rating>
360 <description>$text</description>
366 // Update the writable flag if it changed
367 logger('dfrn_poll: post request feed: ' . print_r($_POST, true), LOGGER_DATA);
368 if ($dfrn_version >= 2.21) {
369 if ($perm === 'rw') {
375 if ($writable != $contact['writable']) {
376 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
383 header("Content-type: application/atom+xml");
384 $o = DFRN::feed($dfrn_id, $a->argv[1], $last_update, $direction);
390 function dfrn_poll_content(App $a)
392 $dfrn_id = x($_GET,'dfrn_id') ? $_GET['dfrn_id'] : '';
393 $type = x($_GET,'type') ? $_GET['type'] : 'data';
394 $last_update = x($_GET,'last_update') ? $_GET['last_update'] : '';
395 $destination_url = x($_GET,'destination_url') ? $_GET['destination_url'] : '';
396 $sec = x($_GET,'sec') ? $_GET['sec'] : '';
397 $dfrn_version = x($_GET,'dfrn_version') ? (float) $_GET['dfrn_version'] : 2.0;
398 $perm = x($_GET,'perm') ? $_GET['perm'] : 'r';
399 $quiet = x($_GET,'quiet') ? true : false;
402 if (strpos($dfrn_id, ':') == 1) {
403 $direction = intval(substr($dfrn_id, 0, 1));
404 $dfrn_id = substr($dfrn_id, 2);
407 if ($dfrn_id != '') {
408 // initial communication from external contact
409 $hash = random_string();
413 dba::delete('challenge', ["`expire` < ?", time()]);
415 if ($type !== 'profile') {
416 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
417 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
420 intval(time() + 60 ),
427 switch ($direction) {
429 if ($type === 'profile') {
430 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
432 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
438 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
439 $my_id = '1:' . $dfrn_id;
442 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
443 $my_id = '0:' . $dfrn_id;
446 goaway(System::baseUrl());
450 $nickname = $a->argv[1];
452 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
453 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
454 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
455 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
458 if (DBM::is_result($r)) {
461 $id_str = $my_id . '.' . mt_rand(1000, 9999);
463 if (($r[0]['duplex'] && strlen($r[0]['pubkey'])) || !strlen($r[0]['prvkey'])) {
464 openssl_public_encrypt($hash, $challenge, $r[0]['pubkey']);
465 openssl_public_encrypt($id_str, $encrypted_id, $r[0]['pubkey']);
467 openssl_private_encrypt($hash, $challenge, $r[0]['prvkey']);
468 openssl_private_encrypt($id_str, $encrypted_id, $r[0]['prvkey']);
471 $challenge = bin2hex($challenge);
472 $encrypted_id = bin2hex($encrypted_id);
479 if (($type === 'profile') && (strlen($sec))) {
481 if ($dfrn_version < 2.2) {
482 $s = Network::fetchUrl($r[0]['poll']
483 . '?dfrn_id=' . $encrypted_id
484 . '&type=profile-check'
485 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
486 . '&challenge=' . $challenge
490 $s = Network::post($r[0]['poll'], [
491 'dfrn_id' => $encrypted_id,
492 'type' => 'profile-check',
493 'dfrn_version' => DFRN_PROTOCOL_VERSION,
494 'challenge' => $challenge,
499 $profile = ((DBM::is_result($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
501 switch ($destination_url) {
503 $dest = System::baseUrl() . '/profile/' . $profile . '?f=&tab=profile';
506 $dest = System::baseUrl() . '/photos/' . $profile;
510 $dest = System::baseUrl() . '/profile/' . $profile;
513 $appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
514 $dest = $destination_url . $appendix;
518 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
520 if (strlen($s) && strstr($s, '<?xml')) {
521 $xml = XML::parseString($s);
523 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml, true), LOGGER_DATA);
525 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
526 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
528 if (((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
529 $_SESSION['authenticated'] = 1;
530 if (!x($_SESSION, 'remote')) {
531 $_SESSION['remote'] = [];
534 $_SESSION['remote'][] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']];
535 $_SESSION['visitor_id'] = $r[0]['id'];
536 $_SESSION['visitor_home'] = $r[0]['url'];
537 $_SESSION['visitor_visiting'] = $r[0]['uid'];
539 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
542 // Visitors get 1 day session.
543 $session_id = session_id();
544 $expire = time() + 86400;
545 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
557 header("Content-type: text/xml");
558 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
559 . '<dfrn_poll>' . "\r\n"
560 . "\t" . '<status>' . $status . '</status>' . "\r\n"
561 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
562 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
563 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
564 . '</dfrn_poll>' . "\r\n";