Commit c19bc30a authored by Carsten  Rose's avatar Carsten Rose

WIP: separate class DB from class STORE

parent 0bd821dd
Pipeline #3658 failed with stages
in 3 minutes and 31 seconds
......@@ -27,6 +27,7 @@ class Database {
*/
private $store = null;
/**
* @var \mysqli
*/
......@@ -54,30 +55,38 @@ class Database {
private $dbName = '';
private $storeSystem = array();
/**
* Returns current data base handle from Store[System][SYSTEM_DBH].
* If not exists: open database and store the new dbh in Store[System][SYSTEM_DBH]
*
* @param string $dbIndex Typically '1' for Data, optional 2 for external Form/FormElement
*
* @param array $storeSystem
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
* @throws \UserReportException
*/
public function __construct($dbIndex = DB_INDEX_DEFAULT) {
public function __construct($dbIndex = DB_INDEX_DEFAULT, $storeSystem = array()) {
if (empty($dbIndex)) {
$dbIndex = DB_INDEX_DEFAULT;
}
$this->store = Store::getInstance();
$storeSystem = $this->store->getStore(STORE_SYSTEM);
// On early bootstrap there is no STORE.
if (empty($storeSystem)) {
$this->store = Store::getInstance();
$this->storeSystem = $this->store->getStore(STORE_SYSTEM);
} else {
$this->storeSystem = $storeSystem;
}
$this->sqlLog = $storeSystem[SYSTEM_SQL_LOG];
$this->sqlLog = $this->storeSystem[SYSTEM_SQL_LOG];
$dbInit = $storeSystem[SYSTEM_DB_INIT];
$config = $this->getConnectionDetails($dbIndex, $storeSystem);
$config = $this->getConnectionDetails($dbIndex, $this->storeSystem);
$this->dbName = $config[SYSTEM_DB_NAME];
if ($this->mysqli === null) {
......@@ -87,7 +96,7 @@ class Database {
// DB Init
if ($dbInit !== false && $dbInit != '') {
$arr = explode(';', $dbInit);
foreach ($arr AS $sql) {
foreach ($arr as $sql) {
$sql = trim($sql);
if ('' != $sql) {
$this->sql($sql);
......@@ -262,9 +271,11 @@ class Database {
$this->closeMysqliStmt();
$this->store->setVar(SYSTEM_SQL_RAW, '', STORE_SYSTEM);
$this->store->setVar(SYSTEM_SQL_FINAL, '', STORE_SYSTEM);
$this->store->setVar(SYSTEM_SQL_PARAM_ARRAY, '', STORE_SYSTEM);
if ($this->store !== null) {
$this->store->setVar(SYSTEM_SQL_RAW, '', STORE_SYSTEM);
$this->store->setVar(SYSTEM_SQL_FINAL, '', STORE_SYSTEM);
$this->store->setVar(SYSTEM_SQL_PARAM_ARRAY, '', STORE_SYSTEM);
}
return $result;
}
......@@ -537,7 +548,8 @@ class Database {
$status = '';
// If no sqlLogMode is defined/available, choose SQL_LOG_MODE_ERROR
$sqlLogMode = $this->store->getVar(SYSTEM_SQL_LOG_MODE, STORE_SYSTEM);
$sqlLogMode = $this->storeSystem[SYSTEM_SQL_LOG_MODE];
if ($sqlLogMode === false) {
$sqlLogMode = SQL_LOG_MODE_ERROR;
}
......@@ -554,27 +566,31 @@ class Database {
return;
}
// Client IP Address
$remoteAddress = ($this->store === null) ? '0.0.0.0' : $this->store->getVar(CLIENT_REMOTE_ADDRESS, STORE_CLIENT);
$logArr = [
['FE', TYPO3_FE_USER, STORE_TYPO3],
['FESU', TYPO3_FE_USER, STORE_USER],
['Page', TYPO3_PAGE_ID, STORE_TYPO3],
['tt', TYPO3_TT_CONTENT_UID, STORE_TYPO3],
['level', SYSTEM_REPORT_FULL_LEVEL, STORE_SYSTEM],
['form', SIP_FORM, STORE_SIP],
];
$t3msg = '';
foreach ($logArr as $logItem) {
$value = $this->store->getVar($logItem[1], $logItem[2]);
if (!empty($value)) {
$t3msg .= $logItem[0] . ":" . $value . ",";
if ($this->store === null) {
$remoteAddress = '0.0.0.0';
} else {
// Client IP Address
$remoteAddress = $this->store->getVar(CLIENT_REMOTE_ADDRESS, STORE_CLIENT);
$logArr = [
['FE', TYPO3_FE_USER, STORE_TYPO3],
['FESU', TYPO3_FE_USER, STORE_USER],
['Page', TYPO3_PAGE_ID, STORE_TYPO3],
['tt', TYPO3_TT_CONTENT_UID, STORE_TYPO3],
['level', SYSTEM_REPORT_FULL_LEVEL, STORE_SYSTEM],
['form', SIP_FORM, STORE_SIP],
];
foreach ($logArr as $logItem) {
$value = $this->store->getVar($logItem[1], $logItem[2]);
if (!empty($value)) {
$t3msg .= $logItem[0] . ":" . $value . ",";
}
}
$t3msg = substr($t3msg, 0, strlen($t3msg) - 1);
}
$t3msg = substr($t3msg, 0, strlen($t3msg) - 1);
$msg = '[' . date('Y.m.d H:i:s O') . '][' . $remoteAddress . '][' . $t3msg . ']';
......@@ -752,7 +768,7 @@ class Database {
private function getFieldDefinitionFromTable($table, $columnName) {
$tableDefinition = $this->getTableDefinition($table);
foreach ($tableDefinition AS $row) {
foreach ($tableDefinition as $row) {
if ($row["Field"] == $columnName) {
return $row;
}
......@@ -853,7 +869,7 @@ class Database {
if (!$this->existTable($table)) {
$sql = "CREATE TABLE $table (";
foreach ($columnDefinition AS $key => $value) {
foreach ($columnDefinition as $key => $value) {
$cols[] = "`" . $key . "` " . $value . " NOT NULL,";
}
$sql .= implode(',', $cols);
......@@ -1044,7 +1060,7 @@ class Database {
* @throws \UserFormException
*/
public function getQfqLogFile() {
return ($this->store == null) ? SYSTEM_QFQ_LOG_FILE : $this->store->getVar(SYSTEM_QFQ_LOG, STORE_SYSTEM);
return ($this->store == null) ? SYSTEM_QFQ_LOG_FILE : $this->storeSystem[SYSTEM_QFQ_LOG];
}
/**
......@@ -1061,7 +1077,7 @@ class Database {
$sql = 'SELECT `pathFileName` FROM `' . TABLE_NAME_SPLIT . '` WHERE `tableName`=? AND `xId`=?';
$data = $this->sql($sql, ROW_REGULAR, [$tableName, $xId]);
foreach ($data AS $row) {
foreach ($data as $row) {
if (!empty($row['pathFileName']) && is_writable($row['pathFileName'])) {
HelperFile::unlink($row['pathFileName']);
}
......
......@@ -20,6 +20,8 @@ class Client {
/**
* @return array|string
* @throws \CodeException
* @throws \UserFormException
* @throws \UserReportException
*/
public static function getParam() {
......
......@@ -21,6 +21,7 @@ class Sip {
private $phpUnit = false;
private $staticUniqId = false;
private $storeSystem = array();
/**
* @var SipPersistent
......@@ -29,8 +30,13 @@ class Sip {
/**
* @param bool|false $phpUnit
* @param array $storeSystem
*/
public function __construct($phpUnit = false) {
public function __construct($phpUnit = false, $storeSystem = array()) {
if (!empty($storeSystem)) {
$this->storeSystem = $storeSystem;
}
#TODO: rewrite $phpUnit to: "if (!defined('PHPUNIT_QFQ')) {...}"
$this->phpUnit = $phpUnit;
......@@ -55,6 +61,7 @@ class Sip {
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
* @throws \UserReportException
*/
public function queryStringToSip($queryString, $mode = RETURN_URL, $phpScriptName = INDEX_PHP, $sipExpire = '') {
......@@ -92,7 +99,7 @@ class Sip {
} else {
if ($this->SipPersistent === null) {
//TODO #10819 Make 'new SipPersistent()' MultiDB aware
$this->SipPersistent = new SipPersistent();
$this->SipPersistent = new SipPersistent(DB_INDEX_DEFAULT, $this->storeSystem);
}
if (false === ($sessionParamSip = $this->SipPersistent->get($sipParamString))) {
$s = $this->SipPersistent->set($sipParamString, $sipExpire);
......@@ -364,9 +371,9 @@ class Sip {
case SIP_P_TOKEN_LENGTH:
if ($this->SipPersistent === null) {
//TODO #10819 Make 'new SipPersistent()' MultiDB aware
$this->SipPersistent = new SipPersistent();
$this->SipPersistent = new SipPersistent(DB_INDEX_DEFAULT, $this->storeSystem);
}
$sessionVar = $this->SipPersistent->get($s);
$sessionVar = $this->SipPersistent->getQueryStringFromSip($s);
break;
default:
Config::attackDetectedExitNow(array(), 'Invalid SIP token length: ' . strlen($s) . " _GET['s']=" . htmlentities($s));
......@@ -434,7 +441,7 @@ class Sip {
} else {
if ($this->SipPersistent === null) {
//TODO #10819 Make 'new SipPersistent()' MultiDB aware
$this->SipPersistent = new SipPersistent();
$this->SipPersistent = new SipPersistent(DB_INDEX_DEFAULT, $this->storeSystem);
}
return $this->SipPersistent->getQueryStringFromSip($s);
}
......
......@@ -2,8 +2,6 @@
namespace IMATHUZH\Qfq\Core\Store;
use IMATHUZH\Qfq\Core\Helper\KeyValueStringParser;
use IMATHUZH\Qfq\Core\Helper\OnArray;
use IMATHUZH\Qfq\Core\Database\Database;
/**
......@@ -19,17 +17,18 @@ class SipPersistent {
/**
* PersistentSip constructor.
* @param $dbIndexQfq
* @param string $dbIndexQfq
* @param $storeSystem
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
* @throws \UserReportException
*/
public function __construct($dbIndexQfq = DB_INDEX_DEFAULT) {
public function __construct($dbIndexQfq = DB_INDEX_DEFAULT, $storeSystem) {
if ($dbIndexQfq === false) {
throw new \CodeException("Missing dbIndexQfq for SipPersistent() initialization.", ERROR_DB_MISSING_INDEX_QFQ);
}
$this->db = new Database($dbIndexQfq);
$this->db = new Database($dbIndexQfq, $storeSystem);
}
/**
......
......@@ -80,6 +80,7 @@ class Store {
* @param string $fileConfigPhp
*
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
* @throws \UserReportException
*/
......@@ -173,7 +174,7 @@ class Store {
self::fillStorePhpSession(STORE_EXTRA); // should be filled before fillStoreSystem() to restore variables like SYSTEM_SHOW_DEBUG_INFO
self::fillStorePhpSession(STORE_USER); // should be filled before fillStoreSystem() to restore variables like SYSTEM_SHOW_DEBUG_INFO
self::fillStoreSystem($fileConfigPhp);
self::fillStoreSip();
self::fillStoreSip(false);
}
/**
......@@ -462,6 +463,7 @@ class Store {
* @throws \CodeException
* @throws \UserFormException
* @throws \UserReportException
*
*/
private static function fillStoreSystem($fileConfigPhp = '') {
......@@ -538,14 +540,16 @@ class Store {
* Fills the STORE_SIP. Reads therefore specified SIP, decode the values and stores them in STORE_SIP.
*
* @param bool|string $s
* @param array $storeSystem
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
* @throws \UserReportException
*/
public static function fillStoreSip($s = false) {
public static function fillStoreSip($s = false, $storeSystem = array()) {
if (self::$sip === null) {
self::$sip = new Sip(self::$phpUnit);
self::$sip = new Sip(self::$phpUnit, self::$raw[STORE_SYSTEM]);
}
if ($s === false) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment