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 = (float) $_GET['dfrn_version'] ;
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'];
121 $dfrn_version = (float) $_POST['dfrn_version'];
124 if(strpos($dfrn_id,':') == 1) {
125 $direction = intval(substr($dfrn_id,0,1));
126 $dfrn_id = substr($dfrn_id,2);
130 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
138 $type = $r[0]['type'];
139 $last_update = $r[0]['last_update'];
141 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
150 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
154 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
155 $my_id = '1:' . $dfrn_id;
158 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
159 $my_id = '0:' . $dfrn_id;
162 goaway($a->get_baseurl());
167 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
173 $owner_uid = $r[0]['uid'];
174 $contact_id = $r[0]['id'];
177 if($type === 'reputation' && strlen($url)) {
178 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
186 $reputation = $r[0]['rating'];
187 $text = $r[0]['reason'];
189 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
195 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
198 <rating>$reputation</rating>
199 <description>$text</description>
206 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
213 function dfrn_poll_content(&$a) {
219 if(x($_GET,'dfrn_id'))
220 $dfrn_id = $_GET['dfrn_id'];
222 $type = $_GET['type'];
223 if(x($_GET,'last_update'))
224 $last_update = $_GET['last_update'];
226 $dfrn_version = (float) $_GET['dfrn_version'];
229 if(strpos($dfrn_id,':') == 1) {
230 $direction = intval(substr($dfrn_id,0,1));
231 $dfrn_id = substr($dfrn_id,2);
236 // initial communication from external contact
237 $hash = random_string();
241 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
243 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
244 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
247 intval(time() + 60 ),
256 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
260 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
261 $my_id = '1:' . $dfrn_id;
264 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
265 $my_id = '0:' . $dfrn_id;
268 goaway($a->get_baseurl());
275 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
281 $id_str = $my_id . '.' . mt_rand(1000,9999);
284 if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
285 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
286 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
289 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
290 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
293 $challenge = bin2hex($challenge);
294 $encrypted_id = bin2hex($encrypted_id);
299 header("Content-type: text/xml");
300 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
301 . '<dfrn_poll>' . "\r\n"
302 . "\t" . '<status>' .$status . '</status>' . "\r\n"
303 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
304 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
305 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
306 . '</dfrn_poll>' . "\r\n" ;