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 if (($a->argc > 1) && ($dfrn_id == '') && !strstr($_SERVER["HTTP_USER_AGENT"], 'Friendica')) {
36 $nickname = $a->argv[1];
37 header("Content-type: application/atom+xml");
38 echo OStatus::feed($nickname, $last_update, 10);
44 if (strpos($dfrn_id, ':') == 1) {
45 $direction = intval(substr($dfrn_id, 0, 1));
46 $dfrn_id = substr($dfrn_id, 2);
51 if (($dfrn_id === '') && (!x($_POST, 'dfrn_id'))) {
52 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
53 System::httpExit(403);
58 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
62 System::httpExit(404);
65 $hidewall = ($r[0]['hidewall'] && !local_user());
67 $user = $r[0]['nickname'];
70 logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
71 header("Content-type: application/atom+xml");
72 echo DFRN::feed('', $user, $last_update, 0, $hidewall);
76 if (($type === 'profile') && (!strlen($sec))) {
80 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
84 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
85 $my_id = '1:' . $dfrn_id;
88 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
89 $my_id = '0:' . $dfrn_id;
92 goaway(System::baseUrl());
96 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
97 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
98 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
99 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
103 if (DBA::is_result($r)) {
104 $s = Network::fetchUrl($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
106 logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
109 $xml = XML::parseString($s);
111 if ((int) $xml->status === 1) {
112 $_SESSION['authenticated'] = 1;
113 if (!x($_SESSION, 'remote')) {
114 $_SESSION['remote'] = [];
117 $_SESSION['remote'][] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']];
119 $_SESSION['visitor_id'] = $r[0]['id'];
120 $_SESSION['visitor_home'] = $r[0]['url'];
121 $_SESSION['visitor_handle'] = $r[0]['addr'];
122 $_SESSION['visitor_visiting'] = $r[0]['uid'];
123 $_SESSION['my_url'] = $r[0]['url'];
125 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
128 // Visitors get 1 day session.
129 $session_id = session_id();
130 $expire = time() + 86400;
131 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
137 $profile = $r[0]['nickname'];
138 goaway((strlen($destination_url)) ? $destination_url : System::baseUrl() . '/profile/' . $profile);
140 goaway(System::baseUrl());
143 if ($type === 'profile-check' && $dfrn_version < 2.2) {
144 if ((strlen($challenge)) && (strlen($sec))) {
145 DBA::delete('profile_check', ["`expire` < ?", time()]);
146 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
149 if (!DBA::is_result($r)) {
150 System::xmlExit(3, 'No ticket');
154 $orig_id = $r[0]['dfrn_id'];
155 if (strpos($orig_id, ':')) {
156 $orig_id = substr($orig_id, 2);
159 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
162 if (!DBA::is_result($c)) {
163 System::xmlExit(3, 'No profile');
168 $sent_dfrn_id = hex2bin($dfrn_id);
169 $challenge = hex2bin($challenge);
173 if (($contact['duplex']) && strlen($contact['prvkey'])) {
174 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
175 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
177 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
178 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
181 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
183 if (strpos($final_dfrn_id, ':') == 1) {
184 $final_dfrn_id = substr($final_dfrn_id, 2);
187 if ($final_dfrn_id != $orig_id) {
188 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
189 // did not decode properly - cannot trust this site
190 System::xmlExit(3, 'Bad decryption');
193 header("Content-type: text/xml");
194 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
199 switch ($direction) {
201 $dfrn_id = '0:' . $dfrn_id;
204 $dfrn_id = '1:' . $dfrn_id;
210 DBA::delete('profile_check', ["`expire` < ?", time()]);
211 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
213 if (DBA::is_result($r)) {
215 return; // NOTREACHED
218 return; // NOTREACHED
223 function dfrn_poll_post(App $a)
225 $dfrn_id = x($_POST,'dfrn_id') ? $_POST['dfrn_id'] : '';
226 $challenge = x($_POST,'challenge') ? $_POST['challenge'] : '';
227 $url = x($_POST,'url') ? $_POST['url'] : '';
228 $sec = x($_POST,'sec') ? $_POST['sec'] : '';
229 $ptype = x($_POST,'type') ? $_POST['type'] : '';
230 $dfrn_version = x($_POST,'dfrn_version') ? (float) $_POST['dfrn_version'] : 2.0;
231 $perm = x($_POST,'perm') ? $_POST['perm'] : 'r';
233 if ($ptype === 'profile-check') {
234 if (strlen($challenge) && strlen($sec)) {
235 logger('dfrn_poll: POST: profile-check');
237 DBA::delete('profile_check', ["`expire` < ?", time()]);
238 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
241 if (!DBA::is_result($r)) {
242 System::xmlExit(3, 'No ticket');
246 $orig_id = $r[0]['dfrn_id'];
247 if (strpos($orig_id, ':')) {
248 $orig_id = substr($orig_id, 2);
251 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
254 if (!DBA::is_result($c)) {
255 System::xmlExit(3, 'No profile');
260 $sent_dfrn_id = hex2bin($dfrn_id);
261 $challenge = hex2bin($challenge);
265 if ($contact['duplex'] && strlen($contact['prvkey'])) {
266 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
267 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
269 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
270 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
273 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
275 if (strpos($final_dfrn_id, ':') == 1) {
276 $final_dfrn_id = substr($final_dfrn_id, 2);
279 if ($final_dfrn_id != $orig_id) {
280 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
281 // did not decode properly - cannot trust this site
282 System::xmlExit(3, 'Bad decryption');
285 header("Content-type: text/xml");
286 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
293 if (strpos($dfrn_id, ':') == 1) {
294 $direction = intval(substr($dfrn_id, 0, 1));
295 $dfrn_id = substr($dfrn_id, 2);
298 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
303 if (!DBA::is_result($r)) {
307 $type = $r[0]['type'];
308 $last_update = $r[0]['last_update'];
310 DBA::delete('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge]);
313 switch ($direction) {
315 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
319 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
320 $my_id = '1:' . $dfrn_id;
323 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
324 $my_id = '0:' . $dfrn_id;
327 goaway(System::baseUrl());
331 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
332 if (!DBA::is_result($r)) {
337 $owner_uid = $r[0]['uid'];
338 $contact_id = $r[0]['id'];
340 if ($type === 'reputation' && strlen($url)) {
341 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
348 if (DBA::is_result($r)) {
349 $reputation = $r[0]['rating'];
350 $text = $r[0]['reason'];
352 if ($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
358 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
361 <rating>$reputation</rating>
362 <description>$text</description>
368 // Update the writable flag if it changed
369 logger('dfrn_poll: post request feed: ' . print_r($_POST, true), LOGGER_DATA);
370 if ($dfrn_version >= 2.21) {
371 if ($perm === 'rw') {
377 if ($writable != $contact['writable']) {
378 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
385 header("Content-type: application/atom+xml");
386 $o = DFRN::feed($dfrn_id, $a->argv[1], $last_update, $direction);
392 function dfrn_poll_content(App $a)
394 $dfrn_id = x($_GET,'dfrn_id') ? $_GET['dfrn_id'] : '';
395 $type = x($_GET,'type') ? $_GET['type'] : 'data';
396 $last_update = x($_GET,'last_update') ? $_GET['last_update'] : '';
397 $destination_url = x($_GET,'destination_url') ? $_GET['destination_url'] : '';
398 $sec = x($_GET,'sec') ? $_GET['sec'] : '';
399 $dfrn_version = x($_GET,'dfrn_version') ? (float) $_GET['dfrn_version'] : 2.0;
400 $perm = x($_GET,'perm') ? $_GET['perm'] : 'r';
401 $quiet = x($_GET,'quiet') ? true : false;
404 if (strpos($dfrn_id, ':') == 1) {
405 $direction = intval(substr($dfrn_id, 0, 1));
406 $dfrn_id = substr($dfrn_id, 2);
409 if ($dfrn_id != '') {
410 // initial communication from external contact
411 $hash = random_string();
415 DBA::delete('challenge', ["`expire` < ?", time()]);
417 if ($type !== 'profile') {
418 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
419 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
422 intval(time() + 60 ),
429 switch ($direction) {
431 if ($type === 'profile') {
432 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
434 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
440 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
441 $my_id = '1:' . $dfrn_id;
444 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
445 $my_id = '0:' . $dfrn_id;
448 goaway(System::baseUrl());
452 $nickname = $a->argv[1];
454 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
455 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
456 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
457 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
460 if (DBA::is_result($r)) {
463 $id_str = $my_id . '.' . mt_rand(1000, 9999);
465 if (($r[0]['duplex'] && strlen($r[0]['pubkey'])) || !strlen($r[0]['prvkey'])) {
466 openssl_public_encrypt($hash, $challenge, $r[0]['pubkey']);
467 openssl_public_encrypt($id_str, $encrypted_id, $r[0]['pubkey']);
469 openssl_private_encrypt($hash, $challenge, $r[0]['prvkey']);
470 openssl_private_encrypt($id_str, $encrypted_id, $r[0]['prvkey']);
473 $challenge = bin2hex($challenge);
474 $encrypted_id = bin2hex($encrypted_id);
481 if (($type === 'profile') && (strlen($sec))) {
483 if ($dfrn_version < 2.2) {
484 $s = Network::fetchUrl($r[0]['poll']
485 . '?dfrn_id=' . $encrypted_id
486 . '&type=profile-check'
487 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
488 . '&challenge=' . $challenge
492 $s = Network::post($r[0]['poll'], [
493 'dfrn_id' => $encrypted_id,
494 'type' => 'profile-check',
495 'dfrn_version' => DFRN_PROTOCOL_VERSION,
496 'challenge' => $challenge,
501 $profile = ((DBA::is_result($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
503 switch ($destination_url) {
505 $dest = System::baseUrl() . '/profile/' . $profile . '?f=&tab=profile';
508 $dest = System::baseUrl() . '/photos/' . $profile;
512 $dest = System::baseUrl() . '/profile/' . $profile;
515 $appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
516 $dest = $destination_url . $appendix;
520 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
522 if (strlen($s) && strstr($s, '<?xml')) {
523 $xml = XML::parseString($s);
525 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml, true), LOGGER_DATA);
527 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
528 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
530 if (((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
531 $_SESSION['authenticated'] = 1;
532 if (!x($_SESSION, 'remote')) {
533 $_SESSION['remote'] = [];
536 $_SESSION['remote'][] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']];
537 $_SESSION['visitor_id'] = $r[0]['id'];
538 $_SESSION['visitor_home'] = $r[0]['url'];
539 $_SESSION['visitor_visiting'] = $r[0]['uid'];
540 $_SESSION['my_url'] = $r[0]['url'];
542 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
545 // Visitors get 1 day session.
546 $session_id = session_id();
547 $expire = time() + 86400;
548 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
560 header("Content-type: text/xml");
561 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
562 . '<dfrn_poll>' . "\r\n"
563 . "\t" . '<status>' . $status . '</status>' . "\r\n"
564 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
565 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
566 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
567 . '</dfrn_poll>' . "\r\n";