4 * @file mod/dfrn_poll.php
7 use Friendica\Core\Config;
8 use Friendica\Core\System;
9 use Friendica\Database\DBM;
10 use Friendica\Module\Login;
11 use Friendica\Protocol\DFRN;
12 use Friendica\Protocol\OStatus;
14 require_once 'include/items.php';
16 function dfrn_poll_init(App $a)
20 $dfrn_id = defaults($_GET, 'dfrn_id' , '');
21 $type = defaults($_GET, 'type' , 'data');
22 $last_update = defaults($_GET, 'last_update' , '');
23 $destination_url = defaults($_GET, 'destination_url', '');
24 $challenge = defaults($_GET, 'challenge' , '');
25 $sec = defaults($_GET, 'sec' , '');
26 $dfrn_version = (float) defaults($_GET, 'dfrn_version' , 2.0);
27 $perm = defaults($_GET, 'perm' , 'r');
28 $quiet = x($_GET, 'quiet');
30 // Possibly it is an OStatus compatible server that requests a user feed
31 if (($a->argc > 1) && ($dfrn_id == '') && !strstr($_SERVER["HTTP_USER_AGENT"], 'Friendica')) {
32 $nickname = $a->argv[1];
33 header("Content-type: application/atom+xml");
34 echo OStatus::feed($nickname, $last_update, 10);
40 if (strpos($dfrn_id, ':') == 1) {
41 $direction = intval(substr($dfrn_id, 0, 1));
42 $dfrn_id = substr($dfrn_id, 2);
47 if (($dfrn_id === '') && (!x($_POST, 'dfrn_id'))) {
48 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
49 http_status_exit(403);
54 $r = q("SELECT `hidewall`,`nickname` FROM `user` WHERE `user`.`nickname` = '%s' LIMIT 1",
58 http_status_exit(404);
61 $hidewall = ($r[0]['hidewall'] && !local_user());
63 $user = $r[0]['nickname'];
66 logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
67 header("Content-type: application/atom+xml");
68 echo DFRN::feed('', $user, $last_update, 0, $hidewall);
72 if (($type === 'profile') && (!strlen($sec))) {
76 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
80 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
81 $my_id = '1:' . $dfrn_id;
84 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
85 $my_id = '0:' . $dfrn_id;
88 goaway(System::baseUrl());
92 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
93 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
94 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
95 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
99 if (DBM::is_result($r)) {
100 $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
102 logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
105 $xml = parse_xml_string($s);
107 if ((int) $xml->status === 1) {
108 $_SESSION['authenticated'] = 1;
109 if (!x($_SESSION, 'remote')) {
110 $_SESSION['remote'] = array();
113 $_SESSION['remote'][] = array('cid' => $r[0]['id'], 'uid' => $r[0]['uid'], 'url' => $r[0]['url']);
115 $_SESSION['visitor_id'] = $r[0]['id'];
116 $_SESSION['visitor_home'] = $r[0]['url'];
117 $_SESSION['visitor_handle'] = $r[0]['addr'];
118 $_SESSION['visitor_visiting'] = $r[0]['uid'];
120 info(sprintf(t('%1$s welcomes %2$s'), $r[0]['username'], $r[0]['name']) . EOL);
123 // Visitors get 1 day session.
124 $session_id = session_id();
125 $expire = time() + 86400;
126 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
132 $profile = $r[0]['nickname'];
133 goaway((strlen($destination_url)) ? $destination_url : System::baseUrl() . '/profile/' . $profile);
135 goaway(System::baseUrl());
138 if ($type === 'profile-check' && $dfrn_version < 2.2) {
139 if ((strlen($challenge)) && (strlen($sec))) {
140 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
141 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
144 if (!DBM::is_result($r)) {
145 xml_status(3, 'No ticket');
149 $orig_id = $r[0]['dfrn_id'];
150 if (strpos($orig_id, ':')) {
151 $orig_id = substr($orig_id, 2);
154 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
157 if (!DBM::is_result($c)) {
158 xml_status(3, 'No profile');
163 $sent_dfrn_id = hex2bin($dfrn_id);
164 $challenge = hex2bin($challenge);
168 if (($contact['duplex']) && strlen($contact['prvkey'])) {
169 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
170 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
172 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
173 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
176 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
178 if (strpos($final_dfrn_id, ':') == 1) {
179 $final_dfrn_id = substr($final_dfrn_id, 2);
182 if ($final_dfrn_id != $orig_id) {
183 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
184 // did not decode properly - cannot trust this site
185 xml_status(3, 'Bad decryption');
188 header("Content-type: text/xml");
189 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
194 switch ($direction) {
196 $dfrn_id = '0:' . $dfrn_id;
199 $dfrn_id = '1:' . $dfrn_id;
205 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
206 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
208 if (DBM::is_result($r)) {
210 return; // NOTREACHED
213 return; // NOTREACHED
218 function dfrn_poll_post(App $a)
220 $dfrn_id = x($_POST,'dfrn_id') ? $_POST['dfrn_id'] : '';
221 $challenge = x($_POST,'challenge') ? $_POST['challenge'] : '';
222 $url = x($_POST,'url') ? $_POST['url'] : '';
223 $sec = x($_POST,'sec') ? $_POST['sec'] : '';
224 $ptype = x($_POST,'type') ? $_POST['type'] : '';
225 $dfrn_version = x($_POST,'dfrn_version') ? (float) $_POST['dfrn_version'] : 2.0;
226 $perm = x($_POST,'perm') ? $_POST['perm'] : 'r';
228 if ($ptype === 'profile-check') {
229 if (strlen($challenge) && strlen($sec)) {
230 logger('dfrn_poll: POST: profile-check');
232 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
233 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
236 if (!DBM::is_result($r)) {
237 xml_status(3, 'No ticket');
241 $orig_id = $r[0]['dfrn_id'];
242 if (strpos($orig_id, ':')) {
243 $orig_id = substr($orig_id, 2);
246 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
249 if (!DBM::is_result($c)) {
250 xml_status(3, 'No profile');
255 $sent_dfrn_id = hex2bin($dfrn_id);
256 $challenge = hex2bin($challenge);
260 if ($contact['duplex'] && strlen($contact['prvkey'])) {
261 openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
262 openssl_private_decrypt($challenge, $decoded_challenge, $contact['prvkey']);
264 openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
265 openssl_public_decrypt($challenge, $decoded_challenge, $contact['pubkey']);
268 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
270 if (strpos($final_dfrn_id, ':') == 1) {
271 $final_dfrn_id = substr($final_dfrn_id, 2);
274 if ($final_dfrn_id != $orig_id) {
275 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
276 // did not decode properly - cannot trust this site
277 xml_status(3, 'Bad decryption');
280 header("Content-type: text/xml");
281 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
288 if (strpos($dfrn_id, ':') == 1) {
289 $direction = intval(substr($dfrn_id, 0, 1));
290 $dfrn_id = substr($dfrn_id, 2);
293 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
298 if (!DBM::is_result($r)) {
302 $type = $r[0]['type'];
303 $last_update = $r[0]['last_update'];
305 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'",
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 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(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 = fetch_url($r[0]['poll']
484 . '?dfrn_id=' . $encrypted_id
485 . '&type=profile-check'
486 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
487 . '&challenge=' . $challenge
491 $s = post_url($r[0]['poll'], array(
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 = parse_xml_string($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'] = array();
535 $_SESSION['remote'][] = array('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'];
540 info(sprintf(t('%1$s welcomes %2$s'), $r[0]['username'], $r[0]['name']) . EOL);
543 // Visitors get 1 day session.
544 $session_id = session_id();
545 $expire = time() + 86400;
546 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'",
558 header("Content-type: text/xml");
559 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
560 . '<dfrn_poll>' . "\r\n"
561 . "\t" . '<status>' . $status . '</status>' . "\r\n"
562 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
563 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
564 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
565 . '</dfrn_poll>' . "\r\n";