- $r = q("UPDATE `contact` SET `dfrn-id` = '%s', `pubkey` = '%s' WHERE `id` = %d LIMIT 1",
- dbesc($decrypted_dfrn_id),
- dbesc($dfrn_pubkey),
- intval($dfrn_record));
- if($r) {
-
- // We're good but now we have to scrape the profile photo and send notifications.
-
- require_once("Photo.php");
-
- $photo_failure = false;
-
- $r = q("SELECT `photo` FROM `contact` WHERE `id` = %d LIMIT 1",
- intval($dfrn_record));
- if(count($r)) {
-
- $filename = basename($r[0]['photo']);
- $img_str = fetch_url($r[0]['photo'],true);
- $img = new Photo($img_str);
- if($img) {
-
- $img->scaleImageSquare(175);
-
- $hash = hash('md5',uniqid(mt_rand(),true));
-
- $r = q("INSERT INTO `photo` ( `uid`, `contact-id`, `resource-id`, `created`, `edited`, `filename`,
- `height`, `width`, `data`, `scale` )
- VALUES ( %d, %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 4 )",
- intval($local_uid),
- intval($dfrn_record),
- dbesc($hash),
- datetime_convert(),
- datetime_convert(),
- dbesc(basename($r[0]['photo'])),
- intval($img->getHeight()),
- intval($img->getWidth()),
- dbesc($img->imageString())
- );
- if($r === false)
- $photo_failure = true;
-
- $img->scaleImage(80);
- $r = q("INSERT INTO `photo` ( `uid`, `contact-id`, `resource-id`, `created`, `edited`, `filename`,
- `height`, `width`, `data`, `scale` )
- VALUES ( %d, %d, '%s', '%s', '%s', '%s', %d, %d, '%s', 5 )",
- intval($local_uid),
- intval($dfrn_record),
- dbesc($hash),
- datetime_convert(),
- datetime_convert(),
- dbesc(basename($r[0]['photo'])),
- intval($img->getHeight()),
- intval($img->getWidth()),
- dbesc($img->imageString())
- );
- if($r === false)
- $photo_failure = true;
-
- $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
- $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
-
- }
- else
- $photo_failure = true;
+ $r = q("UPDATE `contact` SET `prvkey` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
+ dbesc($private_key),
+ intval($contact_id),
+ intval($uid)
+ );
+
+ $params = array();
+
+ /**
+ *
+ * Per the DFRN protocol, we will verify both ends by encrypting the dfrn_id with our
+ * site private key (person on the other end can decrypt it with our site public key).
+ * Then encrypt our profile URL with the other person's site public key. They can decrypt
+ * it with their site private key. If the decryption on the other end fails for either
+ * item, it indicates tampering or key failure on at least one site and we will not be
+ * able to provide a secure communication pathway.
+ *
+ * If other site is willing to accept full encryption, (aes_allow is 1 AND we have php5.3
+ * or later) then we encrypt the personal public key we send them using AES-256-CBC and a
+ * random key which is encrypted with their site public key.
+ *
+ */
+
+ $src_aes_key = random_string();
+
+ $result = '';
+ openssl_private_encrypt($dfrn_id,$result,$user[0]['prvkey']);
+
+ $params['dfrn_id'] = bin2hex($result);
+ $params['public_key'] = $public_key;
+
+
+ $my_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname'];
+
+ openssl_public_encrypt($my_url, $params['source_url'], $site_pubkey);
+ $params['source_url'] = bin2hex($params['source_url']);
+
+ if($aes_allow && function_exists('openssl_encrypt')) {
+ openssl_public_encrypt($src_aes_key, $params['aes_key'], $site_pubkey);
+ $params['aes_key'] = bin2hex($params['aes_key']);
+ $params['public_key'] = bin2hex(openssl_encrypt($public_key,'AES-256-CBC',$src_aes_key));