]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_notify.php
Removed Google Maps
[friendica.git] / mod / dfrn_notify.php
1 <?php
2
3 require_once('library/simplepie/simplepie.inc');
4 require_once('include/items.php');
5 require_once('include/event.php');
6
7 require_once('library/defuse/php-encryption-1.2.1/Crypto.php');
8
9 function dfrn_notify_post(&$a) {
10     logger(__function__, LOGGER_TRACE);
11         $dfrn_id      = ((x($_POST,'dfrn_id'))      ? notags(trim($_POST['dfrn_id']))   : '');
12         $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version']    : 2.0);
13         $challenge    = ((x($_POST,'challenge'))    ? notags(trim($_POST['challenge'])) : '');
14         $data         = ((x($_POST,'data'))         ? $_POST['data']                    : '');
15         $key          = ((x($_POST,'key'))          ? $_POST['key']                     : '');
16         $rino_remote  = ((x($_POST,'rino'))         ? intval($_POST['rino'])            :  0);
17         $dissolve     = ((x($_POST,'dissolve'))     ? intval($_POST['dissolve'])        :  0);
18         $perm         = ((x($_POST,'perm'))         ? notags(trim($_POST['perm']))      : 'r');
19         $ssl_policy   = ((x($_POST,'ssl_policy'))   ? notags(trim($_POST['ssl_policy'])): 'none');
20         $page         = ((x($_POST,'page'))         ? intval($_POST['page'])            :  0);
21
22         $forum = (($page == 1) ? 1 : 0);
23         $prv   = (($page == 2) ? 1 : 0);
24
25         $writable = (-1);
26         if($dfrn_version >= 2.21) {
27                 $writable = (($perm === 'rw') ? 1 : 0);
28         }
29
30         $direction = (-1);
31         if(strpos($dfrn_id,':') == 1) {
32                 $direction = intval(substr($dfrn_id,0,1));
33                 $dfrn_id = substr($dfrn_id,2);
34         }
35
36         $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
37                 dbesc($dfrn_id),
38                 dbesc($challenge)
39         );
40         if(! count($r)) {
41                 logger('dfrn_notify: could not match challenge to dfrn_id ' . $dfrn_id . ' challenge=' . $challenge);
42                 xml_status(3);
43         }
44
45         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'",
46                 dbesc($dfrn_id),
47                 dbesc($challenge)
48         );
49
50         // find the local user who owns this relationship.
51
52         $sql_extra = '';
53         switch($direction) {
54                 case (-1):
55                         $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
56                         break;
57                 case 0:
58                         $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
59                         break;
60                 case 1:
61                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
62                         break;
63                 default:
64                         xml_status(3);
65                         break; // NOTREACHED
66         }
67
68         // be careful - $importer will contain both the contact information for the contact
69         // sending us the post, and also the user information for the person receiving it.
70         // since they are mixed together, it is easy to get them confused.
71
72         $r = q("SELECT  `contact`.*, `contact`.`uid` AS `importer_uid`,
73                                         `contact`.`pubkey` AS `cpubkey`,
74                                         `contact`.`prvkey` AS `cprvkey`,
75                                         `contact`.`thumb` AS `thumb`,
76                                         `contact`.`url` as `url`,
77                                         `contact`.`name` as `senderName`,
78                                         `user`.*
79                         FROM `contact`
80                         LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
81                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0
82                                 AND `user`.`nickname` = '%s' AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1",
83                 dbesc($a->argv[1])
84         );
85
86         if(! count($r)) {
87                 logger('dfrn_notify: contact not found for dfrn_id ' . $dfrn_id);
88                 xml_status(3);
89                 //NOTREACHED
90         }
91
92         // $importer in this case contains the contact record for the remote contact joined with the user record of our user.
93
94         $importer = $r[0];
95
96         if((($writable != (-1)) && ($writable != $importer['writable'])) || ($importer['forum'] != $forum) || ($importer['prv'] != $prv)) {
97                 q("UPDATE `contact` SET `writable` = %d, forum = %d, prv = %d WHERE `id` = %d",
98                         intval(($writable == (-1)) ? $importer['writable'] : $writable),
99                         intval($forum),
100                         intval($prv),
101                         intval($importer['id'])
102                 );
103                 if($writable != (-1))
104                         $importer['writable'] = $writable;
105                 $importer['forum'] = $page;
106         }
107
108
109         // if contact's ssl policy changed, update our links
110
111         fix_contact_ssl_policy($importer,$ssl_policy);
112
113         logger('dfrn_notify: received notify from ' . $importer['name'] . ' for ' . $importer['username']);
114         logger('dfrn_notify: data: ' . $data, LOGGER_DATA);
115
116         if($dissolve == 1) {
117
118                 /**
119                  * Relationship is dissolved permanently
120                  */
121
122                 require_once('include/Contact.php');
123                 contact_remove($importer['id']);
124                 logger('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']);
125                 xml_status(0);
126
127         }
128
129
130         // If we are setup as a soapbox we aren't accepting input from this person
131
132         if($importer['page-flags'] == PAGE_SOAPBOX)
133                 xml_status(0);
134
135         
136         if(strlen($key)) {
137                 
138                 // if local rino is lower than remote rino, abort: should not happen!
139                 // but only for $remote_rino > 1, because old code did't send rino version
140                 if ($rino_remote_version > 1 && $rino < $rino_remote) {
141                         logger("rino version '$rino_remote' is lower than supported '$rino'");
142                         xml_status(0,"rino version '$rino_remote' is lower than supported '$rino'");
143                 }
144                 
145                 $rawkey = hex2bin(trim($key));
146                 logger('rino: md5 raw key: ' . md5($rawkey));
147                 $final_key = '';
148
149                 if($dfrn_version >= 2.1) {
150                         if((($importer['duplex']) && strlen($importer['cprvkey'])) || (! strlen($importer['cpubkey']))) {
151                                 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
152                         }
153                         else {
154                                 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
155                         }
156                 }
157                 else {
158                         if((($importer['duplex']) && strlen($importer['cpubkey'])) || (! strlen($importer['cprvkey']))) {
159                                 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
160                         }
161                         else {
162                                 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
163                         }
164                 }
165
166                 #logger('rino: received key : ' . $final_key);
167                 
168                 switch($rino_remote) {
169                         case 0:
170                         case 1:
171                                 // we got a key. old code send only the key, without RINO version.
172                                 // we assume RINO 1 if key and no RINO version
173                                 $data = aes_decrypt(hex2bin($data),$final_key);
174                                 break;
175                         case 2:
176                                 try {
177                                         $data = Crypto::decrypt(hex2bin($data),$final_key);
178                                 } catch (InvalidCiphertext $ex) { // VERY IMPORTANT
179                                         // Either:
180                                         //   1. The ciphertext was modified by the attacker,
181                                         //   2. The key is wrong, or
182                                         //   3. $ciphertext is not a valid ciphertext or was corrupted.
183                                         // Assume the worst.
184                                         logger('The ciphertext has been tampered with!');
185                                         xml_status(0,'The ciphertext has been tampered with!');
186                                 } catch (Ex\CryptoTestFailed $ex) {
187                                         logger('Cannot safely perform dencryption');
188                                         xml_status(0,'CryptoTestFailed');
189                                 } catch (Ex\CannotPerformOperation $ex) {
190                                         logger('Cannot safely perform decryption');
191                                         xml_status(0,'Cannot safely perform decryption');
192                                 }
193                                 break;
194                         default:
195                                 logger("rino: invalid sent verision '$rino_remote'");
196                                 xml_status(0);
197                 }
198                 
199                 
200                 logger('rino: decrypted data: ' . $data, LOGGER_DATA);
201         }
202
203         $ret = local_delivery($importer,$data);
204         xml_status($ret);
205
206         // NOTREACHED
207 }
208
209
210 function dfrn_notify_content(&$a) {
211
212         if(x($_GET,'dfrn_id')) {
213
214                 // initial communication from external contact, $direction is their direction.
215                 // If this is a duplex communication, ours will be the opposite.
216
217                 $dfrn_id = notags(trim($_GET['dfrn_id']));
218                 $dfrn_version = (float) $_GET['dfrn_version'];
219                 $rino_remote = ((x($_GET,'rino')) ? intval($_GET['rino']) : 0);
220                 $type = "";
221                 $last_update = "";
222
223                 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
224
225                 $direction = (-1);
226                 if(strpos($dfrn_id,':') == 1) {
227                         $direction = intval(substr($dfrn_id,0,1));
228                         $dfrn_id = substr($dfrn_id,2);
229                 }
230
231                 $hash = random_string();
232
233                 $status = 0;
234
235                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
236
237                 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
238                         VALUES( '%s', '%s', %d, '%s', '%s' ) ",
239                         dbesc($hash),
240                         dbesc($dfrn_id),
241                         intval(time() + 90 ),
242                         dbesc($type),
243                         dbesc($last_update)
244                 );
245
246                 logger('dfrn_notify: challenge=' . $hash, LOGGER_DEBUG );
247
248                 $sql_extra = '';
249                 switch($direction) {
250                         case (-1):
251                                 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
252                                 $my_id = $dfrn_id;
253                                 break;
254                         case 0:
255                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
256                                 $my_id = '1:' . $dfrn_id;
257                                 break;
258                         case 1:
259                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
260                                 $my_id = '0:' . $dfrn_id;
261                                 break;
262                         default:
263                                 $status = 1;
264                                 break; // NOTREACHED
265                 }
266
267                 $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`page-flags` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
268                                 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s'
269                                 AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1",
270                                 dbesc($a->argv[1])
271                 );
272
273                 if(! count($r))
274                         $status = 1;
275
276                 $challenge = '';
277                 $encrypted_id = '';
278                 $id_str = $my_id . '.' . mt_rand(1000,9999);
279
280                 $prv_key = trim($r[0]['prvkey']);
281                 $pub_key = trim($r[0]['pubkey']);
282                 $dplx = intval($r[0]['duplex']);
283
284                 if((($dplx) && (strlen($prv_key))) || ((strlen($prv_key)) && (!(strlen($pub_key))))) {
285                         openssl_private_encrypt($hash,$challenge,$prv_key);
286                         openssl_private_encrypt($id_str,$encrypted_id,$prv_key);
287                 }
288                 elseif(strlen($pub_key)) {
289                         openssl_public_encrypt($hash,$challenge,$pub_key);
290                         openssl_public_encrypt($id_str,$encrypted_id,$pub_key);
291                 }
292                 else
293                         $status = 1;
294
295                 $challenge    = bin2hex($challenge);
296                 $encrypted_id = bin2hex($encrypted_id);
297
298                 
299                 $rino = get_config('system','rino_encrypt');
300                 $rino = intval($rino);
301                 
302                 // if requested rino is lower than enabled local rino, lower local rino version
303                 // if requested rino is higher than enabled local rino, reply with local rino
304                 if ($rino_remote < $rino) $rino = $rino_remote;
305                 
306                 if((($r[0]['rel']) && ($r[0]['rel'] != CONTACT_IS_SHARING)) || ($r[0]['page-flags'] == PAGE_COMMUNITY)) {
307                         $perm = 'rw';
308                 }
309                 else {
310                         $perm = 'r';
311                 }
312
313                 header("Content-type: text/xml");
314
315                 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n"
316                         . '<dfrn_notify>' . "\r\n"
317                         . "\t" . '<status>' . $status . '</status>' . "\r\n"
318                         . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
319                         . "\t" . '<rino>' . $rino . '</rino>' . "\r\n"
320                         . "\t" . '<perm>' . $perm . '</perm>' . "\r\n"
321                         . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n"
322                         . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
323                         . '</dfrn_notify>' . "\r\n" ;
324
325                 killme();
326         }
327
328 }