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'];
122 $_SESSION['my_url'] = $r[0]['url'];
124 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
127 // Visitors get 1 day session.
128 $session_id = session_id();
129 $expire = time() + 86400;
130 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
136 $profile = $r[0]['nickname'];
137 goaway((strlen($destination_url)) ? $destination_url : System::baseUrl() . '/profile/' . $profile);
139 goaway(System::baseUrl());
142 if ($type === 'profile-check' && $dfrn_version < 2.2) {
143 if ((strlen($challenge)) && (strlen($sec))) {
144 dba::delete('profile_check', ["`expire` < ?", time()]);
145 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
148 if (!DBM::is_result($r)) {
149 System::xmlExit(3, 'No ticket');
153 $orig_id = $r[0]['dfrn_id'];
154 if (strpos($orig_id, ':')) {
155 $orig_id = substr($orig_id, 2);
158 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
161 if (!DBM::is_result($c)) {
162 System::xmlExit(3, 'No profile');
167 $sent_dfrn_id = hex2bin($dfrn_id);
168 $challenge = hex2bin($challenge);
172 if (($contact['duplex']) && strlen($contact['prvkey'])) {
173 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
174 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
176 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
177 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
180 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
182 if (strpos($final_dfrn_id, ':') == 1) {
183 $final_dfrn_id = substr($final_dfrn_id, 2);
186 if ($final_dfrn_id != $orig_id) {
187 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
188 // did not decode properly - cannot trust this site
189 System::xmlExit(3, 'Bad decryption');
192 header("Content-type: text/xml");
193 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
198 switch ($direction) {
200 $dfrn_id = '0:' . $dfrn_id;
203 $dfrn_id = '1:' . $dfrn_id;
209 dba::delete('profile_check', ["`expire` < ?", time()]);
210 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
212 if (DBM::is_result($r)) {
214 return; // NOTREACHED
217 return; // NOTREACHED
222 function dfrn_poll_post(App $a)
224 $dfrn_id = x($_POST,'dfrn_id') ? $_POST['dfrn_id'] : '';
225 $challenge = x($_POST,'challenge') ? $_POST['challenge'] : '';
226 $url = x($_POST,'url') ? $_POST['url'] : '';
227 $sec = x($_POST,'sec') ? $_POST['sec'] : '';
228 $ptype = x($_POST,'type') ? $_POST['type'] : '';
229 $dfrn_version = x($_POST,'dfrn_version') ? (float) $_POST['dfrn_version'] : 2.0;
230 $perm = x($_POST,'perm') ? $_POST['perm'] : 'r';
232 if ($ptype === 'profile-check') {
233 if (strlen($challenge) && strlen($sec)) {
234 logger('dfrn_poll: POST: profile-check');
236 dba::delete('profile_check', ["`expire` < ?", time()]);
237 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
240 if (!DBM::is_result($r)) {
241 System::xmlExit(3, 'No ticket');
245 $orig_id = $r[0]['dfrn_id'];
246 if (strpos($orig_id, ':')) {
247 $orig_id = substr($orig_id, 2);
250 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
253 if (!DBM::is_result($c)) {
254 System::xmlExit(3, 'No profile');
259 $sent_dfrn_id = hex2bin($dfrn_id);
260 $challenge = hex2bin($challenge);
264 if ($contact['duplex'] && strlen($contact['prvkey'])) {
265 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
266 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
268 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
269 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
272 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
274 if (strpos($final_dfrn_id, ':') == 1) {
275 $final_dfrn_id = substr($final_dfrn_id, 2);
278 if ($final_dfrn_id != $orig_id) {
279 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
280 // did not decode properly - cannot trust this site
281 System::xmlExit(3, 'Bad decryption');
284 header("Content-type: text/xml");
285 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
292 if (strpos($dfrn_id, ':') == 1) {
293 $direction = intval(substr($dfrn_id, 0, 1));
294 $dfrn_id = substr($dfrn_id, 2);
297 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
302 if (!DBM::is_result($r)) {
306 $type = $r[0]['type'];
307 $last_update = $r[0]['last_update'];
309 dba::delete('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge]);
312 switch ($direction) {
314 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
318 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
319 $my_id = '1:' . $dfrn_id;
322 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
323 $my_id = '0:' . $dfrn_id;
326 goaway(System::baseUrl());
330 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
331 if (!DBM::is_result($r)) {
336 $owner_uid = $r[0]['uid'];
337 $contact_id = $r[0]['id'];
339 if ($type === 'reputation' && strlen($url)) {
340 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
347 if (DBM::is_result($r)) {
348 $reputation = $r[0]['rating'];
349 $text = $r[0]['reason'];
351 if ($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
357 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
360 <rating>$reputation</rating>
361 <description>$text</description>
367 // Update the writable flag if it changed
368 logger('dfrn_poll: post request feed: ' . print_r($_POST, true), LOGGER_DATA);
369 if ($dfrn_version >= 2.21) {
370 if ($perm === 'rw') {
376 if ($writable != $contact['writable']) {
377 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d",
384 header("Content-type: application/atom+xml");
385 $o = DFRN::feed($dfrn_id, $a->argv[1], $last_update, $direction);
391 function dfrn_poll_content(App $a)
393 $dfrn_id = x($_GET,'dfrn_id') ? $_GET['dfrn_id'] : '';
394 $type = x($_GET,'type') ? $_GET['type'] : 'data';
395 $last_update = x($_GET,'last_update') ? $_GET['last_update'] : '';
396 $destination_url = x($_GET,'destination_url') ? $_GET['destination_url'] : '';
397 $sec = x($_GET,'sec') ? $_GET['sec'] : '';
398 $dfrn_version = x($_GET,'dfrn_version') ? (float) $_GET['dfrn_version'] : 2.0;
399 $perm = x($_GET,'perm') ? $_GET['perm'] : 'r';
400 $quiet = x($_GET,'quiet') ? true : false;
403 if (strpos($dfrn_id, ':') == 1) {
404 $direction = intval(substr($dfrn_id, 0, 1));
405 $dfrn_id = substr($dfrn_id, 2);
408 if ($dfrn_id != '') {
409 // initial communication from external contact
410 $hash = random_string();
414 dba::delete('challenge', ["`expire` < ?", time()]);
416 if ($type !== 'profile') {
417 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
418 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
421 intval(time() + 60 ),
428 switch ($direction) {
430 if ($type === 'profile') {
431 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
433 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
439 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
440 $my_id = '1:' . $dfrn_id;
443 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
444 $my_id = '0:' . $dfrn_id;
447 goaway(System::baseUrl());
451 $nickname = $a->argv[1];
453 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
454 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
455 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
456 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
459 if (DBM::is_result($r)) {
462 $id_str = $my_id . '.' . mt_rand(1000, 9999);
464 if (($r[0]['duplex'] && strlen($r[0]['pubkey'])) || !strlen($r[0]['prvkey'])) {
465 openssl_public_encrypt($hash, $challenge, $r[0]['pubkey']);
466 openssl_public_encrypt($id_str, $encrypted_id, $r[0]['pubkey']);
468 openssl_private_encrypt($hash, $challenge, $r[0]['prvkey']);
469 openssl_private_encrypt($id_str, $encrypted_id, $r[0]['prvkey']);
472 $challenge = bin2hex($challenge);
473 $encrypted_id = bin2hex($encrypted_id);
480 if (($type === 'profile') && (strlen($sec))) {
482 if ($dfrn_version < 2.2) {
483 $s = Network::fetchUrl($r[0]['poll']
484 . '?dfrn_id=' . $encrypted_id
485 . '&type=profile-check'
486 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
487 . '&challenge=' . $challenge
491 $s = Network::post($r[0]['poll'], [
492 'dfrn_id' => $encrypted_id,
493 'type' => 'profile-check',
494 'dfrn_version' => DFRN_PROTOCOL_VERSION,
495 'challenge' => $challenge,
500 $profile = ((DBM::is_result($r) && $r[0]['nickname']) ? $r[0]['nickname'] : $nickname);
502 switch ($destination_url) {
504 $dest = System::baseUrl() . '/profile/' . $profile . '?f=&tab=profile';
507 $dest = System::baseUrl() . '/photos/' . $profile;
511 $dest = System::baseUrl() . '/profile/' . $profile;
514 $appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
515 $dest = $destination_url . $appendix;
519 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
521 if (strlen($s) && strstr($s, '<?xml')) {
522 $xml = XML::parseString($s);
524 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml, true), LOGGER_DATA);
526 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
527 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
529 if (((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
530 $_SESSION['authenticated'] = 1;
531 if (!x($_SESSION, 'remote')) {
532 $_SESSION['remote'] = [];
535 $_SESSION['remote'][] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']];
536 $_SESSION['visitor_id'] = $r[0]['id'];
537 $_SESSION['visitor_home'] = $r[0]['url'];
538 $_SESSION['visitor_visiting'] = $r[0]['uid'];
539 $_SESSION['my_url'] = $r[0]['url'];
541 info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
544 // Visitors get 1 day session.
545 $session_id = session_id();
546 $expire = time() + 86400;
547 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
559 header("Content-type: text/xml");
560 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
561 . '<dfrn_poll>' . "\r\n"
562 . "\t" . '<status>' . $status . '</status>' . "\r\n"
563 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
564 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
565 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
566 . '</dfrn_poll>' . "\r\n";