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 = $_GET['dfrn_id'];
14 $type = $_GET['type'];
15 if(x($_GET,'last_update'))
16 $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'] : '');
24 if(strpos($dfrn_id,':') == 1) {
25 $direction = intval(substr($dfrn_id,0,1));
26 $dfrn_id = substr($dfrn_id,2);
29 if(($dfrn_id == '') && (! x($_POST,'dfrn_id')) && ($a->argc > 1)) {
30 $o = get_feed_for($a, '*', $a->argv[1],$last_update);
35 if((x($type)) && ($type == 'profile')) {
40 $sql_extra = sprintf(" AND `dfrn-id` = '%s' ", 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`.`nickname`
57 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
58 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 $sql_extra LIMIT 1");
62 $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
66 $xml = simplexml_load_string($s);
68 if((int) $xml->status == 1) {
69 $_SESSION['authenticated'] = 1;
70 $_SESSION['visitor_id'] = $r[0]['id'];
71 notice( t('Hi ') . $r[0]['name'] . EOL);
72 // Visitors get 1 day session.
73 $session_id = session_id();
74 $expire = time() + 86400;
75 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
81 $profile = $r[0]['nickname'];
82 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
84 goaway($a->get_baseurl());
88 if((x($type)) && ($type == 'profile-check')) {
92 $dfrn_id = '0:' . $dfrn_id;
95 $dfrn_id = '1:' . $dfrn_id;
100 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
101 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
105 return; // NOTREACHED
108 return; // NOTREACHED
116 function dfrn_poll_post(&$a) {
118 $dfrn_id = $_POST['dfrn_id'];
119 $challenge = $_POST['challenge'];
120 $url = $_POST['url'];
123 if(strpos($dfrn_id,':') == 1) {
124 $direction = intval(substr($dfrn_id,0,1));
125 $dfrn_id = substr($dfrn_id,2);
129 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
137 $type = $r[0]['type'];
138 $last_update = $r[0]['last_update'];
140 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
149 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
153 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
154 $my_id = '1:' . $dfrn_id;
157 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
158 $my_id = '0:' . $dfrn_id;
161 goaway($a->get_baseurl());
166 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
172 $owner_uid = $r[0]['uid'];
173 $contact_id = $r[0]['id'];
176 if($type == 'reputation' && strlen($url)) {
177 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
185 $reputation = $r[0]['rating'];
186 $text = $r[0]['reason'];
188 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
194 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
197 <rating>$reputation</rating>
198 <description>$text</description>
202 return; // NOTREACHED
205 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
212 function dfrn_poll_content(&$a) {
218 if(x($_GET,'dfrn_id'))
219 $dfrn_id = $_GET['dfrn_id'];
221 $type = $_GET['type'];
222 if(x($_GET,'last_update'))
223 $last_update = $_GET['last_update'];
226 if(strpos($dfrn_id,':') == 1) {
227 $direction = intval(substr($dfrn_id,0,1));
228 $dfrn_id = substr($dfrn_id,2);
233 // initial communication from external contact
234 $hash = random_string();
238 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
240 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
241 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
244 intval(time() + 60 ),
253 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
257 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
258 $my_id = '1:' . $dfrn_id;
261 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
262 $my_id = '0:' . $dfrn_id;
265 goaway($a->get_baseurl());
272 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
278 $id_str = $my_id . '.' . mt_rand(1000,9999);
281 if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
282 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
283 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
286 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
287 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
290 $challenge = bin2hex($challenge);
291 $encrypted_id = bin2hex($encrypted_id);
297 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>'
298 . '<challenge>' . $challenge . '</challenge></dfrn_poll>' . "\r\n" ;
299 session_write_close();