3 require_once('include/items.php');
4 require_once('include/auth.php');
7 function dfrn_poll_init(&$a) {
9 $dfrn_id = ((x($_GET,'dfrn_id')) ? $_GET['dfrn_id'] : '');
10 $type = ((x($_GET,'type')) ? $_GET['type'] : '');
11 $last_update = ((x($_GET,'last_update')) ? $_GET['last_update'] : '');
12 $destination_url = ((x($_GET,'destination_url')) ? $_GET['destination_url'] : '');
13 $dfrn_version = ((x($_GET,'dfrn_version')) ? (float) $_GET['dfrn_version'] : 0);
19 if(strpos($dfrn_id,':') == 1) {
20 $direction = intval(substr($dfrn_id,0,1));
21 $dfrn_id = substr($dfrn_id,2);
24 if(($dfrn_id === '') && (! x($_POST,'dfrn_id')) && ($a->argc > 1)) {
25 header("Content-type: application/atom+xml");
26 $o = get_feed_for($a, '*', $a->argv[1],$last_update);
31 if((isset($type)) && ($type === 'profile')) {
36 $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id),dbesc($dfrn_id));
40 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
41 $my_id = '1:' . $dfrn_id;
44 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
45 $my_id = '0:' . $dfrn_id;
48 goaway($a->get_baseurl());
52 $r = q("SELECT `contact`.*, `user`.`nickname`
53 FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
54 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
55 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1",
61 $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check');
65 $xml = simplexml_load_string($s);
67 if((int) $xml->status == 1) {
68 $_SESSION['authenticated'] = 1;
69 $_SESSION['visitor_id'] = $r[0]['id'];
70 notice( t('Hi ') . $r[0]['name'] . EOL);
71 // Visitors get 1 day session.
72 $session_id = session_id();
73 $expire = time() + 86400;
74 q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
80 $profile = $r[0]['nickname'];
81 goaway((strlen($destination_url)) ? $destination_url : $a->get_baseurl() . '/profile/' . $profile);
83 goaway($a->get_baseurl());
87 if((isset($type)) && ($type === 'profile-check')) {
91 $dfrn_id = '0:' . $dfrn_id;
94 $dfrn_id = '1:' . $dfrn_id;
99 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
100 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
104 return; // NOTREACHED
107 return; // NOTREACHED
115 function dfrn_poll_post(&$a) {
117 $dfrn_id = ((x($_POST,'dfrn_id')) ? $_POST['dfrn_id'] : '');
118 $challenge = ((x($_POST,'challenge')) ? $_POST['challenge'] : '');
119 $url = ((x($_POST,'url')) ? $_POST['url'] : '');
120 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 0);
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>
205 header("Content-type: application/atom+xml");
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" ;