Commit 666ddff1 authored by Carsten  Rose's avatar Carsten Rose
Browse files

Store*.php: Storage Classes unified to one class. Storage is now one array for...

Store*.php: Storage Classes unified to one class. Storage is now one array for all stores in class 'Store'
parent 4892d66a
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 1/3/16
* Time: 9:09 PM
*/
namespace qfq\store;
class AbstractStore {
private static $instance = null;
// $cache[key]: null: never looked for, false: previous looked for, but not found, else: value
private static $cache = array();
private static $raw = array();
/**
* Design Pattern: Singleton - class should only be instanced internally
*/
private function __construct() {
}
/**
* Design Pattern: Singleton - class should only be instanced once.
*
* @return Store
*/
public static function getInstance() {
// Design Pattern: Singleton
if (self::$instance == null) {
self::$instance = new self;
}
return self::$instance;
}
/**
* @param array $dataArray
*/
public static function setVarArray(array $dataArray) {
if (count(self::$raw) > 0) {
throw new CodeException("Raw value already been copied. Do this only one time.", ERROR_VALUE_ALREADY_CODPIED);
}
self::$raw = $dataArray;
}
/**
* @param array $dataArray
*/
public static function setVar($key, $value) {
if (isset(self::$raw[$key])) {
throw new CodeException("Raw value already been set. Do this only one time.", ERROR_VALUE_ALREADY_CODPIED);
}
self::$raw[$key] = $value;
}
/**
* Get var, sanatize it and store it in cache.
* Further access will be delivered by cache.
* @param string $key
* @param string $sanatizeClass
* @return string a) if not found: false, b) if found but SANATIZE failed: empty string, c) given value
* @throws CodeException
*/
public static function getVar($key, $sanatizeClass = '') {
// Already cached?
if (isset(self::$cache[$key]))
return self::$cache[$key];
// No value? break
if (!isset(self::$raw[$key])) {
self::$cache[$key] = false;
return self::$cache[$key];
}
if ($sanatizeClass === '') {
$sanatizeClass = isset(self::$defaultSanatizeClass[$key]) ? self::$defaultSanatizeClass[$key] : SANATIZE_DEFAULT;
}
// sanatize
self::$cache[$key] = Sanatize::sanatize(self::$raw[$key], $sanatizeClass);
return self::$cache[$key];
}
}
\ No newline at end of file
......@@ -11,12 +11,9 @@ namespace qfq\store;
use qfq\exceptions\CodeException;
use qfq;
//use qfq\store\StoreT3Bodytext;
require_once(__DIR__ . '/../../qfq/store/AbstractStore.php');
require_once(__DIR__ . '/../../qfq/store/StoreWebVar.php');
require_once(__DIR__ . '/../../qfq/store/StoreT3Bodytext.php');
require_once(__DIR__ . '/../../qfq/store/StoreSystem.php');
require_once(__DIR__ . '/../../qfq/helper/KeyValueStringParser.php');
require_once(__DIR__ . '/../../qfq/helper/Sanatize.php');
require_once(__DIR__ . '/../../qfq/Constants.php');
......@@ -33,110 +30,170 @@ class Store {
private static $instance = null;
// store Classes
public $StoreWebVar = null;
public $StoreT3Bodytext = null;
public $StoreSystem = null;
private static $raw = array();
private static $cache = array();
private static $defaultSanatizeClass = array();
/**
* Design Pattern: Singleton - class 'store' should only be instanced once.
*/
private function __construct() {
$this->StoreWebVar = StoreWebVar::getInstance();
$this->StoreT3Bodytext = StoreT3Bodytext::getInstance();
private function __construct($bodytext = '') {
self::$defaultSanatizeClass = [
T3_BODYTEXT_DEBUG_LOAD => SANATIZE_DIGIT,
T3_BODYTEXT_DEBUG_SAVE => SANATIZE_DIGIT,
CLIENT_SIP => SANATIZE_DIGIT,
CLIENT_RECORD_ID => SANATIZE_DIGIT,
CLIENT_KEY_SEM_ID => SANATIZE_DIGIT,
CLIENT_KEY_SEM_ID_USER => SANATIZE_DIGIT,
CLIENT_PAGE_ID => SANATIZE_DIGIT,
CLIENT_PAGE_TYPE => SANATIZE_DIGIT,
CLIENT_PAGE_LANGUAGE => SANATIZE_DIGIT,
CLIENT_FORM => SANATIZE_ALNUMX,
SYSTEM_DBUSER => SANATIZE_ALNUMX,
SYSTEM_DBSERVER => SANATIZE_ALNUMX,
SYSTEM_DBPW => SANATIZE_ALL,
SYSTEM_DB => SANATIZE_ALNUMX,
SYSTEM_TESTDB => SANATIZE_ALNUMX,
SYSTEM_SESSIONNAME => SANATIZE_ALNUMX
];
self::fillSystemStore();
self::fillStoreT3Bodytext($bodytext);
self::fillStoreClient();
}
$this->StoreSystem = StoreSystem::getInstance();
/**
* @throws CodeException
* @throws qfq\exceptions\UserException
*/
private function fillSystemStore() {
try {
$config = parse_ini_file(QFQ_INI, false);
} catch (Exception $e) {
} catch (\Exception $e) {
throw new qfq\exceptions\UserException ("Error read file " . QFQ_INI . ": " . $e->getMessage(), ERROR_READ_FILE);
}
$this->StoreSystem->setVarArray($config);
self::setVarArray($config, STORE_SYSTEM);
}
/**
* @param array $dataArray
* @param $store
* @throws CodeException
*/
public function setVarArray(array $dataArray, $store) {
switch ($store) {
if (isset(self::$raw[$store]) && count(self::$raw[$store]) > 0) {
throw new CodeException("Raw values already been copied to store '$store'. Do this only one time.", ERROR_VALUE_ALREADY_CODPIED);
}
case STORE_CLIENT:
throw new CodeException("setVarArray not implemented for store: $store", ERROR_SETVARARRAY_NOT_IMPLEMENTED);
break;
self::$raw[$store] = $dataArray;
}
case STORE_T3_BODYTEXT:
$this->StoreT3Bodytext->setVarArray($dataArray);
break;
/**
* @param $bodytext
* @throws CodeException
*/
private function fillStoreT3Bodytext($bodytext) {
$keyValue = new \qfq\helper\KeyValueStringParser("=", "\n");
case STORE_FORM:
case STORE_SIP:
case STORE_RECORD:
case STORE_TABLE_DEFINITION:
case STORE_ZERO:
throw new CodeException("store not implemented: $store", ERROR_STORE_NOT_IMPLEMENTED);
break;
self::setVarArray($keyValue->parse($bodytext), STORE_T3_BODYTEXT);
}
default:
throw new CodeException("Unknown store: $store", ERROR_UNKNOWN_STORE);
break;
/**
* @throws CodeException
*/
private function fillStoreClient() {
// copy GET and POST Parameter. POST Parameter have a higher priority.
$arr = $_GET;
foreach ($_POST as $key => $value) {
$arr[$key] = $value;
}
self::setVarArray($arr, STORE_CLIENT);
}
/**
*
* @return Store
*/
public static function getInstance() {
public static function getInstance($bodytext = '') {
// Design Pattern: Singleton
if (self::$instance == null) {
self::$instance = new Store;
self::$instance = new Store($bodytext);
}
return self::$instance;
}
/**
* @param $key
* @param $value
* @param $store
* @throws CodeException
*/
public static function setVar($key, $value, $store) {
if (isset(self::$raw[$store][$key])) {
throw new CodeException("Raw value already been copied to store '$store'. Do this only one time.", ERROR_VALUE_ALREADY_CODPIED);
}
self::$raw[$store][$key] = $value;
}
/**
* Cycles throught all stores in $useStore.
* First match will return the found value.
* During cycling:, fill cache and sanatize a raw value.
*
* @param string $key
* @param string $respectStores
* @param string $useStores f.e.: 'FSRD'
* @param string $sanatizeClass
* @return string a) if found: value, b) false
* @throws CodeException
*/
public function getVar($key, $respectStores = USE_STORE_DEFAULT, $sanatizeClass = '') {
$value = false;
// f.e.: $useStore='FSRD'
$ii = 0;
$store = substr($respectStores, $ii, 1);
// Iterate over all stores, to search for parameter $key
while ($store !== false) {
switch ($store) {
case STORE_CLIENT:
$value = $this->StoreWebVar->getVar($key, $sanatizeClass);
break;
case STORE_T3_BODYTEXT:
$value = $this->StoreT3Bodytext->getVar($key, $sanatizeClass);
break;
case STORE_FORM:
case STORE_SIP:
case STORE_RECORD:
case STORE_TABLE_DEFINITION:
case STORE_ZERO:
throw new CodeException("store not implemented: $store", ERROR_STORE_NOT_IMPLEMENTED);
break;
default:
throw new CodeException("Unknown store: $store", ERROR_UNKNOWN_STORE);
break;
public function getVar($key, $useStores = USE_STORE_DEFAULT, $sanatizeClass = '') {
// no store specifed?
if ($useStores === "") {
return false;
}
while ($useStores !== false) {
$store = substr($useStores, 0, 1); // next store
$useStores = substr($useStores, 1); // shift left remaining stores
$cache = isset(self::$cache[$store][$key]) ? self::$cache[$store][$key] : null;
$raw = isset(self::$raw[$store][$key]) ? self::$raw[$store][$key] : null;
// Already cached?
if (isset($cache) && $cache !== false)
return $cache;
// No value in store: try next store
if ($cache === false) {
continue;
}
if ($value !== false)
return $value;
// No value in raw? cache 'nothing found' and try next store
if (!isset($raw)) {
self::$cache[$store][$key] = false;
continue;
}
// get final sanatizeClass
if ($sanatizeClass === '') {
$sanatizeClass = isset(self::$defaultSanatizeClass[$key]) ? self::$defaultSanatizeClass[$key] : SANATIZE_DEFAULT;
}
// sanatize
self::$cache[$store][$key] = \qfq\Sanatize::sanatize($raw, $sanatizeClass);
return self::$cache[$store][$key];
$ii++;
$store = substr($respectStores, $ii, 1);
}
return false;
}
}
......
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 1/3/16
* Time: 9:20 PM
*/
namespace qfq\store;
class StoreSystem extends AbstractStore {
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 1/3/16
* Time: 12:52 PM
*/
namespace qfq\store;
use qfq\Sanatize;
use qfq\exceptions;
use qfq\exceptions\CodeException;
require_once(__DIR__ . '/../../qfq/Constants.php');
require_once(__DIR__ . '/../../qfq/Sanatize.php');
class StoreT3Bodytext {
private static $instance = null;
// $cache[key]: null: never looked for, false: previous looked for, but not found, else: value
private static $cache = array();
private static $raw = array();
private static $defaultSanatizeClass = array();
/**
* Design Pattern: Singleton - class should only be instanced internally
*
*/
private function __construct() {
self::$defaultSanatizeClass = [
T3_BODYTEXT_DEBUG_LOAD => SANATIZE_DIGIT,
T3_BODYTEXT_DEBUG_SAVE => SANATIZE_DIGIT,
T3_BODYTEXT_FORM => SANATIZE_ALNUMX
];
}
/**
* Design Pattern: Singleton - class should only be instanced once.
*
* @return StoreT3Bodytext
*/
public static function getInstance() {
// Design Pattern: Singleton
if (self::$instance == null) {
self::$instance = new StoreT3Bodytext;
}
return self::$instance;
}
/**
* @param array $dataArray
*/
public static function setVarArray(array $dataArray) {
if (count(self::$raw) > 0) {
throw new CodeException("Raw values already been copied. Do this only one time.", ERROR_VALUE_ALREADY_CODPIED);
}
self::$raw = $dataArray;
}
/**
* @param $key
* @param string $sanatizeClass
* @return mixed
* @throws exceptions\CodeException
*/
public static function getVar($key, $sanatizeClass = '') {
// Already cached?
if (isset(self::$cache[$key]))
return self::$cache[$key];
// No value? break
if (!isset(self::$raw[$key])) {
self::$cache[$key] = false;
return self::$cache[$key];
}
if ($sanatizeClass === '') {
$sanatizeClass = isset(self::$defaultSanatizeClass[$key]) ? self::$defaultSanatizeClass[$key] : SANATIZE_DEFAULT;
}
// sanatize
self::$cache[$key] = Sanatize::sanatize(self::$raw[$key], $sanatizeClass);
return self::$cache[$key];
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 1/1/16
* Time: 6:51 PM
*/
namespace qfq\store;
use qfq\Sanatize;
use qfq\exceptions;
use qfq\exceptions\CodeException;
require_once(__DIR__ . '/../../qfq/Constants.php');
require_once(__DIR__ . '/../../qfq/Sanatize.php');
class StoreWebVar {
private static $instance = null;
// $cache[key]: null: never looked for, false: previous looked for, but not found, else: value
private static $cache = array();
private static $defaultSanatizeClass = array();
/**
* Design Pattern: Singleton - class should only be instanced internally
*/
private function __construct() {
self::$defaultSanatizeClass = [
CLIENT_SIP => SANATIZE_DIGIT,
CLIENT_RECORD_ID => SANATIZE_DIGIT,
CLIENT_KEY_SEM_ID => SANATIZE_DIGIT,
CLIENT_KEY_SEM_ID_USER => SANATIZE_DIGIT,
CLIENT_PAGE_ID => SANATIZE_DIGIT,
CLIENT_PAGE_TYPE => SANATIZE_DIGIT,
CLIENT_PAGE_LANGUAGE => SANATIZE_DIGIT,
CLIENT_FORM => SANATIZE_ALNUMX
];
}
/**
* Design Pattern: Singleton - class should only be instanced once.
*
* @return Store
*/
public static function getInstance() {
// Design Pattern: Singleton
if (self::$instance == null) {
self::$instance = new StoreWebVar;
}
return self::$instance;
}
/**
* Get webvar, sanatize it and store it.
* Further access will be delivered by cache.
* @param string $key
* @param string $sanatizeClass
* @return string a) if not found: false, b) if found but SANATIZE failed: empty string, c) given value
* @throws CodeException
*/
public static function getVar($key, $sanatizeClass = '') {
unset($value);
// Already cached?
if (isset(self::$cache[$key]))
return self::$cache[$key];
if (isset($_GET[$key]))
$value = $_GET[$key];
if (isset($_POST[$key]))
$value = $_POST[$key];
// No value? break
if (!isset($value)) {
self::$cache[$key] = false;
return self::$cache[$key];
}
if ($sanatizeClass === '') {
$sanatizeClass = isset(self::$defaultSanatizeClass[$key]) ? self::$defaultSanatizeClass[$key] : SANATIZE_DEFAULT;
}
// sanatize
self::$cache[$key] = Sanatize::sanatize($value, $sanatizeClass);
return self::$cache[$key];
}
}
\ No newline at end of file
......@@ -27,8 +27,8 @@ require_once(__DIR__ . '/../qfq/exceptions/CodeException.php');
function renderForm($bodytext = "") {
try {
$form = new Form();
echo $form->process($bodytext);
$form = new Form($bodytext);
echo $form->process();
echo "test";
} catch (UserException $e) {
......
......@@ -6,6 +6,8 @@
namespace qfq;
use qfq;
//use qfq\helper;
use qfq\helper\KeyValueStringParser;
require_once(__DIR__ . '/../../qfq/helper/KeyValueStringParser.php');
......
......@@ -19,6 +19,23 @@ require_once(__DIR__ . '/../../qfq/exceptions/CodeException.php');
class StoreTest extends \PHPUnit_Framework_TestCase {
private $store = null;
public function setUp() {
// Client Variables has to setup before the first instantiation of 'Store'
$_GET[CLIENT_RECORD_ID] = '1234';
$_GET[CLIENT_SIP] = '1234abcd';
$_GET['key01'] = '1234';
$_POST['key02'] = '2345';
$_POST['key03'] = '3456';
$_POST[CLIENT_FORM] = 'mytestform';
$_GET['key04'] = '4567';
$_POST['key04'] = '5678';
$this->store = Store::getInstance("\n; some comment\n" . T3_BODYTEXT_FORM . "=formnameviabodytext\n" . T3_BODYTEXT_DEBUG_SAVE . "=6\n" . T3_BODYTEXT_DEBUG_LOAD . "=7\n");
}
public function testGetInstance() {
$a = Store::getInstance();
$b = Store::getInstance();
......@@ -28,30 +45,64 @@ class StoreTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals($a, $b, "Both classes should be the same");
}
public function testGetVarClient() {
public function testGetVarT3Bodytext() {
// T3 Bodytext
$this->assertEquals('formnameviabodytext', $this->store->getVar(T3_BODYTEXT_FORM, STORE_T3_BODYTEXT), "System: SESSIONNAME");
// T3 Bodytext
$this->assertEquals('6', $this->store->getVar(T3_BODYTEXT_DEBUG_SAVE, STORE_T3_BODYTEXT), "System: SESSIONNAME");
// T3 Bodytext
$this->assertEquals('7', $this->store->getVar(T3_BODYTEXT_DEBUG_LOAD, STORE_T3_BODYTEXT), "System: SESSIONNAME");
}
public function testGetVarSystem() {
// DBUSER in qfq.ini
$dbuser = $this->store->getVar(SYSTEM_DBUSER, STORE_SYSTEM);
$this->assertTrue($dbuser !== false && $dbuser !== '', "System: DBUSER found in qfq.ini");
$store = Store::getInstance();
// Sessionname
$this->assertEquals('qfq', $this->store->getVar(SYSTEM_SESSIONNAME, STORE_SYSTEM), "System: SESSIONNAME");
}
public function testGetVarClient() {
# Violates SANATIZE class: sanatized string is always an empty string.
# Access are cached: use new variables for every test.
# Access are cached:
# Test: default sanatize class
$_GET[CLIENT_RECORD_ID] = '1234';
$this->assertEquals('1234', $store->getVar(CLIENT_RECORD_ID, STORE_CLIENT), "FormName: default sanatize class");
# Test: Retrieve a variable, default sanatize class