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 header("Content-type: application/atom+xml");
31 $o = get_feed_for($a, '*', $a->argv[1],$last_update);
36 if((x($type)) && ($type === 'profile')) {
41 $sql_extra = sprintf(" AND `dfrn-id` = '%s' ", dbesc($dfrn_id));
45 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
46 $my_id = '1:' . $dfrn_id;
49 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
50 $my_id = '0:' . $dfrn_id;
53 goaway($a->get_baseurl());
57 $r = q("SELECT `contact`.*, `user`.`nickname`
58 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
59 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 $sql_extra LIMIT 1");
63 $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
67 $xml = simplexml_load_string($s);
69 if((int) $xml->status == 1) {
70 $_SESSION['authenticated'] = 1;
71 $_SESSION['visitor_id'] = $r[0]['id'];
72 notice( t('Hi ') . $r[0]['name'] . EOL);
73 // Visitors get 1 day session.
74 $session_id = session_id();
75 $expire = time() + 86400;
76 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
82 $profile = $r[0]['nickname'];
83 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
85 goaway($a->get_baseurl());
89 if((x($type)) && ($type === 'profile-check')) {
93 $dfrn_id = '0:' . $dfrn_id;
96 $dfrn_id = '1:' . $dfrn_id;
101 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
102 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
106 return; // NOTREACHED
109 return; // NOTREACHED
117 function dfrn_poll_post(&$a) {
119 $dfrn_id = $_POST['dfrn_id'];
120 $challenge = $_POST['challenge'];
121 $url = $_POST['url'];
122 $dfrn_version = (float) $_POST['dfrn_version'];
125 if(strpos($dfrn_id,':') == 1) {
126 $direction = intval(substr($dfrn_id,0,1));
127 $dfrn_id = substr($dfrn_id,2);
131 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
139 $type = $r[0]['type'];
140 $last_update = $r[0]['last_update'];
142 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
151 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
155 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
156 $my_id = '1:' . $dfrn_id;
159 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
160 $my_id = '0:' . $dfrn_id;
163 goaway($a->get_baseurl());
168 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
174 $owner_uid = $r[0]['uid'];
175 $contact_id = $r[0]['id'];
178 if($type === 'reputation' && strlen($url)) {
179 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
187 $reputation = $r[0]['rating'];
188 $text = $r[0]['reason'];
190 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
196 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
199 <rating>$reputation</rating>
200 <description>$text</description>
207 header("Content-type: application/atom+xml");
208 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
215 function dfrn_poll_content(&$a) {
221 if(x($_GET,'dfrn_id'))
222 $dfrn_id = $_GET['dfrn_id'];
224 $type = $_GET['type'];
225 if(x($_GET,'last_update'))
226 $last_update = $_GET['last_update'];
228 $dfrn_version = (float) $_GET['dfrn_version'];
231 if(strpos($dfrn_id,':') == 1) {
232 $direction = intval(substr($dfrn_id,0,1));
233 $dfrn_id = substr($dfrn_id,2);
238 // initial communication from external contact
239 $hash = random_string();
243 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
245 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
246 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
249 intval(time() + 60 ),
258 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
262 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
263 $my_id = '1:' . $dfrn_id;
266 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
267 $my_id = '0:' . $dfrn_id;
270 goaway($a->get_baseurl());
277 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
283 $id_str = $my_id . '.' . mt_rand(1000,9999);
286 if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
287 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
288 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
291 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
292 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
295 $challenge = bin2hex($challenge);
296 $encrypted_id = bin2hex($encrypted_id);
301 header("Content-type: text/xml");
302 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
303 . '<dfrn_poll>' . "\r\n"
304 . "\t" . '<status>' .$status . '</status>' . "\r\n"
305 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
306 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
307 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
308 . '</dfrn_poll>' . "\r\n" ;