You've already forked postfixadmin
mirror of
https://github.com/postfixadmin/postfixadmin.git
synced 2025-08-09 05:02:44 +03:00
split db_connect() up to separate out db_connection_string() (which returns a PDO DSN string)
This commit is contained in:
@@ -1559,6 +1559,47 @@ $DEBUG_TEXT = <<<EOF
|
|||||||
</ul>
|
</ul>
|
||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string - PDO DSN for PHP.
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
function db_connection_string() {
|
||||||
|
global $CONF;
|
||||||
|
$dsn = null;
|
||||||
|
if (db_mysql()) {
|
||||||
|
$socket = false;
|
||||||
|
if (Config::has('database_socket')) {
|
||||||
|
$socket = Config::read_string('database_socket');
|
||||||
|
}
|
||||||
|
|
||||||
|
$database_name = Config::read_string('database_name');
|
||||||
|
|
||||||
|
if ($socket) {
|
||||||
|
$dsn = "mysql:unix_socket={$socket};dbname={$database_name};charset=UTF8";
|
||||||
|
} else {
|
||||||
|
$dsn = "mysql:host={$CONF['database_host']};dbname={$database_name};charset=UTF8";
|
||||||
|
}
|
||||||
|
} elseif (db_sqlite()) {
|
||||||
|
$db = $CONF['database_name'];
|
||||||
|
|
||||||
|
$dsn = "sqlite:{$db}";
|
||||||
|
} elseif (db_pgsql()) {
|
||||||
|
$dsn = "pgsql:dbname={$CONF['database_name']}";
|
||||||
|
if (isset($CONF['database_host'])) {
|
||||||
|
$dsn .= ";host={$CONF['database_host']}";
|
||||||
|
}
|
||||||
|
if (isset($CONF['database_port'])) {
|
||||||
|
$dsn .= ";port={$CONF['database_port']}";
|
||||||
|
}
|
||||||
|
$dsn .= ";options='-c client_encoding=utf8'";
|
||||||
|
} else {
|
||||||
|
throw new Exception("<p style='color: red'>FATAL Error:<br />Invalid \$CONF['database_type'] <br/>'pgsql', 'mysql' or 'sqlite' supported. <br/> Please fix your config.inc.php!</p>");
|
||||||
|
}
|
||||||
|
|
||||||
|
return $dsn;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* db_connect
|
* db_connect
|
||||||
* Action: Makes a connection to the database if it doesn't exist
|
* Action: Makes a connection to the database if it doesn't exist
|
||||||
@@ -1579,28 +1620,19 @@ function db_connect() {
|
|||||||
|
|
||||||
$link = false;
|
$link = false;
|
||||||
|
|
||||||
|
// throws.
|
||||||
|
$dsn = db_connection_string();
|
||||||
|
|
||||||
$options = array(
|
$options = array(
|
||||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||||
|
PDO::ATTR_TIMEOUT => 5,
|
||||||
);
|
);
|
||||||
$username_password = true;
|
$username_password = true;
|
||||||
|
|
||||||
$queries = array();
|
$queries = array();
|
||||||
|
|
||||||
$dsn = null;
|
|
||||||
|
|
||||||
if (db_mysql()) {
|
if (db_mysql()) {
|
||||||
$socket = false;
|
|
||||||
if (Config::has('database_socket')) {
|
|
||||||
$socket = Config::read_string('database_socket');
|
|
||||||
}
|
|
||||||
|
|
||||||
$database_name = Config::read_string('database_name');
|
|
||||||
|
|
||||||
if ($socket) {
|
|
||||||
$dsn = "mysql:unix_socket={$socket};dbname={$database_name};charset=UTF8";
|
|
||||||
} else {
|
|
||||||
$dsn = "mysql:host={$CONF['database_host']};dbname={$database_name};charset=UTF8";
|
|
||||||
}
|
|
||||||
if (Config::bool('database_use_ssl')) {
|
if (Config::bool('database_use_ssl')) {
|
||||||
$options[PDO::MYSQL_ATTR_SSL_KEY] = Config::read_string('database_ssl_key');
|
$options[PDO::MYSQL_ATTR_SSL_KEY] = Config::read_string('database_ssl_key');
|
||||||
$options[PDO::MYSQL_ATTR_SSL_CA] = Config::read_string('database_ssl_ca');
|
$options[PDO::MYSQL_ATTR_SSL_CA] = Config::read_string('database_ssl_ca');
|
||||||
@@ -1636,17 +1668,9 @@ function db_connect() {
|
|||||||
throw new Exception($error_text);
|
throw new Exception($error_text);
|
||||||
}
|
}
|
||||||
|
|
||||||
$dsn = "sqlite:{$db}";
|
|
||||||
$username_password = false;
|
$username_password = false;
|
||||||
} elseif (db_pgsql()) {
|
} elseif (db_pgsql()) {
|
||||||
$dsn = "pgsql:dbname={$CONF['database_name']}";
|
// nothing to do.
|
||||||
if (isset($CONF['database_host'])) {
|
|
||||||
$dsn .= ";host={$CONF['database_host']}";
|
|
||||||
}
|
|
||||||
if (isset($CONF['database_port'])) {
|
|
||||||
$dsn .= ";port={$CONF['database_port']}";
|
|
||||||
}
|
|
||||||
$dsn .= ";options='-c client_encoding=utf8'";
|
|
||||||
} else {
|
} else {
|
||||||
throw new Exception("<p style='color: red'>FATAL Error:<br />Invalid \$CONF['database_type']! Please fix your config.inc.php!</p>");
|
throw new Exception("<p style='color: red'>FATAL Error:<br />Invalid \$CONF['database_type']! Please fix your config.inc.php!</p>");
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user