191 final function connect(array &$settings = []): void
193 if (!defined(
"DB_USER"))
195 $ini_file_name = LEPTON_PATH.
"/config/lepton.ini.php";
197 if (
true === file_exists($ini_file_name))
199 $config = parse_ini_string(
";" . file_get_contents($ini_file_name),
true);
202 if (!isset($settings[
'host']))
204 $settings[
'host'] = $config[
'database'][
'host'];
207 if (!isset($settings[
'user']))
209 $settings[
'user'] = $config[
'database'][
'user'];
212 if (!isset($settings[
'pass']))
214 $settings[
'pass'] = $config[
'database'][
'pass'];
217 if (!isset($settings[
'name']))
219 $settings[
'name'] = $config[
'database'][
'name'];
222 if (!isset($settings[
'port']))
224 $settings[
'port'] = $config[
'database'][
'port'];
227 if ((isset($config[
'database'][
'charset'])) && (!isset($settings[
'charset'])))
229 $settings[
'charset'] = $config[
'database'][
'charset'];
232 if (!defined(
"TABLE_PREFIX"))
234 define(
"TABLE_PREFIX", $config[
'database'][
'prefix']);
238 if (isset($config[
'database'][
'key']))
240 self::$db_key = $config[
'database'][
'key'];
243 if (isset($config[
'database'][
'options']))
245 self::$default_openssl_options = intval($config[
'database'][
'options']);
248 if (isset($config[
'database'][
'cipher']))
250 if (
"0" === $config[
'database'][
'cipher'])
252 $config[
'database'][
'cipher'] = 0;
254 self::$default_openssl_method = $config[
'database'][
'cipher'];
257 if (isset($config[
'database'][
'iv']))
259 self::$default_openssl_iv = $config[
'database'][
'iv'];
262 if (isset($config[
'database'][
'ivlen']))
264 self::$default_openssl_ivlen = $config[
'database'][
'ivlen'];
270 if (isset($config[
'system_const']))
272 foreach ($config[
'system_const'] as $key => $value)
276 define($key, $value);
283 exit(
'<p><b>Sorry, but this installation seems to be damaged! Please contact your webmaster!</b></p>');
288 if (isset($settings[
'key']))
290 self::$db_key = $settings[
'key'];
292 if (isset($settings[
'options']))
294 self::$default_openssl_options = $settings[
'options'];
296 if (isset($settings[
'cipher']))
298 self::$default_openssl_method = $settings[
'cipher'];
300 if (isset($settings[
'iv']))
302 self::$default_openssl_iv = $settings[
'iv'];
304 if (isset($settings[
'ivlen']))
306 self::$default_openssl_ivlen = $settings[
'ivlen'];
310 'host' => array_key_exists(
'host', $settings) ? $settings[
'host'] : DB_HOST,
311 'user' => array_key_exists(
'user', $settings) ? $settings[
'user'] : DB_USERNAME,
312 'pass' => array_key_exists(
'pass', $settings) ? $settings[
'pass'] : DB_PASSWORD,
313 'name' => array_key_exists(
'name', $settings) ? $settings[
'name'] : DB_NAME,
314 'port' => array_key_exists(
'port', $settings) ? $settings[
'port'] : DB_PORT
317 if (array_key_exists(
'charset', $settings))
319 $setup[
'charset'] = $settings[
'charset'];
321 $setup[
'charset'] = (defined(
'DB_CHARSET') ? DB_CHARSET :
"utf8");
325 if ($setup[
'port'] !==
'3306')
327 $setup[
'host'] .=
';port=' . $setup[
'port'];
331 $dsn =
"mysql:dbname=".$setup[
'name'].
";host=".$setup[
'host'].
";charset=".$setup[
'charset'];
335 $this->db_handle =
new PDO(
340 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
341 PDO::ATTR_PERSISTENT =>
true,
342 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
349 $this->
simple_query(
"SET NAMES '".$setup[
'charset'].
"'");
358 if(isset( $config[
'database'][
'mode'] ))
360 $this->
simple_query(
"SET GLOBAL sql_mode='".$config[
'database'][
'mode'].
"'");
367 }
catch (PDOException $oError) {
368 $this->
set_error( $oError->getMessage() );
404 public function simple_query(
string $sMySQL_Query =
"", array $aParams = []): int
409 $oStatement=$this->db_handle->prepare( $sMySQL_Query );
410 if (!empty($aParams))
412 if ((isset($aParams[0])) && (is_array($aParams[0])))
414 foreach ($aParams as $temp_params)
416 $oStatement->execute($temp_params);
419 $oStatement->execute($aParams);
422 $oStatement->execute();
424 return $oStatement->rowCount();
425 }
catch (PDOException $error)
427 $this->error = $error->getMessage() .
"\n<p>Query: " . $sMySQL_Query .
"\n</p>\n";
428 $this->HandleDisplayError(
"301");
487 $oStatement=$this->db_handle->prepare(
"SHOW tables" );
488 $oStatement->execute();
490 $data = $oStatement->fetchAll();
492 catch (Exception $error)
494 $this->error = $error->getMessage();
495 $this->HandleDisplayError(
"1");
500 foreach($data as &$ref)
502 $ret_value[] = array_shift( $ref );
508 foreach ($ret_value as &$ref2)
510 $ref2 = str_replace($strip,
"", $ref2);
528 public function describe_table(
string $sTableName, array &$aStorage = [],
int $iForm = self::DESCRIBE_RAW ): bool
533 $oStatement=$this->db_handle->prepare(
"DESCRIBE `" . $sTableName .
"`" );
534 $oStatement->execute();
536 $aStorage = $oStatement->fetchAll();
540 case (self::DESCRIBE_ASSOC):
542 foreach($aStorage as $values)
544 $aTemp[ $values[
"Field"] ] = $values;
550 case (self::DESCRIBE_ONLY_NAMES):
552 foreach($aStorage as $values)
554 $aTemp[] = $values[
"Field"];
567 catch (Exception $error)
569 $this->error = $error->getMessage();
570 $this->HandleDisplayError(
"2");
596 public function execute_query(
string $aQuery=
"",
bool $bFetch=
false, array &$aStorage=[],
bool $bFetchAll=
true ) : int
600 $oStatement=$this->db_handle->prepare($aQuery);
601 $oStatement->execute();
603 if (($oStatement->rowCount() > 0) && (
true === $bFetch))
605 $aStorage = (
true === $bFetchAll)
606 ? $oStatement->fetchAll()
607 : $oStatement->fetch()
611 return $oStatement->rowCount();
612 }
catch( PDOException $error) {
613 $this->error = $error->getMessage();
614 $this->HandleDisplayError(
"10");
633 public function build_and_execute(
string $type,
string $table_name, array $table_values,
string $condition=
"",
string $key=
""): int
636 switch( strtolower($type) )
639 $q =
"UPDATE `" . $table_name .
"` SET ";
640 foreach ($table_values as $field => $value)
642 $q .=
"`" . $field .
"`= :" . $field .
", ";
644 $q = substr($q, 0, -2) . (($condition !=
"") ?
" WHERE " . $condition :
"");
648 $keys = array_keys($table_values);
649 $q =
"INSERT into `" . $table_name .
"` (`";
650 $q .= implode(
"`,`", $keys) .
"`) VALUES (:";
651 $q .= implode(
", :", $keys) .
")";
654 case 'insert_on_duplicate_key_update':
655 $keys = array_keys($table_values);
656 $q =
"INSERT into `" . $table_name .
"` (`";
657 $q .= implode(
"`,`", $keys) .
"`) VALUES (:";
658 $q .= implode(
", :", $keys) .
")";
659 $q .=
"ON DUPLICATE KEY UPDATE";
660 foreach($table_values as $field => $value)
664 $q .=
"`" . $field .
"`= :" . $field .
", ";
667 $q = substr($q, 0, -2);
671 die(__line__.
": build_and_execute-> type unknown!");
677 $oStatement=$this->db_handle->prepare($q);
678 $oStatement->execute( $table_values );
679 return $oStatement->rowCount();
681 catch( PDOException $error)
683 $this->error = $error->getMessage().
"\n<p>Query: ".$q.
"\n</p>\n";
684 $this->HandleDisplayError(
"12");
736 public function query(
string $sSqlQuery =
""): object|null
740 $oStatement = $this->db_handle->prepare($sSqlQuery);
741 $oStatement->execute();
743 if ((
true === $this->bXrunInstalled) && (
true === lib_comp::$bXRunInit))
745 return new lib_comp_query($oStatement);
750 }
catch( PDOException $error ) {
751 $this->
set_error( $error->getMessage() );
752 $this->HandleDisplayError(
"xRun 101");