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, '');
28 $public = empty($nick);
30 $user = dba::selectFirst('user', [], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
31 if (!DBM::is_result($user)) {
32 System::httpExit(500);
35 // We don't need the user with public posts
38 $msg = Diaspora::decodeRaw($user, $postdata);
41 dfrn_dispatch_public($msg);
43 dfrn_dispatch_private($user, $msg);
46 require_once 'mod/salmon.php';
47 salmon_post($a, $postdata);
51 $dfrn_id = ((x($_POST,'dfrn_id')) ? notags(trim($_POST['dfrn_id'])) : '');
52 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
53 $challenge = ((x($_POST,'challenge')) ? notags(trim($_POST['challenge'])) : '');
54 $data = ((x($_POST,'data')) ? $_POST['data'] : '');
55 $key = ((x($_POST,'key')) ? $_POST['key'] : '');
56 $rino_remote = ((x($_POST,'rino')) ? intval($_POST['rino']) : 0);
57 $dissolve = ((x($_POST,'dissolve')) ? intval($_POST['dissolve']) : 0);
58 $perm = ((x($_POST,'perm')) ? notags(trim($_POST['perm'])) : 'r');
59 $ssl_policy = ((x($_POST,'ssl_policy')) ? notags(trim($_POST['ssl_policy'])): 'none');
60 $page = ((x($_POST,'page')) ? intval($_POST['page']) : 0);
62 $forum = (($page == 1) ? 1 : 0);
63 $prv = (($page == 2) ? 1 : 0);
66 if ($dfrn_version >= 2.21) {
67 $writable = (($perm === 'rw') ? 1 : 0);
71 if (strpos($dfrn_id, ':') == 1) {
72 $direction = intval(substr($dfrn_id, 0, 1));
73 $dfrn_id = substr($dfrn_id, 2);
76 if (!dba::exists('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge])) {
77 logger('could not match challenge to dfrn_id ' . $dfrn_id . ' challenge=' . $challenge);
78 System::xmlExit(3, 'Could not match challenge');
81 dba::delete('challenge', ['dfrn-id' => $dfrn_id, 'challenge' => $challenge]);
83 // find the local user who owns this relationship.
88 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
91 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
94 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
97 System::xmlExit(3, 'Invalid direction');
102 * be careful - $importer will contain both the contact information for the contact
103 * sending us the post, and also the user information for the person receiving it.
104 * since they are mixed together, it is easy to get them confused.
107 $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
108 `contact`.`pubkey` AS `cpubkey`,
109 `contact`.`prvkey` AS `cprvkey`,
110 `contact`.`thumb` AS `thumb`,
111 `contact`.`url` as `url`,
112 `contact`.`name` as `senderName`,
115 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
116 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
117 AND `user`.`nickname` = '%s' AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1",
121 if (!DBM::is_result($r)) {
122 logger('contact not found for dfrn_id ' . $dfrn_id);
123 System::xmlExit(3, 'Contact not found');
127 // $importer in this case contains the contact record for the remote contact joined with the user record of our user.
131 if ((($writable != (-1)) && ($writable != $importer['writable'])) || ($importer['forum'] != $forum) || ($importer['prv'] != $prv)) {
132 $fields = ['writable' => ($writable == (-1)) ? $importer['writable'] : $writable,
133 'forum' => $forum, 'prv' => $prv];
134 dba::update('contact', $fields, ['id' => $importer['id']]);
136 if ($writable != (-1)) {
137 $importer['writable'] = $writable;
139 $importer['forum'] = $page;
143 // if contact's ssl policy changed, update our links
145 $importer = Contact::updateSslPolicy($importer, $ssl_policy);
147 logger('data: ' . $data, LOGGER_DATA);
149 if ($dissolve == 1) {
150 // Relationship is dissolved permanently
151 Contact::remove($importer['id']);
152 logger('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']);
153 System::xmlExit(0, 'relationship dissolved');
156 $rino = Config::get('system', 'rino_encrypt');
157 $rino = intval($rino);
161 // if local rino is lower than remote rino, abort: should not happen!
162 // but only for $remote_rino > 1, because old code did't send rino version
163 if ($rino_remote > 1 && $rino < $rino_remote) {
164 logger("rino version '$rino_remote' is lower than supported '$rino'");
165 System::xmlExit(0, "rino version '$rino_remote' is lower than supported '$rino'");
168 $rawkey = hex2bin(trim($key));
169 logger('rino: md5 raw key: ' . md5($rawkey), LOGGER_DATA);
173 if ($dfrn_version >= 2.1) {
174 if (($importer['duplex'] && strlen($importer['cprvkey'])) || !strlen($importer['cpubkey'])) {
175 openssl_private_decrypt($rawkey, $final_key, $importer['cprvkey']);
177 openssl_public_decrypt($rawkey, $final_key, $importer['cpubkey']);
180 if (($importer['duplex'] && strlen($importer['cpubkey'])) || !strlen($importer['cprvkey'])) {
181 openssl_public_decrypt($rawkey, $final_key, $importer['cpubkey']);
183 openssl_private_decrypt($rawkey, $final_key, $importer['cprvkey']);
187 switch ($rino_remote) {
190 // we got a key. old code send only the key, without RINO version.
191 // we assume RINO 1 if key and no RINO version
192 $data = DFRN::aesDecrypt(hex2bin($data), $final_key);
195 logger("rino: invalid sent version '$rino_remote'");
196 System::xmlExit(0, "Invalid sent version '$rino_remote'");
199 logger('rino: decrypted data: ' . $data, LOGGER_DATA);
202 logger('Importing post from ' . $importer['addr'] . ' to ' . $importer['nickname'] . ' with the RINO ' . $rino_remote . ' encryption.', LOGGER_DEBUG);
204 $ret = DFRN::import($data, $importer);
205 System::xmlExit($ret, 'Processed');
210 function dfrn_dispatch_public($msg)
212 // Fetch the corresponding public contact
213 $contact = getDetailsByAddr($msg['author'], 0);
215 logger('Contact not found for address ' . $msg['author']);
216 System::xmlExit(3, 'Contact not found');
219 // We now have some contact, so we fetch it
220 $importer = dba::fetch_first("SELECT *, `name` as `senderName`
222 WHERE NOT `blocked` AND `id` = ? LIMIT 1",
225 // This should never fail
226 if (!DBM::is_result($importer)) {
227 logger('Contact not found for address ' . $msg['author']);
228 System::xmlExit(3, 'Contact not found');
231 logger('Importing post from ' . $msg['author'] . ' with the public envelope.', LOGGER_DEBUG);
233 // Now we should be able to import it
234 $ret = DFRN::import($msg['message'], $importer);
235 System::xmlExit($ret, 'Done');
238 function dfrn_dispatch_private($user, $msg)
240 // Check if the user has got this contact
241 $cid = Contact::getIdForURL($msg['author'], $user['uid']);
243 // Otherwise there should be a public contact
244 $cid = Contact::getIdForURL($msg['author']);
246 logger('Contact not found for address ' . $msg['author']);
247 System::xmlExit(3, 'Contact not found');
251 // We now have some contact, so we fetch it
252 $importer = dba::fetch_first("SELECT *, `name` as `senderName`
254 WHERE NOT `blocked` AND `id` = ? LIMIT 1",
257 // This should never fail
258 if (!DBM::is_result($importer)) {
259 logger('Contact not found for address ' . $msg['author']);
260 System::xmlExit(3, 'Contact not found');
263 // Set the user id. This is important if this is a public contact
264 $importer['importer_uid'] = $user['uid'];
266 logger('Importing post from ' . $msg['author'] . ' to ' . $user['nickname'] . ' with the private envelope.', LOGGER_DEBUG);
268 // Now we should be able to import it
269 $ret = DFRN::import($msg['message'], $importer);
270 System::xmlExit($ret, 'Done');
273 function dfrn_notify_content(App $a) {
275 if (x($_GET,'dfrn_id')) {
278 * initial communication from external contact, $direction is their direction.
279 * If this is a duplex communication, ours will be the opposite.
282 $dfrn_id = notags(trim($_GET['dfrn_id']));
283 $dfrn_version = (float) $_GET['dfrn_version'];
284 $rino_remote = ((x($_GET,'rino')) ? intval($_GET['rino']) : 0);
288 logger('new notification dfrn_id=' . $dfrn_id);
291 if (strpos($dfrn_id,':') == 1) {
292 $direction = intval(substr($dfrn_id,0,1));
293 $dfrn_id = substr($dfrn_id,2);
296 $hash = random_string();
300 dba::delete('challenge', ["`expire` < ?", time()]);
302 $fields = ['challenge' => $hash, 'dfrn-id' => $dfrn_id, 'expire' => time() + 90,
303 'type' => $type, 'last_update' => $last_update];
304 dba::insert('challenge', $fields);
306 logger('challenge=' . $hash, LOGGER_DATA);
311 $sql_extra = sprintf(" AND (`issued-id` = '%s' OR `dfrn-id` = '%s') ", dbesc($dfrn_id), dbesc($dfrn_id));
315 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
316 $my_id = '1:' . $dfrn_id;
319 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
320 $my_id = '0:' . $dfrn_id;
327 $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`page-flags` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
328 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s'
329 AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1",
333 if (!DBM::is_result($r)) {
337 logger("Remote rino version: ".$rino_remote." for ".$r[0]["url"], LOGGER_DATA);
341 $id_str = $my_id . '.' . mt_rand(1000,9999);
343 $prv_key = trim($r[0]['prvkey']);
344 $pub_key = trim($r[0]['pubkey']);
345 $dplx = intval($r[0]['duplex']);
347 if (($dplx && strlen($prv_key)) || (strlen($prv_key) && !strlen($pub_key))) {
348 openssl_private_encrypt($hash, $challenge, $prv_key);
349 openssl_private_encrypt($id_str, $encrypted_id, $prv_key);
350 } elseif (strlen($pub_key)) {
351 openssl_public_encrypt($hash, $challenge, $pub_key);
352 openssl_public_encrypt($id_str, $encrypted_id, $pub_key);
354 /// @TODO these kind of else-blocks are making the code harder to understand
358 $challenge = bin2hex($challenge);
359 $encrypted_id = bin2hex($encrypted_id);
362 $rino = Config::get('system', 'rino_encrypt');
363 $rino = intval($rino);
365 logger("Local rino version: ". $rino, LOGGER_DATA);
367 // if requested rino is lower than enabled local rino, lower local rino version
368 // if requested rino is higher than enabled local rino, reply with local rino
369 if ($rino_remote < $rino) {
370 $rino = $rino_remote;
373 if (($r[0]['rel'] && ($r[0]['rel'] != CONTACT_IS_SHARING)) || ($r[0]['page-flags'] == PAGE_COMMUNITY)) {
379 header("Content-type: text/xml");
381 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
382 . '<dfrn_notify>' . "\r\n"
383 . "\t" . '<status>' . $status . '</status>' . "\r\n"
384 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
385 . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
386 . "\t" . '<perm>' . $perm . '</perm>' . "\r\n"
387 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
388 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
389 . '</dfrn_notify>' . "\r\n" ;