Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
20 changes: 20 additions & 0 deletions lib/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
18 changes: 9 additions & 9 deletions lib/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}