Commit 77eba1a2 authored by Carsten  Rose's avatar Carsten Rose
Browse files

Store.php: Removed caching functionality - the speed advantage is too few...

Store.php: Removed caching functionality - the speed advantage is too few against code complexity. Introduced sanatizeStore - Only STORE_CLIENT and STORE_FORM are still sanatized. All others will be trusted. This reduces overhead by accessing variables.
New function 'fillStoreTableDefinition'. Rewrote and simplify class getVar(). New function getStore() - at the moment used for debugging.
parent de5ca086
......@@ -15,6 +15,7 @@ require_once(__DIR__ . '/../../qfq/helper/KeyValueStringParser.php');
require_once(__DIR__ . '/../../qfq/helper/Sanatize.php');
require_once(__DIR__ . '/../../qfq/Constants.php');
require_once(__DIR__ . '/../../qfq/store/Sip.php');
require_once(__DIR__ . '/../../qfq/Database.php');
/*
......@@ -33,21 +34,21 @@ class Store {
private static $sip = null;
private static $raw = array();
private static $cache = array();
private static $defaultSanatizeClass = array();
private static $sanatizeClass = array();
private static $sanatizeStore = array();
/**
* @param string $bodytext
*/
private function __construct($bodytext = '') {
self::$defaultSanatizeClass = [
TYPO3_DEBUG_LOAD => SANATIZE_ALLOW_DIGIT,
TYPO3_DEBUG_SAVE => SANATIZE_ALLOW_DIGIT,
TYPO3_FORM => SANATIZE_ALLOW_ALNUMX,
TYPO3_FE_USER => SANATIZE_ALLOW_ALNUMX,
TYPO3_FE_USER_UID => SANATIZE_ALLOW_DIGIT,
TYPO3_FE_USER_GROUP => SANATIZE_ALLOW_ALNUMX,
self::$sanatizeClass = [
// TYPO3_DEBUG_LOAD => SANATIZE_ALLOW_DIGIT,
// TYPO3_DEBUG_SAVE => SANATIZE_ALLOW_DIGIT,
// TYPO3_FORM => SANATIZE_ALLOW_ALNUMX,
// TYPO3_FE_USER => SANATIZE_ALLOW_ALNUMX,
// TYPO3_FE_USER_UID => SANATIZE_ALLOW_DIGIT,
// TYPO3_FE_USER_GROUP => SANATIZE_ALLOW_ALNUMX,
CLIENT_SIP => SANATIZE_ALLOW_ALNUMX,
CLIENT_GET_SIP => SANATIZE_ALLOW_DIGIT,
......@@ -76,34 +77,42 @@ class Store {
CLIENT_SCRIPT_NAME => SANATIZE_ALLOW_ALNUMX,
CLIENT_PHP_SELF => SANATIZE_ALLOW_ALNUMX,
SYSTEM_DBUSER => SANATIZE_ALLOW_ALNUMX,
SYSTEM_DBSERVER => SANATIZE_ALLOW_ALNUMX,
SYSTEM_DBPW => SANATIZE_ALLOW_ALL,
SYSTEM_DB => SANATIZE_ALLOW_ALNUMX,
SYSTEM_TESTDB => SANATIZE_ALLOW_ALNUMX,
SYSTEM_SESSIONNAME => SANATIZE_ALLOW_ALNUMX,
SYSTEM_DBH => SANATIZE_ALLOW_ALL,
// SYSTEM_FORM_DEF => SANATIZE_ALL,
// SYSTEM_FORM_ELEMENT_DEF => SANATIZE_ALL,
// SYSTEM_FORM_ELEMENT_FIELD => SANATIZE_ALL,
SYSTEM_SQL_RAW => SANATIZE_ALLOW_ALL,
SYSTEM_SQL_FINAL => SANATIZE_ALLOW_ALL,
SYSTEM_SQL_COUNT => SANATIZE_ALLOW_DIGIT,
SYSTEM_SQL_PARAM_ARRAY => SANATIZE_ALLOW_ALL,
SIP_SIP => SANATIZE_ALLOW_ALNUMX,
SIP_RECORD_ID => SANATIZE_ALLOW_DIGIT,
SIP_FORM => SANATIZE_ALLOW_ALNUMX,
SIP_URLPARAM => SANATIZE_ALLOW_ALL
// SYSTEM_DBUSER => SANATIZE_ALLOW_ALNUMX,
// SYSTEM_DBSERVER => SANATIZE_ALLOW_ALNUMX,
// SYSTEM_DBPW => SANATIZE_ALLOW_ALL,
// SYSTEM_DB => SANATIZE_ALLOW_ALNUMX,
// SYSTEM_TESTDB => SANATIZE_ALLOW_ALNUMX,
// SYSTEM_SESSIONNAME => SANATIZE_ALLOW_ALNUMX,
// SYSTEM_DBH => SANATIZE_ALLOW_ALL,
// SYSTEM_SQL_RAW => SANATIZE_ALLOW_ALL,
// SYSTEM_SQL_FINAL => SANATIZE_ALLOW_ALL,
// SYSTEM_SQL_COUNT => SANATIZE_ALLOW_DIGIT,
// SYSTEM_SQL_PARAM_ARRAY => SANATIZE_ALLOW_ALL,
// SIP_SIP => SANATIZE_ALLOW_ALNUMX,
// SIP_RECORD_ID => SANATIZE_ALLOW_DIGIT,
// SIP_FORM => SANATIZE_ALLOW_ALNUMX,
// SIP_URLPARAM => SANATIZE_ALLOW_ALL
];
self::$sanatizeStore = [
STORE_FORM => true,
STORE_SIP => false,
STORE_RECORD => false,
STORE_PARENT_RECORD => false,
STORE_TABLE_DEFINITION => false,
STORE_CLIENT => true,
STORE_TYPO3 => false,
STORE_ZERO => false,
STORE_SYSTEM => false
];
self::fillSystemStore();
self::fillStoreTypo3($bodytext);
self::fillStoreClient();
self::fillStoreSip();
}
/**
......@@ -154,6 +163,10 @@ class Store {
self::setVarArray($arr, STORE_TYPO3);
}
/*
*
*/
/**
* @throws CodeException
*/
......@@ -171,9 +184,6 @@ class Store {
self::setVarArray($arr, STORE_CLIENT);
}
/*
*
*/
private function fillStoreSip() {
$sessionName = self::getVar(SYSTEM_SESSIONNAME, STORE_SYSTEM);
......@@ -206,39 +216,29 @@ class Store {
$useStores = USE_STORE_DEFAULT;
}
// no sanatizeClass specified: take last/default
if ($sanatizeClass === '') {
$sanatizeClass = isset(self::$sanatizeClass[$key]) ? self::$sanatizeClass[$key] : SANATIZE_DEFAULT;
}
while ($useStores !== false) {
$store = substr($useStores, 0, 1); // next store
$useStores = substr($useStores, 1); // shift left remaining stores
$cacheVal = isset(self::$cache[$store][$key]) ? self::$cache[$store][$key] : null;
$rawVal = isset(self::$raw[$store][$key]) ? self::$raw[$store][$key] : null;
// Already cached?
if (isset($cacheVal) && $cacheVal !== false)
return $cacheVal;
// No value in store: try next store
if ($cacheVal === false) {
continue;
if (!isset(self::$raw[$store][$key])) {
continue; // no value provided
}
// No value in raw? set cache to 'nothing found' and try next store
if (!isset($rawVal)) {
self::$cache[$store][$key] = false;
continue;
}
$rawVal = isset(self::$raw[$store][$key]) ? self::$raw[$store][$key] : null;
// get final sanatizeClass
if ($sanatizeClass === '' || $sanatizeClass === null) {
$sanatizeClass = isset(self::$defaultSanatizeClass[$key]) ? self::$defaultSanatizeClass[$key] : SANATIZE_DEFAULT;
if (self::$sanatizeStore[$store] && $sanatizeClass != '') {
return \qfq\Sanatize::sanatize($rawVal, $sanatizeClass);
} else {
return $rawVal;
}
// sanatize
self::$cache[$store][$key] = \qfq\Sanatize::sanatize($rawVal, $sanatizeClass);
return self::$cache[$store][$key];
}
return false;
}
......@@ -303,10 +303,29 @@ class Store {
public static function setVar($key, $value, $store) {
self::$raw[$store][$key] = $value;
// Invalidate cache to force new sanatize
self::$cache[$store][$key] = null;
}
/**
* @param $store
* @return mixed
*/
public static function getStore($store) {
return self::$raw[$store];
}
/**
* @param $tableName
* @throws CodeException
*/
public function fillStoreTableDefinition($tableName) {
$db = new qfq\Database();
$tableDefinition = $db->getTableDefinition($tableName);
self::setVarArray(array_column($tableDefinition, 'Default', 'Field'), STORE_TABLE_DEFINITION);
}
}
......
......@@ -49,7 +49,7 @@ class StoreTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals($a, $b, "Both classes should be the same");
}
public function testGetVarT3Bodytext() {
public function testGetVarStoreTypo3() {
// T3 Bodytext
$this->store->setVar(TYPO3_FORM, "testformnameDoNotChange", STORE_TYPO3);
$this->assertEquals('testformnameDoNotChange', $this->store->getVar(TYPO3_FORM, STORE_TYPO3), "System: " . TYPO3_FORM);
......@@ -64,14 +64,14 @@ class StoreTest extends \PHPUnit_Framework_TestCase {
}
public function testGetVarSystem() {
public function testGetVarStoreSystem() {
// DBUSER in qfq.ini
$dbuser = $this->store->getVar(SYSTEM_DBUSER, STORE_SYSTEM);
$this->assertTrue($dbuser !== false && $dbuser !== '', "System: DBUSER found in qfq.ini");
}
public function testSetVarSystem() {
public function testSetVarStoreSystem() {
// Sessionname: default value
$this->assertEquals('qfq', $this->store->getVar(SYSTEM_SESSIONNAME, STORE_SYSTEM), "System: SESSIONNAME");
......@@ -84,7 +84,7 @@ class StoreTest extends \PHPUnit_Framework_TestCase {
}
public function testGetVarClient() {
public function testGetVarStoreClient() {
//TODO: CR: phpunit running _all_ tests, fails with this tests always. Found no way to debug the situation.
return;
......@@ -124,6 +124,31 @@ return;
public function testStorePriority() {
//TODO: tbd
//default prio FSRD
$this->store->fillStoreTableDefinition('Person');
$this->assertEquals('male', $this->store->getVar('gender'), "Get default definition from table person.gender");
$this->store->setVar('gender', 'female', STORE_RECORD);
$this->assertEquals('female', $this->store->getVar('gender'), "Retrieve 'gender' from STORE_RECORD");
$this->store->setVar('gender', 'male2', STORE_SIP);
$this->assertEquals('male2', $this->store->getVar('gender'), "Retrieve 'gender' from STORE_SIP");
$this->store->setVar('gender', 'female2', STORE_FORM);
$this->assertEquals('female2', $this->store->getVar('gender', '', SANATIZE_ALLOW_ALNUMX), "Retrieve 'gender' from STORE_SIP");
}
public function testStoreDifferentSanatizeClass() {
//default prio FSRD
$this->store->setVar('gender', 'female', STORE_RECORD);
$this->assertEquals('female', $this->store->getVar('gender'), "Retrieve 'gender' from STORE_RECORD");
$this->store->setVar('gender', 'female2', STORE_FORM);
$this->assertEquals(false, $this->store->getVar('gender'), "Retrieve 'gender' from STORE_SIP");
$this->assertEquals('female2', $this->store->getVar('gender', '', SANATIZE_ALLOW_ALNUMX), "Retrieve 'gender' from STORE_SIP");
}
}
Supports Markdown
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