4 * @file mod/dfrn_notify.php
5 * @brief The dfrn notify endpoint
6 * @see PDF with dfrn specs: https://github.com/friendica/friendica/blob/master/spec/dfrn2.pdf
10 use Friendica\Core\Config;
11 use Friendica\Core\System;
12 use Friendica\Database\DBM;
13 use Friendica\Model\Contact;
14 use Friendica\Protocol\DFRN;
16 require_once 'include/items.php';
17 require_once 'include/event.php';
19 function dfrn_notify_post(App $a) {
20 logger(__function__, LOGGER_TRACE);
21 $dfrn_id = ((x($_POST,'dfrn_id')) ? notags(trim($_POST['dfrn_id'])) : '');
22 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
23 $challenge = ((x($_POST,'challenge')) ? notags(trim($_POST['challenge'])) : '');
24 $data = ((x($_POST,'data')) ? $_POST['data'] : '');
25 $key = ((x($_POST,'key')) ? $_POST['key'] : '');
26 $rino_remote = ((x($_POST,'rino')) ? intval($_POST['rino']) : 0);
27 $dissolve = ((x($_POST,'dissolve')) ? intval($_POST['dissolve']) : 0);
28 $perm = ((x($_POST,'perm')) ? notags(trim($_POST['perm'])) : 'r');
29 $ssl_policy = ((x($_POST,'ssl_policy')) ? notags(trim($_POST['ssl_policy'])): 'none');
30 $page = ((x($_POST,'page')) ? intval($_POST['page']) : 0);
32 $forum = (($page == 1) ? 1 : 0);
33 $prv = (($page == 2) ? 1 : 0);
36 if ($dfrn_version >= 2.21) {
37 $writable = (($perm === 'rw') ? 1 : 0);
41 if (strpos($dfrn_id, ':') == 1) {
42 $direction = intval(substr($dfrn_id, 0, 1));
43 $dfrn_id = substr($dfrn_id, 2);
46 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
50 if (! DBM::is_result($r)) {
51 logger('dfrn_notify: could not match challenge to dfrn_id ' . $dfrn_id . ' challenge=' . $challenge);
52 System::xmlExit(3, 'Could not match challenge');
55 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'",
60 // find the local user who owns this relationship.
65 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
68 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
71 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
74 System::xmlExit(3, 'Invalid direction');
79 * be careful - $importer will contain both the contact information for the contact
80 * sending us the post, and also the user information for the person receiving it.
81 * since they are mixed together, it is easy to get them confused.
84 $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
85 `contact`.`pubkey` AS `cpubkey`,
86 `contact`.`prvkey` AS `cprvkey`,
87 `contact`.`thumb` AS `thumb`,
88 `contact`.`url` as `url`,
89 `contact`.`name` as `senderName`,
92 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
93 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
94 AND `user`.`nickname` = '%s' AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1",
98 if (! DBM::is_result($r)) {
99 logger('dfrn_notify: contact not found for dfrn_id ' . $dfrn_id);
100 System::xmlExit(3, 'Contact not found');
104 // $importer in this case contains the contact record for the remote contact joined with the user record of our user.
108 logger("Remote rino version: ".$rino_remote." for ".$importer["url"], LOGGER_DEBUG);
110 if ((($writable != (-1)) && ($writable != $importer['writable'])) || ($importer['forum'] != $forum) || ($importer['prv'] != $prv)) {
111 q("UPDATE `contact` SET `writable` = %d, forum = %d, prv = %d WHERE `id` = %d",
112 intval(($writable == (-1)) ? $importer['writable'] : $writable),
115 intval($importer['id'])
117 if ($writable != (-1)) {
118 $importer['writable'] = $writable;
120 $importer['forum'] = $page;
124 // if contact's ssl policy changed, update our links
126 $importer = Contact::updateSslPolicy($importer, $ssl_policy);
128 logger('dfrn_notify: received notify from ' . $importer['name'] . ' for ' . $importer['username']);
129 logger('dfrn_notify: data: ' . $data, LOGGER_DATA);
131 if ($dissolve == 1) {
132 // Relationship is dissolved permanently
133 Contact::remove($importer['id']);
134 logger('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']);
135 System::xmlExit(0, 'relationship dissolved');
138 $rino = Config::get('system', 'rino_encrypt');
139 $rino = intval($rino);
141 logger("Local rino version: " . $rino, LOGGER_DEBUG);
145 // if local rino is lower than remote rino, abort: should not happen!
146 // but only for $remote_rino > 1, because old code did't send rino version
147 if ($rino_remote > 1 && $rino < $rino_remote) {
148 logger("rino version '$rino_remote' is lower than supported '$rino'");
149 System::xmlExit(0, "rino version '$rino_remote' is lower than supported '$rino'");
152 $rawkey = hex2bin(trim($key));
153 logger('rino: md5 raw key: ' . md5($rawkey));
156 if ($dfrn_version >= 2.1) {
157 if ((($importer['duplex']) && strlen($importer['cprvkey'])) || (! strlen($importer['cpubkey']))) {
158 openssl_private_decrypt($rawkey, $final_key, $importer['cprvkey']);
160 openssl_public_decrypt($rawkey, $final_key, $importer['cpubkey']);
163 if ((($importer['duplex']) && strlen($importer['cpubkey'])) || (! strlen($importer['cprvkey']))) {
164 openssl_public_decrypt($rawkey, $final_key, $importer['cpubkey']);
166 openssl_private_decrypt($rawkey, $final_key, $importer['cprvkey']);
170 switch($rino_remote) {
173 // we got a key. old code send only the key, without RINO version.
174 // we assume RINO 1 if key and no RINO version
175 $data = DFRN::aesDecrypt(hex2bin($data), $final_key);
178 logger("rino: invalid sent version '$rino_remote'");
179 System::xmlExit(0, "Invalid sent version '$rino_remote'");
182 logger('rino: decrypted data: ' . $data, LOGGER_DATA);
185 $ret = DFRN::import($data, $importer);
186 System::xmlExit($ret, 'Processed');
192 function dfrn_notify_content(App $a) {
194 if(x($_GET,'dfrn_id')) {
197 * initial communication from external contact, $direction is their direction.
198 * If this is a duplex communication, ours will be the opposite.
201 $dfrn_id = notags(trim($_GET['dfrn_id']));
202 $dfrn_version = (float) $_GET['dfrn_version'];
203 $rino_remote = ((x($_GET,'rino')) ? intval($_GET['rino']) : 0);
207 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
210 if(strpos($dfrn_id,':') == 1) {
211 $direction = intval(substr($dfrn_id,0,1));
212 $dfrn_id = substr($dfrn_id,2);
215 $hash = random_string();
219 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
221 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
222 VALUES( '%s', '%s', %d, '%s', '%s' ) ",
225 intval(time() + 90 ),
230 logger('dfrn_notify: challenge=' . $hash, LOGGER_DEBUG);
235 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
239 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
240 $my_id = '1:' . $dfrn_id;
243 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
244 $my_id = '0:' . $dfrn_id;
251 $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`page-flags` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
252 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s'
253 AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1",
257 if (! DBM::is_result($r)) {
261 logger("Remote rino version: ".$rino_remote." for ".$r[0]["url"], LOGGER_DEBUG);
265 $id_str = $my_id . '.' . mt_rand(1000,9999);
267 $prv_key = trim($r[0]['prvkey']);
268 $pub_key = trim($r[0]['pubkey']);
269 $dplx = intval($r[0]['duplex']);
271 if ((($dplx) && (strlen($prv_key))) || ((strlen($prv_key)) && (!(strlen($pub_key))))) {
272 openssl_private_encrypt($hash, $challenge, $prv_key);
273 openssl_private_encrypt($id_str, $encrypted_id, $prv_key);
274 } elseif (strlen($pub_key)) {
275 openssl_public_encrypt($hash, $challenge, $pub_key);
276 openssl_public_encrypt($id_str, $encrypted_id, $pub_key);
278 /// @TODO these kind of else-blocks are making the code harder to understand
282 $challenge = bin2hex($challenge);
283 $encrypted_id = bin2hex($encrypted_id);
286 $rino = Config::get('system', 'rino_encrypt');
287 $rino = intval($rino);
289 logger("Local rino version: ". $rino, LOGGER_DEBUG);
291 // if requested rino is lower than enabled local rino, lower local rino version
292 // if requested rino is higher than enabled local rino, reply with local rino
293 if ($rino_remote < $rino) {
294 $rino = $rino_remote;
297 if((($r[0]['rel']) && ($r[0]['rel'] != CONTACT_IS_SHARING)) || ($r[0]['page-flags'] == PAGE_COMMUNITY)) {
303 header("Content-type: text/xml");
305 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
306 . '<dfrn_notify>' . "\r\n"
307 . "\t" . '<status>' . $status . '</status>' . "\r\n"
308 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
309 . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
310 . "\t" . '<perm>' . $perm . '</perm>' . "\r\n"
311 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
312 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
313 . '</dfrn_notify>' . "\r\n" ;