3 require_once('include/items.php');
4 require_once('include/auth.php');
7 function dfrn_poll_init(&$a) {
11 if(x($_GET,'dfrn_id'))
12 $dfrn_id = $a->config['dfrn_poll_dfrn_id'] = $_GET['dfrn_id'];
14 $type = $a->config['dfrn_poll_type'] = $_GET['type'];
15 if(x($_GET,'last_update'))
16 $last_update = $a->config['dfrn_poll_last_update'] = $_GET['last_update'];
17 $dfrn_version = ((x($_GET,'dfrn_version')) ? $_GET['dfrn_version'] : '1.0');
18 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
20 if(($dfrn_id == '') && (! x($_POST,'dfrn_id')) && ($a->argc > 1)) {
21 $o = get_feed_for($a,'*', $a->argv[1],$last_update);
26 if((x($type)) && ($type == 'profile')) {
28 $r = q("SELECT `contact`.*, `user`.`nickname`
29 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
30 WHERE ( `dfrn-id` = '%s' OR ( `issued-id` = '%s' AND `duplex` = 1 )) ",
38 // On a multi-user site, the query above *may* have returned two results.
39 // One of those could be the logged-in user who is now visiting "this" cell,
40 // as both share the dfrn_id. We will skip that entry if it unfortunately
41 // happens to come up first.
43 if(local_user() && ($rr['uid'] == get_uid()))
46 $s = fetch_url($rr['poll'] . '?dfrn_id=' . $dfrn_id . '&type=profile-check');
48 $xml = simplexml_load_string($s);
49 if((int) $xml->status == 1) {
50 $_SESSION['authenticated'] = 1;
51 $_SESSION['visitor_id'] = $rr['id'];
52 notice( t('Hi ') . $rr['name'] . EOL);
53 // Visitors get 1 day session.
54 $session_id = session_id();
55 $expire = time() + 86400;
56 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
62 $profile = $rr['nickname'];
63 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
66 goaway($a->get_baseurl());
70 if((x($type)) && ($type == 'profile-check')) {
72 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
73 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
88 function dfrn_poll_post(&$a) {
90 $dfrn_id = notags(trim($_POST['dfrn_id']));
91 $challenge = notags(trim($_POST['challenge']));
94 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
101 $type = $r[0]['type'];
102 $last_update = $r[0]['last_update'];
104 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
110 $r = q("SELECT * FROM `contact` WHERE ( `issued-id` = '%s' OR ( `dfrn-id` = '%s' AND `duplex` = 1 )) LIMIT 1",
118 $owner_uid = $r[0]['uid'];
119 $contact_id = $r[0]['id'];
122 if($type == 'reputation' && strlen($url)) {
123 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
131 $reputation = $r[0]['rating'];
132 $text = $r[0]['reason'];
134 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
140 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
143 <rating>$reputation</rating>
144 <description>$text</description>
148 return; // NOTREACHED
152 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update);
159 function dfrn_poll_content(&$a) {
165 if(x($_GET,'dfrn_id'))
166 $dfrn_id = $a->config['dfrn_poll_dfrn_id'] = $_GET['dfrn_id'];
168 $type = $a->config['dfrn_poll_type'] = $_GET['type'];
169 if(x($_GET,'last_update'))
170 $last_update = $a->config['dfrn_poll_last_update'] = $_GET['last_update'];
174 // initial communication from external contact
175 $hash = random_string();
179 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
181 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
182 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
184 dbesc(notags(trim($_GET['dfrn_id']))),
185 intval(time() + 60 ),
190 $r = q("SELECT * FROM `contact` WHERE ( `issued-id` = '%s' OR ( `dfrn-id` = '%s' AND `duplex` = 1 ))
191 AND `blocked` = 0 AND `pending` = 0 LIMIT 1",
192 dbesc($_GET['dfrn_id']),
193 dbesc($_GET['dfrn_id'])
199 $id_str = $_GET['dfrn_id'] . '.' . mt_rand(1000,9999);
202 if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
203 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
204 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
207 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
208 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
211 $challenge = bin2hex($challenge);
212 $encrypted_id = bin2hex($encrypted_id);
218 echo '<?xml version="1.0" encoding="UTF-8"?><dfrn_poll><status>' .$status . '</status><dfrn_version>2.0</dfrn_version><dfrn_id>' . $encrypted_id . '</dfrn_id>'
219 . '<challenge>' . $challenge . '</challenge></dfrn_poll>' . "\r\n" ;
220 session_write_close();