5 function dfrn_confirm_post(&$a) {
10 if(x($_POST,'source_url')) {
12 // We are processing an external confirmation to an introduction created by our user.
14 $public_key = $_POST['public_key'];
15 $dfrn_id = $_POST['dfrn_id'];
16 $source_url = $_POST['source_url'];
17 $aes_key = $_POST['aes_key'];
20 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
23 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1",
27 xml_status(3); // failure
30 $my_prvkey = $r[0]['prvkey'];
31 $local_uid = $r[0]['uid'];
33 $decrypted_source_url = "";
35 openssl_private_decrypt($source_url,$decrypted_source_url,$my_prvkey);
38 $ret = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
39 dbesc($decrypted_source_url),
43 // this is either a bogus confirmation or we deleted the original introduction.
47 // Decrypt all this stuff we just received
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);
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);
60 $dfrn_pubkey = $public_key;
63 $r = q("SELECT * FROM `contact` WHERE `dfrn-id` = '%s' LIMIT 1",
64 dbesc($decrypted_dfrn_id),
67 xml_status(1); // Birthday paradox - duplicate dfrn-id
69 $r = q("UPDATE `contact` SET `dfrn-id` = '%s', `pubkey` = '%s' WHERE `id` = %d LIMIT 1",
70 dbesc($decrypted_dfrn_id),
72 intval($dfrn_record));
75 // We're good but now we have to scrape the profile photo and send notifications.
77 require_once("Photo.php");
79 $photo_failure = false;
81 $r = q("SELECT `photo` FROM `contact` WHERE `id` = %d LIMIT 1",
82 intval($dfrn_record));
85 $filename = basename($r[0]['photo']);
86 $img_str = fetch_url($r[0]['photo'],true);
87 $img = new Photo($img_str);
90 $img->scaleImageSquare(175);
92 $hash = hash('md5',uniqid(mt_rand(),true));
94 $r = q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`,
95 `height`, `width`, `data`, `scale` )
96 VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 4 )",
101 dbesc(basename($r[0]['photo'])),
102 intval($img->getHeight()),
103 intval($img->getWidth()),
104 dbesc($img->imageString())
107 $photo_failure = true;
108 $img->scaleImage(80);
109 $r = q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`,
110 `height`, `width`, `data`, `scale` )
111 VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 5 )",
116 dbesc(basename($r[0]['photo'])),
117 intval($img->getHeight()),
118 intval($img->getWidth()),
119 dbesc($img->imageString())
122 $photo_failure = true;
124 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
125 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
129 $photo_failure = true;
132 $photo_failure = true;
135 $photo = $a->get_baseurl() . '/images/default-profile.jpg';
136 $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
139 $r = q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `blocked` = 0, `pending` = 0 WHERE `id` = %d LIMIT 1",
145 $_SESSION['sysmsg'] .= "Unable to set contact photo info." . EOL;
147 // Otherwise everything seems to have worked and we are almost done. Yay!
148 // Send an email notification
150 $r = q("SELECT * FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
151 WHERE `contact`.`id` = %d LIMIT 1",
152 intval($dfrn_record));
154 $tpl = file_get_contents('view/intro_complete_eml.tpl');
156 $email_tpl = replace_macros($tpl, array(
157 '$sitename' => $a->config['sitename'],
158 '$siteurl' => $a->get_baseurl(),
159 '$username' => $r[0]['username'],
160 '$email' => $r[0]['email'],
161 '$fn' => $r[0]['name'],
162 '$dfrn_url' => $r[0]['url'],
163 '$uid' => $newuid ));
166 $res = mail($r[0]['email'],"Introduction accepted at {$a->config['sitename']}",
167 $email_tpl,"From: Administrator@{$_SERVER[SERVER_NAME]}");
169 notice( "Email notification failed." . EOL );
171 xml_status(0); // Success
173 return; // NOTREACHED
177 xml_status(2); // Hopefully temporary problem that can be retried.
179 return; // NOTREACHED
181 ////////////////////// End of this scenario ///////////////////////////////////////////////
185 // We are processing a local confirmation initiated on this system by our user to an external introduction.
187 $uid = $_SESSION['uid'];
190 notice("Permission denied." . EOL );
194 $dfrn_id = ((x($_POST,'dfrn_id')) ? notags(trim($_POST['dfrn_id'])) : "");
195 $intro_id = intval($_POST['intro_id']);
197 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' AND `uid` = %d LIMIT 1",
202 if((! $r) || (! count($r))) {
203 $_SESSION['sysmsg'] = 'Node does not exist.' . EOL ;
207 $contact_id = $r[0]['id'];
208 $site_pubkey = $r[0]['site-pubkey'];
209 $dfrn_confirm = $r[0]['confirm'];
210 $aes_allow = $r[0]['aes_allow'];
212 $res=openssl_pkey_new(array(
213 'digest_alg' => 'whirlpool',
214 'private_key_bits' => 4096,
215 'encrypt_key' => false ));
220 openssl_pkey_export($res, $private_key);
223 $pubkey = openssl_pkey_get_details($res);
224 $public_key = $pubkey["key"];
226 $r = q("UPDATE `contact` SET `issued-pubkey` = '%s', `prvkey` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
236 $src_aes_key = random_string();
239 openssl_private_encrypt($dfrn_id,$result,$a->user['prvkey']);
241 $params['dfrn_id'] = $result;
242 $params['public_key'] = $public_key;
245 openssl_public_encrypt($_SESSION['my_url'], $params['source_url'], $site_pubkey);
247 if($aes_allow && function_exists('openssl_encrypt')) {
248 openssl_public_encrypt($src_aes_key, $params['aes_key'], $site_pubkey);
249 $params['public_key'] = openssl_encrypt($public_key,'AES-256-CBC',$src_aes_key);
252 $res = post_url($dfrn_confirm,$params);
254 // uncomment the following two lines and comment the following xml/status lines
255 // to debug the remote confirmation section (when both confirmations
256 // and responses originate on this system)
261 $xml = simplexml_load_string($res);
262 $status = (int) $xml->status;
265 $_SESSION['sysmsg'] .= "Confirmation completed successfully" . EOL;
269 // birthday paradox - generate new dfrn-id and fall through.
271 $new_dfrn_id = random_string();
272 $r = q("UPDATE contact SET `issued-id` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
279 $_SESSION['sysmsg'] .= "Temporary failure. Please wait and try again." . EOL;
284 $_SESSION['sysmsg'] .= "Introduction failed or was revoked. Cannot complete." . EOL;
288 if(($status == 0 || $status == 3) && ($intro_id)) {
290 //delete the notification
292 $r = q("DELETE FROM `intro` WHERE `id` = %d AND `uid` = %d LIMIT 1",
302 require_once("Photo.php");
304 $photo_failure = false;
306 $r = q("SELECT `photo` FROM `contact` WHERE `id` = %d LIMIT 1",
307 intval($contact_id));
310 $filename = basename($r[0]['photo']);
311 $img_str = fetch_url($r[0]['photo'],true);
312 $img = new Photo($img_str);
315 $img->scaleImageSquare(175);
317 $hash = hash('md5',uniqid(mt_rand(),true));
319 $r = q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`,
320 `height`, `width`, `data`, `scale` )
321 VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 4 )",
326 dbesc(basename($r[0]['photo'])),
327 intval($img->getHeight()),
328 intval($img->getWidth()),
329 dbesc($img->imageString())
332 $photo_failure = true;
333 $img->scaleImage(80);
334 $r = q("INSERT INTO `photo` ( `uid`, `resource-id`, `created`, `edited`, `filename`,
335 `height`, `width`, `data`, `scale` )
336 VALUES ( %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 5 )",
341 dbesc(basename($r[0]['photo'])),
342 intval($img->getHeight()),
343 intval($img->getWidth()),
344 dbesc($img->imageString())
347 $photo_failure = true;
349 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
350 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
354 $photo_failure = true;
357 $photo_failure = true;
360 $photo = $a->get_baseurl() . '/images/default-profile.jpg';
361 $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
364 $r = q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `blocked` = 0, `pending` = 0 WHERE `id` = %d LIMIT 1",
370 $_SESSION['sysmsg'] .= "Unable to set contact photo info." . EOL;