3 require_once('library/simplepie/simplepie.inc');
4 require_once('include/items.php');
5 require_once('include/event.php');
7 require_once('library/defuse/php-encryption-1.2.1/Crypto.php');
9 function dfrn_notify_post(&$a) {
10 logger(__function__, LOGGER_TRACE);
11 $dfrn_id = ((x($_POST,'dfrn_id')) ? notags(trim($_POST['dfrn_id'])) : '');
12 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
13 $challenge = ((x($_POST,'challenge')) ? notags(trim($_POST['challenge'])) : '');
14 $data = ((x($_POST,'data')) ? $_POST['data'] : '');
15 $key = ((x($_POST,'key')) ? $_POST['key'] : '');
16 $rino_remote = ((x($_POST,'rino')) ? intval($_POST['rino']) : 0);
17 $dissolve = ((x($_POST,'dissolve')) ? intval($_POST['dissolve']) : 0);
18 $perm = ((x($_POST,'perm')) ? notags(trim($_POST['perm'])) : 'r');
19 $ssl_policy = ((x($_POST,'ssl_policy')) ? notags(trim($_POST['ssl_policy'])): 'none');
20 $page = ((x($_POST,'page')) ? intval($_POST['page']) : 0);
22 $forum = (($page == 1) ? 1 : 0);
23 $prv = (($page == 2) ? 1 : 0);
26 if($dfrn_version >= 2.21) {
27 $writable = (($perm === 'rw') ? 1 : 0);
31 if(strpos($dfrn_id,':') == 1) {
32 $direction = intval(substr($dfrn_id,0,1));
33 $dfrn_id = substr($dfrn_id,2);
36 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
41 logger('dfrn_notify: could not match challenge to dfrn_id ' . $dfrn_id . ' challenge=' . $challenge);
45 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'",
50 // find the local user who owns this relationship.
55 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
58 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
61 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
68 // be careful - $importer will contain both the contact information for the contact
69 // sending us the post, and also the user information for the person receiving it.
70 // since they are mixed together, it is easy to get them confused.
72 $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
73 `contact`.`pubkey` AS `cpubkey`,
74 `contact`.`prvkey` AS `cprvkey`,
75 `contact`.`thumb` AS `thumb`,
76 `contact`.`url` as `url`,
77 `contact`.`name` as `senderName`,
80 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
81 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
82 AND `user`.`nickname` = '%s' AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1",
87 logger('dfrn_notify: contact not found for dfrn_id ' . $dfrn_id);
92 // $importer in this case contains the contact record for the remote contact joined with the user record of our user.
96 if((($writable != (-1)) && ($writable != $importer['writable'])) || ($importer['forum'] != $forum) || ($importer['prv'] != $prv)) {
97 q("UPDATE `contact` SET `writable` = %d, forum = %d, prv = %d WHERE `id` = %d",
98 intval(($writable == (-1)) ? $importer['writable'] : $writable),
101 intval($importer['id'])
103 if($writable != (-1))
104 $importer['writable'] = $writable;
105 $importer['forum'] = $page;
109 // if contact's ssl policy changed, update our links
111 fix_contact_ssl_policy($importer,$ssl_policy);
113 logger('dfrn_notify: received notify from ' . $importer['name'] . ' for ' . $importer['username']);
114 logger('dfrn_notify: data: ' . $data, LOGGER_DATA);
119 * Relationship is dissolved permanently
122 require_once('include/Contact.php');
123 contact_remove($importer['id']);
124 logger('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']);
130 // If we are setup as a soapbox we aren't accepting input from this person
132 if($importer['page-flags'] == PAGE_SOAPBOX)
137 $rawkey = hex2bin(trim($key));
138 logger('rino: md5 raw key: ' . md5($rawkey));
141 if($dfrn_version >= 2.1) {
142 if((($importer['duplex']) && strlen($importer['cprvkey'])) || (! strlen($importer['cpubkey']))) {
143 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
146 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
150 if((($importer['duplex']) && strlen($importer['cpubkey'])) || (! strlen($importer['cprvkey']))) {
151 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
154 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
158 #logger('rino: received key : ' . $final_key);
160 switch($rino_remote) {
163 // we got a key. old code send only the key, without RINO version.
164 // we assume RINO 1 if key and no RINO version
165 $data = aes_decrypt(hex2bin($data),$final_key);
169 $data = Crypto::decrypt(hex2bin($data),$final_key);
170 } catch (InvalidCiphertext $ex) { // VERY IMPORTANT
172 // 1. The ciphertext was modified by the attacker,
173 // 2. The key is wrong, or
174 // 3. $ciphertext is not a valid ciphertext or was corrupted.
176 logger('The ciphertext has been tampered with!');
177 xml_status(0,'The ciphertext has been tampered with!');
178 } catch (Ex\CryptoTestFailed $ex) {
179 logger('Cannot safely perform dencryption');
180 xml_status(0,'CryptoTestFailed');
181 } catch (Ex\CannotPerformOperation $ex) {
182 logger('Cannot safely perform decryption');
183 xml_status(0,'Cannot safely perform decryption');
187 logger("rino: invalid sent verision '$rino_remote'");
192 logger('rino: decrypted data: ' . $data, LOGGER_DATA);
195 $ret = local_delivery($importer,$data);
202 function dfrn_notify_content(&$a) {
204 if(x($_GET,'dfrn_id')) {
206 // initial communication from external contact, $direction is their direction.
207 // If this is a duplex communication, ours will be the opposite.
209 $dfrn_id = notags(trim($_GET['dfrn_id']));
210 $dfrn_version = (float) $_GET['dfrn_version'];
214 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
217 if(strpos($dfrn_id,':') == 1) {
218 $direction = intval(substr($dfrn_id,0,1));
219 $dfrn_id = substr($dfrn_id,2);
222 $hash = random_string();
226 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
228 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
229 VALUES( '%s', '%s', %d, '%s', '%s' ) ",
232 intval(time() + 90 ),
237 logger('dfrn_notify: challenge=' . $hash, LOGGER_DEBUG );
242 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
246 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
247 $my_id = '1:' . $dfrn_id;
250 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
251 $my_id = '0:' . $dfrn_id;
258 $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`page-flags` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
259 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s'
260 AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1",
269 $id_str = $my_id . '.' . mt_rand(1000,9999);
271 $prv_key = trim($r[0]['prvkey']);
272 $pub_key = trim($r[0]['pubkey']);
273 $dplx = intval($r[0]['duplex']);
275 if((($dplx) && (strlen($prv_key))) || ((strlen($prv_key)) && (!(strlen($pub_key))))) {
276 openssl_private_encrypt($hash,$challenge,$prv_key);
277 openssl_private_encrypt($id_str,$encrypted_id,$prv_key);
279 elseif(strlen($pub_key)) {
280 openssl_public_encrypt($hash,$challenge,$pub_key);
281 openssl_public_encrypt($id_str,$encrypted_id,$pub_key);
286 $challenge = bin2hex($challenge);
287 $encrypted_id = bin2hex($encrypted_id);
290 $rino = get_config('system','rino_encrypt');
298 if((($r[0]['rel']) && ($r[0]['rel'] != CONTACT_IS_SHARING)) || ($r[0]['page-flags'] == PAGE_COMMUNITY)) {
305 header("Content-type: text/xml");
307 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
308 . '<dfrn_notify>' . "\r\n"
309 . "\t" . '<status>' . $status . '</status>' . "\r\n"
310 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
311 . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
312 . "\t" . '<perm>' . $perm . '</perm>' . "\r\n"
313 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
314 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
315 . '</dfrn_notify>' . "\r\n" ;