3 require_once('library/simplepie/simplepie.inc');
4 require_once('include/items.php');
5 require_once('include/event.php');
8 function dfrn_notify_post(&$a) {
10 $dfrn_id = ((x($_POST,'dfrn_id')) ? notags(trim($_POST['dfrn_id'])) : '');
11 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
12 $challenge = ((x($_POST,'challenge')) ? notags(trim($_POST['challenge'])) : '');
13 $data = ((x($_POST,'data')) ? $_POST['data'] : '');
14 $key = ((x($_POST,'key')) ? $_POST['key'] : '');
15 $dissolve = ((x($_POST,'dissolve')) ? intval($_POST['dissolve']) : 0);
16 $perm = ((x($_POST,'perm')) ? notags(trim($_POST['perm'])) : 'r');
17 $ssl_policy = ((x($_POST,'ssl_policy')) ? notags(trim($_POST['ssl_policy'])): 'none');
20 if($dfrn_version >= 2.21) {
21 $writable = (($perm === 'rw') ? 1 : 0);
25 if(strpos($dfrn_id,':') == 1) {
26 $direction = intval(substr($dfrn_id,0,1));
27 $dfrn_id = substr($dfrn_id,2);
30 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
35 logger('dfrn_notify: could not match challenge to dfrn_id ' . $dfrn_id . ' challenge=' . $challenge);
39 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
44 // find the local user who owns this relationship.
49 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
52 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
55 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
62 // be careful - $importer will contain both the contact information for the contact
63 // sending us the post, and also the user information for the person receiving it.
64 // since they are mixed together, it is easy to get them confused.
66 $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
67 `contact`.`pubkey` AS `cpubkey`,
68 `contact`.`prvkey` AS `cprvkey`,
69 `contact`.`thumb` AS `thumb`,
70 `contact`.`url` as `url`,
71 `contact`.`name` as `senderName`,
74 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
75 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
76 AND `user`.`nickname` = '%s' AND `user`.`account_expired` = 0 $sql_extra LIMIT 1",
81 logger('dfrn_notify: contact not found for dfrn_id ' . $dfrn_id);
86 // $importer in this case contains the contact record for the remote contact joined with the user record of our user.
90 if(($writable != (-1)) && ($writable != $importer['writable'])) {
91 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d LIMIT 1",
93 intval($importer['id'])
95 $importer['writable'] = $writable;
98 // if contact's ssl policy changed, update our links
100 $ssl_changed = false;
102 if($ssl_policy == 'self' && strstr($importer['url'],'https:')) {
104 $importer['url'] = str_replace('https:','http:',$importer['url']);
105 $importer['nurl'] = normalise_link($importer['url']);
106 $importer['photo'] = str_replace('https:','http:',$importer['photo']);
107 $importer['thumb'] = str_replace('https:','http:',$importer['thumb']);
108 $importer['micro'] = str_replace('https:','http:',$importer['micro']);
109 $importer['request'] = str_replace('https:','http:',$importer['request']);
110 $importer['notify'] = str_replace('https:','http:',$importer['notify']);
111 $importer['poll'] = str_replace('https:','http:',$importer['poll']);
112 $importer['confirm'] = str_replace('https:','http:',$importer['confirm']);
113 $importer['poco'] = str_replace('https:','http:',$importer['poco']);
116 if($ssl_policy == 'full' && strstr($importer['url'],'http:')) {
118 $importer['url'] = str_replace('http:','https:',$importer['url']);
119 $importer['nurl'] = normalise_link($importer['url']);
120 $importer['photo'] = str_replace('http:','https:',$importer['photo']);
121 $importer['thumb'] = str_replace('http:','https:',$importer['thumb']);
122 $importer['micro'] = str_replace('http:','https:',$importer['micro']);
123 $importer['request'] = str_replace('http:','https:',$importer['request']);
124 $importer['notify'] = str_replace('http:','https:',$importer['notify']);
125 $importer['poll'] = str_replace('http:','https:',$importer['poll']);
126 $importer['confirm'] = str_replace('http:','https:',$importer['confirm']);
127 $importer['poco'] = str_replace('http:','https:',$importer['poco']);
131 q("update contact set
142 where id = %d limit 1",
143 dbesc($importer['url']),
144 dbesc($importer['nurl']),
145 dbesc($importer['photo']),
146 dbesc($importer['thumb']),
147 dbesc($importer['micro']),
148 dbesc($importer['request']),
149 dbesc($importer['notify']),
150 dbesc($importer['poll']),
151 dbesc($importer['confirm']),
152 dbesc($importer['poco']),
153 intval($importer['id'])
157 logger('dfrn_notify: received notify from ' . $importer['name'] . ' for ' . $importer['username']);
158 logger('dfrn_notify: data: ' . $data, LOGGER_DATA);
163 * Relationship is dissolved permanently
166 require_once('include/Contact.php');
167 contact_remove($importer['id']);
168 logger('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']);
174 $rawkey = hex2bin(trim($key));
175 logger('rino: md5 raw key: ' . md5($rawkey));
178 if($dfrn_version >= 2.1) {
179 if((($importer['duplex']) && strlen($importer['cprvkey'])) || (! strlen($importer['cpubkey']))) {
180 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
183 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
187 if((($importer['duplex']) && strlen($importer['cpubkey'])) || (! strlen($importer['cprvkey']))) {
188 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
191 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
195 logger('rino: received key : ' . $final_key);
196 $data = aes_decrypt(hex2bin($data),$final_key);
197 logger('rino: decrypted data: ' . $data, LOGGER_DATA);
201 $ret = local_delivery($importer,$data);
208 function dfrn_notify_content(&$a) {
210 if(x($_GET,'dfrn_id')) {
212 // initial communication from external contact, $direction is their direction.
213 // If this is a duplex communication, ours will be the opposite.
215 $dfrn_id = notags(trim($_GET['dfrn_id']));
216 $dfrn_version = (float) $_GET['dfrn_version'];
218 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
221 if(strpos($dfrn_id,':') == 1) {
222 $direction = intval(substr($dfrn_id,0,1));
223 $dfrn_id = substr($dfrn_id,2);
226 $hash = random_string();
230 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
232 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
233 VALUES( '%s', '%s', %d ) ",
239 logger('dfrn_notify: challenge=' . $hash, LOGGER_DEBUG );
244 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
248 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
249 $my_id = '1:' . $dfrn_id;
252 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
253 $my_id = '0:' . $dfrn_id;
260 $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
261 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s'
262 AND `user`.`account_expired` = 0 $sql_extra LIMIT 1",
271 $id_str = $my_id . '.' . mt_rand(1000,9999);
273 $prv_key = trim($r[0]['prvkey']);
274 $pub_key = trim($r[0]['pubkey']);
275 $dplx = intval($r[0]['duplex']);
277 if((($dplx) && (strlen($prv_key))) || ((strlen($prv_key)) && (!(strlen($pub_key))))) {
278 openssl_private_encrypt($hash,$challenge,$prv_key);
279 openssl_private_encrypt($id_str,$encrypted_id,$prv_key);
281 elseif(strlen($pub_key)) {
282 openssl_public_encrypt($hash,$challenge,$pub_key);
283 openssl_public_encrypt($id_str,$encrypted_id,$pub_key);
288 $challenge = bin2hex($challenge);
289 $encrypted_id = bin2hex($encrypted_id);
291 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
293 $rino_enable = get_config('system','rino_encrypt');
299 header("Content-type: text/xml");
301 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
302 . '<dfrn_notify>' . "\r\n"
303 . "\t" . '<status>' . $status . '</status>' . "\r\n"
304 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
305 . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
306 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
307 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
308 . '</dfrn_notify>' . "\r\n" ;