{
public $__table = 'core_setting';
- function initKeys($dir)
+ function initKeys()
{
- //reject when keys exist
+ // return false when fail
+ $dir = $this->getKeyDirectory();
+
+ if(!$dir) {
+ return false; // only fail case?
+ }
+
if(
- !file_exists($dir) ||
file_exists("{$dir}/pub.key") ||
file_exists("{$dir}/pri.key")
){
- return;
+ return true;
}
$ssl = openssl_pkey_new(array(
"digest_alg" => "sha512",
- "private_key_bits" => 1024, //returns cipher in 128 characters
+ "private_key_bits" => 1024, //returns cipher in 128 char
"private_key_type" => OPENSSL_KEYTYPE_RSA
));
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' => $q['module'],
- 'name' => $q['name']
+ 'module' => $m,
+ 'name' => $n
));
+
if($s->find(true)) {
return $s;
}
+
return false;
}
function beforeInsert($q, $roo)
{
- return;
+ exit;
+ }
+
+ function getKeyDirectory()
+ {
+ $client_dir = HTML_FlexyFramework::get()->Pman['storedir'];
+
+ $key_dir = $client_dir.'/keys';
+
+ if(file_exists($key_dir)) {
+ return $key_dir;
+ }
+
+ if(!is_writable($key_dir)) {
+ return false;
+ }
+
+ mkdir($key_dir, 0755, true);
+
+ return $key_dir;
}
+ // FIXME - this needs to go in beforeInsert/beforeUpdate
+ // should not be sending this the values..
function initSetting($a)
{
- //foreach ($a) > insert
if(empty($a)) {
return;
}
- foreach ($a as $s) {
-
- }
+ $this->initKeys();
+
+ $c = $this->lookup($a['module'], $a['name']);
+
+ $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
+ ));
- return;
+ $o ? $c->update($o) : $c->insert();
}
- function encryptVal($v) {
+ //one key for encrypting all the settings
+ function encrypt($v)
+ {
+ $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, $ciphertext, $pub_key);
+ return $ciphertext;
}
+ function getDecryptVal()
+ {
+ $dir = $this->getKeyDirectory();
+
+ if(!$dir) {
+ return false;
+ }
+
+ if(empty($this->val)) {
+ return false;
+ }
+
+ if(empty($this->is_encrypt)) {
+ return $this->val;
+ }
+
+ $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;
+ }
}