Commit 119fdfd7 authored by Carsten  Rose's avatar Carsten Rose
Browse files

Store.php: Added SIP Store. Rearranged code.

parent 1817fa1b
...@@ -11,10 +11,10 @@ namespace qfq\store; ...@@ -11,10 +11,10 @@ namespace qfq\store;
use qfq\exceptions\CodeException; use qfq\exceptions\CodeException;
use qfq; use qfq;
require_once(__DIR__ . '/../../qfq/helper/KeyValueStringParser.php'); require_once(__DIR__ . '/../../qfq/helper/KeyValueStringParser.php');
require_once(__DIR__ . '/../../qfq/helper/Sanatize.php'); require_once(__DIR__ . '/../../qfq/helper/Sanatize.php');
require_once(__DIR__ . '/../../qfq/Constants.php'); require_once(__DIR__ . '/../../qfq/Constants.php');
require_once(__DIR__ . '/../../qfq/store/Sip.php');
/* /*
...@@ -30,51 +30,61 @@ class Store { ...@@ -30,51 +30,61 @@ class Store {
private static $instance = null; private static $instance = null;
private static $sip = null;
private static $raw = array(); private static $raw = array();
private static $cache = array(); private static $cache = array();
private static $defaultSanatizeClass = array(); private static $defaultSanatizeClass = array();
/** /**
* Design Pattern: Singleton - class 'store' should only be instanced once. * @param string $bodytext
*/ */
private function __construct($bodytext = '') { private function __construct($bodytext = '') {
self::$defaultSanatizeClass = [ self::$defaultSanatizeClass = [
T3_BODYTEXT_DEBUG_LOAD => SANATIZE_DIGIT, T3_BODYTEXT_DEBUG_LOAD => SANATIZE_ALLOW_DIGIT,
T3_BODYTEXT_DEBUG_SAVE => SANATIZE_DIGIT, T3_BODYTEXT_DEBUG_SAVE => SANATIZE_ALLOW_DIGIT,
T3_BODYTEXT_FORM => SANATIZE_ALLOW_ALNUMX,
CLIENT_SIP => SANATIZE_DIGIT,
CLIENT_GET_SIP => SANATIZE_DIGIT, CLIENT_SIP => SANATIZE_ALLOW_ALNUMX,
CLIENT_POST_SIP => SANATIZE_DIGIT, CLIENT_GET_SIP => SANATIZE_ALLOW_DIGIT,
CLIENT_RECORD_ID => SANATIZE_DIGIT, CLIENT_POST_SIP => SANATIZE_ALLOW_DIGIT,
CLIENT_KEY_SEM_ID => SANATIZE_DIGIT, CLIENT_RECORD_ID => SANATIZE_ALLOW_DIGIT,
CLIENT_KEY_SEM_ID_USER => SANATIZE_DIGIT, CLIENT_KEY_SEM_ID => SANATIZE_ALLOW_DIGIT,
CLIENT_PAGE_ID => SANATIZE_DIGIT, CLIENT_KEY_SEM_ID_USER => SANATIZE_ALLOW_DIGIT,
CLIENT_PAGE_TYPE => SANATIZE_DIGIT, CLIENT_PAGE_ID => SANATIZE_ALLOW_DIGIT,
CLIENT_PAGE_LANGUAGE => SANATIZE_DIGIT, CLIENT_PAGE_TYPE => SANATIZE_ALLOW_DIGIT,
CLIENT_FORM => SANATIZE_ALNUMX, CLIENT_PAGE_LANGUAGE => SANATIZE_ALLOW_DIGIT,
CLIENT_FORM => SANATIZE_ALLOW_ALNUMX,
SYSTEM_DBUSER => SANATIZE_ALNUMX,
SYSTEM_DBSERVER => SANATIZE_ALNUMX, SYSTEM_DBUSER => SANATIZE_ALLOW_ALNUMX,
SYSTEM_DBPW => SANATIZE_ALL, SYSTEM_DBSERVER => SANATIZE_ALLOW_ALNUMX,
SYSTEM_DB => SANATIZE_ALNUMX, SYSTEM_DBPW => SANATIZE_ALLOW_ALL,
SYSTEM_TESTDB => SANATIZE_ALNUMX, SYSTEM_DB => SANATIZE_ALLOW_ALNUMX,
SYSTEM_SESSIONNAME => SANATIZE_ALNUMX, SYSTEM_TESTDB => SANATIZE_ALLOW_ALNUMX,
SYSTEM_DBH => SANATIZE_ALL, SYSTEM_SESSIONNAME => SANATIZE_ALLOW_ALNUMX,
SYSTEM_DBH => SANATIZE_ALLOW_ALL,
// SYSTEM_FORM_DEF => SANATIZE_ALL, // SYSTEM_FORM_DEF => SANATIZE_ALL,
// SYSTEM_FORM_ELEMENT_DEF => SANATIZE_ALL, // SYSTEM_FORM_ELEMENT_DEF => SANATIZE_ALL,
// SYSTEM_FORM_ELEMENT_FIELD => SANATIZE_ALL, // SYSTEM_FORM_ELEMENT_FIELD => SANATIZE_ALL,
SYSTEM_SQL_RAW => SANATIZE_ALL, SYSTEM_SQL_RAW => SANATIZE_ALLOW_ALL,
SYSTEM_SQL_FINAL => SANATIZE_ALL, SYSTEM_SQL_FINAL => SANATIZE_ALLOW_ALL,
SYSTEM_SQL_COUNT => SANATIZE_DIGIT, SYSTEM_SQL_COUNT => SANATIZE_ALLOW_DIGIT,
SYSTEM_SQL_PARAM_ARRAY => SANATIZE_ALL 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::fillSystemStore(); self::fillSystemStore();
self::fillStoreT3Bodytext($bodytext); self::fillStoreT3Bodytext($bodytext);
self::fillStoreClient(); self::fillStoreClient();
self::fillStoreSip();
} }
/** /**
...@@ -109,9 +119,9 @@ class Store { ...@@ -109,9 +119,9 @@ class Store {
* @throws CodeException * @throws CodeException
*/ */
private function fillStoreT3Bodytext($bodytext) { private function fillStoreT3Bodytext($bodytext) {
$keyValue = new \qfq\helper\KeyValueStringParser("=", "\n"); $parser = new \qfq\helper\KeyValueStringParser("=", "\n");
self::setVarArray($keyValue->parse($bodytext), STORE_T3_BODYTEXT); self::setVarArray($parser->parse($bodytext), STORE_T3_BODYTEXT);
} }
/** /**
...@@ -128,10 +138,16 @@ class Store { ...@@ -128,10 +138,16 @@ class Store {
# SIP: if POST exist, this means 'form save'. To distinguish load and save, store both. # SIP: if POST exist, this means 'form save'. To distinguish load and save, store both.
if (isset($_GET[CLIENT_SIP])) if (isset($_GET[CLIENT_SIP]))
self::setVar(CLIENT_GET_SIP, $_GET[CLIENT_SIP], STORE_CLIENT); self::setVar(CLIENT_GET_SIP, $_GET[CLIENT_SIP], STORE_CLIENT);
if (isset($_POST[CLIENT_SIP])) if (isset($_POST[CLIENT_SIP]))
self::setVar(CLIENT_POST_SIP, $_GET[CLIENT_SIP], STORE_CLIENT); self::setVar(CLIENT_POST_SIP, $_GET[CLIENT_SIP], STORE_CLIENT);
} }
/*
*
*/
/** /**
* @param $key * @param $key
* @param $value * @param $value
...@@ -146,24 +162,19 @@ class Store { ...@@ -146,24 +162,19 @@ class Store {
} }
/** private function fillStoreSip() {
*
* @return Store
*/
public static function getInstance($bodytext = '', $phpunit = false) {
if ($phpunit) { $sessionName = self::getVar(SYSTEM_SESSIONNAME, STORE_SYSTEM);
if (self::$instance === null) { self::$sip = new \qfq\store\Sip($sessionName);
unset(self::$instance);
self::$instance = null;
}
}
// Design Pattern: Singleton $s = self::getVar(CLIENT_SIP, STORE_CLIENT);
if (self::$instance === null) { if ($s !== false) {
self::$instance = new Store($bodytext); // if session is given, copy values to store
$param = self::$sip->getVarsFromSip($s);
$parser = new \qfq\helper\KeyValueStringParser("=", "&");
self::setVarArray($parser->parse($param), STORE_SIP);
} }
return self::$instance;
} }
/** /**
...@@ -176,7 +187,7 @@ class Store { ...@@ -176,7 +187,7 @@ class Store {
* @param string $sanatizeClass * @param string $sanatizeClass
* @return string a) if found: value, b) false * @return string a) if found: value, b) false
*/ */
public function getVar($key, $useStores = USE_STORE_DEFAULT, $sanatizeClass = '') { public static function getVar($key, $useStores = USE_STORE_DEFAULT, $sanatizeClass = '') {
// no store specifed? // no store specifed?
if ($useStores === "") { if ($useStores === "") {
...@@ -217,9 +228,59 @@ class Store { ...@@ -217,9 +228,59 @@ class Store {
} }
return false; return false;
}
/**
* @param string $bodytext
* @param bool|false $phpunit
* @return null|\qfq\store\Store
*/
public static function getInstance($bodytext = '', $phpunit = false) {
if ($phpunit) {
if (self::$instance === null) {
// unset(self::$instance);
self::$instance = null;
}
} }
// Design Pattern: Singleton
if (self::$instance === null) {
self::$instance = new self($bodytext);
}
return self::$instance;
}
/**
* @param $formName
* @throws CodeException
*/
public static function createFakeSipAfterFormLoad($formName) {
$recordId = self::getVar(CLIENT_RECORD_ID, STORE_T3_BODYTEXT . STORE_CLIENT);
if ($recordId === false) {
$recordId = 0;
}
$tmpParam = [SIP_RECORD_ID => $recordId, SIP_FORM => $formName];
// Store in SIP Store (cause it's empty until now).
self::setVarArray($tmpParam, STORE_SIP);
// Construct fake urlparam
$tmpUrlparam = \qfq\helper\OnArray::toString($tmpParam);
// Create a fake SIP which has never been passed by URL - further processing might expect this to exist.
$sip = self::getSip()->urlparamToSip($tmpUrlparam, RETURN_SIP);
self::setVar(CLIENT_SIP, $sip, STORE_CLIENT);
}
/**
* @return null
*/
public static function getSip() {
return self::$sip;
}
} }
......
...@@ -23,11 +23,12 @@ class StoreTest extends \PHPUnit_Framework_TestCase { ...@@ -23,11 +23,12 @@ class StoreTest extends \PHPUnit_Framework_TestCase {
* @var Store * @var Store
*/ */
private $store = null; private $store = null;
private $setUpDone = false;
public function setUp() { public function setUp() {
// Client Variables has to setup before the first instantiation of 'Store' // Client Variables has to setup before the first instantiation of 'Store'
$_GET[CLIENT_RECORD_ID] = '1234'; $_GET[CLIENT_RECORD_ID] = '1234';
$_GET[CLIENT_SIP] = '1234abcd'; // $_GET[CLIENT_SIP] = '12badcaffee34';
$_GET['key01'] = '1234'; $_GET['key01'] = '1234';
$_POST['key02'] = '2345'; $_POST['key02'] = '2345';
$_POST['key03'] = '3456'; $_POST['key03'] = '3456';
...@@ -35,13 +36,13 @@ class StoreTest extends \PHPUnit_Framework_TestCase { ...@@ -35,13 +36,13 @@ class StoreTest extends \PHPUnit_Framework_TestCase {
$_GET['key04'] = '4567'; $_GET['key04'] = '4567';
$_POST['key04'] = '5678'; $_POST['key04'] = '5678';
$this->store = Store::getInstance("\n; some comment\n" . T3_BODYTEXT_FORM . "=testformnameDoNotChange\n" . T3_BODYTEXT_DEBUG_SAVE . "=6\n" . T3_BODYTEXT_DEBUG_LOAD . "=7\n"); $this->store = Store::getInstance("\n; some comment\n" . T3_BODYTEXT_FORM . "=testformnameDoNotChange\n" . T3_BODYTEXT_DEBUG_SAVE . "=6\n" . T3_BODYTEXT_DEBUG_LOAD . "=7\n", true);
} }
public function testGetInstance() { public function testGetInstance() {
$a = Store::getInstance(); $a = Store::getInstance('', true);
$b = Store::getInstance(); $b = Store::getInstance('', true);
$this->assertFalse($a === null, "There should be a class"); $this->assertFalse($a === null, "There should be a class");
...@@ -115,7 +116,7 @@ return; ...@@ -115,7 +116,7 @@ return;
$this->assertFalse($this->store->getVar('keyUnknown2', STORE_CLIENT), "Param: unknown from cache"); $this->assertFalse($this->store->getVar('keyUnknown2', STORE_CLIENT), "Param: unknown from cache");
// Test overwrite default sanatize class // Test overwrite default sanatize class
$this->assertEquals('', $this->store->getVar(CLIENT_FORM, STORE_CLIENT, SANATIZE_DIGIT), "Param: overwrite default sanatize class"); $this->assertEquals('', $this->store->getVar(CLIENT_FORM, STORE_CLIENT, SANATIZE_ALLOW_DIGIT), "Param: overwrite default sanatize class");
// Test: POST higher priority than GET // Test: POST higher priority than GET
$this->assertEquals('5678', $this->store->getVar('key04', STORE_CLIENT), "Param: POST higher priority than GET"); $this->assertEquals('5678', $this->store->getVar('key04', STORE_CLIENT), "Param: POST higher priority than GET");
......
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