]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_confirm.php
default acl's
[friendica.git] / mod / dfrn_confirm.php
1 <?php
2
3
4
5 function dfrn_confirm_post(&$a) {
6         
7         if($a->argc > 1)
8                 $node = $a->argv[1];
9
10         if(x($_POST,'source_url')) {
11
12                 // We are processing an external confirmation to an introduction created by our user.
13
14                 $public_key = $_POST['public_key'];
15                 $dfrn_id = $_POST['dfrn_id'];
16                 $source_url = $_POST['source_url'];
17                 $aes_key = $_POST['aes_key'];
18
19                 // Find our user's account
20
21                 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1",
22                         dbesc($node));
23
24                 if(! count($r)) {
25                         xml_status(3); // failure
26                 }
27
28                 $my_prvkey = $r[0]['prvkey'];
29                 $local_uid = $r[0]['uid'];
30
31                 $decrypted_source_url = "";
32
33                 // verify everything
34
35                 openssl_private_decrypt($source_url,$decrypted_source_url,$my_prvkey);
36
37
38                 $ret = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
39                         dbesc($decrypted_source_url),
40                         intval($local_uid));
41
42                 if(! count($ret)) {
43                         // this is either a bogus confirmation or we deleted the original introduction.
44                         xml_status(3); 
45                 }
46
47                 // Decrypt all this stuff we just received
48
49                 $foreign_pubkey = $ret[0]['site-pubkey'];
50                 $dfrn_record = $ret[0]['id'];
51                 $decrypted_dfrn_id = "";
52                 openssl_public_decrypt($dfrn_id,$decrypted_dfrn_id,$foreign_pubkey);
53
54                 if(strlen($aes_key)) {
55                         $decrypted_aes_key = "";
56                         openssl_private_decrypt($aes_key,$decrypted_aes_key,$my_prvkey);
57                         $dfrn_pubkey = openssl_decrypt($public_key,'AES-256-CBC',$decrypted_aes_key);
58                 }
59                 else {
60                         $dfrn_pubkey = $public_key;
61                 }
62
63                 $r = q("SELECT * FROM `contact` WHERE `dfrn-id` = '%s' LIMIT 1",
64                         dbesc($decrypted_dfrn_id),
65                         intval($local_uid));
66                 if(count($r))
67                         xml_status(1); // Birthday paradox - duplicate dfrn-id
68
69                 $r = q("UPDATE `contact` SET `dfrn-id` = '%s', `pubkey` = '%s' WHERE `id` = %d LIMIT 1",
70                         dbesc($decrypted_dfrn_id),
71                         dbesc($dfrn_pubkey),
72                         intval($dfrn_record));
73                 if($r) {
74
75                         // We're good but now we have to scrape the profile photo and send notifications.
76
77                         require_once("Photo.php");
78
79                         $photo_failure = false;
80
81                         $r = q("SELECT `photo` FROM `contact` WHERE `id` = %d LIMIT 1",
82                                 intval($dfrn_record));
83                         if(count($r)) {
84
85                                 $filename = basename($r[0]['photo']);
86                                 $img_str = fetch_url($r[0]['photo'],true);
87                                 $img = new Photo($img_str);
88                                 if($img) {
89
90                                         $img->scaleImageSquare(175);
91                                         
92                                         $hash = hash('md5',uniqid(mt_rand(),true));
93
94                                         $r = $img->store($local_uid, $dfrn_record, $hash, $filename, t('Contact Photos') , 4);
95
96                                         if($r === false)
97                                                 $photo_failure = true;
98                                         
99                                         $img->scaleImage(80);
100                                         $r = $img->store($local_uid, $dfrn_record, $hash, $filename, t('Contact Photos') , 5);
101
102                                         if($r === false)
103                                                 $photo_failure = true;
104
105                                         $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
106                                         $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';      
107                                 }
108                                 else
109                                         $photo_failure = true;
110                         }
111                         else
112                                 $photo_failure = true;
113
114                         if($photo_failure) {
115                                 $photo = $a->get_baseurl() . '/images/default-profile.jpg';
116                                 $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
117                         }
118
119                         $r = q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `blocked` = 0, `pending` = 0 WHERE `id` = %d LIMIT 1",
120                                 dbesc($photo),
121                                 dbesc($thumb),
122                                 intval($dfrn_record)
123                         );
124                         if($r === false)
125                                 notice( t("Unable to set contact photo info.") . EOL);
126
127                         // Otherwise everything seems to have worked and we are almost done. Yay!
128                         // Send an email notification
129
130                         $r = q("SELECT * FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
131                                 WHERE `contact`.`id` = %d LIMIT 1",
132                                 intval($dfrn_record));
133                         if((count($r)) && ($r[0]['notify-flags'] & NOTIFY_CONFIRM)) {
134
135                                 $tpl = file_get_contents('view/intro_complete_eml.tpl');
136                         
137                                 $email_tpl = replace_macros($tpl, array(
138                                         '$sitename' => $a->config['sitename'],
139                                         '$siteurl' =>  $a->get_baseurl(),
140                                         '$username' => $r[0]['username'],
141                                         '$email' => $r[0]['email'],
142                                         '$fn' => $r[0]['name'],
143                                         '$dfrn_url' => $r[0]['url'],
144                                         '$uid' => $newuid )
145                                 );
146         
147                                 $res = mail($r[0]['email'], t("Introduction accepted at ") . $a->config['sitename'],
148                                         $email_tpl,t("From: Administrator@") . $_SERVER[SERVER_NAME] );
149                                 if(!$res) {
150                                         notice( t("Email notification failed.") . EOL );
151                                 }
152                         }
153                         xml_status(0); // Success
154
155                         return; // NOTREACHED
156                 }
157                 else {
158                         xml_status(2);  // Hopefully temporary problem that can be retried.
159                 }
160                 return; // NOTREACHED
161
162         ////////////////////// End of this scenario ///////////////////////////////////////////////
163         }
164         else {
165
166                 // We are processing a local confirmation initiated on this system by our user to an external introduction.
167
168                 $uid = $_SESSION['uid'];
169
170                 if(! $uid) {
171                         notice( t("Permission denied.") . EOL );
172                         return;
173                 }       
174         
175                 $dfrn_id = ((x($_POST,'dfrn_id')) ? notags(trim($_POST['dfrn_id'])) : "");
176                 $intro_id = intval($_POST['intro_id']);
177
178                 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' AND `uid` = %d LIMIT 1",
179                                 dbesc($dfrn_id),
180                                 intval($uid)
181                                 );
182
183                 if((! $r) || (! count($r))) {
184                         notice( t('Node does not exist.') . EOL );
185                         return;
186                 }
187
188                 $contact_id = $r[0]['id'];
189                 $site_pubkey = $r[0]['site-pubkey'];
190                 $dfrn_confirm = $r[0]['confirm'];
191                 $aes_allow = $r[0]['aes_allow'];
192
193                 $res=openssl_pkey_new(array(
194                         'digest_alg' => 'whirlpool',
195                         'private_key_bits' => 4096,
196                         'encrypt_key' => false ));
197
198
199                 $private_key = '';
200
201                 openssl_pkey_export($res, $private_key);
202
203
204                 $pubkey = openssl_pkey_get_details($res);
205                 $public_key = $pubkey["key"];
206
207                 $r = q("UPDATE `contact` SET `issued-pubkey` = '%s', `prvkey` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
208                         dbesc($public_key),
209                         dbesc($private_key),
210                         intval($contact_id),
211                         intval($uid) 
212                 );
213
214
215                 $params = array();
216
217                 $src_aes_key = random_string();
218                 $result = "";
219
220                 openssl_private_encrypt($dfrn_id,$result,$a->user['prvkey']);
221
222                 $params['dfrn_id'] = $result;
223                 $params['public_key'] = $public_key;
224
225
226                 openssl_public_encrypt($_SESSION['my_url'], $params['source_url'], $site_pubkey);
227
228                 if($aes_allow && function_exists('openssl_encrypt')) {
229                         openssl_public_encrypt($src_aes_key, $params['aes_key'], $site_pubkey);
230                         $params['public_key'] = openssl_encrypt($public_key,'AES-256-CBC',$src_aes_key);
231                 }
232
233                 $res = post_url($dfrn_confirm,$params);
234
235 // uncomment the following two lines and comment the following xml/status lines
236 // to debug the remote confirmation section (when both confirmations 
237 // and responses originate on this system)
238
239 // echo $res;
240 // $status = 0;
241
242                 $xml = simplexml_load_string($res);
243                 $status = (int) $xml->status;
244                 switch($status) {
245                         case 0:
246                                 notice( t("Confirmation completed successfully") . EOL);
247                                 break;
248                         case 1:
249
250                                 // birthday paradox - generate new dfrn-id and fall through.
251
252                                 $new_dfrn_id = random_string();
253                                 $r = q("UPDATE contact SET `issued-id` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
254                                         dbesc($new_dfrn_id),
255                                         intval($contact_id),
256                                         intval($uid) 
257                                 );
258
259                         case 2:
260                                 notice( t("Temporary failure. Please wait and try again.") . EOL);
261                                 break;
262
263
264                         case 3:
265                                 notice( t("Introduction failed or was revoked. Cannot complete.") . EOL);
266                                 break;
267                 }
268
269                 if(($status == 0 || $status == 3) && ($intro_id)) {
270
271                         //delete the notification
272
273                         $r = q("DELETE FROM `intro` WHERE `id` = %d AND `uid` = %d LIMIT 1",
274                                 intval($intro_id),
275                                 intval($uid)
276                         );
277                         
278                 }
279                 if($status != 0) 
280                         return;
281                 
282
283                 require_once("Photo.php");
284
285                 $photo_failure = false;
286
287                 $r = q("SELECT `photo` FROM `contact` WHERE `id` = %d LIMIT 1",
288                         intval($contact_id));
289                 if(count($r)) {
290
291                         $filename = basename($r[0]['photo']);
292                         $img_str = fetch_url($r[0]['photo'],true);
293                         $img = new Photo($img_str);
294                         if($img) {
295
296                                 $img->scaleImageSquare(175);
297                                         
298                                 $hash = hash('md5',uniqid(mt_rand(),true));
299
300                                 $r = $img->store($uid, $contact_id, $hash, $filename, t('Contact Photos'), 4 );
301
302                                 if($r === false)
303                                         $photo_failure = true;
304                                 $img->scaleImage(80);
305
306                                 $r = $img->store($uid, $contact_id, $hash, $filename, t('Contact Photos'), 5 );
307
308                                 if($r === false)
309                                         $photo_failure = true;
310
311                                 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
312                                 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
313                         }
314                         else
315                                 $photo_failure = true;
316                 }
317                 else
318                         $photo_failure = true;
319
320                 if($photo_failure) {
321                         $photo = $a->get_baseurl() . '/images/default-profile.jpg';
322                         $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
323                 }
324
325                 $r = q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `blocked` = 0, `pending` = 0 WHERE `id` = %d LIMIT 1",
326                         dbesc($photo),
327                         dbesc($thumb),
328                         intval($contact_id)
329                 );
330                 if($r === false)
331                         notice( t('Unable to set contact photo.') . EOL);
332
333                 goaway($a->get_baseurl() . '/contacts/' . intval($contact_id));
334                 return;  //NOTREACHED
335
336         }
337
338         return;
339 }