3 require_once('include/items.php');
4 require_once('include/auth.php');
7 function dfrn_poll_init(&$a) {
9 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
10 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
11 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
12 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
13 $challenge = ((x($_GET,'challenge')) ? $_GET['challenge'] : '');
14 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
15 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
20 if(strpos($dfrn_id,':') == 1) {
21 $direction = intval(substr($dfrn_id,0,1));
22 $dfrn_id = substr($dfrn_id,2);
25 if(($dfrn_id === '') && (! x($_POST,'dfrn_id')) && ($a->argc > 1)) {
26 header("Content-type: application/atom+xml");
27 $o = get_feed_for($a, '*', $a->argv[1],$last_update);
32 if(($type === 'profile') && (! strlen($sec))) {
37 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
41 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
42 $my_id = '1:' . $dfrn_id;
45 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
46 $my_id = '0:' . $dfrn_id;
49 goaway($a->get_baseurl());
53 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
54 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
55 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
56 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
62 $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
64 logger("dfrn_poll: old profile returns " . $s, LOGGER_DATA);
68 $xml = simplexml_load_string($s);
70 if((int) $xml->status == 1) {
71 $_SESSION['authenticated'] = 1;
72 $_SESSION['visitor_id'] = $r[0]['id'];
73 notice( $r[0]['username'] . t(' welcomes ') . $r[0]['name'] . EOL);
74 // Visitors get 1 day session.
75 $session_id = session_id();
76 $expire = time() + 86400;
77 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
83 $profile = $r[0]['nickname'];
84 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
86 goaway($a->get_baseurl());
90 if($type === 'profile-check') {
92 if((strlen($challenge)) && (strlen($sec))) {
94 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
95 $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1",
102 $orig_id = $r[0]['dfrn_id'];
103 if(strpos(':',$orig_id))
104 $orig_id = substr($orig_id,2);
106 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
114 $sent_dfrn_id = hex2bin($dfrn_id);
115 $challenge = hex2bin($challenge);
119 if(($contact['duplex']) && strlen($contact['prvkey'])) {
120 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
121 openssl_private_decrypt($challenge,$decoded_challenge,$contact['prvkey']);
124 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
125 openssl_public_decrypt($challenge,$decoded_challenge,$contact['pubkey']);
128 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
130 if(strpos($final_dfrn_id,':') == 1)
131 $final_dfrn_id = substr($final_dfrn_id,2);
133 if($final_dfrn_id != $orig_id) {
135 // did not decode properly - cannot trust this site
139 header("Content-type: text/xml");
140 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><dfrn_poll><status>0</status><challenge>$decoded_challenge</challenge><sec>$sec</sec></dfrn_poll>";
149 $dfrn_id = '0:' . $dfrn_id;
152 $dfrn_id = '1:' . $dfrn_id;
159 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
160 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
164 return; // NOTREACHED
167 return; // NOTREACHED
175 function dfrn_poll_post(&$a) {
177 $dfrn_id = ((x($_POST,'dfrn_id')) ? $_POST['dfrn_id'] : '');
178 $challenge = ((x($_POST,'challenge')) ? $_POST['challenge'] : '');
179 $url = ((x($_POST,'url')) ? $_POST['url'] : '');
180 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
183 if(strpos($dfrn_id,':') == 1) {
184 $direction = intval(substr($dfrn_id,0,1));
185 $dfrn_id = substr($dfrn_id,2);
189 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
197 $type = $r[0]['type'];
198 $last_update = $r[0]['last_update'];
200 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
209 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
213 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
214 $my_id = '1:' . $dfrn_id;
217 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
218 $my_id = '0:' . $dfrn_id;
221 goaway($a->get_baseurl());
226 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
232 $owner_uid = $r[0]['uid'];
233 $contact_id = $r[0]['id'];
236 if($type === 'reputation' && strlen($url)) {
237 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
245 $reputation = $r[0]['rating'];
246 $text = $r[0]['reason'];
248 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
254 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
257 <rating>$reputation</rating>
258 <description>$text</description>
265 header("Content-type: application/atom+xml");
266 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
273 function dfrn_poll_content(&$a) {
275 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
276 $type = ((x($_GET,'type')) ? $_GET['type'] : 'data');
277 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
278 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
279 $sec = ((x($_GET,'sec')) ? $_GET['sec'] : '');
280 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 2.0);
283 if(strpos($dfrn_id,':') == 1) {
284 $direction = intval(substr($dfrn_id,0,1));
285 $dfrn_id = substr($dfrn_id,2);
290 // initial communication from external contact
291 $hash = random_string();
295 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
297 if($type !== 'profile') {
298 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
299 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
302 intval(time() + 60 ),
310 if($type === 'profile')
311 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
313 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
317 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
318 $my_id = '1:' . $dfrn_id;
321 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
322 $my_id = '0:' . $dfrn_id;
325 goaway($a->get_baseurl());
329 $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname`
330 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
331 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
332 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
340 $id_str = $my_id . '.' . mt_rand(1000,9999);
342 if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
343 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
344 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
347 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
348 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
351 $challenge = bin2hex($challenge);
352 $encrypted_id = bin2hex($encrypted_id);
360 if(($type === 'profile') && (strlen($sec))) {
363 $s = fetch_url($r[0]['poll']
364 . '?dfrn_id=' . $encrypted_id
365 . '&type=profile-check'
366 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
367 . '&challenge=' . $challenge
371 logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
373 if(strlen($s) && strstr($s,'<?xml')) {
375 $xml = simplexml_load_string($s);
377 logger('dfrn_poll: profile: parsed xml: ' . print_r($xml,true), LOGGER_DATA);
379 logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
380 logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
383 if(((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
384 $_SESSION['authenticated'] = 1;
385 $_SESSION['visitor_id'] = $r[0]['id'];
386 notice( $r[0]['username'] . t(' welcomes ') . $r[0]['name'] . EOL);
387 // Visitors get 1 day session.
388 $session_id = session_id();
389 $expire = time() + 86400;
390 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
395 $profile = $r[0]['nickname'];
396 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
398 goaway($a->get_baseurl());
404 header("Content-type: text/xml");
405 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
406 . '<dfrn_poll>' . "\r\n"
407 . "\t" . '<status>' . $status . '</status>' . "\r\n"
408 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
409 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
410 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
411 . '</dfrn_poll>' . "\r\n" ;