Convert getPdoAccessData() : array to a RainLoop\Pdo\Settings object instance

This commit is contained in:
the-djmaze 2023-07-04 11:43:23 +02:00
parent 4cd24b87ff
commit 19ea5de1bf
5 changed files with 65 additions and 62 deletions

View file

@ -636,7 +636,7 @@ class Actions
$aResult['determineUserLanguage'] = (bool)$oConfig->Get('login', 'determine_user_language', true);
$aResult['determineUserDomain'] = (bool)$oConfig->Get('login', 'determine_user_domain', false);
$aResult['supportedPdoDrivers'] = \RainLoop\Common\PdoAbstract::getAvailableDrivers();
$aResult['supportedPdoDrivers'] = \RainLoop\Pdo\Base::getAvailableDrivers();
$aResult['contactsEnable'] = (bool)$oConfig->Get('contacts', 'enable', false);
$aResult['contactsSync'] = (bool)$oConfig->Get('contacts', 'allow_sync', false);

View file

@ -344,7 +344,7 @@ class ActionsAdmin extends Actions
$aResult['determineUserLanguage'] = (bool)$oConfig->Get('login', 'determine_user_language', true);
$aResult['determineUserDomain'] = (bool)$oConfig->Get('login', 'determine_user_domain', false);
$aResult['supportedPdoDrivers'] = \RainLoop\Common\PdoAbstract::getAvailableDrivers();
$aResult['supportedPdoDrivers'] = \RainLoop\Pdo\Base::getAvailableDrivers();
$aResult['contactsEnable'] = (bool)$oConfig->Get('contacts', 'enable', false);
$aResult['contactsSync'] = (bool)$oConfig->Get('contacts', 'allow_sync', false);

View file

