]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_confirm.php
0298e76a52612f5e3ce5144ddbbadf9587eb6861
[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 = q("INSERT INTO `photo` ( `uid`, `contact-id`, `resource-id`, `created`, `edited`, `filename`,
95                                                 `height`, `width`, `data`, `scale` )
96                                                 VALUES ( %d, %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 4 )",
97                                                 intval($local_uid),
98                                                 intval($dfrn_record),
99                                                 dbesc($hash),
100                                                 datetime_convert(),
101                                                 datetime_convert(),
102                                                 dbesc(basename($r[0]['photo'])),
103                                                 intval($img->getHeight()),
104                                                 intval($img->getWidth()),
105                                                 dbesc($img->imageString())
106                                         );
107                                         if($r === false)
108                                                 $photo_failure = true;
109                                         
110                                         $img->scaleImage(80);
111                                         $r =  q("INSERT INTO `photo` ( `uid`, `contact-id`, `resource-id`, `created`, `edited`, `filename`,
112                                                 `height`, `width`, `data`, `scale` )
113                                                 VALUES ( %d, %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 5 )",
114                                                 intval($local_uid),
115                                                 intval($dfrn_record),
116                                                 dbesc($hash),
117                                                 datetime_convert(),
118                                                 datetime_convert(),
119                                                 dbesc(basename($r[0]['photo'])),
120                                                 intval($img->getHeight()),
121                                                 intval($img->getWidth()),
122                                                 dbesc($img->imageString())
123                                         );
124                                         if($r === false)
125                                                 $photo_failure = true;
126
127                                         $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
128                                         $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
129                                         
130                                 }
131                                 else
132                                         $photo_failure = true;
133                         }
134                         else
135                                 $photo_failure = true;
136
137                         if($photo_failure) {
138                                 $photo = $a->get_baseurl() . '/images/default-profile.jpg';
139                                 $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
140                         }
141
142                         $r = q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `blocked` = 0, `pending` = 0 WHERE `id` = %d LIMIT 1",
143                                 dbesc($photo),
144                                 dbesc($thumb),
145                                 intval($dfrn_record)
146                         );
147                         if($r === false)
148                                 notice( t("Unable to set contact photo info.") . EOL);
149
150                         // Otherwise everything seems to have worked and we are almost done. Yay!
151                         // Send an email notification
152
153                         $r = q("SELECT * FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
154                                 WHERE `contact`.`id` = %d LIMIT 1",
155                                 intval($dfrn_record));
156                         if((count($r)) && ($r[0]['notify-flags'] & NOTIFY_CONFIRM)) {
157
158                                 $tpl = file_get_contents('view/intro_complete_eml.tpl');
159                         
160                                 $email_tpl = replace_macros($tpl, array(
161                                         '$sitename' => $a->config['sitename'],
162                                         '$siteurl' =>  $a->get_baseurl(),
163                                         '$username' => $r[0]['username'],
164                                         '$email' => $r[0]['email'],
165                                         '$fn' => $r[0]['name'],
166                                         '$dfrn_url' => $r[0]['url'],
167                                         '$uid' => $newuid )
168                                 );
169         
170                                 $res = mail($r[0]['email'], t("Introduction accepted at ") . $a->config['sitename'],
171                                         $email_tpl,t("From: Administrator@") . $_SERVER[SERVER_NAME] );
172                                 if(!$res) {
173                                         notice( t("Email notification failed.") . EOL );
174                                 }
175                         }
176                         xml_status(0); // Success
177
178                         return; // NOTREACHED
179                 }
180                 else {
181                         xml_status(2);  // Hopefully temporary problem that can be retried.
182                 }
183                 return; // NOTREACHED
184
185         ////////////////////// End of this scenario ///////////////////////////////////////////////
186         }
187         else {
188
189                 // We are processing a local confirmation initiated on this system by our user to an external introduction.
190
191                 $uid = $_SESSION['uid'];
192
193                 if(! $uid) {
194                         notice(t("Permission denied.") . EOL );
195                         return;
196                 }       
197         
198                 $dfrn_id = ((x($_POST,'dfrn_id')) ? notags(trim($_POST['dfrn_id'])) : "");
199                 $intro_id = intval($_POST['intro_id']);
200
201                 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' AND `uid` = %d LIMIT 1",
202                                 dbesc($dfrn_id),
203                                 intval($uid)
204                                 );
205
206                 if((! $r) || (! count($r))) {
207                         notice( t('Node does not exist.') . EOL );
208                         return;
209                 }
210
211                 $contact_id = $r[0]['id'];
212                 $site_pubkey = $r[0]['site-pubkey'];
213                 $dfrn_confirm = $r[0]['confirm'];
214                 $aes_allow = $r[0]['aes_allow'];
215
216                 $res=openssl_pkey_new(array(
217                         'digest_alg' => 'whirlpool',
218                         'private_key_bits' => 4096,
219                         'encrypt_key' => false ));
220
221
222                 $private_key = '';
223
224                 openssl_pkey_export($res, $private_key);
225
226
227                 $pubkey = openssl_pkey_get_details($res);
228                 $public_key = $pubkey["key"];
229
230                 $r = q("UPDATE `contact` SET `issued-pubkey` = '%s', `prvkey` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
231                         dbesc($public_key),
232                         dbesc($private_key),
233                         intval($contact_id),
234                         intval($uid) 
235                 );
236
237
238                 $params = array();
239
240                 $src_aes_key = random_string();
241                 $result = "";
242
243                 openssl_private_encrypt($dfrn_id,$result,$a->user['prvkey']);
244
245                 $params['dfrn_id'] = $result;
246                 $params['public_key'] = $public_key;
247
248
249                 openssl_public_encrypt($_SESSION['my_url'], $params['source_url'], $site_pubkey);
250
251                 if($aes_allow && function_exists('openssl_encrypt')) {
252                         openssl_public_encrypt($src_aes_key, $params['aes_key'], $site_pubkey);
253                         $params['public_key'] = openssl_encrypt($public_key,'AES-256-CBC',$src_aes_key);
254                 }
255
256                 $res = post_url($dfrn_confirm,$params);
257
258 // uncomment the following two lines and comment the following xml/status lines
259 // to debug the remote confirmation section (when both confirmations 
260 // and responses originate on this system)
261
262 // echo $res;
263 // $status = 0;
264
265                 $xml = simplexml_load_string($res);
266                 $status = (int) $xml->status;
267                 switch($status) {
268                         case 0:
269                                 notice( t("Confirmation completed successfully") . EOL);
270                                 break;
271                         case 1:
272
273                                 // birthday paradox - generate new dfrn-id and fall through.
274
275                                 $new_dfrn_id = random_string();
276                                 $r = q("UPDATE contact SET `issued-id` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
277                                         dbesc($new_dfrn_id),
278                                         intval($contact_id),
279                                         intval($uid) 
280                                 );
281
282                         case 2:
283                                 notice( t("Temporary failure. Please wait and try again.") . EOL);
284                                 break;
285
286
287                         case 3:
288                                 notice( t("Introduction failed or was revoked. Cannot complete.") . EOL);
289                                 break;
290                 }
291
292                 if(($status == 0 || $status == 3) && ($intro_id)) {
293
294                         //delete the notification
295
296                         $r = q("DELETE FROM `intro` WHERE `id` = %d AND `uid` = %d LIMIT 1",
297                                 intval($intro_id),
298                                 intval($uid)
299                         );
300                         
301                 }
302                 if($status != 0) 
303                         return;
304                 
305
306                 require_once("Photo.php");
307
308                 $photo_failure = false;
309
310                 $r = q("SELECT `photo` FROM `contact` WHERE `id` = %d LIMIT 1",
311                         intval($contact_id));
312                 if(count($r)) {
313
314                         $filename = basename($r[0]['photo']);
315                         $img_str = fetch_url($r[0]['photo'],true);
316                         $img = new Photo($img_str);
317                         if($img) {
318
319                                 $img->scaleImageSquare(175);
320                                         
321                                 $hash = hash('md5',uniqid(mt_rand(),true));
322
323                                 $r = q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`,
324                                         `height`, `width`, `data`, `scale` )
325                                         VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 4 )",
326                                         intval($local_uid),
327                                         dbesc($hash),
328                                         datetime_convert(),
329                                         datetime_convert(),
330                                         dbesc(basename($r[0]['photo'])),
331                                         intval($img->getHeight()),
332                                         intval($img->getWidth()),
333                                         dbesc($img->imageString())
334                                 );
335                                 if($r === false)
336                                         $photo_failure = true;
337                                 $img->scaleImage(80);
338                                 $r =  q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`,
339                                         `height`, `width`, `data`, `scale` )
340                                          VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 5 )",
341                                          intval($local_uid),
342                                          dbesc($hash),
343                                          datetime_convert(),
344                                          datetime_convert(),
345                                          dbesc(basename($r[0]['photo'])),
346                                          intval($img->getHeight()),
347                                          intval($img->getWidth()),
348                                          dbesc($img->imageString())
349                                 );
350                                 if($r === false)
351                                         $photo_failure = true;
352
353                                 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
354                                 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
355                                         
356                         }
357                         else
358                                 $photo_failure = true;
359                 }
360                 else
361                         $photo_failure = true;
362
363                 if($photo_failure) {
364                         $photo = $a->get_baseurl() . '/images/default-profile.jpg';
365                         $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
366                 }
367
368                 $r = q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `blocked` = 0, `pending` = 0 WHERE `id` = %d LIMIT 1",
369                         dbesc($photo),
370                         dbesc($thumb),
371                         intval($contact_id)
372                 );
373                 if($r === false)
374                         notice( t("Unable to set contact photo info.") . EOL);
375
376                 goaway($a->get_baseurl() . '/contacts/' . intval($contact_id));
377                 return;  //NOTREACHED
378
379         }
380
381         return;
382 }