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;
15 use Friendica\Protocol\Diaspora;
17 require_once 'include/items.php';
19 function dfrn_notify_post(App $a) {
20 logger(__function__, LOGGER_TRACE);
22 $postdata = file_get_contents('php://input');
24 if (empty($_POST) || !empty($postdata)) {
25 $data = json_decode($postdata);
26 if (is_object($data)) {
27 $nick = defaults($a->argv, 1, '');
29 $user = dba::selectFirst('user', [], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
30 if (!DBM::is_result($user)) {
31 System::httpExit(500);
33 dfrn_dispatch_private($user, $postdata);
34 } elseif (!dfrn_dispatch_public($postdata)) {
35 require_once 'mod/salmon.php';
36 salmon_post($a, $postdata);
40 $dfrn_id = ((x($_POST,'dfrn_id')) ? notags(trim($_POST['dfrn_id'])) : '');
41 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
42 $challenge = ((x($_POST,'challenge')) ? notags(trim($_POST['challenge'])) : '');
43 $data = ((x($_POST,'data')) ? $_POST['data'] : '');
44 $key = ((x($_POST,'key')) ? $_POST['key'] : '');
45 $rino_remote = ((x($_POST,'rino')) ? intval($_POST['rino']) : 0);
46 $dissolve = ((x($_POST,'dissolve')) ? intval($_POST['dissolve']) : 0);
47 $perm = ((x($_POST,'perm')) ? notags(trim($_POST['perm'])) : 'r');
48 $ssl_policy = ((x($_POST,'ssl_policy')) ? notags(trim($_POST['ssl_policy'])): 'none');
49 $page = ((x($_POST,'page')) ? intval($_POST['page']) : 0);
51 $forum = (($page == 1) ? 1 : 0);
52 $prv = (($page == 2) ? 1 : 0);
55 if ($dfrn_version >= 2.21) {
56 $writable = (($perm === 'rw') ? 1 : 0);
60 if (strpos($dfrn_id, ':') == 1) {
61 $direction = intval(substr($dfrn_id, 0, 1));
62 $dfrn_id = substr($dfrn_id, 2);
65 if (!dba::exists('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge])) {
66 logger('could not match challenge to dfrn_id ' . $dfrn_id . ' challenge=' . $challenge);
67 System::xmlExit(3, 'Could not match challenge');
70 dba::delete('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge]);
72 // find the local user who owns this relationship.
77 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
80 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
83 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
86 System::xmlExit(3, 'Invalid direction');
91 * be careful - $importer will contain both the contact information for the contact
92 * sending us the post, and also the user information for the person receiving it.
93 * since they are mixed together, it is easy to get them confused.
96 $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
97 `contact`.`pubkey` AS `cpubkey`,
98 `contact`.`prvkey` AS `cprvkey`,
99 `contact`.`thumb` AS `thumb`,
100 `contact`.`url` as `url`,
101 `contact`.`name` as `senderName`,
104 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
105 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
106 AND `user`.`nickname` = '%s' AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1",
110 if (!DBM::is_result($r)) {
111 logger('contact not found for dfrn_id ' . $dfrn_id);
112 System::xmlExit(3, 'Contact not found');
116 // $importer in this case contains the contact record for the remote contact joined with the user record of our user.
120 if ((($writable != (-1)) && ($writable != $importer['writable'])) || ($importer['forum'] != $forum) || ($importer['prv'] != $prv)) {
121 $fields = ['writable' => ($writable == (-1)) ? $importer['writable'] : $writable,
122 'forum' => $forum, 'prv' => $prv];
123 dba::update('contact', $fields, ['id' => $importer['id']]);
125 if ($writable != (-1)) {
126 $importer['writable'] = $writable;
128 $importer['forum'] = $page;
132 // if contact's ssl policy changed, update our links
134 $importer = Contact::updateSslPolicy($importer, $ssl_policy);
136 logger('data: ' . $data, LOGGER_DATA);
138 if ($dissolve == 1) {
139 // Relationship is dissolved permanently
140 Contact::remove($importer['id']);
141 logger('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']);
142 System::xmlExit(0, 'relationship dissolved');
145 $rino = Config::get('system', 'rino_encrypt');
146 $rino = intval($rino);
150 // if local rino is lower than remote rino, abort: should not happen!
151 // but only for $remote_rino > 1, because old code did't send rino version
152 if ($rino_remote > 1 && $rino < $rino_remote) {
153 logger("rino version '$rino_remote' is lower than supported '$rino'");
154 System::xmlExit(0, "rino version '$rino_remote' is lower than supported '$rino'");
157 $rawkey = hex2bin(trim($key));
158 logger('rino: md5 raw key: ' . md5($rawkey), LOGGER_DATA);
162 if ($dfrn_version >= 2.1) {
163 if (($importer['duplex'] && strlen($importer['cprvkey'])) || !strlen($importer['cpubkey'])) {
164 openssl_private_decrypt($rawkey, $final_key, $importer['cprvkey']);
166 openssl_public_decrypt($rawkey, $final_key, $importer['cpubkey']);
169 if (($importer['duplex'] && strlen($importer['cpubkey'])) || !strlen($importer['cprvkey'])) {
170 openssl_public_decrypt($rawkey, $final_key, $importer['cpubkey']);
172 openssl_private_decrypt($rawkey, $final_key, $importer['cprvkey']);
176 switch ($rino_remote) {
179 // we got a key. old code send only the key, without RINO version.
180 // we assume RINO 1 if key and no RINO version
181 $data = DFRN::aesDecrypt(hex2bin($data), $final_key);
184 logger("rino: invalid sent version '$rino_remote'");
185 System::xmlExit(0, "Invalid sent version '$rino_remote'");
188 logger('rino: decrypted data: ' . $data, LOGGER_DATA);
191 logger('Importing post from ' . $importer['addr'] . ' to ' . $importer['nickname'] . ' with the RINO ' . $rino_remote . ' encryption.', LOGGER_DEBUG);
193 $ret = DFRN::import($data, $importer);
194 System::xmlExit($ret, 'Processed');
199 function dfrn_dispatch_public($postdata)
201 $msg = Diaspora::decodeRaw([], $postdata);
203 // We have to fail silently to be able to hand it over to the salmon parser
207 // Fetch the corresponding public contact
208 $contact = Contact::getDetailsByAddr($msg['author'], 0);
210 logger('Contact not found for address ' . $msg['author']);
211 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
214 // We now have some contact, so we fetch it
215 $importer = dba::fetch_first("SELECT *, `name` as `senderName`
217 WHERE NOT `blocked` AND `id` = ? LIMIT 1",
220 $importer['importer_uid'] = 0;
222 // This should never fail
223 if (!DBM::is_result($importer)) {
224 logger('Contact not found for address ' . $msg['author']);
225 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
228 logger('Importing post from ' . $msg['author'] . ' with the public envelope.', LOGGER_DEBUG);
230 // Now we should be able to import it
231 $ret = DFRN::import($msg['message'], $importer);
232 System::xmlExit($ret, 'Done');
235 function dfrn_dispatch_private($user, $postdata)
237 $msg = Diaspora::decodeRaw($user, $postdata);
239 System::xmlExit(4, 'Unable to parse message');
242 // Check if the user has got this contact
243 $cid = Contact::getIdForURL($msg['author'], $user['uid']);
245 // Otherwise there should be a public contact
246 $cid = Contact::getIdForURL($msg['author']);
248 logger('Contact not found for address ' . $msg['author']);
249 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
253 // We now have some contact, so we fetch it
254 $importer = dba::fetch_first("SELECT *, `name` as `senderName`
256 WHERE NOT `blocked` AND `id` = ? LIMIT 1",
259 // This should never fail
260 if (!DBM::is_result($importer)) {
261 logger('Contact not found for address ' . $msg['author']);
262 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
265 // Set the user id. This is important if this is a public contact
266 $importer['importer_uid'] = $user['uid'];
268 logger('Importing post from ' . $msg['author'] . ' to ' . $user['nickname'] . ' with the private envelope.', LOGGER_DEBUG);
270 // Now we should be able to import it
271 $ret = DFRN::import($msg['message'], $importer);
272 System::xmlExit($ret, 'Done');
275 function dfrn_notify_content(App $a) {
277 if (x($_GET,'dfrn_id')) {
280 * initial communication from external contact, $direction is their direction.
281 * If this is a duplex communication, ours will be the opposite.
284 $dfrn_id = notags(trim($_GET['dfrn_id']));
285 $dfrn_version = (float) $_GET['dfrn_version'];
286 $rino_remote = ((x($_GET,'rino')) ? intval($_GET['rino']) : 0);
290 logger('new notification dfrn_id=' . $dfrn_id);
293 if (strpos($dfrn_id,':') == 1) {
294 $direction = intval(substr($dfrn_id,0,1));
295 $dfrn_id = substr($dfrn_id,2);
298 $hash = random_string();
302 dba::delete('challenge', ["`expire` < ?", time()]);
304 $fields = ['challenge' => $hash, 'dfrn-id' => $dfrn_id, 'expire' => time() + 90,
305 'type' => $type, 'last_update' => $last_update];
306 dba::insert('challenge', $fields);
308 logger('challenge=' . $hash, LOGGER_DATA);
313 $sql_extra = sprintf(" AND (`issued-id` = '%s' OR `dfrn-id` = '%s') ", dbesc($dfrn_id), dbesc($dfrn_id));
317 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
318 $my_id = '1:' . $dfrn_id;
321 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
322 $my_id = '0:' . $dfrn_id;
329 $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`page-flags` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
330 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s'
331 AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1",
335 if (!DBM::is_result($r)) {
339 logger("Remote rino version: ".$rino_remote." for ".$r[0]["url"], LOGGER_DATA);
343 $id_str = $my_id . '.' . mt_rand(1000,9999);
345 $prv_key = trim($r[0]['prvkey']);
346 $pub_key = trim($r[0]['pubkey']);
347 $dplx = intval($r[0]['duplex']);
349 if (($dplx && strlen($prv_key)) || (strlen($prv_key) && !strlen($pub_key))) {
350 openssl_private_encrypt($hash, $challenge, $prv_key);
351 openssl_private_encrypt($id_str, $encrypted_id, $prv_key);
352 } elseif (strlen($pub_key)) {
353 openssl_public_encrypt($hash, $challenge, $pub_key);
354 openssl_public_encrypt($id_str, $encrypted_id, $pub_key);
356 /// @TODO these kind of else-blocks are making the code harder to understand
360 $challenge = bin2hex($challenge);
361 $encrypted_id = bin2hex($encrypted_id);
364 $rino = Config::get('system', 'rino_encrypt');
365 $rino = intval($rino);
367 logger("Local rino version: ". $rino, LOGGER_DATA);
369 // if requested rino is lower than enabled local rino, lower local rino version
370 // if requested rino is higher than enabled local rino, reply with local rino
371 if ($rino_remote < $rino) {
372 $rino = $rino_remote;
375 if (($r[0]['rel'] && ($r[0]['rel'] != CONTACT_IS_SHARING)) || ($r[0]['page-flags'] == PAGE_COMMUNITY)) {
381 header("Content-type: text/xml");
383 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
384 . '<dfrn_notify>' . "\r\n"
385 . "\t" . '<status>' . $status . '</status>' . "\r\n"
386 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
387 . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
388 . "\t" . '<perm>' . $perm . '</perm>' . "\r\n"
389 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
390 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
391 . '</dfrn_notify>' . "\r\n" ;