DataObjects/Core_setting.php
[Pman.Core] / DataObjects / Core_setting.php
1 <?php
2
3 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
4
5 class Pman_Core_DataObjects_Core_setting extends DB_DataObject
6 {
7     public $__table = 'core_setting';
8     
9     function initKeys()
10     {
11         $d = HTML_FlexyFramework::get()->Pman['storedir'].'/key';
12         
13         if(
14             file_exists("{$d}/pub.key") ||
15             file_exists("{$d}/pri.key")
16         ){
17             return;
18         }
19         
20         $ssl = openssl_pkey_new(array(
21             "digest_alg" => "sha512",  
22             "private_key_bits" => 1024, //returns cipher in 128 char
23             "private_key_type" => OPENSSL_KEYTYPE_RSA
24         ));
25         
26         openssl_pkey_export($ssl, $pri_key);
27         $pub_key = openssl_pkey_get_details($ssl);
28         $pub_key = $pub_key["key"];
29         
30         file_put_contents("{$d}/pub.key",$pub_key);
31         file_put_contents("{$d}/pri.key",$pri_key);
32     }
33     
34     function getSetting($m,$n)
35     {
36         $s = DB_DataObject::factory('core_setting');
37         $s->setFrom(array(
38             'module' => $m,
39             'name' => $n
40         ));
41         if($s->find(true)) {
42             return $s;
43         }
44         return false;
45     }
46     
47     function beforeInsert($q, $roo)
48     {
49         exit;
50         
51         return;
52     }
53     
54     function initSetting($a)
55     {
56         if(empty($a)) {
57             return;
58         }
59         
60         $c = $this->getSetting($a['module'], $a['name']);
61         if($c) {
62             return;
63         }
64         
65         $d = HTML_FlexyFramework::get()->Pman['storedir'].'/key';
66         
67         $this->setStoreDir($dir);
68         
69         $this->initKeys();
70         
71         $val = $a['val'];
72         if(!isset($a['is_encrypt']) || $a['is_encrypt'] == 1) {
73             $val = $this->encrypt($val);
74         }
75         
76         $s = DB_DataObject::factory('core_setting');
77         $s->setFrom(array(
78             'module' => $a['module'],
79             'name' => $a['name'],
80             'description' => $a['description'],
81             'val' =>$val,
82             'is_encrypt' => isset($a['is_encrypt']) ? $a['is_encrypt'] : 1
83         ));
84         
85         $s->insert();
86     }
87     
88     function encrypt($v)
89     {
90         $pub_key = file_get_contents("{$this->storedir}/pub.key");
91         if(!$pub_key) {
92             return;
93         }
94         openssl_public_encrypt($v, $cipher, $pub_key);
95         return $cipher;
96     }
97 }