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'];
36 if(! strstr($my_prvkey,'BEGIN RSA PRIVATE KEY')) {
37 $message = t('Our site encryption key is apparently messed up.');
38 xml_status(3,$message);
43 $decrypted_source_url = "";
44 openssl_private_decrypt($source_url,$decrypted_source_url,$my_prvkey);
47 if(! strlen($decrypted_source_url)) {
48 $message = t('Empty site URL was provided or URL could not be decrypted by us.');
49 xml_status(3,$message);
53 $ret = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
54 dbesc($decrypted_source_url),
59 // this is either a bogus confirmation (?) or we deleted the original introduction.
60 $message = t('Contact record was not found for you on our site.');
61 xml_status(3,$message);
65 $relation = $ret[0]['rel'];
67 // Decrypt all this stuff we just received
69 $foreign_pubkey = $ret[0]['site-pubkey'];
70 $dfrn_record = $ret[0]['id'];
72 $decrypted_dfrn_id = "";
73 openssl_public_decrypt($dfrn_id,$decrypted_dfrn_id,$foreign_pubkey);
75 if(strlen($aes_key)) {
76 $decrypted_aes_key = "";
77 openssl_private_decrypt($aes_key,$decrypted_aes_key,$my_prvkey);
78 $dfrn_pubkey = openssl_decrypt($public_key,'AES-256-CBC',$decrypted_aes_key);
81 $dfrn_pubkey = $public_key;
84 $r = q("SELECT * FROM `contact` WHERE `dfrn-id` = '%s' LIMIT 1",
85 dbesc($decrypted_dfrn_id),
89 $message = t('The ID provided by your system is a duplicate on our system. It should work if you try again.');
90 xml_status(1,$message); // Birthday paradox - duplicate dfrn-id
94 $r = q("UPDATE `contact` SET `dfrn-id` = '%s', `pubkey` = '%s' WHERE `id` = %d LIMIT 1",
95 dbesc($decrypted_dfrn_id),
100 $message = t('Unable to set your contact credentials on our system.');
101 xml_status(3,$message);
104 // We're good but now we have to scrape the profile photo and send notifications.
106 require_once("Photo.php");
108 $photo_failure = false;
110 $r = q("SELECT `photo` FROM `contact` WHERE `id` = %d LIMIT 1",
111 intval($dfrn_record));
114 $filename = basename($r[0]['photo']);
115 $img_str = fetch_url($r[0]['photo'],true);
116 $img = new Photo($img_str);
117 if($img->is_valid()) {
119 $img->scaleImageSquare(175);
121 $hash = photo_new_resource();
123 $r = $img->store($local_uid, $dfrn_record, $hash, $filename, t('Contact Photos') , 4);
126 $photo_failure = true;
128 $img->scaleImage(80);
129 $r = $img->store($local_uid, $dfrn_record, $hash, $filename, t('Contact Photos') , 5);
132 $photo_failure = true;
134 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
135 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
138 $photo_failure = true;
141 $photo_failure = true;
144 $photo = $a->get_baseurl() . '/images/default-profile.jpg';
145 $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
148 $new_relation = REL_FAN;
149 if(($relation == REL_VIP) || ($duplex))
150 $new_relation = REL_BUD;
152 $r = q("UPDATE `contact` SET
158 `avatar-date` = '%s',
162 `network` = 'dfrn' WHERE `id` = %d LIMIT 1
166 intval($new_relation),
167 dbesc(datetime_convert()),
168 dbesc(datetime_convert()),
169 dbesc(datetime_convert()),
173 if($r === false) { // should not happen unless schema is messed up
174 $message = t('Unable to update your contact profile details on our system');
175 xml_status(3,$message);
178 // Otherwise everything seems to have worked and we are almost done. Yay!
179 // Send an email notification
181 $r = q("SELECT * FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
182 WHERE `contact`.`id` = %d LIMIT 1",
185 if((count($r)) && ($r[0]['notify-flags'] & NOTIFY_CONFIRM)) {
187 $tpl = (($new_relation == REL_BUD)
188 ? load_view_file('view/friend_complete_eml.tpl')
189 : load_view_file('view/intro_complete_eml.tpl'));
191 $email_tpl = replace_macros($tpl, array(
192 '$sitename' => $a->config['sitename'],
193 '$siteurl' => $a->get_baseurl(),
194 '$username' => $r[0]['username'],
195 '$email' => $r[0]['email'],
196 '$fn' => $r[0]['name'],
197 '$dfrn_url' => $r[0]['url'],
201 $res = mail($r[0]['email'], t("Connection accepted at ") . $a->config['sitename'],
202 $email_tpl, 'From: ' . t('Administrator') . '@' . $_SERVER[SERVER_NAME] );
204 notice( t("Email notification failed.") . EOL );
207 xml_status(0); // Success
208 return; // NOTREACHED
210 ////////////////////// End of this scenario ///////////////////////////////////////////////
214 // We are processing a local confirmation initiated on this system by our user to an external introduction.
219 notice( t('Permission denied.') . EOL );
223 $dfrn_id = ((x($_POST,'dfrn_id')) ? notags(trim($_POST['dfrn_id'])) : "");
224 $intro_id = intval($_POST['intro_id']);
225 $duplex = intval($_POST['duplex']);
227 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' AND `uid` = %d LIMIT 1",
233 notice( t('Node does not exist.') . EOL );
237 $contact_id = $r[0]['id'];
238 $relation = $r[0]['rel'];
239 $site_pubkey = $r[0]['site-pubkey'];
240 $dfrn_confirm = $r[0]['confirm'];
241 $aes_allow = $r[0]['aes_allow'];
243 $res = openssl_pkey_new(array(
244 'digest_alg' => 'whirlpool',
245 'private_key_bits' => 4096,
246 'encrypt_key' => false )
252 openssl_pkey_export($res, $private_key);
254 $pubkey = openssl_pkey_get_details($res);
255 $public_key = $pubkey["key"];
257 $r = q("UPDATE `contact` SET `prvkey` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
266 $src_aes_key = random_string();
269 openssl_private_encrypt($dfrn_id,$result,$a->user['prvkey']);
271 $params['dfrn_id'] = $result;
272 $params['public_key'] = $public_key;
275 $my_url = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
277 openssl_public_encrypt($my_url, $params['source_url'], $site_pubkey);
279 if($aes_allow && function_exists('openssl_encrypt')) {
280 openssl_public_encrypt($src_aes_key, $params['aes_key'], $site_pubkey);
281 $params['public_key'] = openssl_encrypt($public_key,'AES-256-CBC',$src_aes_key);
284 $params['dfrn_version'] = '2.0';
286 $params['duplex'] = 1;
288 $res = post_url($dfrn_confirm,$params);
290 // Try to be robust if the remote site is having difficulty and throwing up
291 // errors of some kind.
293 $leading_junk = substr($res,0,strpos($res,'<?xml'));
295 $res = substr($res,strpos($res,'<?xml'));
298 // No XML at all, this exchange is messed up really bad.
299 // We shouldn't proceed, because the xml parser might choke,
300 // and $status is going to be zero, which indicates success.
301 // We can hardly call this a success.
303 notice( t('Response from remote site was not understood.') . EOL);
307 if(strlen($leading_junk) && get_config('system','debugging')) {
309 // This might be more common. Mixed error text and some XML.
310 // If we're configured for debugging, show the text. Proceed in either case.
312 notice( t('Unexpected response from remote site: ') . EOL . $leading_junk . EOL );
315 $xml = simplexml_load_string($res);
316 $status = (int) $xml->status;
317 $message = unxmlify($xml->message);
320 notice( t("Confirmation completed successfully.") . EOL);
322 notice( t('Remote site reported: ') . $message . EOL);
325 // birthday paradox - generate new dfrn-id and fall through.
326 $new_dfrn_id = random_string();
327 $r = q("UPDATE contact SET `issued-id` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
334 notice( t("Temporary failure. Please wait and try again.") . EOL);
336 notice( t('Remote site reported: ') . $message . EOL);
341 notice( t("Introduction failed or was revoked.") . EOL);
343 notice( t('Remote site reported: ') . $message . EOL);
347 if(($status == 0) && ($intro_id)) {
349 //delete the notification
351 $r = q("DELETE FROM `intro` WHERE `id` = %d AND `uid` = %d LIMIT 1",
361 require_once("Photo.php");
363 $photo_failure = false;
365 $r = q("SELECT `photo` FROM `contact` WHERE `id` = %d LIMIT 1",
366 intval($contact_id));
369 $filename = basename($r[0]['photo']);
370 $img_str = fetch_url($r[0]['photo'],true);
371 $img = new Photo($img_str);
372 if($img->is_valid()) {
374 $img->scaleImageSquare(175);
376 $hash = photo_new_resource();
378 $r = $img->store($uid, $contact_id, $hash, $filename, t('Contact Photos'), 4 );
381 $photo_failure = true;
383 $img->scaleImage(80);
385 $r = $img->store($uid, $contact_id, $hash, $filename, t('Contact Photos'), 5 );
388 $photo_failure = true;
390 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
391 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
394 $photo_failure = true;
397 $photo_failure = true;
400 $photo = $a->get_baseurl() . '/images/default-profile.jpg';
401 $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
404 $new_relation = REL_VIP;
405 if(($relation == REL_FAN) || ($duplex))
406 $new_relation = REL_BUD;
408 $r = q("UPDATE `contact` SET `photo` = '%s',
413 `avatar-date` = '%s',
417 `network` = 'dfrn' WHERE `id` = %d LIMIT 1
421 intval($new_relation),
422 dbesc(datetime_convert()),
423 dbesc(datetime_convert()),
424 dbesc(datetime_convert()),
429 notice( t('Unable to set contact photo.') . EOL);
431 goaway($a->get_baseurl() . '/contacts/' . intval($contact_id));