LEPTON CMS 7.0.0
feel free to keep it strictly simple...
Loading...
Searching...
No Matches
lepton_secure_database.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
25{
26
27 static private string $db_key = "";
28
29 static private string $default_openssl_method = "";
30
31 static private string $default_openssl_iv = "";
32
33 static private string $default_openssl_ivlen = "";
34
35 static private int $default_openssl_options = 0;
36
37
43 protected function cryptString(string $sSource=""): string
44 {
45 if( "" === self::$default_openssl_method )
46 {
47 echo LEPTON_tools::display("UNABLE to crypt source! Keep source untouched.", "pre", "ui message red");
48 return $sSource;
49 }
50 return openssl_encrypt(
51 $sSource,
52 self::$default_openssl_method,
53 self::$db_key,
54 self::$default_openssl_options,
55 self::$default_openssl_iv
56 );
57 }
58
64 protected function decryptString(string $sSource): string
65 {
66 if( "" === self::$default_openssl_method )
67 {
68 return $sSource;
69 }
70 return openssl_decrypt(
71 $sSource,
72 self::$default_openssl_method,
73 self::$db_key,
74 self::$default_openssl_options,
75 self::$default_openssl_iv
76 );
77 }
78
89 public function setOpenSSLMethod(string $sMethodName): void
90 {
91 self::$default_openssl_method = $sMethodName;
92 }
93
99 public function setOpenSSLIv(string $sNewIV): void
100 {
101 self::$default_openssl_iv = $sNewIV;
102 }
103
117 public static function __callStatic($method, $args)
118 {
119 switch (strtolower($method))
120 {
121 case 'cryptstring':
122 return self::$instance->cryptString($args[0]);
123 break;
124
125 case 'decryptstring':
126 return self::$instance->decryptString($args[0]);
127 break;
128
129 default:
130 echo LEPTON_tools::display("Static call to unknown method!", "pre", "ui message red");
131 return NULL;
132 break;
133 }
134 }
135
148 public function secure_get_one(string $SQL): string|null
149 {
150 $result = $this->get_one($SQL);
151 return ($result === NULL)
152 ? NULL
153 : $this->decryptString( $result )
154 ;
155 }
156
180 public function secure_execute_query(
181 string $aQuery = "",
182 bool $bFetch = false,
183 array &$aStorage = array(),
184 bool $bFetchAll = true,
185 array $aListOfFields = array()
186 ): bool
187 {
188 if (!is_array($aListOfFields))
189 {
190 LEPTON_tools::display("REQUIRED list of fieldnames must be an array!", "div", "ui message red");
191 return false;
192 }
193
194 $result = $this->execute_query(
195 $aQuery,
196 $bFetch,
197 $aStorage,
198 $bFetchAll
199 );
200
201 if (false === $result)
202 {
203 LEPTON_tools::display($this->get_error(), "div", "ui message red");
204 return false;
205 }
206
207 if (true === $bFetchAll)
208 {
209 foreach ($aStorage as &$ref)
210 {
211 foreach ($aListOfFields as $sFieldname)
212 {
213 if (isset($ref[$sFieldname]))
214 {
215 $ref[$sFieldname] = $this->decryptString($ref[$sFieldname]);
216 }
217 }
218 }
219 } else {
220 foreach ($aListOfFields as $sFieldname)
221 {
222 if (isset($aStorage[$sFieldname]))
223 {
224 $aStorage[$sFieldname] = $this->decryptString($aStorage[$sFieldname]);
225 }
226 }
227 }
228
229 return true;
230 }
231
248 string $type,
249 string $table_name,
250 array $table_values = [] ,
251 string $condition = "",
252 array $aListOfFields = [],
253 string $key=""
254 ): int
255 {
256
257 if (!is_array($aListOfFields))
258 {
259 LEPTON_tools::display("REQUIRED list of fieldnames must be an array!", "div", "ui message red");
260 return 0;
261 }
262
263 foreach ($aListOfFields as $sName)
264 {
265 if (isset($table_values[$sName]))
266 {
267 $table_values[ $sName ] = $this->cryptString( $table_values[ $sName ] );
268 }
269 }
270
271 return $this->build_and_execute(
272 $type,
273 $table_name,
274 $table_values,
275 $condition,
276 $key
277 );
278 }
279}
static display(mixed $something_to_display="", string $tag="pre", string|null $css_class=null)
setOpenSSLIv(string $sNewIV)
secure_execute_query(string $aQuery="", bool $bFetch=false, array &$aStorage=array(), bool $bFetchAll=true, array $aListOfFields=array())
cryptString(string $sSource="")
secure_build_and_execute(string $type, string $table_name, array $table_values=[], string $condition="", array $aListOfFields=[], string $key="")
trait LEPTON_secure_database
secure_get_one(string $SQL)
decryptString(string $sSource)
setOpenSSLMethod(string $sMethodName)