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 $user = dba::selectFirst('user', [], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
29 if (!DBM::is_result($user)) {
30 System::httpExit(500);
32 $msg = Diaspora::decodeRaw($user, $postdata);
34 // Check if the user has got this contact
35 $cid = getIdForURL($msg['author'], $user['uid']);
37 // Otherwise there should be a public contact
38 $cid = getIdForURL($msg['author']);
40 logger('Contact not found for address ' . $msg['author']);
41 System::xmlExit(3, 'Contact not found');
45 // We now have some contact, so we fetch it
46 $importer = dba::fetch_first("SELECT *, `name` as `senderName`
48 WHERE NOT `blocked` AND `id` = ? LIMIT 1",
51 // This should never fail
52 if (!DBM::is_result($importer)) {
53 logger('Contact not found for address ' . $msg['author']);
54 System::xmlExit(3, 'Contact not found');
57 // Set the user id. This is important if this is a public contact
58 $importer['uid'] = $user['uid'];
59 $importer['importer_uid'] = $user['uid'];
61 // Now we should be able to import it
62 $ret = DFRN::import($msg['message'], $importer);
63 System::xmlExit($ret, 'Processed');
65 require_once 'mod/salmon.php';
66 salmon_post($a, $postdata);
70 $dfrn_id = ((x($_POST,'dfrn_id')) ? notags(trim($_POST['dfrn_id'])) : '');
71 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
72 $challenge = ((x($_POST,'challenge')) ? notags(trim($_POST['challenge'])) : '');
73 $data = ((x($_POST,'data')) ? $_POST['data'] : '');
74 $key = ((x($_POST,'key')) ? $_POST['key'] : '');
75 $rino_remote = ((x($_POST,'rino')) ? intval($_POST['rino']) : 0);
76 $dissolve = ((x($_POST,'dissolve')) ? intval($_POST['dissolve']) : 0);
77 $perm = ((x($_POST,'perm')) ? notags(trim($_POST['perm'])) : 'r');
78 $ssl_policy = ((x($_POST,'ssl_policy')) ? notags(trim($_POST['ssl_policy'])): 'none');
79 $page = ((x($_POST,'page')) ? intval($_POST['page']) : 0);
81 $forum = (($page == 1) ? 1 : 0);
82 $prv = (($page == 2) ? 1 : 0);
85 if ($dfrn_version >= 2.21) {
86 $writable = (($perm === 'rw') ? 1 : 0);
90 if (strpos($dfrn_id, ':') == 1) {
91 $direction = intval(substr($dfrn_id, 0, 1));
92 $dfrn_id = substr($dfrn_id, 2);
95 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
99 if (! DBM::is_result($r)) {
100 logger('dfrn_notify: could not match challenge to dfrn_id ' . $dfrn_id . ' challenge=' . $challenge);
101 System::xmlExit(3, 'Could not match challenge');
104 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'",
109 // find the local user who owns this relationship.
112 switch ($direction) {
114 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
117 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
120 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
123 System::xmlExit(3, 'Invalid direction');
128 * be careful - $importer will contain both the contact information for the contact
129 * sending us the post, and also the user information for the person receiving it.
130 * since they are mixed together, it is easy to get them confused.
133 $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
134 `contact`.`pubkey` AS `cpubkey`,
135 `contact`.`prvkey` AS `cprvkey`,
136 `contact`.`thumb` AS `thumb`,
137 `contact`.`url` as `url`,
138 `contact`.`name` as `senderName`,
141 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
142 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
143 AND `user`.`nickname` = '%s' AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1",
147 if (! DBM::is_result($r)) {
148 logger('dfrn_notify: contact not found for dfrn_id ' . $dfrn_id);
149 System::xmlExit(3, 'Contact not found');
153 // $importer in this case contains the contact record for the remote contact joined with the user record of our user.
157 logger("Remote rino version: ".$rino_remote." for ".$importer["url"], LOGGER_DEBUG);
159 if ((($writable != (-1)) && ($writable != $importer['writable'])) || ($importer['forum'] != $forum) || ($importer['prv'] != $prv)) {
160 q("UPDATE `contact` SET `writable` = %d, forum = %d, prv = %d WHERE `id` = %d",
161 intval(($writable == (-1)) ? $importer['writable'] : $writable),
164 intval($importer['id'])
166 if ($writable != (-1)) {
167 $importer['writable'] = $writable;
169 $importer['forum'] = $page;
173 // if contact's ssl policy changed, update our links
175 $importer = Contact::updateSslPolicy($importer, $ssl_policy);
177 logger('dfrn_notify: received notify from ' . $importer['name'] . ' for ' . $importer['username']);
178 logger('dfrn_notify: data: ' . $data, LOGGER_DATA);
180 if ($dissolve == 1) {
181 // Relationship is dissolved permanently
182 Contact::remove($importer['id']);
183 logger('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']);
184 System::xmlExit(0, 'relationship dissolved');
187 $rino = Config::get('system', 'rino_encrypt');
188 $rino = intval($rino);
190 logger("Local rino version: " . $rino, LOGGER_DEBUG);
194 // if local rino is lower than remote rino, abort: should not happen!
195 // but only for $remote_rino > 1, because old code did't send rino version
196 if ($rino_remote > 1 && $rino < $rino_remote) {
197 logger("rino version '$rino_remote' is lower than supported '$rino'");
198 System::xmlExit(0, "rino version '$rino_remote' is lower than supported '$rino'");
201 $rawkey = hex2bin(trim($key));
202 logger('rino: md5 raw key: ' . md5($rawkey));
205 if ($dfrn_version >= 2.1) {
206 if ((($importer['duplex']) && strlen($importer['cprvkey'])) || (! strlen($importer['cpubkey']))) {
207 openssl_private_decrypt($rawkey, $final_key, $importer['cprvkey']);
209 openssl_public_decrypt($rawkey, $final_key, $importer['cpubkey']);
212 if ((($importer['duplex']) && strlen($importer['cpubkey'])) || (! strlen($importer['cprvkey']))) {
213 openssl_public_decrypt($rawkey, $final_key, $importer['cpubkey']);
215 openssl_private_decrypt($rawkey, $final_key, $importer['cprvkey']);
219 switch($rino_remote) {
222 // we got a key. old code send only the key, without RINO version.
223 // we assume RINO 1 if key and no RINO version
224 $data = DFRN::aesDecrypt(hex2bin($data), $final_key);
227 logger("rino: invalid sent version '$rino_remote'");
228 System::xmlExit(0, "Invalid sent version '$rino_remote'");
231 logger('rino: decrypted data: ' . $data, LOGGER_DATA);
234 $ret = DFRN::import($data, $importer);
235 System::xmlExit($ret, 'Processed');
241 function dfrn_notify_content(App $a) {
243 if(x($_GET,'dfrn_id')) {
246 * initial communication from external contact, $direction is their direction.
247 * If this is a duplex communication, ours will be the opposite.
250 $dfrn_id = notags(trim($_GET['dfrn_id']));
251 $dfrn_version = (float) $_GET['dfrn_version'];
252 $rino_remote = ((x($_GET,'rino')) ? intval($_GET['rino']) : 0);
256 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
259 if(strpos($dfrn_id,':') == 1) {
260 $direction = intval(substr($dfrn_id,0,1));
261 $dfrn_id = substr($dfrn_id,2);
264 $hash = random_string();
268 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
270 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
271 VALUES( '%s', '%s', %d, '%s', '%s' ) ",
274 intval(time() + 90 ),
279 logger('dfrn_notify: challenge=' . $hash, LOGGER_DEBUG);
284 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
288 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
289 $my_id = '1:' . $dfrn_id;
292 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
293 $my_id = '0:' . $dfrn_id;
300 $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`page-flags` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
301 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s'
302 AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1",
306 if (! DBM::is_result($r)) {
310 logger("Remote rino version: ".$rino_remote." for ".$r[0]["url"], LOGGER_DEBUG);
314 $id_str = $my_id . '.' . mt_rand(1000,9999);
316 $prv_key = trim($r[0]['prvkey']);
317 $pub_key = trim($r[0]['pubkey']);
318 $dplx = intval($r[0]['duplex']);
320 if ((($dplx) && (strlen($prv_key))) || ((strlen($prv_key)) && (!(strlen($pub_key))))) {
321 openssl_private_encrypt($hash, $challenge, $prv_key);
322 openssl_private_encrypt($id_str, $encrypted_id, $prv_key);
323 } elseif (strlen($pub_key)) {
324 openssl_public_encrypt($hash, $challenge, $pub_key);
325 openssl_public_encrypt($id_str, $encrypted_id, $pub_key);
327 /// @TODO these kind of else-blocks are making the code harder to understand
331 $challenge = bin2hex($challenge);
332 $encrypted_id = bin2hex($encrypted_id);
335 $rino = Config::get('system', 'rino_encrypt');
336 $rino = intval($rino);
338 logger("Local rino version: ". $rino, LOGGER_DEBUG);
340 // if requested rino is lower than enabled local rino, lower local rino version
341 // if requested rino is higher than enabled local rino, reply with local rino
342 if ($rino_remote < $rino) {
343 $rino = $rino_remote;
346 if((($r[0]['rel']) && ($r[0]['rel'] != CONTACT_IS_SHARING)) || ($r[0]['page-flags'] == PAGE_COMMUNITY)) {
352 header("Content-type: text/xml");
354 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
355 . '<dfrn_notify>' . "\r\n"
356 . "\t" . '<status>' . $status . '</status>' . "\r\n"
357 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
358 . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
359 . "\t" . '<perm>' . $perm . '</perm>' . "\r\n"
360 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
361 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
362 . '</dfrn_notify>' . "\r\n" ;