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\DBA;
13 use Friendica\Database\DBM;
14 use Friendica\Model\Contact;
15 use Friendica\Protocol\DFRN;
16 use Friendica\Protocol\Diaspora;
18 require_once 'include/items.php';
20 function dfrn_notify_post(App $a) {
21 logger(__function__, LOGGER_TRACE);
23 $postdata = file_get_contents('php://input');
25 if (empty($_POST) || !empty($postdata)) {
26 $data = json_decode($postdata);
27 if (is_object($data)) {
28 $nick = defaults($a->argv, 1, '');
30 $user = DBA::selectFirst('user', [], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
31 if (!DBM::is_result($user)) {
32 System::httpExit(500);
34 dfrn_dispatch_private($user, $postdata);
35 } elseif (!dfrn_dispatch_public($postdata)) {
36 require_once 'mod/salmon.php';
37 salmon_post($a, $postdata);
41 $dfrn_id = ((x($_POST,'dfrn_id')) ? notags(trim($_POST['dfrn_id'])) : '');
42 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
43 $challenge = ((x($_POST,'challenge')) ? notags(trim($_POST['challenge'])) : '');
44 $data = ((x($_POST,'data')) ? $_POST['data'] : '');
45 $key = ((x($_POST,'key')) ? $_POST['key'] : '');
46 $rino_remote = ((x($_POST,'rino')) ? intval($_POST['rino']) : 0);
47 $dissolve = ((x($_POST,'dissolve')) ? intval($_POST['dissolve']) : 0);
48 $perm = ((x($_POST,'perm')) ? notags(trim($_POST['perm'])) : 'r');
49 $ssl_policy = ((x($_POST,'ssl_policy')) ? notags(trim($_POST['ssl_policy'])): 'none');
50 $page = ((x($_POST,'page')) ? intval($_POST['page']) : 0);
52 $forum = (($page == 1) ? 1 : 0);
53 $prv = (($page == 2) ? 1 : 0);
56 if ($dfrn_version >= 2.21) {
57 $writable = (($perm === 'rw') ? 1 : 0);
61 if (strpos($dfrn_id, ':') == 1) {
62 $direction = intval(substr($dfrn_id, 0, 1));
63 $dfrn_id = substr($dfrn_id, 2);
66 if (!DBA::exists('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge])) {
67 logger('could not match challenge to dfrn_id ' . $dfrn_id . ' challenge=' . $challenge);
68 System::xmlExit(3, 'Could not match challenge');
71 DBA::delete('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge]);
73 // find the local user who owns this relationship.
78 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
81 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
84 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
87 System::xmlExit(3, 'Invalid direction');
92 * be careful - $importer will contain both the contact information for the contact
93 * sending us the post, and also the user information for the person receiving it.
94 * since they are mixed together, it is easy to get them confused.
97 $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
98 `contact`.`pubkey` AS `cpubkey`,
99 `contact`.`prvkey` AS `cprvkey`,
100 `contact`.`thumb` AS `thumb`,
101 `contact`.`url` as `url`,
102 `contact`.`name` as `senderName`,
105 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
106 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
107 AND `user`.`nickname` = '%s' AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1",
111 if (!DBM::is_result($r)) {
112 logger('contact not found for dfrn_id ' . $dfrn_id);
113 System::xmlExit(3, 'Contact not found');
117 // $importer in this case contains the contact record for the remote contact joined with the user record of our user.
121 if ((($writable != (-1)) && ($writable != $importer['writable'])) || ($importer['forum'] != $forum) || ($importer['prv'] != $prv)) {
122 $fields = ['writable' => ($writable == (-1)) ? $importer['writable'] : $writable,
123 'forum' => $forum, 'prv' => $prv];
124 DBA::update('contact', $fields, ['id' => $importer['id']]);
126 if ($writable != (-1)) {
127 $importer['writable'] = $writable;
129 $importer['forum'] = $page;
133 // if contact's ssl policy changed, update our links
135 $importer = Contact::updateSslPolicy($importer, $ssl_policy);
137 logger('data: ' . $data, LOGGER_DATA);
139 if ($dissolve == 1) {
140 // Relationship is dissolved permanently
141 Contact::remove($importer['id']);
142 logger('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']);
143 System::xmlExit(0, 'relationship dissolved');
146 $rino = Config::get('system', 'rino_encrypt');
147 $rino = intval($rino);
151 // if local rino is lower than remote rino, abort: should not happen!
152 // but only for $remote_rino > 1, because old code did't send rino version
153 if ($rino_remote > 1 && $rino < $rino_remote) {
154 logger("rino version '$rino_remote' is lower than supported '$rino'");
155 System::xmlExit(0, "rino version '$rino_remote' is lower than supported '$rino'");
158 $rawkey = hex2bin(trim($key));
159 logger('rino: md5 raw key: ' . md5($rawkey), LOGGER_DATA);
163 if ($dfrn_version >= 2.1) {
164 if (($importer['duplex'] && strlen($importer['cprvkey'])) || !strlen($importer['cpubkey'])) {
165 openssl_private_decrypt($rawkey, $final_key, $importer['cprvkey']);
167 openssl_public_decrypt($rawkey, $final_key, $importer['cpubkey']);
170 if (($importer['duplex'] && strlen($importer['cpubkey'])) || !strlen($importer['cprvkey'])) {
171 openssl_public_decrypt($rawkey, $final_key, $importer['cpubkey']);
173 openssl_private_decrypt($rawkey, $final_key, $importer['cprvkey']);
177 switch ($rino_remote) {
180 // we got a key. old code send only the key, without RINO version.
181 // we assume RINO 1 if key and no RINO version
182 $data = DFRN::aesDecrypt(hex2bin($data), $final_key);
185 logger("rino: invalid sent version '$rino_remote'");
186 System::xmlExit(0, "Invalid sent version '$rino_remote'");
189 logger('rino: decrypted data: ' . $data, LOGGER_DATA);
192 logger('Importing post from ' . $importer['addr'] . ' to ' . $importer['nickname'] . ' with the RINO ' . $rino_remote . ' encryption.', LOGGER_DEBUG);
194 $ret = DFRN::import($data, $importer);
195 System::xmlExit($ret, 'Processed');
200 function dfrn_dispatch_public($postdata)
202 $msg = Diaspora::decodeRaw([], $postdata);
204 // We have to fail silently to be able to hand it over to the salmon parser
208 // Fetch the corresponding public contact
209 $contact = Contact::getDetailsByAddr($msg['author'], 0);
211 logger('Contact not found for address ' . $msg['author']);
212 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
215 // We now have some contact, so we fetch it
216 $importer = DBA::fetch_first("SELECT *, `name` as `senderName`
218 WHERE NOT `blocked` AND `id` = ? LIMIT 1",
221 $importer['importer_uid'] = 0;
223 // This should never fail
224 if (!DBM::is_result($importer)) {
225 logger('Contact not found for address ' . $msg['author']);
226 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
229 logger('Importing post from ' . $msg['author'] . ' with the public envelope.', LOGGER_DEBUG);
231 // Now we should be able to import it
232 $ret = DFRN::import($msg['message'], $importer);
233 System::xmlExit($ret, 'Done');
236 function dfrn_dispatch_private($user, $postdata)
238 $msg = Diaspora::decodeRaw($user, $postdata);
240 System::xmlExit(4, 'Unable to parse message');
243 // Check if the user has got this contact
244 $cid = Contact::getIdForURL($msg['author'], $user['uid']);
246 // Otherwise there should be a public contact
247 $cid = Contact::getIdForURL($msg['author']);
249 logger('Contact not found for address ' . $msg['author']);
250 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
254 // We now have some contact, so we fetch it
255 $importer = DBA::fetch_first("SELECT *, `name` as `senderName`
257 WHERE NOT `blocked` AND `id` = ? LIMIT 1",
260 // This should never fail
261 if (!DBM::is_result($importer)) {
262 logger('Contact not found for address ' . $msg['author']);
263 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
266 // Set the user id. This is important if this is a public contact
267 $importer['importer_uid'] = $user['uid'];
269 $importer = array_merge($importer, $user);
271 logger('Importing post from ' . $msg['author'] . ' to ' . $user['nickname'] . ' with the private envelope.', LOGGER_DEBUG);
273 // Now we should be able to import it
274 $ret = DFRN::import($msg['message'], $importer);
275 System::xmlExit($ret, 'Done');
278 function dfrn_notify_content(App $a) {
280 if (x($_GET,'dfrn_id')) {
283 * initial communication from external contact, $direction is their direction.
284 * If this is a duplex communication, ours will be the opposite.
287 $dfrn_id = notags(trim($_GET['dfrn_id']));
288 $dfrn_version = (float) $_GET['dfrn_version'];
289 $rino_remote = ((x($_GET,'rino')) ? intval($_GET['rino']) : 0);
293 logger('new notification dfrn_id=' . $dfrn_id);
296 if (strpos($dfrn_id,':') == 1) {
297 $direction = intval(substr($dfrn_id,0,1));
298 $dfrn_id = substr($dfrn_id,2);
301 $hash = random_string();
305 DBA::delete('challenge', ["`expire` < ?", time()]);
307 $fields = ['challenge' => $hash, 'dfrn-id' => $dfrn_id, 'expire' => time() + 90,
308 'type' => $type, 'last_update' => $last_update];
309 DBA::insert('challenge', $fields);
311 logger('challenge=' . $hash, LOGGER_DATA);
316 $sql_extra = sprintf(" AND (`issued-id` = '%s' OR `dfrn-id` = '%s') ", dbesc($dfrn_id), dbesc($dfrn_id));
320 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
321 $my_id = '1:' . $dfrn_id;
324 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
325 $my_id = '0:' . $dfrn_id;
332 $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`page-flags` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
333 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s'
334 AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1",
338 if (!DBM::is_result($r)) {
339 logger('No user data found for ' . $a->argv[1] . ' - SQL: ' . $sql_extra);
343 logger("Remote rino version: ".$rino_remote." for ".$r[0]["url"], LOGGER_DATA);
347 $id_str = $my_id . '.' . mt_rand(1000,9999);
349 $prv_key = trim($r[0]['prvkey']);
350 $pub_key = trim($r[0]['pubkey']);
351 $dplx = intval($r[0]['duplex']);
353 if (($dplx && strlen($prv_key)) || (strlen($prv_key) && !strlen($pub_key))) {
354 openssl_private_encrypt($hash, $challenge, $prv_key);
355 openssl_private_encrypt($id_str, $encrypted_id, $prv_key);
356 } elseif (strlen($pub_key)) {
357 openssl_public_encrypt($hash, $challenge, $pub_key);
358 openssl_public_encrypt($id_str, $encrypted_id, $pub_key);
360 /// @TODO these kind of else-blocks are making the code harder to understand
364 $challenge = bin2hex($challenge);
365 $encrypted_id = bin2hex($encrypted_id);
368 $rino = Config::get('system', 'rino_encrypt');
369 $rino = intval($rino);
371 logger("Local rino version: ". $rino, LOGGER_DATA);
373 // if requested rino is lower than enabled local rino, lower local rino version
374 // if requested rino is higher than enabled local rino, reply with local rino
375 if ($rino_remote < $rino) {
376 $rino = $rino_remote;
379 if (($r[0]['rel'] && ($r[0]['rel'] != CONTACT_IS_SHARING)) || ($r[0]['page-flags'] == PAGE_COMMUNITY)) {
385 header("Content-type: text/xml");
387 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
388 . '<dfrn_notify>' . "\r\n"
389 . "\t" . '<status>' . $status . '</status>' . "\r\n"
390 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
391 . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
392 . "\t" . '<perm>' . $perm . '</perm>' . "\r\n"
393 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
394 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
395 . '</dfrn_notify>' . "\r\n" ;