LEPTON CMS 7.2.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|bool
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|bool
65 {
66 if (("" === self::$default_openssl_method) || ("" === $sSource))
67 {
68 return $sSource;
69 }
70
71 return openssl_decrypt(
72 $sSource,
73 self::$default_openssl_method,
74 self::$db_key,
75 self::$default_openssl_options,
76 self::$default_openssl_iv
77 );
78 }
79
90 public function setOpenSSLMethod(string $sMethodName): void
91 {
92 self::$default_openssl_method = $sMethodName;
93 }
94
100 public function setOpenSSLIv(string $sNewIV): void
101 {
102 self::$default_openssl_iv = $sNewIV;
103 }
104
105
118 public function secure_get_one(string $SQL): string|null
119 {
120 $result = $this->get_one($SQL);
121 return (is_null($result))
122 ? NULL
123 : $this->decryptString( (string)$result )
124 ;
125 }
126
150 public function secure_execute_query(
151 string $aQuery = "",
152 bool $bFetch = false,
153 array &$aStorage = array(),
154 bool $bFetchAll = true,
155 array $aListOfFields = array()
156 ): bool
157 {
158 if (!is_array($aListOfFields))
159 {
160 LEPTON_tools::display("REQUIRED list of fieldnames must be an array!", "div", "ui message red");
161 return false;
162 }
163
164 $result = $this->execute_query(
165 $aQuery,
166 $bFetch,
167 $aStorage,
168 $bFetchAll
169 );
170
171 if (false === $result)
172 {
173 LEPTON_tools::display($this->get_error(), "div", "ui message red");
174 return false;
175 }
176
177 if (true === $bFetchAll)
178 {
179 foreach ($aStorage as &$ref)
180 {
181 foreach ($aListOfFields as $sFieldname)
182 {
183 if (isset($ref[$sFieldname]))
184 {
185 $ref[$sFieldname] = $this->decryptString( (string)$ref[$sFieldname]);
186 }
187 }
188 }
189 } else {
190 foreach ($aListOfFields as $sFieldname)
191 {
192 if (isset($aStorage[$sFieldname]))
193 {
194 $aStorage[$sFieldname] = $this->decryptString( (string)$aStorage[$sFieldname]);
195 }
196 }
197 }
198
199 return true;
200 }
201
218 string $type,
219 string $table_name,
220 array $table_values = [] ,
221 string $condition = "",
222 array $aListOfFields = [],
223 string $key=""
224 ): int
225 {
226
227 if (!is_array($aListOfFields))
228 {
229 LEPTON_tools::display("REQUIRED list of fieldnames must be an array!", "div", "ui message red");
230 return 0;
231 }
232
233 foreach ($aListOfFields as $sName)
234 {
235 if (isset($table_values[$sName]))
236 {
237 $table_values[ $sName ] = $this->cryptString( (string)$table_values[$sName]);
238 }
239 }
240
241 return $this->build_and_execute(
242 $type,
243 $table_name,
244 $table_values,
245 $condition,
246 $key
247 );
248 }
249
263 public static function __callStatic($method, $args)
264 {
265 switch (strtolower($method))
266 {
267 case 'cryptstring':
268 return self::$instance->cryptString((string)$args[0]);
269 break;
270
271 case 'decryptstring':
272 return self::$instance->decryptString((string)$args[0]);
273 break;
274
275 default:
276 echo LEPTON_tools::display("Static call to unknown method!", "pre", "ui message red");
277 return NULL;
278 break;
279 }
280 }
281}
static display(mixed $something_to_display="", string $tag="pre", string|null $css_class=null, bool|null $useVarDump=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)