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';
18 function dfrn_notify_post(App $a) {
19 logger(__function__, LOGGER_TRACE);
22 require_once 'mod/salmon.php';
26 $dfrn_id = ((x($_POST,'dfrn_id')) ? notags(trim($_POST['dfrn_id'])) : '');
27 $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0);
28 $challenge = ((x($_POST,'challenge')) ? notags(trim($_POST['challenge'])) : '');
29 $data = ((x($_POST,'data')) ? $_POST['data'] : '');
30 $key = ((x($_POST,'key')) ? $_POST['key'] : '');
31 $rino_remote = ((x($_POST,'rino')) ? intval($_POST['rino']) : 0);
32 $dissolve = ((x($_POST,'dissolve')) ? intval($_POST['dissolve']) : 0);
33 $perm = ((x($_POST,'perm')) ? notags(trim($_POST['perm'])) : 'r');
34 $ssl_policy = ((x($_POST,'ssl_policy')) ? notags(trim($_POST['ssl_policy'])): 'none');
35 $page = ((x($_POST,'page')) ? intval($_POST['page']) : 0);
37 $forum = (($page == 1) ? 1 : 0);
38 $prv = (($page == 2) ? 1 : 0);
41 if ($dfrn_version >= 2.21) {
42 $writable = (($perm === 'rw') ? 1 : 0);
46 if (strpos($dfrn_id, ':') == 1) {
47 $direction = intval(substr($dfrn_id, 0, 1));
48 $dfrn_id = substr($dfrn_id, 2);
51 $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
55 if (! DBM::is_result($r)) {
56 logger('dfrn_notify: could not match challenge to dfrn_id ' . $dfrn_id . ' challenge=' . $challenge);
57 System::xmlExit(3, 'Could not match challenge');
60 $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'",
65 // find the local user who owns this relationship.
70 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
73 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
76 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
79 System::xmlExit(3, 'Invalid direction');
84 * be careful - $importer will contain both the contact information for the contact
85 * sending us the post, and also the user information for the person receiving it.
86 * since they are mixed together, it is easy to get them confused.
89 $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
90 `contact`.`pubkey` AS `cpubkey`,
91 `contact`.`prvkey` AS `cprvkey`,
92 `contact`.`thumb` AS `thumb`,
93 `contact`.`url` as `url`,
94 `contact`.`name` as `senderName`,
97 LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
98 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
99 AND `user`.`nickname` = '%s' AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1",
103 if (! DBM::is_result($r)) {
104 logger('dfrn_notify: contact not found for dfrn_id ' . $dfrn_id);
105 System::xmlExit(3, 'Contact not found');
109 // $importer in this case contains the contact record for the remote contact joined with the user record of our user.
113 logger("Remote rino version: ".$rino_remote." for ".$importer["url"], LOGGER_DEBUG);
115 if ((($writable != (-1)) && ($writable != $importer['writable'])) || ($importer['forum'] != $forum) || ($importer['prv'] != $prv)) {
116 q("UPDATE `contact` SET `writable` = %d, forum = %d, prv = %d WHERE `id` = %d",
117 intval(($writable == (-1)) ? $importer['writable'] : $writable),
120 intval($importer['id'])
122 if ($writable != (-1)) {
123 $importer['writable'] = $writable;
125 $importer['forum'] = $page;
129 // if contact's ssl policy changed, update our links
131 $importer = Contact::updateSslPolicy($importer, $ssl_policy);
133 logger('dfrn_notify: received notify from ' . $importer['name'] . ' for ' . $importer['username']);
134 logger('dfrn_notify: data: ' . $data, LOGGER_DATA);
136 if ($dissolve == 1) {
137 // Relationship is dissolved permanently
138 Contact::remove($importer['id']);
139 logger('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']);
140 System::xmlExit(0, 'relationship dissolved');
143 $rino = Config::get('system', 'rino_encrypt');
144 $rino = intval($rino);
146 logger("Local rino version: " . $rino, LOGGER_DEBUG);
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));
161 if ($dfrn_version >= 2.1) {
162 if ((($importer['duplex']) && strlen($importer['cprvkey'])) || (! strlen($importer['cpubkey']))) {
163 openssl_private_decrypt($rawkey, $final_key, $importer['cprvkey']);
165 openssl_public_decrypt($rawkey, $final_key, $importer['cpubkey']);
168 if ((($importer['duplex']) && strlen($importer['cpubkey'])) || (! strlen($importer['cprvkey']))) {
169 openssl_public_decrypt($rawkey, $final_key, $importer['cpubkey']);
171 openssl_private_decrypt($rawkey, $final_key, $importer['cprvkey']);
175 switch($rino_remote) {
178 // we got a key. old code send only the key, without RINO version.
179 // we assume RINO 1 if key and no RINO version
180 $data = DFRN::aesDecrypt(hex2bin($data), $final_key);
183 logger("rino: invalid sent version '$rino_remote'");
184 System::xmlExit(0, "Invalid sent version '$rino_remote'");
187 logger('rino: decrypted data: ' . $data, LOGGER_DATA);
190 $ret = DFRN::import($data, $importer);
191 System::xmlExit($ret, 'Processed');
197 function dfrn_notify_content(App $a) {
199 if(x($_GET,'dfrn_id')) {
202 * initial communication from external contact, $direction is their direction.
203 * If this is a duplex communication, ours will be the opposite.
206 $dfrn_id = notags(trim($_GET['dfrn_id']));
207 $dfrn_version = (float) $_GET['dfrn_version'];
208 $rino_remote = ((x($_GET,'rino')) ? intval($_GET['rino']) : 0);
212 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
215 if(strpos($dfrn_id,':') == 1) {
216 $direction = intval(substr($dfrn_id,0,1));
217 $dfrn_id = substr($dfrn_id,2);
220 $hash = random_string();
224 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
226 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
227 VALUES( '%s', '%s', %d, '%s', '%s' ) ",
230 intval(time() + 90 ),
235 logger('dfrn_notify: challenge=' . $hash, LOGGER_DEBUG);
240 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
244 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
245 $my_id = '1:' . $dfrn_id;
248 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
249 $my_id = '0:' . $dfrn_id;
256 $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`page-flags` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
257 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s'
258 AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1",
262 if (! DBM::is_result($r)) {
266 logger("Remote rino version: ".$rino_remote." for ".$r[0]["url"], LOGGER_DEBUG);
270 $id_str = $my_id . '.' . mt_rand(1000,9999);
272 $prv_key = trim($r[0]['prvkey']);
273 $pub_key = trim($r[0]['pubkey']);
274 $dplx = intval($r[0]['duplex']);
276 if ((($dplx) && (strlen($prv_key))) || ((strlen($prv_key)) && (!(strlen($pub_key))))) {
277 openssl_private_encrypt($hash, $challenge, $prv_key);
278 openssl_private_encrypt($id_str, $encrypted_id, $prv_key);
279 } elseif (strlen($pub_key)) {
280 openssl_public_encrypt($hash, $challenge, $pub_key);
281 openssl_public_encrypt($id_str, $encrypted_id, $pub_key);
283 /// @TODO these kind of else-blocks are making the code harder to understand
287 $challenge = bin2hex($challenge);
288 $encrypted_id = bin2hex($encrypted_id);
291 $rino = Config::get('system', 'rino_encrypt');
292 $rino = intval($rino);
294 logger("Local rino version: ". $rino, LOGGER_DEBUG);
296 // if requested rino is lower than enabled local rino, lower local rino version
297 // if requested rino is higher than enabled local rino, reply with local rino
298 if ($rino_remote < $rino) {
299 $rino = $rino_remote;
302 if((($r[0]['rel']) && ($r[0]['rel'] != CONTACT_IS_SHARING)) || ($r[0]['page-flags'] == PAGE_COMMUNITY)) {
308 header("Content-type: text/xml");
310 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
311 . '<dfrn_notify>' . "\r\n"
312 . "\t" . '<status>' . $status . '</status>' . "\r\n"
313 . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
314 . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
315 . "\t" . '<perm>' . $perm . '</perm>' . "\r\n"
316 . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
317 . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
318 . '</dfrn_notify>' . "\r\n" ;