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');
18 $page = ((x($_POST,'page')) ? intval($_POST['page']) : 0);
21 if($dfrn_version >= 2.21) {
22 $writable = (($perm === 'rw') ? 1 : 0);
26 if(strpos($dfrn_id,':') == 1) {
27 $direction = intval(substr($dfrn_id,0,1));
28 $dfrn_id = substr($dfrn_id,2);
31 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
36 logger('dfrn_notify: could not match challenge to dfrn_id ' . $dfrn_id . ' challenge=' . $challenge);
40 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
45 // find the local user who owns this relationship.
50 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
53 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
56 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
63 // be careful - $importer will contain both the contact information for the contact
64 // sending us the post, and also the user information for the person receiving it.
65 // since they are mixed together, it is easy to get them confused.
67 $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
68 `contact`.`pubkey` AS `cpubkey`,
69 `contact`.`prvkey` AS `cprvkey`,
70 `contact`.`thumb` AS `thumb`,
71 `contact`.`url` as `url`,
72 `contact`.`name` as `senderName`,
75 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
76 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
77 AND `user`.`nickname` = '%s' AND `user`.`account_expired` = 0 $sql_extra LIMIT 1",
82 logger('dfrn_notify: contact not found for dfrn_id ' . $dfrn_id);
87 // $importer in this case contains the contact record for the remote contact joined with the user record of our user.
91 if((($writable != (-1)) && ($writable != $importer['writable'])) || ($importer['forum'] != $page)) {
92 q("UPDATE `contact` SET `writable` = %d, forum = %d WHERE `id` = %d LIMIT 1",
93 intval(($writable == (-1)) ? $importer['writable'] : $writable),
95 intval($importer['id'])
98 $importer['writable'] = $writable;
99 $importer['forum'] = $page;
103 // if contact's ssl policy changed, update our links
105 fix_contact_ssl_policy($importer,$ssl_policy);
107 logger('dfrn_notify: received notify from ' . $importer['name'] . ' for ' . $importer['username']);
108 logger('dfrn_notify: data: ' . $data, LOGGER_DATA);
113 * Relationship is dissolved permanently
116 require_once('include/Contact.php');
117 contact_remove($importer['id']);
118 logger('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']);
124 // If we are setup as a soapbox we aren't accepting input from this person
126 if($importer['page-flags'] == PAGE_SOAPBOX)
131 $rawkey = hex2bin(trim($key));
132 logger('rino: md5 raw key: ' . md5($rawkey));
135 if($dfrn_version >= 2.1) {
136 if((($importer['duplex']) && strlen($importer['cprvkey'])) || (! strlen($importer['cpubkey']))) {
137 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
140 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
144 if((($importer['duplex']) && strlen($importer['cpubkey'])) || (! strlen($importer['cprvkey']))) {
145 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
148 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
152 logger('rino: received key : ' . $final_key);
153 $data = aes_decrypt(hex2bin($data),$final_key);
154 logger('rino: decrypted data: ' . $data, LOGGER_DATA);
158 $ret = local_delivery($importer,$data);
165 function dfrn_notify_content(&$a) {
167 if(x($_GET,'dfrn_id')) {
169 // initial communication from external contact, $direction is their direction.
170 // If this is a duplex communication, ours will be the opposite.
172 $dfrn_id = notags(trim($_GET['dfrn_id']));
173 $dfrn_version = (float) $_GET['dfrn_version'];
175 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
178 if(strpos($dfrn_id,':') == 1) {
179 $direction = intval(substr($dfrn_id,0,1));
180 $dfrn_id = substr($dfrn_id,2);
183 $hash = random_string();
187 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
189 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
190 VALUES( '%s', '%s', %d ) ",
196 logger('dfrn_notify: challenge=' . $hash, LOGGER_DEBUG );
201 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
205 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
206 $my_id = '1:' . $dfrn_id;
209 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
210 $my_id = '0:' . $dfrn_id;
217 $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`page-flags` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
218 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s'
219 AND `user`.`account_expired` = 0 $sql_extra LIMIT 1",
228 $id_str = $my_id . '.' . mt_rand(1000,9999);
230 $prv_key = trim($r[0]['prvkey']);
231 $pub_key = trim($r[0]['pubkey']);
232 $dplx = intval($r[0]['duplex']);
234 if((($dplx) && (strlen($prv_key))) || ((strlen($prv_key)) && (!(strlen($pub_key))))) {
235 openssl_private_encrypt($hash,$challenge,$prv_key);
236 openssl_private_encrypt($id_str,$encrypted_id,$prv_key);
238 elseif(strlen($pub_key)) {
239 openssl_public_encrypt($hash,$challenge,$pub_key);
240 openssl_public_encrypt($id_str,$encrypted_id,$pub_key);
245 $challenge = bin2hex($challenge);
246 $encrypted_id = bin2hex($encrypted_id);
248 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
250 $rino_enable = get_config('system','rino_encrypt');
255 if((($r[0]['rel']) && ($r[0]['rel'] != CONTACT_IS_SHARING)) || ($r[0]['page-flags'] == PAGE_COMMUNITY)) {
262 header("Content-type: text/xml");
264 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
265 . '<dfrn_notify>' . "\r\n"
266 . "\t" . '<status>' . $status . '</status>' . "\r\n"
267 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
268 . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
269 . "\t" . '<perm>' . $perm . '</perm>' . "\r\n"
270 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
271 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
272 . '</dfrn_notify>' . "\r\n" ;