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 logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] );
30 header("Content-type: application/atom+xml");
31 $o = get_feed_for($a, '', $a->argv[1],$last_update);
36 if(($type === 'profile') && (! strlen($sec))) {
41 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
45 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
46 $my_id = '1:' . $dfrn_id;
49 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
50 $my_id = '0:' . $dfrn_id;
53 goaway($a->get_baseurl());
57 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
58 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
59 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
60 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
66 $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
68 logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
72 $xml = simplexml_load_string($s);
74 if((int) $xml->status == 1) {
75 $_SESSION['authenticated'] = 1;
76 $_SESSION['visitor_id'] = $r[0]['id'];
77 $_SESSION['visitor_home'] = $r[0]['url'];
78 notice( sprintf(t('%s welcomes %s'), $r[0]['username'] , $r[0]['name']) . EOL);
79 // Visitors get 1 day session.
80 $session_id = session_id();
81 $expire = time() + 86400;
82 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
88 $profile = $r[0]['nickname'];
89 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
91 goaway($a->get_baseurl());
95 if($type === 'profile-check') {
97 if((strlen($challenge)) && (strlen($sec))) {
99 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
100 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
104 xml_status(3, 'No ticket');
107 $orig_id = $r[0]['dfrn_id'];
108 if(strpos($orig_id, ':'))
109 $orig_id = substr($orig_id,2);
111 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
115 xml_status(3, 'No profile');
119 $sent_dfrn_id = hex2bin($dfrn_id);
120 $challenge = hex2bin($challenge);
124 if(($contact['duplex']) && strlen($contact['prvkey'])) {
125 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
126 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
129 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
130 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
133 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
135 if(strpos($final_dfrn_id,':') == 1)
136 $final_dfrn_id = substr($final_dfrn_id,2);
138 if($final_dfrn_id != $orig_id) {
139 logger('profile_check: ' . $final_dfrn_id . ' != ' . $orig_id, LOGGER_DEBUG);
140 // did not decode properly - cannot trust this site
141 xml_status(3, 'Bad decryption');
144 header("Content-type: text/xml");
145 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
154 $dfrn_id = '0:' . $dfrn_id;
157 $dfrn_id = '1:' . $dfrn_id;
164 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
165 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
169 return; // NOTREACHED
172 return; // NOTREACHED
180 function dfrn_poll_post(&$a) {
182 $dfrn_id = ((x($_POST,'dfrn_id')) ? $_POST['dfrn_id'] : '');
183 $challenge = ((x($_POST,'challenge')) ? $_POST['challenge'] : '');
184 $url = ((x($_POST,'url')) ? $_POST['url'] : '');
185 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
188 if(strpos($dfrn_id,':') == 1) {
189 $direction = intval(substr($dfrn_id,0,1));
190 $dfrn_id = substr($dfrn_id,2);
194 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
202 $type = $r[0]['type'];
203 $last_update = $r[0]['last_update'];
205 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
214 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
218 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
219 $my_id = '1:' . $dfrn_id;
222 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
223 $my_id = '0:' . $dfrn_id;
226 goaway($a->get_baseurl());
231 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
237 $owner_uid = $r[0]['uid'];
238 $contact_id = $r[0]['id'];
241 if($type === 'reputation' && strlen($url)) {
242 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
250 $reputation = $r[0]['rating'];
251 $text = $r[0]['reason'];
253 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
259 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
262 <rating>$reputation</rating>
263 <description>$text</description>
270 header("Content-type: application/atom+xml");
271 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
278 function dfrn_poll_content(&$a) {
280 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
281 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
282 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
283 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
284 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
285 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
288 if(strpos($dfrn_id,':') == 1) {
289 $direction = intval(substr($dfrn_id,0,1));
290 $dfrn_id = substr($dfrn_id,2);
295 // initial communication from external contact
296 $hash = random_string();
300 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
302 if($type !== 'profile') {
303 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
304 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
307 intval(time() + 60 ),
315 if($type === 'profile')
316 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
318 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
322 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
323 $my_id = '1:' . $dfrn_id;
326 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
327 $my_id = '0:' . $dfrn_id;
330 goaway($a->get_baseurl());
334 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
335 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
336 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
337 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
345 $id_str = $my_id . '.' . mt_rand(1000,9999);
347 if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
348 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
349 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
352 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
353 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
356 $challenge = bin2hex($challenge);
357 $encrypted_id = bin2hex($encrypted_id);
365 if(($type === 'profile') && (strlen($sec))) {
368 $s = fetch_url($r[0]['poll']
369 . '?dfrn_id=' . $encrypted_id
370 . '&type=profile-check'
371 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
372 . '&challenge=' . $challenge
376 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
378 if(strlen($s) && strstr($s,'<?xml')) {
380 $xml = simplexml_load_string($s);
382 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
384 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
385 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
388 if(((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
389 $_SESSION['authenticated'] = 1;
390 $_SESSION['visitor_id'] = $r[0]['id'];
391 $_SESSION['visitor_home'] = $r[0]['url'];
392 notice( sprintf(t('%s welcomes %s'), $r[0]['username'] , $r[0]['name']) . EOL);
393 // Visitors get 1 day session.
394 $session_id = session_id();
395 $expire = time() + 86400;
396 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
401 $profile = $r[0]['nickname'];
402 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
404 goaway($a->get_baseurl());
410 header("Content-type: text/xml");
411 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
412 . '<dfrn_poll>' . "\r\n"
413 . "\t" . '<status>' . $status . '</status>' . "\r\n"
414 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
415 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
416 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
417 . '</dfrn_poll>' . "\r\n" ;