5 require_once('include/items.php');
6 require_once('include/auth.php');
9 function dfrn_poll_init(&$a) {
12 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
13 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
14 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
15 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
16 $challenge = ((x($_GET,'challenge')) ? $_GET['challenge'] : '');
17 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
18 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
23 if(strpos($dfrn_id,':') == 1) {
24 $direction = intval(substr($dfrn_id,0,1));
25 $dfrn_id = substr($dfrn_id,2);
28 if(($dfrn_id === '') && (! x($_POST,'dfrn_id')) && ($a->argc > 1)) {
29 header("Content-type: application/atom+xml");
30 $o = get_feed_for($a, '*', $a->argv[1],$last_update);
35 if(($type === 'profile') && (! strlen($sec))) {
40 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
44 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
45 $my_id = '1:' . $dfrn_id;
48 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
49 $my_id = '0:' . $dfrn_id;
52 goaway($a->get_baseurl());
56 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
57 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
58 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
59 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
65 $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
67 logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
71 $xml = simplexml_load_string($s);
73 if((int) $xml->status == 1) {
74 $_SESSION['authenticated'] = 1;
75 $_SESSION['visitor_id'] = $r[0]['id'];
76 $_SESSION['visitor_home'] = $r[0]['url'];
77 notice( $r[0]['username'] . t(' welcomes ') . $r[0]['name'] . EOL);
78 // Visitors get 1 day session.
79 $session_id = session_id();
80 $expire = time() + 86400;
81 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
87 $profile = $r[0]['nickname'];
88 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
90 goaway($a->get_baseurl());
94 if($type === 'profile-check') {
96 if((strlen($challenge)) && (strlen($sec))) {
98 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
99 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
103 xml_status(3, 'No ticket');
106 $orig_id = $r[0]['dfrn_id'];
107 if(strpos($orig_id, ':'))
108 $orig_id = substr($orig_id,2);
110 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
114 xml_status(3, 'No profile');
118 $sent_dfrn_id = hex2bin($dfrn_id);
119 $challenge = hex2bin($challenge);
123 if(($contact['duplex']) && strlen($contact['prvkey'])) {
124 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
125 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
128 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
129 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
132 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
134 if(strpos($final_dfrn_id,':') == 1)
135 $final_dfrn_id = substr($final_dfrn_id,2);
137 if($final_dfrn_id != $orig_id) {
138 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
139 // did not decode properly - cannot trust this site
140 xml_status(3, 'Bad decryption');
143 header("Content-type: text/xml");
144 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
153 $dfrn_id = '0:' . $dfrn_id;
156 $dfrn_id = '1:' . $dfrn_id;
163 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
164 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
168 return; // NOTREACHED
171 return; // NOTREACHED
179 function dfrn_poll_post(&$a) {
181 $dfrn_id = ((x($_POST,'dfrn_id')) ? $_POST['dfrn_id'] : '');
182 $challenge = ((x($_POST,'challenge')) ? $_POST['challenge'] : '');
183 $url = ((x($_POST,'url')) ? $_POST['url'] : '');
184 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
187 if(strpos($dfrn_id,':') == 1) {
188 $direction = intval(substr($dfrn_id,0,1));
189 $dfrn_id = substr($dfrn_id,2);
193 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
201 $type = $r[0]['type'];
202 $last_update = $r[0]['last_update'];
204 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
213 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
217 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
218 $my_id = '1:' . $dfrn_id;
221 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
222 $my_id = '0:' . $dfrn_id;
225 goaway($a->get_baseurl());
230 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
236 $owner_uid = $r[0]['uid'];
237 $contact_id = $r[0]['id'];
240 if($type === 'reputation' && strlen($url)) {
241 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
249 $reputation = $r[0]['rating'];
250 $text = $r[0]['reason'];
252 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
258 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
261 <rating>$reputation</rating>
262 <description>$text</description>
269 header("Content-type: application/atom+xml");
270 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
277 function dfrn_poll_content(&$a) {
279 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
280 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
281 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
282 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
283 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
284 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
287 if(strpos($dfrn_id,':') == 1) {
288 $direction = intval(substr($dfrn_id,0,1));
289 $dfrn_id = substr($dfrn_id,2);
294 // initial communication from external contact
295 $hash = random_string();
299 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
301 if($type !== 'profile') {
302 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
303 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
306 intval(time() + 60 ),
314 if($type === 'profile')
315 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
317 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
321 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
322 $my_id = '1:' . $dfrn_id;
325 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
326 $my_id = '0:' . $dfrn_id;
329 goaway($a->get_baseurl());
333 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
334 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
335 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
336 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
344 $id_str = $my_id . '.' . mt_rand(1000,9999);
346 if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
347 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
348 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
351 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
352 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
355 $challenge = bin2hex($challenge);
356 $encrypted_id = bin2hex($encrypted_id);
364 if(($type === 'profile') && (strlen($sec))) {
367 $s = fetch_url($r[0]['poll']
368 . '?dfrn_id=' . $encrypted_id
369 . '&type=profile-check'
370 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
371 . '&challenge=' . $challenge
375 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
377 if(strlen($s) && strstr($s,'<?xml')) {
379 $xml = simplexml_load_string($s);
381 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
383 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
384 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
387 if(((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
388 $_SESSION['authenticated'] = 1;
389 $_SESSION['visitor_id'] = $r[0]['id'];
390 $_SESSION['visitor_home'] = $r[0]['url'];
391 notice( $r[0]['username'] . t(' welcomes ') . $r[0]['name'] . EOL);
392 // Visitors get 1 day session.
393 $session_id = session_id();
394 $expire = time() + 86400;
395 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
400 $profile = $r[0]['nickname'];
401 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
403 goaway($a->get_baseurl());
409 header("Content-type: text/xml");
410 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
411 . '<dfrn_poll>' . "\r\n"
412 . "\t" . '<status>' . $status . '</status>' . "\r\n"
413 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
414 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
415 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
416 . '</dfrn_poll>' . "\r\n" ;