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