]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_notify.php
Merge branch 'master' of https://github.com/friendica/friendica
[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
8 function dfrn_notify_post(&$a) {
9
10         $dfrn_id      = ((x($_POST,'dfrn_id'))      ? notags(trim($_POST['dfrn_id']))   : '');
11         $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version']    : 2.0);
12         $challenge    = ((x($_POST,'challenge'))    ? notags(trim($_POST['challenge'])) : '');
13         $data         = ((x($_POST,'data'))         ? $_POST['data']                    : '');
14         $key          = ((x($_POST,'key'))          ? $_POST['key']                     : '');
15         $dissolve     = ((x($_POST,'dissolve'))     ? intval($_POST['dissolve'])        :  0);
16         $perm         = ((x($_POST,'perm'))         ? notags(trim($_POST['perm']))      : 'r');
17         $ssl_policy   = ((x($_POST,'ssl_policy'))   ? notags(trim($_POST['ssl_policy'])): 'none');
18
19         $writable = (-1);
20         if($dfrn_version >= 2.21) {
21                 $writable = (($perm === 'rw') ? 1 : 0);
22         }
23
24         $direction = (-1);
25         if(strpos($dfrn_id,':') == 1) {
26                 $direction = intval(substr($dfrn_id,0,1));
27                 $dfrn_id = substr($dfrn_id,2);
28         }
29
30         $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
31                 dbesc($dfrn_id),
32                 dbesc($challenge)
33         );
34         if(! count($r)) {
35                 logger('dfrn_notify: could not match challenge to dfrn_id ' . $dfrn_id . ' challenge=' . $challenge);
36                 xml_status(3);
37         }
38
39         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
40                 dbesc($dfrn_id),
41                 dbesc($challenge)
42         );
43
44         // find the local user who owns this relationship.
45
46         $sql_extra = '';
47         switch($direction) {
48                 case (-1):
49                         $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
50                         break;
51                 case 0:
52                         $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
53                         break;
54                 case 1:
55                         $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
56                         break;
57                 default:
58                         xml_status(3);
59                         break; // NOTREACHED
60         }
61                  
62         // be careful - $importer will contain both the contact information for the contact
63         // sending us the post, and also the user information for the person receiving it.
64         // since they are mixed together, it is easy to get them confused.
65
66         $r = q("SELECT  `contact`.*, `contact`.`uid` AS `importer_uid`, 
67                                         `contact`.`pubkey` AS `cpubkey`, 
68                                         `contact`.`prvkey` AS `cprvkey`, 
69                                         `contact`.`thumb` AS `thumb`, 
70                                         `contact`.`url` as `url`,
71                                         `contact`.`name` as `senderName`,
72                                         `user`.* 
73                         FROM `contact` 
74                         LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
75                         WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
76                                 AND `user`.`nickname` = '%s' AND `user`.`account_expired` = 0 $sql_extra LIMIT 1",
77                 dbesc($a->argv[1])
78         );
79
80         if(! count($r)) {
81                 logger('dfrn_notify: contact not found for dfrn_id ' . $dfrn_id);
82                 xml_status(3);
83                 //NOTREACHED
84         }
85
86         // $importer in this case contains the contact record for the remote contact joined with the user record of our user. 
87
88         $importer = $r[0];
89
90         if(($writable != (-1)) && ($writable != $importer['writable'])) {
91                 q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d LIMIT 1",
92                         intval($writable),
93                         intval($importer['id'])
94                 );
95                 $importer['writable'] = $writable;
96         }
97
98         // if contact's ssl policy changed, update our links
99
100         $ssl_changed = false;
101
102         if($ssl_policy == 'self' && strstr($importer['url'],'https:')) {
103                 $ssl_changed = true;
104                 $importer['url']     =  str_replace('https:','http:',$importer['url']);
105                 $importer['nurl']    = normalise_link($importer['url']);
106                 $importer['photo']   =  str_replace('https:','http:',$importer['photo']);
107                 $importer['thumb']   =  str_replace('https:','http:',$importer['thumb']);
108                 $importer['micro']   =  str_replace('https:','http:',$importer['micro']);
109                 $importer['request'] =  str_replace('https:','http:',$importer['request']);
110                 $importer['notify']  =  str_replace('https:','http:',$importer['notify']);
111                 $importer['poll']    =  str_replace('https:','http:',$importer['poll']);
112                 $importer['confirm'] =  str_replace('https:','http:',$importer['confirm']);
113                 $importer['poco']    =  str_replace('https:','http:',$importer['poco']);
114         }
115
116         if($ssl_policy == 'full' && strstr($importer['url'],'http:')) {
117                 $ssl_changed = true;
118                 $importer['url']     =  str_replace('http:','https:',$importer['url']);
119                 $importer['nurl']    = normalise_link($importer['url']);
120                 $importer['photo']   =  str_replace('http:','https:',$importer['photo']);
121                 $importer['thumb']   =  str_replace('http:','https:',$importer['thumb']);
122                 $importer['micro']   =  str_replace('http:','https:',$importer['micro']);
123                 $importer['request'] =  str_replace('http:','https:',$importer['request']);
124                 $importer['notify']  =  str_replace('http:','https:',$importer['notify']);
125                 $importer['poll']    =  str_replace('http:','https:',$importer['poll']);
126                 $importer['confirm'] =  str_replace('http:','https:',$importer['confirm']);
127                 $importer['poco']    =  str_replace('http:','https:',$importer['poco']);
128         }
129
130         if($ssl_changed) {
131                 q("update contact set 
132                         url = '%s', 
133                         nurl = '%s',
134                         photo = '%s',
135                         thumb = '%s',
136                         micro = '%s',
137                         request = '%s',
138                         notify = '%s',
139                         poll = '%s',
140                         confirm = '%s',
141                         poco = '%s'
142                         where id = %d limit 1",
143                         dbesc($importer['url']),
144                         dbesc($importer['nurl']),
145                         dbesc($importer['photo']),
146                         dbesc($importer['thumb']),
147                         dbesc($importer['micro']),
148                         dbesc($importer['request']),
149                         dbesc($importer['notify']),
150                         dbesc($importer['poll']),
151                         dbesc($importer['confirm']),
152                         dbesc($importer['poco']),
153                         intval($importer['id'])
154                 );
155         }
156                         
157         logger('dfrn_notify: received notify from ' . $importer['name'] . ' for ' . $importer['username']);
158         logger('dfrn_notify: data: ' . $data, LOGGER_DATA);
159
160         if($dissolve == 1) {
161
162                 /**
163                  * Relationship is dissolved permanently
164                  */
165
166                 require_once('include/Contact.php'); 
167                 contact_remove($importer['id']);
168                 logger('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']);
169                 xml_status(0);
170
171         }
172
173         if(strlen($key)) {
174                 $rawkey = hex2bin(trim($key));
175                 logger('rino: md5 raw key: ' . md5($rawkey));
176                 $final_key = '';
177
178                 if($dfrn_version >= 2.1) {
179                         if((($importer['duplex']) && strlen($importer['cprvkey'])) || (! strlen($importer['cpubkey']))) {
180                                 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
181                         }
182                         else {
183                                 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
184                         }
185                 }
186                 else {
187                         if((($importer['duplex']) && strlen($importer['cpubkey'])) || (! strlen($importer['cprvkey']))) {
188                                 openssl_public_decrypt($rawkey,$final_key,$importer['cpubkey']);
189                         }
190                         else {
191                                 openssl_private_decrypt($rawkey,$final_key,$importer['cprvkey']);
192                         }
193                 }
194
195                 logger('rino: received key : ' . $final_key);
196                 $data = aes_decrypt(hex2bin($data),$final_key);
197                 logger('rino: decrypted data: ' . $data, LOGGER_DATA);
198         }
199
200
201         $ret = local_delivery($importer,$data);
202         xml_status($ret);
203
204         // NOTREACHED
205 }
206
207
208 function dfrn_notify_content(&$a) {
209
210         if(x($_GET,'dfrn_id')) {
211
212                 // initial communication from external contact, $direction is their direction.
213                 // If this is a duplex communication, ours will be the opposite.
214
215                 $dfrn_id = notags(trim($_GET['dfrn_id']));
216                 $dfrn_version = (float) $_GET['dfrn_version'];
217
218                 logger('dfrn_notify: new notification dfrn_id=' . $dfrn_id);
219
220                 $direction = (-1);
221                 if(strpos($dfrn_id,':') == 1) {
222                         $direction = intval(substr($dfrn_id,0,1));
223                         $dfrn_id = substr($dfrn_id,2);
224                 }
225
226                 $hash = random_string();
227
228                 $status = 0;
229
230                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
231
232                 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` )
233                         VALUES( '%s', '%s', %d ) ",
234                         dbesc($hash),
235                         dbesc($dfrn_id),
236                         intval(time() + 90 )
237                 );
238
239                 logger('dfrn_notify: challenge=' . $hash, LOGGER_DEBUG );
240
241                 $sql_extra = '';
242                 switch($direction) {
243                         case (-1):
244                                 $sql_extra = sprintf(" AND ( `issued-id` = '%s' OR `dfrn-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
245                                 $my_id = $dfrn_id;
246                                 break;
247                         case 0:
248                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
249                                 $my_id = '1:' . $dfrn_id;
250                                 break;
251                         case 1:
252                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
253                                 $my_id = '0:' . $dfrn_id;
254                                 break;
255                         default:
256                                 $status = 1;
257                                 break; // NOTREACHED
258                 }
259
260                 $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
261                                 WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s' 
262                                 AND `user`.`account_expired` = 0 $sql_extra LIMIT 1",
263                                 dbesc($a->argv[1])
264                 );
265
266                 if(! count($r))
267                         $status = 1;
268
269                 $challenge = '';
270                 $encrypted_id = '';
271                 $id_str = $my_id . '.' . mt_rand(1000,9999);
272
273                 $prv_key = trim($r[0]['prvkey']);
274                 $pub_key = trim($r[0]['pubkey']);
275                 $dplx = intval($r[0]['duplex']);
276
277                 if((($dplx) && (strlen($prv_key))) || ((strlen($prv_key)) && (!(strlen($pub_key))))) {
278                         openssl_private_encrypt($hash,$challenge,$prv_key);
279                         openssl_private_encrypt($id_str,$encrypted_id,$prv_key);
280                 }
281                 elseif(strlen($pub_key)) {
282                         openssl_public_encrypt($hash,$challenge,$pub_key);
283                         openssl_public_encrypt($id_str,$encrypted_id,$pub_key);
284                 }
285                 else
286                         $status = 1;
287
288                 $challenge    = bin2hex($challenge);
289                 $encrypted_id = bin2hex($encrypted_id);
290
291                 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
292
293                 $rino_enable = get_config('system','rino_encrypt');
294
295                 if(! $rino_enable)
296                         $rino = 0;
297
298
299                 header("Content-type: text/xml");
300
301                 echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" 
302                         . '<dfrn_notify>' . "\r\n"
303                         . "\t" . '<status>' . $status . '</status>' . "\r\n"
304                         . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n"
305                         . "\t" . '<rino>' . $rino . '</rino>' . "\r\n" 
306                         . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n" 
307                         . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n"
308                         . '</dfrn_notify>' . "\r\n" ;
309
310                 killme();
311         }
312
313 }