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'];
18 $duplex = $_POST['duplex'];
19 $version_id = $_POST['dfrn_version'];
22 // Find our user's account
24 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1",
28 xml_status(3); // failure
32 $my_prvkey = $r[0]['prvkey'];
33 $local_uid = $r[0]['uid'];
37 $decrypted_source_url = "";
38 openssl_private_decrypt($source_url,$decrypted_source_url,$my_prvkey);
41 $ret = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
42 dbesc($decrypted_source_url),
46 // this is either a bogus confirmation or we deleted the original introduction.
51 $relation = $ret[0]['rel'];
53 // Decrypt all this stuff we just received
55 $foreign_pubkey = $ret[0]['site-pubkey'];
56 $dfrn_record = $ret[0]['id'];
58 $decrypted_dfrn_id = "";
59 openssl_public_decrypt($dfrn_id,$decrypted_dfrn_id,$foreign_pubkey);
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);
67 $dfrn_pubkey = $public_key;
70 $r = q("SELECT * FROM `contact` WHERE `dfrn-id` = '%s' LIMIT 1",
71 dbesc($decrypted_dfrn_id),
75 xml_status(1); // Birthday paradox - duplicate dfrn-id
79 $r = q("UPDATE `contact` SET `dfrn-id` = '%s', `pubkey` = '%s' WHERE `id` = %d LIMIT 1",
80 dbesc($decrypted_dfrn_id),
86 // We're good but now we have to scrape the profile photo and send notifications.
88 require_once("Photo.php");
90 $photo_failure = false;
92 $r = q("SELECT `photo` FROM `contact` WHERE `id` = %d LIMIT 1",
93 intval($dfrn_record));
96 $filename = basename($r[0]['photo']);
97 $img_str = fetch_url($r[0]['photo'],true);
98 $img = new Photo($img_str);
101 $img->scaleImageSquare(175);
103 $hash = photo_new_resource();
105 $r = $img->store($local_uid, $dfrn_record, $hash, $filename, t('Contact Photos') , 4);
108 $photo_failure = true;
110 $img->scaleImage(80);
111 $r = $img->store($local_uid, $dfrn_record, $hash, $filename, t('Contact Photos') , 5);
114 $photo_failure = true;
116 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
117 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
120 $photo_failure = true;
123 $photo_failure = true;
126 $photo = $a->get_baseurl() . '/images/default-profile.jpg';
127 $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
130 $new_relation = DIRECTION_OUT;
131 if(($relation == DIRECTION_IN) || ($duplex))
132 $new_relation = DIRECTION_BOTH;
134 $r = q("UPDATE `contact` SET
140 `avatar-date` = '%s',
144 `network` = 'dfrn' WHERE `id` = %d LIMIT 1
148 intval($new_relation),
149 dbesc(datetime_convert()),
150 dbesc(datetime_convert()),
151 dbesc(datetime_convert()),
156 notice( t("Unable to set contact photo info.") . EOL);
158 // Otherwise everything seems to have worked and we are almost done. Yay!
159 // Send an email notification
161 $r = q("SELECT * FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
162 WHERE `contact`.`id` = %d LIMIT 1",
165 if((count($r)) && ($r[0]['notify-flags'] & NOTIFY_CONFIRM)) {
167 $tpl = (($new_relation == $REL_BUD)
168 ? file_get_contents('view/friend_complete_eml.tpl');
169 : file_get_contents('view/intro_complete_eml.tpl'));
171 $email_tpl = replace_macros($tpl, array(
172 '$sitename' => $a->config['sitename'],
173 '$siteurl' => $a->get_baseurl(),
174 '$username' => $r[0]['username'],
175 '$email' => $r[0]['email'],
176 '$fn' => $r[0]['name'],
177 '$dfrn_url' => $r[0]['url'],
181 $res = mail($r[0]['email'], t("Connection accepted at ") . $a->config['sitename'],
182 $email_tpl, 'From: ' . t('Administrator') . '@' . $_SERVER[SERVER_NAME] );
184 notice( t("Email notification failed.") . EOL );
187 xml_status(0); // Success
188 return; // NOTREACHED
191 xml_status(2); // Hopefully temporary problem that can be retried.
193 return; // NOTREACHED
195 ////////////////////// End of this scenario ///////////////////////////////////////////////
199 // We are processing a local confirmation initiated on this system by our user to an external introduction.
204 notice( t("Permission denied.") . EOL );
208 $dfrn_id = ((x($_POST,'dfrn_id')) ? notags(trim($_POST['dfrn_id'])) : "");
209 $intro_id = intval($_POST['intro_id']);
210 $duplex = intval($_POST['duplex']);
212 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' AND `uid` = %d LIMIT 1",
218 notice( t('Node does not exist.') . EOL );
222 $contact_id = $r[0]['id'];
223 $relation = $r[0]['rel'];
224 $site_pubkey = $r[0]['site-pubkey'];
225 $dfrn_confirm = $r[0]['confirm'];
226 $aes_allow = $r[0]['aes_allow'];
228 $res = openssl_pkey_new(array(
229 'digest_alg' => 'whirlpool',
230 'private_key_bits' => 4096,
231 'encrypt_key' => false )
237 openssl_pkey_export($res, $private_key);
239 $pubkey = openssl_pkey_get_details($res);
240 $public_key = $pubkey["key"];
242 $r = q("UPDATE `contact` SET `issued-pubkey` = '%s', `prvkey` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
252 $src_aes_key = random_string();
255 openssl_private_encrypt($dfrn_id,$result,$a->user['prvkey']);
257 $params['dfrn_id'] = $result;
258 $params['public_key'] = $public_key;
261 openssl_public_encrypt($_SESSION['my_url'], $params['source_url'], $site_pubkey);
263 if($aes_allow && function_exists('openssl_encrypt')) {
264 openssl_public_encrypt($src_aes_key, $params['aes_key'], $site_pubkey);
265 $params['public_key'] = openssl_encrypt($public_key,'AES-256-CBC',$src_aes_key);
268 $params['dfrn_version'] = '2.0';
270 $params['duplex'] = 1;
272 $res = post_url($dfrn_confirm,$params);
274 // uncomment the following two lines and comment the following xml/status lines
275 // to debug the remote confirmation section (when both confirmations
276 // and responses originate on this system)
281 $xml = simplexml_load_string($res);
282 $status = (int) $xml->status;
285 notice( t("Confirmation completed successfully") . EOL);
289 // birthday paradox - generate new dfrn-id and fall through.
291 $new_dfrn_id = random_string();
292 $r = q("UPDATE contact SET `issued-id` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
299 notice( t("Temporary failure. Please wait and try again.") . EOL);
304 notice( t("Introduction failed or was revoked. Cannot complete.") . EOL);
308 if(($status == 0 || $status == 3) && ($intro_id)) {
310 //delete the notification
312 $r = q("DELETE FROM `intro` WHERE `id` = %d AND `uid` = %d LIMIT 1",
322 require_once("Photo.php");
324 $photo_failure = false;
326 $r = q("SELECT `photo` FROM `contact` WHERE `id` = %d LIMIT 1",
327 intval($contact_id));
330 $filename = basename($r[0]['photo']);
331 $img_str = fetch_url($r[0]['photo'],true);
332 $img = new Photo($img_str);
335 $img->scaleImageSquare(175);
337 $hash = photo_new_resource();
339 $r = $img->store($uid, $contact_id, $hash, $filename, t('Contact Photos'), 4 );
342 $photo_failure = true;
344 $img->scaleImage(80);
346 $r = $img->store($uid, $contact_id, $hash, $filename, t('Contact Photos'), 5 );
349 $photo_failure = true;
351 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
352 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
355 $photo_failure = true;
358 $photo_failure = true;
361 $photo = $a->get_baseurl() . '/images/default-profile.jpg';
362 $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
365 $new_relation = DIRECTION_IN;
366 if(($relation == DIRECTION_OUT) || ($duplex))
367 $new_relation = DIRECTION_BOTH;
369 $r = q("UPDATE `contact` SET `photo` = '%s',
374 `avatar-date` = '%s',
378 `network` = 'dfrn' WHERE `id` = %d LIMIT 1
382 intval($new_relation),
383 dbesc(datetime_convert()),
384 dbesc(datetime_convert()),
385 dbesc(datetime_convert()),
390 notice( t('Unable to set contact photo.') . EOL);
392 goaway($a->get_baseurl() . '/contacts/' . intval($contact_id));