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 notice( $r[0]['username'] . t(' welcomes ') . $r[0]['name'] . EOL);
77 // Visitors get 1 day session.
78 $session_id = session_id();
79 $expire = time() + 86400;
80 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
86 $profile = $r[0]['nickname'];
87 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
89 goaway($a->get_baseurl());
93 if($type === 'profile-check') {
95 if((strlen($challenge)) && (strlen($sec))) {
97 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
98 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
105 $orig_id = $r[0]['dfrn_id'];
106 if(strpos(':',$orig_id))
107 $orig_id = substr($orig_id,2);
109 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
117 $sent_dfrn_id = hex2bin($dfrn_id);
118 $challenge = hex2bin($challenge);
122 if(($contact['duplex']) && strlen($contact['prvkey'])) {
123 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
124 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
127 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
128 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
131 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
133 if(strpos($final_dfrn_id,':') == 1)
134 $final_dfrn_id = substr($final_dfrn_id,2);
136 if($final_dfrn_id != $orig_id) {
138 // did not decode properly - cannot trust this site
142 header("Content-type: text/xml");
143 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
152 $dfrn_id = '0:' . $dfrn_id;
155 $dfrn_id = '1:' . $dfrn_id;
162 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
163 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
167 return; // NOTREACHED
170 return; // NOTREACHED
178 function dfrn_poll_post(&$a) {
180 $dfrn_id = ((x($_POST,'dfrn_id')) ? $_POST['dfrn_id'] : '');
181 $challenge = ((x($_POST,'challenge')) ? $_POST['challenge'] : '');
182 $url = ((x($_POST,'url')) ? $_POST['url'] : '');
183 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
186 if(strpos($dfrn_id,':') == 1) {
187 $direction = intval(substr($dfrn_id,0,1));
188 $dfrn_id = substr($dfrn_id,2);
192 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
200 $type = $r[0]['type'];
201 $last_update = $r[0]['last_update'];
203 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
212 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
216 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
217 $my_id = '1:' . $dfrn_id;
220 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
221 $my_id = '0:' . $dfrn_id;
224 goaway($a->get_baseurl());
229 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
235 $owner_uid = $r[0]['uid'];
236 $contact_id = $r[0]['id'];
239 if($type === 'reputation' && strlen($url)) {
240 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
248 $reputation = $r[0]['rating'];
249 $text = $r[0]['reason'];
251 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
257 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
260 <rating>$reputation</rating>
261 <description>$text</description>
268 header("Content-type: application/atom+xml");
269 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
276 function dfrn_poll_content(&$a) {
278 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
279 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
280 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
281 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
282 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
283 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
286 if(strpos($dfrn_id,':') == 1) {
287 $direction = intval(substr($dfrn_id,0,1));
288 $dfrn_id = substr($dfrn_id,2);
293 // initial communication from external contact
294 $hash = random_string();
298 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
300 if($type !== 'profile') {
301 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
302 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
305 intval(time() + 60 ),
313 if($type === 'profile')
314 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
316 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
320 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
321 $my_id = '1:' . $dfrn_id;
324 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
325 $my_id = '0:' . $dfrn_id;
328 goaway($a->get_baseurl());
332 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
333 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
334 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
335 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
343 $id_str = $my_id . '.' . mt_rand(1000,9999);
345 if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
346 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
347 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
350 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
351 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
354 $challenge = bin2hex($challenge);
355 $encrypted_id = bin2hex($encrypted_id);
363 if(($type === 'profile') && (strlen($sec))) {
366 $s = fetch_url($r[0]['poll']
367 . '?dfrn_id=' . $encrypted_id
368 . '&type=profile-check'
369 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
370 . '&challenge=' . $challenge
374 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
376 if(strlen($s) && strstr($s,'<?xml')) {
378 $xml = simplexml_load_string($s);
380 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
382 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
383 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
386 if(((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
387 $_SESSION['authenticated'] = 1;
388 $_SESSION['visitor_id'] = $r[0]['id'];
389 notice( $r[0]['username'] . t(' welcomes ') . $r[0]['name'] . EOL);
390 // Visitors get 1 day session.
391 $session_id = session_id();
392 $expire = time() + 86400;
393 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
398 $profile = $r[0]['nickname'];
399 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
401 goaway($a->get_baseurl());
407 header("Content-type: text/xml");
408 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
409 . '<dfrn_poll>' . "\r\n"
410 . "\t" . '<status>' . $status . '</status>' . "\r\n"
411 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
412 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
413 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
414 . '</dfrn_poll>' . "\r\n" ;