@ -1,8 +1,8 @@
<?php
namespace RainLoop\Common;
namespace RainLoop\Pdo;
abstract class PdoAbstract
abstract class Base
{
use \MailSo\Log\Inherit;
@ -19,7 +19,7 @@ abstract class PdoAbstract
return !!\class_exists('PDO');
}
abstract protected function getPdoAccessData() : array;
abstract protected function getPdoSettings() : \RainLoop\Pdo\Settings;
public function sqliteNoCaseCollationHelper(string $sStr1, string $sStr2) : int
{
@ -48,27 +48,26 @@ abstract class PdoAbstract
throw new \Exception('Class PDO does not exist');
}
$sType = $sDsn = $sDbLogin = $sDbPassword = '';
list($sType, $sDsn, $sDbLogin, $sDbPassword, $sSslCa, $bSslVerify, $sSslCiphers) = $this->getPdoAccessData();
$oSettings = $this->getPdoSettings();
if (!\in_array($sType, static::getAvailableDrivers())) {
if (!\in_array($oSettings->driver, static::getAvailableDrivers())) {
throw new \Exception('Unknown PDO SQL connection type');
}
if (empty($sDsn)) {
if (empty($oSettings->dsn)) {
throw new \Exception('Empty PDO DSN configuration');
}
$this->sDbType = $sType;
$this->sDbType = $oSettings->driver;
$options = [];
if ('mysql' === $sType) {
if ($sSslCa) {
$options[\PDO::MYSQL_ATTR_SSL_CA] = $sSslCa;
if ('mysql' === $oSettings->driver) {
if ($oSettings->sslCa) {
$options[\PDO::MYSQL_ATTR_SSL_CA] = $oSettings->sslCa;
}
$options[\PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] = !empty($bSslVerify);
if ($sSslCiphers) {
$options[\PDO::MYSQL_ATTR_SSL_CIPHER] = $sSslCiphers;
$options[\PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] = $oSettings->sslVerify;
if ($oSettings->sslCiphers) {
$options[\PDO::MYSQL_ATTR_SSL_CIPHER] = $oSettings->sslCiphers;
}
/*
$options[\PDO::MYSQL_ATTR_SSL_CAPATH] = '';
@ -78,15 +77,15 @@ abstract class PdoAbstract
*/
}
$oPdo = new \PDO($sDsn, $sDbLogin, $sDbPassword, $options);
$oPdo = new \PDO($oSettings->dsn, $oSettings->user, $oSettings->password, $options);
$sPdoType = $oPdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
$oPdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
// $bCaseFunc = false;
if ('mysql' === $sType && 'mysql' === $sPdoType) {
if ('mysql' === $oSettings->driver && 'mysql' === $sPdoType) {
$oPdo->exec('SET NAMES utf8mb4 COLLATE utf8mb4_general_ci');
}
// else if ('sqlite' === $sType && 'sqlite' === $sPdoType && $this->bSqliteCollate) {
// else if ('sqlite' === $oSettings->driver && 'sqlite' === $sPdoType && $this->bSqliteCollate) {
// if (\method_exists($oPdo, 'sqliteCreateCollation')) {
// $oPdo->sqliteCreateCollation('SQLITE_NOCASE_UTF8', array($this, 'sqliteNoCaseCollationHelper'));
// $bCaseFunc = true;

View file

@ -0,0 +1,20 @@
<?php
namespace RainLoop\Pdo;
class Settings
{
public string $driver;
public string $dsn;
public string $user = '';
public string $password = '';
public string $sslCa = '';
public bool $sslVerify = true;
public string $sslCiphers = '';
}

View file

@ -9,26 +9,14 @@ use
;
class PdoAddressBook
extends \RainLoop\Common\PdoAbstract
extends \RainLoop\Pdo\Base
implements AddressBookInterface
{
use CardDAV;
private $iUserID = 0;
private string $sDsn;
private string $sDsnType;
private string $sUser;
private string $sPassword;
private string $sSslCa = '';
private bool $bSslVerify = true;
private string $sSslCiphers = '';
private \RainLoop\Pdo\Settings $settings;
private static $aSearchInFields = [
PropertyType::EMAIl,
@ -40,9 +28,9 @@ class PdoAddressBook
public function __construct()
{
$oConfig = \RainLoop\Api::Config();
$sDsnType = static::validPdoType($oConfig->Get('contacts', 'type', 'sqlite'));
if ('sqlite' === $sDsnType) {
$sUser = $sPassword = '';
$oSettings = new \RainLoop\Pdo\Settings;
$oSettings->driver = static::validPdoType($oConfig->Get('contacts', 'type', 'sqlite'));
if ('sqlite' === $oSettings->driver) {
$sDsn = 'sqlite:' . APP_PRIVATE_DATA . 'AddressBook.sqlite';
/*
// TODO: use local db?
@ -60,20 +48,18 @@ class PdoAddressBook
*/
} else {
$sDsn = \trim($oConfig->Get('contacts', 'pdo_dsn', ''));
$sUser = \trim($oConfig->Get('contacts', 'pdo_user', ''));
$sPassword = (string)$oConfig->Get('contacts', 'pdo_password', '');
$sDsn = $sDsnType . ':' . \preg_replace('/^[a-z]+:/', '', $sDsn);
if ('mysql' === $sDsnType) {
$this->sSslCa = \trim($oConfig->Get('contacts', 'mysql_ssl_ca', ''));
$this->bSslVerify = !!$oConfig->Get('contacts', 'mysql_ssl_verify', true);
$this->sSslCiphers = \trim($oConfig->Get('contacts', 'mysql_ssl_ciphers', ''));
$oSettings->user = \trim($oConfig->Get('contacts', 'pdo_user', ''));
$oSettings->password = (string)$oConfig->Get('contacts', 'pdo_password', '');
$sDsn = $oSettings->driver . ':' . \preg_replace('/^[a-z]+:/', '', $sDsn);
if ('mysql' === $oSettings->driver) {
$oSettings->sslCa = \trim($oConfig->Get('contacts', 'mysql_ssl_ca', ''));
$oSettings->sslVerify = !!$oConfig->Get('contacts', 'mysql_ssl_verify', true);
$oSettings->sslCiphers = \trim($oConfig->Get('contacts', 'mysql_ssl_ciphers', ''));
}
}
$this->sDsn = $sDsn;
$this->sUser = $sUser;
$this->sPassword = $sPassword;
$this->sDsnType = $sDsnType;
$oSettings->dsn = $sDsn;
$this->settings = $oSettings;
$this->bExplain = false; // debug
}
@ -87,7 +73,7 @@ class PdoAddressBook
public function IsSupported() : bool
{
$aDrivers = static::getAvailableDrivers();
return \is_array($aDrivers) && \in_array($this->sDsnType, $aDrivers);
return \is_array($aDrivers) && \in_array($this->settings->driver, $aDrivers);
}
public function SetEmail(string $sEmail) : bool
@ -1075,7 +1061,7 @@ class PdoAddressBook
$sResult = '';
try {
$this->SyncDatabase();
if (0 >= $this->getVersion($this->sDsnType.'-ab-version')) {
if (0 >= $this->getVersion($this->settings->driver.'-ab-version')) {
$sResult = 'Unknown database error';
}
}
@ -1222,10 +1208,10 @@ SQLITEINITIAL;
}
$mCache = false;
switch ($this->sDsnType) {
switch ($this->settings->driver) {
case 'mysql':
$mCache = $this->dataBaseUpgrade($this->sDsnType.'-ab-version', array(
1 => $this->getInitialTablesArray($this->sDsnType),
$mCache = $this->dataBaseUpgrade($this->settings->driver.'-ab-version', array(
1 => $this->getInitialTablesArray($this->settings->driver),
2 => array(
'ALTER TABLE rainloop_ab_properties ADD prop_value_lower MEDIUMTEXT NOT NULL AFTER prop_value_custom;'
),
@ -1242,8 +1228,8 @@ SQLITEINITIAL;
));
break;
case 'pgsql':
$mCache = $this->dataBaseUpgrade($this->sDsnType.'-ab-version', array(
1 => $this->getInitialTablesArray($this->sDsnType),
$mCache = $this->dataBaseUpgrade($this->settings->driver.'-ab-version', array(
1 => $this->getInitialTablesArray($this->settings->driver),
2 => array(
'ALTER TABLE rainloop_ab_properties ADD prop_value_lower text NOT NULL DEFAULT \'\';'
),
@ -1252,8 +1238,8 @@ SQLITEINITIAL;
));
break;
case 'sqlite':
$mCache = $this->dataBaseUpgrade($this->sDsnType.'-ab-version', array(
1 => $this->getInitialTablesArray($this->sDsnType),
$mCache = $this->dataBaseUpgrade($this->settings->driver.'-ab-version', array(
1 => $this->getInitialTablesArray($this->settings->driver),
2 => array(
'ALTER TABLE rainloop_ab_properties ADD prop_value_lower text NOT NULL DEFAULT \'\';'
),
@ -1305,17 +1291,15 @@ SQLITEINITIAL;
(string) \mb_strtolower($sSearch)).'%';
}
protected function getPdoAccessData() : array
protected function getPdoSettings() : \RainLoop\Pdo\Settings
{
$sSslCa = $this->sSslCa;
$sSslCa = $this->settings->sslCa;
if ($sSslCa && !\is_file($sSslCa)) {
$sFile = \APP_PRIVATE_DATA . 'configs/contacts_mysql_ssl_ca.pem';
// $sSslCa = (\is_file($sFile) || \file_put_contents($sFile, $sSslCa)) ? $sFile : '';
$sSslCa = \file_put_contents($sFile, $sSslCa) ? $sFile : '';
$this->settings->sslCa = \file_put_contents($sFile, $sSslCa) ? $sFile : '';
}
return array($this->sDsnType, $this->sDsn, $this->sUser, $this->sPassword,
$sSslCa, $this->bSslVerify, $this->sSslCiphers
);
return $this->settings;
}
protected function getUserId(string $sEmail, bool $bSkipInsert = false, bool $bCache = true) : int