3 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
5 class Pman_Core_DataObjects_Core_setting extends DB_DataObject
7 public $__table = 'core_setting';
11 // return false when fail
12 $dir = $this->getKeyDirectory();
15 return false; // only fail case?
19 file_exists("{$dir}/pub.key") ||
20 file_exists("{$dir}/pri.key")
25 $ssl = openssl_pkey_new(array(
26 "digest_alg" => "sha512",
27 "private_key_bits" => 1024, //returns cipher in 128 char
28 "private_key_type" => OPENSSL_KEYTYPE_RSA
31 openssl_pkey_export($ssl, $pri_key);
32 $pub_key = openssl_pkey_get_details($ssl);
33 $pub_key = $pub_key["key"];
35 file_put_contents("{$dir}/pub.key",$pub_key);
36 file_put_contents("{$dir}/pri.key",$pri_key);
41 function lookup($m,$n)
43 $s = DB_DataObject::factory('core_setting');
56 function beforeInsert($q, $roo)
58 if (isset($q['is_encrypt'])) {
59 $roo->jerr("no direct access for setting encrypted data at present?");
61 // we store column data in here now - so it has to be insertable.
65 function getKeyDirectory()
67 $client_dir = HTML_FlexyFramework::get()->Pman['storedir'];
69 $key_dir = $client_dir.'/keys';
71 if(file_exists($key_dir)) {
75 if(!is_writable($key_dir)) {
79 mkdir($key_dir, 0755, true);
84 // FIXME - this needs to go in beforeInsert/beforeUpdate
85 // should not be sending this the values..
86 function initSetting($a)
94 $c = $this->lookup($a['module'], $a['name']);
96 $o = $c ? clone($c) : false;
98 $c = $c ? $c : DB_DataObject::factory('core_setting');
101 'module' => $a['module'],
102 'name' => $a['name'],
103 'description' => $a['description'],
104 'val' => (!isset($a['is_encrypt']) || $a['is_encrypt'] == 1) ?
105 $this->encrypt($a['val']) : $a['val'],
106 'is_encrypt' => isset($a['is_encrypt']) ? $a['is_encrypt'] : 1,
110 $o ? $c->update($o) : $c->insert();
113 //one key for encrypting all the settings
116 $key_dir = "{$this->getKeyDirectory()}/pub.key";
118 if(!file_exists($key_dir)) {
122 $pub_key = file_get_contents($key_dir);
126 openssl_public_encrypt($v, $ciphertext, $pub_key);
132 $key_dir = "{$this->getKeyDirectory()}/pri.key";
134 if(!file_exists($key_dir)) {
138 $pri_key = file_get_contents($key_dir);
144 openssl_private_decrypt($v, $plaintext, $pri_key);
149 function getDecryptVal()
152 if(empty($this->val)) {
156 if(empty($this->is_encrypt)) {
160 return $this->decrypt($this->val);