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['determineUserLanguage'] = (bool)$oConfig->Get('login', 'determine_user_language', true);
$aResult['determineUserDomain'] = (bool)$oConfig->Get('login', 'determine_user_domain', false); $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['contactsEnable'] = (bool)$oConfig->Get('contacts', 'enable', false);
$aResult['contactsSync'] = (bool)$oConfig->Get('contacts', 'allow_sync', 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['determineUserLanguage'] = (bool)$oConfig->Get('login', 'determine_user_language', true);
$aResult['determineUserDomain'] = (bool)$oConfig->Get('login', 'determine_user_domain', false); $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['contactsEnable'] = (bool)$oConfig->Get('contacts', 'enable', false);
$aResult['contactsSync'] = (bool)$oConfig->Get('contacts', 'allow_sync', false); $aResult['contactsSync'] = (bool)$oConfig->Get('contacts', 'allow_sync', false);

View file

@ -1,8 +1,8 @@
<?php <?php
namespace RainLoop\Common; namespace RainLoop\Pdo;
abstract class PdoAbstract abstract class Base
{ {
use \MailSo\Log\Inherit; use \MailSo\Log\Inherit;
@ -19,7 +19,7 @@ abstract class PdoAbstract
return !!\class_exists('PDO'); return !!\class_exists('PDO');
} }
abstract protected function getPdoAccessData() : array; abstract protected function getPdoSettings() : \RainLoop\Pdo\Settings;
public function sqliteNoCaseCollationHelper(string $sStr1, string $sStr2) : int public function sqliteNoCaseCollationHelper(string $sStr1, string $sStr2) : int
{ {
@ -48,27 +48,26 @@ abstract class PdoAbstract
throw new \Exception('Class PDO does not exist'); throw new \Exception('Class PDO does not exist');
} }
$sType = $sDsn = $sDbLogin = $sDbPassword = ''; $oSettings = $this->getPdoSettings();
list($sType, $sDsn, $sDbLogin, $sDbPassword, $sSslCa, $bSslVerify, $sSslCiphers) = $this->getPdoAccessData();
if (!\in_array($sType, static::getAvailableDrivers())) { if (!\in_array($oSettings->driver, static::getAvailableDrivers())) {
throw new \Exception('Unknown PDO SQL connection type'); throw new \Exception('Unknown PDO SQL connection type');
} }
if (empty($sDsn)) { if (empty($oSettings->dsn)) {
throw new \Exception('Empty PDO DSN configuration'); throw new \Exception('Empty PDO DSN configuration');
} }
$this->sDbType = $sType; $this->sDbType = $oSettings->driver;
$options = []; $options = [];
if ('mysql' === $sType) { if ('mysql' === $oSettings->driver) {
if ($sSslCa) { if ($oSettings->sslCa) {
$options[\PDO::MYSQL_ATTR_SSL_CA] = $sSslCa; $options[\PDO::MYSQL_ATTR_SSL_CA] = $oSettings->sslCa;
} }
$options[\PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] = !empty($bSslVerify); $options[\PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] = $oSettings->sslVerify;
if ($sSslCiphers) { if ($oSettings->sslCiphers) {
$options[\PDO::MYSQL_ATTR_SSL_CIPHER] = $sSslCiphers; $options[\PDO::MYSQL_ATTR_SSL_CIPHER] = $oSettings->sslCiphers;
} }
/* /*
$options[\PDO::MYSQL_ATTR_SSL_CAPATH] = ''; $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); $sPdoType = $oPdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
$oPdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $oPdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
// $bCaseFunc = false; // $bCaseFunc = false;
if ('mysql' === $sType && 'mysql' === $sPdoType) { if ('mysql' === $oSettings->driver && 'mysql' === $sPdoType) {
$oPdo->exec('SET NAMES utf8mb4 COLLATE utf8mb4_general_ci'); $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')) { // if (\method_exists($oPdo, 'sqliteCreateCollation')) {
// $oPdo->sqliteCreateCollation('SQLITE_NOCASE_UTF8', array($this, 'sqliteNoCaseCollationHelper')); // $oPdo->sqliteCreateCollation('SQLITE_NOCASE_UTF8', array($this, 'sqliteNoCaseCollationHelper'));
// $bCaseFunc = true; // $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 class PdoAddressBook
extends \RainLoop\Common\PdoAbstract extends \RainLoop\Pdo\Base
implements AddressBookInterface implements AddressBookInterface
{ {
use CardDAV; use CardDAV;
private $iUserID = 0; private $iUserID = 0;
private string $sDsn; private \RainLoop\Pdo\Settings $settings;
private string $sDsnType;
private string $sUser;
private string $sPassword;
private string $sSslCa = '';
private bool $bSslVerify = true;
private string $sSslCiphers = '';
private static $aSearchInFields = [ private static $aSearchInFields = [
PropertyType::EMAIl, PropertyType::EMAIl,
@ -40,9 +28,9 @@ class PdoAddressBook
public function __construct() public function __construct()
{ {
$oConfig = \RainLoop\Api::Config(); $oConfig = \RainLoop\Api::Config();
$sDsnType = static::validPdoType($oConfig->Get('contacts', 'type', 'sqlite')); $oSettings = new \RainLoop\Pdo\Settings;
if ('sqlite' === $sDsnType) { $oSettings->driver = static::validPdoType($oConfig->Get('contacts', 'type', 'sqlite'));
$sUser = $sPassword = ''; if ('sqlite' === $oSettings->driver) {
$sDsn = 'sqlite:' . APP_PRIVATE_DATA . 'AddressBook.sqlite'; $sDsn = 'sqlite:' . APP_PRIVATE_DATA . 'AddressBook.sqlite';
/* /*
// TODO: use local db? // TODO: use local db?
@ -60,20 +48,18 @@ class PdoAddressBook
*/ */
} else { } else {
$sDsn = \trim($oConfig->Get('contacts', 'pdo_dsn', '')); $sDsn = \trim($oConfig->Get('contacts', 'pdo_dsn', ''));
$sUser = \trim($oConfig->Get('contacts', 'pdo_user', '')); $oSettings->user = \trim($oConfig->Get('contacts', 'pdo_user', ''));
$sPassword = (string)$oConfig->Get('contacts', 'pdo_password', ''); $oSettings->password = (string)$oConfig->Get('contacts', 'pdo_password', '');
$sDsn = $sDsnType . ':' . \preg_replace('/^[a-z]+:/', '', $sDsn); $sDsn = $oSettings->driver . ':' . \preg_replace('/^[a-z]+:/', '', $sDsn);
if ('mysql' === $sDsnType) { if ('mysql' === $oSettings->driver) {
$this->sSslCa = \trim($oConfig->Get('contacts', 'mysql_ssl_ca', '')); $oSettings->sslCa = \trim($oConfig->Get('contacts', 'mysql_ssl_ca', ''));
$this->bSslVerify = !!$oConfig->Get('contacts', 'mysql_ssl_verify', true); $oSettings->sslVerify = !!$oConfig->Get('contacts', 'mysql_ssl_verify', true);
$this->sSslCiphers = \trim($oConfig->Get('contacts', 'mysql_ssl_ciphers', '')); $oSettings->sslCiphers = \trim($oConfig->Get('contacts', 'mysql_ssl_ciphers', ''));
} }
} }
$this->sDsn = $sDsn; $oSettings->dsn = $sDsn;
$this->sUser = $sUser; $this->settings = $oSettings;
$this->sPassword = $sPassword;
$this->sDsnType = $sDsnType;
$this->bExplain = false; // debug $this->bExplain = false; // debug
} }
@ -87,7 +73,7 @@ class PdoAddressBook
public function IsSupported() : bool public function IsSupported() : bool
{ {
$aDrivers = static::getAvailableDrivers(); $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 public function SetEmail(string $sEmail) : bool
@ -1075,7 +1061,7 @@ class PdoAddressBook
$sResult = ''; $sResult = '';
try { try {
$this->SyncDatabase(); $this->SyncDatabase();
if (0 >= $this->getVersion($this->sDsnType.'-ab-version')) { if (0 >= $this->getVersion($this->settings->driver.'-ab-version')) {
$sResult = 'Unknown database error'; $sResult = 'Unknown database error';
} }
} }
@ -1222,10 +1208,10 @@ SQLITEINITIAL;
} }
$mCache = false; $mCache = false;
switch ($this->sDsnType) { switch ($this->settings->driver) {
case 'mysql': case 'mysql':
$mCache = $this->dataBaseUpgrade($this->sDsnType.'-ab-version', array( $mCache = $this->dataBaseUpgrade($this->settings->driver.'-ab-version', array(
1 => $this->getInitialTablesArray($this->sDsnType), 1 => $this->getInitialTablesArray($this->settings->driver),
2 => array( 2 => array(
'ALTER TABLE rainloop_ab_properties ADD prop_value_lower MEDIUMTEXT NOT NULL AFTER prop_value_custom;' 'ALTER TABLE rainloop_ab_properties ADD prop_value_lower MEDIUMTEXT NOT NULL AFTER prop_value_custom;'
), ),
@ -1242,8 +1228,8 @@ SQLITEINITIAL;
)); ));
break; break;
case 'pgsql': case 'pgsql':
$mCache = $this->dataBaseUpgrade($this->sDsnType.'-ab-version', array( $mCache = $this->dataBaseUpgrade($this->settings->driver.'-ab-version', array(
1 => $this->getInitialTablesArray($this->sDsnType), 1 => $this->getInitialTablesArray($this->settings->driver),
2 => array( 2 => array(
'ALTER TABLE rainloop_ab_properties ADD prop_value_lower text NOT NULL DEFAULT \'\';' 'ALTER TABLE rainloop_ab_properties ADD prop_value_lower text NOT NULL DEFAULT \'\';'
), ),
@ -1252,8 +1238,8 @@ SQLITEINITIAL;
)); ));
break; break;
case 'sqlite': case 'sqlite':
$mCache = $this->dataBaseUpgrade($this->sDsnType.'-ab-version', array( $mCache = $this->dataBaseUpgrade($this->settings->driver.'-ab-version', array(
1 => $this->getInitialTablesArray($this->sDsnType), 1 => $this->getInitialTablesArray($this->settings->driver),
2 => array( 2 => array(
'ALTER TABLE rainloop_ab_properties ADD prop_value_lower text NOT NULL DEFAULT \'\';' 'ALTER TABLE rainloop_ab_properties ADD prop_value_lower text NOT NULL DEFAULT \'\';'
), ),
@ -1305,17 +1291,15 @@ SQLITEINITIAL;
(string) \mb_strtolower($sSearch)).'%'; (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)) { if ($sSslCa && !\is_file($sSslCa)) {
$sFile = \APP_PRIVATE_DATA . 'configs/contacts_mysql_ssl_ca.pem'; $sFile = \APP_PRIVATE_DATA . 'configs/contacts_mysql_ssl_ca.pem';
// $sSslCa = (\is_file($sFile) || \file_put_contents($sFile, $sSslCa)) ? $sFile : ''; // $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, return $this->settings;
$sSslCa, $this->bSslVerify, $this->sSslCiphers
);
} }
protected function getUserId(string $sEmail, bool $bSkipInsert = false, bool $bCache = true) : int protected function getUserId(string $sEmail, bool $bSkipInsert = false, bool $bCache = true) : int