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'];
21 // Find our user's account
23 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1",
27 $message = t('No user record found for ') . '\'' . $node . '\'';
28 xml_status(3,$message); // 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 if(! strlen($decrypted_source_url)) {
42 $message = t('empty site URL was provided.');
43 xml_status(3,$message);
47 $ret = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
48 dbesc($decrypted_source_url),
53 // this is either a bogus confirmation (?) or we deleted the original introduction.
54 $message = t('Contact record was not found for you on our site.');
55 xml_status(3,$message);
59 $relation = $ret[0]['rel'];
61 // Decrypt all this stuff we just received
63 $foreign_pubkey = $ret[0]['site-pubkey'];
64 $dfrn_record = $ret[0]['id'];
66 $decrypted_dfrn_id = "";
67 openssl_public_decrypt($dfrn_id,$decrypted_dfrn_id,$foreign_pubkey);
69 if(strlen($aes_key)) {
70 $decrypted_aes_key = "";
71 openssl_private_decrypt($aes_key,$decrypted_aes_key,$my_prvkey);
72 $dfrn_pubkey = openssl_decrypt($public_key,'AES-256-CBC',$decrypted_aes_key);
75 $dfrn_pubkey = $public_key;
78 $r = q("SELECT * FROM `contact` WHERE `dfrn-id` = '%s' LIMIT 1",
79 dbesc($decrypted_dfrn_id),
83 $message = t('The ID provided is a duplicate on our system. Please try again.');
84 xml_status(1,$message); // Birthday paradox - duplicate dfrn-id
88 $r = q("UPDATE `contact` SET `dfrn-id` = '%s', `pubkey` = '%s' WHERE `id` = %d LIMIT 1",
89 dbesc($decrypted_dfrn_id),
94 $message = t('Unable to set your credentials on our system.');
95 xml_status(3,$message);
98 // We're good but now we have to scrape the profile photo and send notifications.
100 require_once("Photo.php");
102 $photo_failure = false;
104 $r = q("SELECT `photo` FROM `contact` WHERE `id` = %d LIMIT 1",
105 intval($dfrn_record));
108 $filename = basename($r[0]['photo']);
109 $img_str = fetch_url($r[0]['photo'],true);
110 $img = new Photo($img_str);
111 if($img->is_valid()) {
113 $img->scaleImageSquare(175);
115 $hash = photo_new_resource();
117 $r = $img->store($local_uid, $dfrn_record, $hash, $filename, t('Contact Photos') , 4);
120 $photo_failure = true;
122 $img->scaleImage(80);
123 $r = $img->store($local_uid, $dfrn_record, $hash, $filename, t('Contact Photos') , 5);
126 $photo_failure = true;
128 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
129 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
132 $photo_failure = true;
135 $photo_failure = true;
138 $photo = $a->get_baseurl() . '/images/default-profile.jpg';
139 $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
142 $new_relation = REL_FAN;
143 if(($relation == REL_VIP) || ($duplex))
144 $new_relation = REL_BUD;
146 $r = q("UPDATE `contact` SET
152 `avatar-date` = '%s',
156 `network` = 'dfrn' WHERE `id` = %d LIMIT 1
160 intval($new_relation),
161 dbesc(datetime_convert()),
162 dbesc(datetime_convert()),
163 dbesc(datetime_convert()),
167 if($r === false) { // should not happen unless schema is messed up
168 $message = t('Unable to update your contact profile on our system');
169 xml_status(3,$message);
172 // Otherwise everything seems to have worked and we are almost done. Yay!
173 // Send an email notification
175 $r = q("SELECT * FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
176 WHERE `contact`.`id` = %d LIMIT 1",
179 if((count($r)) && ($r[0]['notify-flags'] & NOTIFY_CONFIRM)) {
181 $tpl = (($new_relation == REL_BUD)
182 ? load_view_file('view/friend_complete_eml.tpl')
183 : load_view_file('view/intro_complete_eml.tpl'));
185 $email_tpl = replace_macros($tpl, array(
186 '$sitename' => $a->config['sitename'],
187 '$siteurl' => $a->get_baseurl(),
188 '$username' => $r[0]['username'],
189 '$email' => $r[0]['email'],
190 '$fn' => $r[0]['name'],
191 '$dfrn_url' => $r[0]['url'],
195 $res = mail($r[0]['email'], t("Connection accepted at ") . $a->config['sitename'],
196 $email_tpl, 'From: ' . t('Administrator') . '@' . $_SERVER[SERVER_NAME] );
198 notice( t("Email notification failed.") . EOL );
201 xml_status(0); // Success
202 return; // NOTREACHED
204 ////////////////////// End of this scenario ///////////////////////////////////////////////
208 // We are processing a local confirmation initiated on this system by our user to an external introduction.
213 notice( t('Permission denied.') . EOL );
217 $dfrn_id = ((x($_POST,'dfrn_id')) ? notags(trim($_POST['dfrn_id'])) : "");
218 $intro_id = intval($_POST['intro_id']);
219 $duplex = intval($_POST['duplex']);
221 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' AND `uid` = %d LIMIT 1",
227 notice( t('Node does not exist.') . EOL );
231 $contact_id = $r[0]['id'];
232 $relation = $r[0]['rel'];
233 $site_pubkey = $r[0]['site-pubkey'];
234 $dfrn_confirm = $r[0]['confirm'];
235 $aes_allow = $r[0]['aes_allow'];
237 $res = openssl_pkey_new(array(
238 'digest_alg' => 'whirlpool',
239 'private_key_bits' => 4096,
240 'encrypt_key' => false )
246 openssl_pkey_export($res, $private_key);
248 $pubkey = openssl_pkey_get_details($res);
249 $public_key = $pubkey["key"];
251 $r = q("UPDATE `contact` SET `prvkey` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
260 $src_aes_key = random_string();
263 openssl_private_encrypt($dfrn_id,$result,$a->user['prvkey']);
265 $params['dfrn_id'] = $result;
266 $params['public_key'] = $public_key;
269 $my_url = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
271 openssl_public_encrypt($my_url, $params['source_url'], $site_pubkey);
273 if($aes_allow && function_exists('openssl_encrypt')) {
274 openssl_public_encrypt($src_aes_key, $params['aes_key'], $site_pubkey);
275 $params['public_key'] = openssl_encrypt($public_key,'AES-256-CBC',$src_aes_key);
278 $params['dfrn_version'] = '2.0';
280 $params['duplex'] = 1;
282 $res = post_url($dfrn_confirm,$params);
284 // Try to be robust if the remote site is having difficulty and throwing up
285 // errors of some kind.
287 $leading_junk = substr($res,0,strpos($res,'<?xml'));
289 $res = substr($res,strpos($res,'<?xml'));
292 // No XML at all, this exchange is messed up really bad.
293 // We shouldn't proceed, because the xml parser might choke,
294 // and $status is going to be zero, which indicates success.
295 // We can hardly call this a success.
297 notice( t('Response from remote site was not understood.') . EOL);
301 if(strlen($leading_junk) && get_config('system','debugging')) {
303 // This might be more common. Mixed error text and some XML.
304 // If we're configured for debugging, show the text. Proceed in either case.
306 notice( t('Unexpected response from remote site: ') . EOL . $leading_junk . EOL );
309 $xml = simplexml_load_string($res);
310 $status = (int) $xml->status;
311 $message = unxmlify($xml->message);
314 notice( t("Confirmation completed successfully.") . EOL);
316 notice( t('Remote site reported: ') . $message . EOL);
319 // birthday paradox - generate new dfrn-id and fall through.
320 $new_dfrn_id = random_string();
321 $r = q("UPDATE contact SET `issued-id` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
328 notice( t("Temporary failure. Please wait and try again.") . EOL);
330 notice( t('Remote site reported: ') . $message . EOL);
335 notice( t("Introduction failed or was revoked.") . EOL);
337 notice( t('Remote site reported: ') . $message . EOL);
341 if(($status == 0) && ($intro_id)) {
343 //delete the notification
345 $r = q("DELETE FROM `intro` WHERE `id` = %d AND `uid` = %d LIMIT 1",
355 require_once("Photo.php");
357 $photo_failure = false;
359 $r = q("SELECT `photo` FROM `contact` WHERE `id` = %d LIMIT 1",
360 intval($contact_id));
363 $filename = basename($r[0]['photo']);
364 $img_str = fetch_url($r[0]['photo'],true);
365 $img = new Photo($img_str);
366 if($img->is_valid()) {
368 $img->scaleImageSquare(175);
370 $hash = photo_new_resource();
372 $r = $img->store($uid, $contact_id, $hash, $filename, t('Contact Photos'), 4 );
375 $photo_failure = true;
377 $img->scaleImage(80);
379 $r = $img->store($uid, $contact_id, $hash, $filename, t('Contact Photos'), 5 );
382 $photo_failure = true;
384 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
385 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
388 $photo_failure = true;
391 $photo_failure = true;
394 $photo = $a->get_baseurl() . '/images/default-profile.jpg';
395 $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
398 $new_relation = REL_VIP;
399 if(($relation == REL_FAN) || ($duplex))
400 $new_relation = REL_BUD;
402 $r = q("UPDATE `contact` SET `photo` = '%s',
407 `avatar-date` = '%s',
411 `network` = 'dfrn' WHERE `id` = %d LIMIT 1
415 intval($new_relation),
416 dbesc(datetime_convert()),
417 dbesc(datetime_convert()),
418 dbesc(datetime_convert()),
423 notice( t('Unable to set contact photo.') . EOL);
425 goaway($a->get_baseurl() . '/contacts/' . intval($contact_id));