function initKeys()
{
- $d = $this->storedir;
+ // return false when fail
+ $dir = $this->getKeyDirectory();
+
+ if(!$dir) {
+ return false; // only fail case?
+ }
if(
- file_exists("{$d}/pub.key") ||
- file_exists("{$d}/pri.key")
+ file_exists("{$dir}/pub.key") ||
+ file_exists("{$dir}/pri.key")
){
- return;
+ return true;
}
$ssl = openssl_pkey_new(array(
$pub_key = openssl_pkey_get_details($ssl);
$pub_key = $pub_key["key"];
- file_put_contents("{$d}/pub.key",$pub_key);
- file_put_contents("{$d}/pri.key",$pri_key);
+ file_put_contents("{$dir}/pub.key",$pub_key);
+ file_put_contents("{$dir}/pri.key",$pri_key);
+
+ return true;
}
- function getSetting($m,$n)
+ function lookup($m,$n)
{
$s = DB_DataObject::factory('core_setting');
$s->setFrom(array(
'module' => $m,
'name' => $n
));
+
if($s->find(true)) {
return $s;
}
+
return false;
}
function beforeInsert($q, $roo)
{
exit;
-
- return;
}
- function initSetting($a, $dir)
+ function getKeyDirectory()
{
- if(empty($a) || empty($dir)) {
- return;
+ $client_dir = HTML_FlexyFramework::get()->Pman['storedir'];
+
+ $key_dir = $client_dir.'/keys';
+
+ if(file_exists($key_dir)) {
+ return $key_dir;
}
- $c = $this->getSetting($a['module'], $a['name']);
- if($c) {
- return;
+ if(!is_writable($key_dir)) {
+ return false;
}
- //$ff->pman['storedir']/key generic for all projects?
+ mkdir($key_dir, 0755, true);
- $this->setStoreDir($dir);
+ return $key_dir;
+ }
+
+ // FIXME - this needs to go in beforeInsert/beforeUpdate
+ // should not be sending this the values..
+ function initSetting($a)
+ {
+ if(empty($a)) {
+ return;
+ }
$this->initKeys();
- $val = $a['val'];
- if(!isset($a['is_encrypt']) || $a['is_encrypt'] == 1) {
- $val = $this->encrypt($val);
- }
+ $c = $this->lookup($a['module'], $a['name']);
- $s = DB_DataObject::factory('core_setting');
- $s->setFrom(array(
- 'module' => $a['module'],
- 'name' => $a['name'],
- 'description' => $a['description'],
- 'val' =>$val,
- 'is_encrypt' => isset($a['is_encrypt']) ? $a['is_encrypt'] : 1
+ $o = $c ? clone($c) : false;
+
+ $c = $c ? $c : DB_DataObject::factory('core_setting');
+
+ $c->setFrom(array(
+ 'module' => $a['module'],
+ 'name' => $a['name'],
+ 'description' => $a['description'],
+ 'val' => (!isset($a['is_encrypt']) || $a['is_encrypt'] == 1) ?
+ $this->encrypt($a['val']) : $a['val'],
+ 'is_encrypt' => isset($a['is_encrypt']) ? $a['is_encrypt'] : 1,
+ 'is_valid' => 1
));
- $s->insert();
+ $o ? $c->update($o) : $c->insert();
}
+ //one key for encrypting all the settings
function encrypt($v)
{
- $pub_key = file_get_contents("{$this->storedir}/pub.key");
+ $key_dir = "{$this->getKeyDirectory()}/pub.key";
+
+ if(!file_exists($key_dir)) {
+ print_r("Cannot find {$key_dir}");
+ exit;
+ }
+
+ $pub_key = file_get_contents($key_dir);
if(!$pub_key) {
return;
}
- openssl_public_encrypt($v, $cipher, $pub_key);
- return $cipher;
+ openssl_public_encrypt($v, $ciphertext, $pub_key);
+ return $ciphertext;
}
- function setStoreDir($dir)
+ function getDecryptVal()
{
- if(!file_exists($dir)) {
- $oldumask = umask(0);
- mkdir($dir, 0775, true);
- umask($oldumask);
+ $dir = $this->getKeyDirectory();
+
+ if(!$dir) {
+ return false;
+ }
+
+ if(empty($this->val)) {
+ return false;
+ }
+
+ if(empty($this->is_encrypt)) {
+ return $this->val;
}
- $this->storedir = $dir;
+
+ $key_dir = "{$dir}/pri.key";
+
+ if(!file_exists($key_dir)) {
+ return false;
+ }
+
+ $pri_key = file_get_contents($key_dir);
+
+ if(!$pri_key) {
+ return false;
+ }
+
+ openssl_private_decrypt($this->val, $plaintext, $pri_key);
+
+ return $plaintext;
}
+
}