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