From 7dd1ff8c11c6337ff23f5c847c2bb01601a5d62e Mon Sep 17 00:00:00 2001 From: Ulrich Goettlich Date: Tue, 1 Jul 2025 10:05:48 +0200 Subject: [PATCH] Added dbdriveroptions for additional PDO-Configuration Options --- README.md | 1 + lib/Config.php | 20 ++++++++++++++++++++ lib/Db.php | 18 +++++++++--------- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index fc1b539..b634065 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,7 @@ that *User Backend SQL Raw* will connect to. * Not more than 100 characters of the first line are read. * Whitespace-like characters are [trimmed](https://www.php.net/manual/en/function.trim.php) from the beginning and end of the read password. +* `dbdriveroptions`: Can be used for additional PDO-Options like SSL-Certificates There are two methods to configure the database connection: diff --git a/lib/Config.php b/lib/Config.php index cbd0191..df81e89 100644 --- a/lib/Config.php +++ b/lib/Config.php @@ -47,6 +47,7 @@ class Config const CONFIG_KEY_COUNT_USERS = 'count_users'; const CONFIG_KEY_GET_HOME = 'get_home'; const CONFIG_KEY_CREATE_USER = 'create_user'; + const CONFIG_KEY_DB_DRIVER_OPTIONS = 'dbdriveroptions'; /* @var LoggerInterface */ private $logger; @@ -237,6 +238,25 @@ public function getQueryCreateUser() + /** + * Returns an array of PDO driver options if set in the config. + * This allows passing custom PDO options (e.g. charset, timeout) to the database connection. + * @return array + */ + public function getDbDriverOptions(): array + { + $options = $this->getConfigValueOrFalse(self::CONFIG_KEY_DB_DRIVER_OPTIONS); + if (!is_array($options)) { + return []; + } + // Cast keys to integer in case they are set as strings in the config + $pdoOptions = []; + foreach ($options as $key => $value) { + $pdoOptions[(int)$key] = $value; + } + return $pdoOptions; + } + /** * Tries to read a config value and throws an exception if it is not set. * This is used for config keys that are mandatory. diff --git a/lib/Db.php b/lib/Db.php index f569b48..5ef4e1d 100644 --- a/lib/Db.php +++ b/lib/Db.php @@ -73,17 +73,17 @@ protected function createDbHandle() $username = $this->config->getDbUser(); $password = $this->config->getDbPassword(); $dsn = $this->config->getDsn(); + $options = $this->config->getDbDriverOptions(); - // The PDO constructor does not seem to be able to handle parameters - // with `false` values. Therefore, feeding it here manually all options. - if ($username and $password) { - return new PDO(dsn: $dsn, username: $username, password: $password); - } elseif ($username and !$password) { - return new PDO(dsn: $dsn, username: $username); - } elseif (!$username and $password) { - return new PDO(dsn: $dsn, password: $password); + // Die PDO-Optionen werden jetzt immer mitgegeben + if ($username && $password) { + return new PDO(dsn: $dsn, username: $username, password: $password, options: $options); + } elseif ($username && !$password) { + return new PDO(dsn: $dsn, username: $username, options: $options); + } elseif (!$username && $password) { + return new PDO(dsn: $dsn, password: $password, options: $options); } else { - return new PDO($dsn); + return new PDO(dsn: $dsn, options: $options); } } }