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