mirror of
https://framagit.org/hubzilla/core.git
synced 2026-06-21 00:52:33 -04:00
Turn dba_driver params into attributes
To allow for inspecting the params used to connect to the database, we now save the params in the dba_driver object instance as readonly attributes. An exception is made to the $pass parameter which is set to protected access, to make it slightly harder to accidentally leak it. Project......: Performance Profiling Sponsored-by.: NLnet NGI0 Commons Fund
This commit is contained in:
@@ -101,19 +101,13 @@ abstract class dba_driver {
|
||||
public $error = false;
|
||||
|
||||
/**
|
||||
* @brief Connect to the database.
|
||||
* Connects to the database.
|
||||
*
|
||||
* This abstract function needs to be implemented in the real driver.
|
||||
*
|
||||
* @param string $server DB server name
|
||||
* @param string $scheme DB scheme
|
||||
* @param string $port DB port
|
||||
* @param string $user DB username
|
||||
* @param string $pass DB password
|
||||
* @param string $db database name
|
||||
* @return bool
|
||||
*/
|
||||
abstract function connect($server, $scheme, $port, $user, $pass, $db, $db_charset);
|
||||
abstract function connect(): bool;
|
||||
|
||||
/**
|
||||
* @brief Perform a DB query with the SQL statement $sql.
|
||||
@@ -147,18 +141,27 @@ abstract class dba_driver {
|
||||
*/
|
||||
abstract function getdriver();
|
||||
|
||||
function __construct($server, $scheme, $port, $user,$pass,$db,$db_charset,$install = false) {
|
||||
if(($install) && (! $this->install($server, $scheme, $port, $user, $pass, $db, $db_charset))) {
|
||||
function __construct(
|
||||
readonly string $server,
|
||||
readonly string $scheme,
|
||||
readonly string $port,
|
||||
readonly string $user,
|
||||
protected string $pass,
|
||||
readonly string $dbname,
|
||||
readonly string $db_charset,
|
||||
$install = false)
|
||||
{
|
||||
if ($install && ! $this->install()) {
|
||||
return;
|
||||
}
|
||||
$this->connect($server, $scheme, $port, $user, $pass, $db, $db_charset);
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
function get_null_date() {
|
||||
return \DBA::$null_date;
|
||||
}
|
||||
|
||||
function get_install_script() {
|
||||
public static function get_install_script() {
|
||||
$platform_name = \Zotlabs\Lib\System::get_platform_name();
|
||||
if(file_exists('install/' . $platform_name . '/' . \DBA::$install_script))
|
||||
return 'install/' . $platform_name . '/' . \DBA::$install_script;
|
||||
@@ -174,8 +177,8 @@ abstract class dba_driver {
|
||||
return \DBA::$utc_now;
|
||||
}
|
||||
|
||||
function install($server,$scheme,$port,$user,$pass,$db) {
|
||||
if (!(strlen($server) && strlen($user))){
|
||||
function install() {
|
||||
if (!strlen($this->server) && strlen($this->user)) {
|
||||
$this->connected = false;
|
||||
$this->db = null;
|
||||
return false;
|
||||
|
||||
@@ -14,31 +14,35 @@ class dba_pdo extends dba_driver {
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @see dba_driver::connect()
|
||||
*/
|
||||
function connect($server, $scheme, $port, $user, $pass, $db, $db_charset) {
|
||||
function connect(): bool {
|
||||
|
||||
$this->driver_dbtype = $scheme;
|
||||
$this->driver_dbtype = $this->scheme;
|
||||
|
||||
if(strpbrk($server,':;')) {
|
||||
$dsn = $this->driver_dbtype . ':unix_socket=' . trim($server, ':;');
|
||||
if(strpbrk($this->server,':;')) {
|
||||
$dsn = $this->driver_dbtype . ':unix_socket=' . trim($this->server, ':;');
|
||||
}
|
||||
else {
|
||||
$dsn = $this->driver_dbtype . ':host=' . $server . (intval($port) ? ';port=' . $port : '');
|
||||
$dsn = $this->driver_dbtype
|
||||
. ':host='
|
||||
. $this->server
|
||||
. (intval($this->port) ? ';port=' . $this->port : '');
|
||||
}
|
||||
|
||||
$dsn .= ';dbname=' . $db;
|
||||
$dsn .= ';dbname=' . $this->dbname;
|
||||
|
||||
if ($this->driver_dbtype === 'mysql') {
|
||||
$dsn .= ';charset=' . $db_charset;
|
||||
$dsn .= ';charset=' . $this->db_charset;
|
||||
}
|
||||
else {
|
||||
$dsn .= ";options='--client_encoding=" . $db_charset . "'";
|
||||
$dsn .= ";options='--client_encoding=" . $this->db_charset . "'";
|
||||
}
|
||||
|
||||
try {
|
||||
$this->db = new PDO($dsn,$user,$pass);
|
||||
$this->db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
|
||||
$this->db = new PDO($dsn, $this->user, $this->pass);
|
||||
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$this->server_version = $this->db->getAttribute(PDO::ATTR_SERVER_VERSION);
|
||||
}
|
||||
catch(PDOException $e) {
|
||||
|
||||
Reference in New Issue
Block a user