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' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),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
60 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
66 $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
70 $xml = simplexml_load_string($s);
72 if((int) $xml->status == 1) {
73 $_SESSION['authenticated'] = 1;
74 $_SESSION['visitor_id'] = $r[0]['id'];
75 notice( t('Hi ') . $r[0]['name'] . EOL);
76 // Visitors get 1 day session.
77 $session_id = session_id();
78 $expire = time() + 86400;
79 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
85 $profile = $r[0]['nickname'];
86 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
88 goaway($a->get_baseurl());
92 if((x($type)) && ($type === 'profile-check')) {
96 $dfrn_id = '0:' . $dfrn_id;
99 $dfrn_id = '1:' . $dfrn_id;
104 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
105 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
109 return; // NOTREACHED
112 return; // NOTREACHED
120 function dfrn_poll_post(&$a) {
122 $dfrn_id = $_POST['dfrn_id'];
123 $challenge = $_POST['challenge'];
124 $url = $_POST['url'];
125 $dfrn_version = (float) $_POST['dfrn_version'];
128 if(strpos($dfrn_id,':') == 1) {
129 $direction = intval(substr($dfrn_id,0,1));
130 $dfrn_id = substr($dfrn_id,2);
134 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
142 $type = $r[0]['type'];
143 $last_update = $r[0]['last_update'];
145 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
154 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
158 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
159 $my_id = '1:' . $dfrn_id;
162 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
163 $my_id = '0:' . $dfrn_id;
166 goaway($a->get_baseurl());
171 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
177 $owner_uid = $r[0]['uid'];
178 $contact_id = $r[0]['id'];
181 if($type === 'reputation' && strlen($url)) {
182 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
190 $reputation = $r[0]['rating'];
191 $text = $r[0]['reason'];
193 if($r[0]['id'] == $contact_id) { // inquiring about own reputation not allowed
199 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
202 <rating>$reputation</rating>
203 <description>$text</description>
210 header("Content-type: application/atom+xml");
211 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction);
218 function dfrn_poll_content(&$a) {
224 if(x($_GET,'dfrn_id'))
225 $dfrn_id = $_GET['dfrn_id'];
227 $type = $_GET['type'];
228 if(x($_GET,'last_update'))
229 $last_update = $_GET['last_update'];
231 $dfrn_version = (float) $_GET['dfrn_version'];
234 if(strpos($dfrn_id,':') == 1) {
235 $direction = intval(substr($dfrn_id,0,1));
236 $dfrn_id = substr($dfrn_id,2);
241 // initial communication from external contact
242 $hash = random_string();
246 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
248 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
249 VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
252 intval(time() + 60 ),
261 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
265 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
266 $my_id = '1:' . $dfrn_id;
269 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
270 $my_id = '0:' . $dfrn_id;
273 goaway($a->get_baseurl());
280 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1");
286 $id_str = $my_id . '.' . mt_rand(1000,9999);
289 if($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
290 openssl_public_encrypt($hash,$challenge,$r[0]['pubkey']);
291 openssl_public_encrypt($id_str,$encrypted_id,$r[0]['pubkey']);
294 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
295 openssl_private_encrypt($id_str,$encrypted_id,$r[0]['prvkey']);
298 $challenge = bin2hex($challenge);
299 $encrypted_id = bin2hex($encrypted_id);
304 header("Content-type: text/xml");
305 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
306 . '<dfrn_poll>' . "\r\n"
307 . "\t" . '<status>' .$status . '</status>' . "\r\n"
308 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
309 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
310 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
311 . '</dfrn_poll>' . "\r\n" ;