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 = hash('md5',uniqid(mt_rand(),true));
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 = file_get_contents('view/intro_complete_eml.tpl');
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'],
179 $res = mail($r[0]['email'], t("Introduction accepted at ") . $a->config['sitename'],
180 $email_tpl, 'From: ' . t('Administrator') . '@' . $_SERVER[SERVER_NAME] );
182 notice( t("Email notification failed.") . EOL );
185 xml_status(0); // Success
186 return; // NOTREACHED
189 xml_status(2); // Hopefully temporary problem that can be retried.
191 return; // NOTREACHED
193 ////////////////////// End of this scenario ///////////////////////////////////////////////
197 // We are processing a local confirmation initiated on this system by our user to an external introduction.
202 notice( t("Permission denied.") . EOL );
206 $dfrn_id = ((x($_POST,'dfrn_id')) ? notags(trim($_POST['dfrn_id'])) : "");
207 $intro_id = intval($_POST['intro_id']);
208 $duplex = intval($_POST['duplex']);
210 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' AND `uid` = %d LIMIT 1",
216 notice( t('Node does not exist.') . EOL );
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'];
226 $res = openssl_pkey_new(array(
227 'digest_alg' => 'whirlpool',
228 'private_key_bits' => 4096,
229 'encrypt_key' => false )
235 openssl_pkey_export($res, $private_key);
237 $pubkey = openssl_pkey_get_details($res);
238 $public_key = $pubkey["key"];
240 $r = q("UPDATE `contact` SET `issued-pubkey` = '%s', `prvkey` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
250 $src_aes_key = random_string();
253 openssl_private_encrypt($dfrn_id,$result,$a->user['prvkey']);
255 $params['dfrn_id'] = $result;
256 $params['public_key'] = $public_key;
259 openssl_public_encrypt($_SESSION['my_url'], $params['source_url'], $site_pubkey);
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);
266 $params['dfrn_version'] = '2.0';
268 $params['duplex'] = 1;
270 $res = post_url($dfrn_confirm,$params);
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)
279 $xml = simplexml_load_string($res);
280 $status = (int) $xml->status;
283 notice( t("Confirmation completed successfully") . EOL);
287 // birthday paradox - generate new dfrn-id and fall through.
289 $new_dfrn_id = random_string();
290 $r = q("UPDATE contact SET `issued-id` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
297 notice( t("Temporary failure. Please wait and try again.") . EOL);
302 notice( t("Introduction failed or was revoked. Cannot complete.") . EOL);
306 if(($status == 0 || $status == 3) && ($intro_id)) {
308 //delete the notification
310 $r = q("DELETE FROM `intro` WHERE `id` = %d AND `uid` = %d LIMIT 1",
320 require_once("Photo.php");
322 $photo_failure = false;
324 $r = q("SELECT `photo` FROM `contact` WHERE `id` = %d LIMIT 1",
325 intval($contact_id));
328 $filename = basename($r[0]['photo']);
329 $img_str = fetch_url($r[0]['photo'],true);
330 $img = new Photo($img_str);
333 $img->scaleImageSquare(175);
335 $hash = hash('md5',uniqid(mt_rand(),true));
337 $r = $img->store($uid, $contact_id, $hash, $filename, t('Contact Photos'), 4 );
340 $photo_failure = true;
341 $img->scaleImage(80);
343 $r = $img->store($uid, $contact_id, $hash, $filename, t('Contact Photos'), 5 );
346 $photo_failure = true;
348 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
349 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
352 $photo_failure = true;
355 $photo_failure = true;
358 $photo = $a->get_baseurl() . '/images/default-profile.jpg';
359 $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
362 $new_relation = DIRECTION_IN;
363 if(($relation == DIRECTION_OUT) || ($duplex))
364 $new_relation = DIRECTION_BOTH;
366 $r = q("UPDATE `contact` SET `photo` = '%s',
371 `avatar-date` = '%s',
374 `network` = 'dfrn' WHERE `id` = %d LIMIT 1
378 intval($new_relation),
379 dbesc(datetime_convert()),
380 dbesc(datetime_convert()),
381 dbesc(datetime_convert()),
385 notice( t('Unable to set contact photo.') . EOL);
387 goaway($a->get_baseurl() . '/contacts/' . intval($contact_id));