Commit f3bf8fd5 authored by Carsten  Rose's avatar Carsten Rose
Browse files

Store, Constants, Database, QuickformQuery, AbstratDatabase, StoreTest:...

Store, Constants, Database, QuickformQuery, AbstratDatabase, StoreTest: renamed several config.ini variables to use the '_'.
parent 2211003c
......@@ -6,6 +6,7 @@
* Time: 8:35 PM
*/
const EXT_KEY = 'qfq';
const CONFIG_INI = "config.ini"; // QFQ configuration file: db access
const GFX_INFO = 'typo3conf/ext/qfq/Resources/Public/icons/note.gif';
......@@ -116,6 +117,7 @@ const ERROR_GET_STORE_ZERO = 1047;
const ERROR_SET_STORE_ZERO = 1048;
const ERROR_MISSING_FORMELEMENT = 1049;
const ERROR_INVALID_OR_MISSING_PARAMETER = 1050;
const ERROR_UNKNOWN_SQL_LOG_MODE = 1051;
// Store
const ERROR_STORE_VALUE_ALREADY_CODPIED = 1100;
......@@ -203,18 +205,22 @@ const TYPO3_TT_CONTENT_UID = 'ttcontentUid';
const TYPO3_PAGE_ID = 'typo3PageId';
// System
const SYSTEM_DBUSER = 'DBUSER';
const SYSTEM_DBSERVER = 'DBSERVER';
const SYSTEM_DBPW = 'DBPW';
const SYSTEM_DB = 'DB';
const SYSTEM_TESTDB = 'TESTDB';
const SYSTEM_SESSIONNAME = 'SESSIONNAME';
const SYSTEM_SQL_LOG = 'SQLLOG'; // Logging to file
const SYSTEM_DB_USER = 'DB_USER';
const SYSTEM_DB_SERVER = 'DB_SERVER';
const SYSTEM_DB_PASSWORD = 'DB_PASSWORD';
const SYSTEM_DB_NAME = 'DB_NAME';
const SYSTEM_DB_NAME_TEST = 'DB_NAME_TEST';
const SYSTEM_SESSION_NAME = 'SESSION_NAME';
const SYSTEM_SQL_LOG = 'SQL_LOG'; // Logging to file
const SYSTEM_SQL_LOG_MODE = 'SQL_LOG_MODE'; // Mode, which statements to log.
const SYSTEM_SHOW_DEBUG_INFO = 'SHOW_DEBUG_INFO';
const SYSTEM_CSS_LINK_CLASS_INTERNAL = 'CSS_LINK_CLASS_INTERNAL';
const SYSTEM_CSS_LINK_CLASS_EXTERNAL = 'CSS_LINK_CLASS_EXTERNAL';
// computed automatically during runtime
const SYSTEM_PATH_EXT = 'EXT_PATH';
// Information for: Log / Debug / Exception
const SYSTEM_SQL_RAW = 'sqlRaw'; // Type: SANITIZE_ALL / String. SQL Query (before substitute). Useful for error reporting.
const SYSTEM_SQL_FINAL = 'sqlFinal'; // Type: SANITIZE_ALL / String. SQL Query (after substitute). Useful for error reporting.
......@@ -243,6 +249,10 @@ const SIP_URLPARAM = 'urlparam';
// FORMELEMENT - copy of all formElements of processed form
//const DEF_FORM_ELEMENT_ID = 'id';
// SQL logging Modes
const SQL_LOG_MODE_ALL = 'all';
const SQL_LOG_MODE_MODIFY = 'modify';
// api/save.php, api/delete.php
const API_STATUS = 'status';
const API_MESSAGE = 'message';
......@@ -257,6 +267,10 @@ const API_ANSWER_REDIRECT_CLIENT = 'client';
const API_ANSWER_REDIRECT_NO = 'no';
const API_ANSWER_REDIRECT_URL = 'url';
// FORM
const SYMBOL_NEW = 'new';
const SYMBOL_EDIT = 'edit';
//CHECKBOX
const CHECKBOX_VALUE_CHECKED = 'checked';
const CHECKBOX_VALUE_UNCHECKED = 'unchecked';
\ No newline at end of file
......@@ -9,12 +9,11 @@
namespace qfq;
use qfq;
use qfq\DbException;
use qfq\Store;
use qfq\CodeException;
use qfq\DbException;
use qfq\UserException;
use qfq\Support;
use qfq\Store;
require_once(__DIR__ . '/exceptions/UserException.php');
require_once(__DIR__ . '/exceptions/CodeException.php');
......@@ -54,7 +53,7 @@ class Database {
/**
* @var string
*/
private $sqlLog = '';
private $sqlLog = '';
/**
* Returns current data base handle from Store[System][SYSTEM_DBH].
......@@ -82,10 +81,10 @@ class Database {
private function dbConnect() {
$mysqli = null;
$dbuser = $this->store->getVar(SYSTEM_DBUSER, STORE_SYSTEM);
$dbserver = $this->store->getVar(SYSTEM_DBSERVER, STORE_SYSTEM);
$dbpw = $this->store->getVar(SYSTEM_DBPW, STORE_SYSTEM);
$db = $this->store->getVar(SYSTEM_DB, STORE_SYSTEM);
$dbuser = $this->store->getVar(SYSTEM_DB_USER, STORE_SYSTEM);
$dbserver = $this->store->getVar(SYSTEM_DB_SERVER, STORE_SYSTEM);
$dbpw = $this->store->getVar(SYSTEM_DB_PASSWORD, STORE_SYSTEM);
$db = $this->store->getVar(SYSTEM_DB_NAME, STORE_SYSTEM);
$mysqli = new \mysqli($dbserver, $dbuser, $dbpw, $db);
......@@ -188,7 +187,7 @@ class Database {
/**
* Fires query $sql and fetches result als assoc array (all modes but ROW_KEYS) or as num array (mode: ROW_EKYS). Throws exception.
* $mode
* $mode
* ROW_REGULAR: Return 2-dimensional assoc array. Every query row is one array row.
* ROW_IMPLODE_ALL: Return string. All cells of all rows imploded to one string.
* ROW_EXPECT_0: Return empty string if there is now record row, Else an exception.
......@@ -334,7 +333,7 @@ class Database {
}
$this->mysqli_result = $result;
$count = $this->mysqli_result->num_rows;
$msg = 'Get rows: ' . $count;
$msg = 'Read rows: ' . $count;
break;
case 'INSERT':
$count = $this->mysqli->insert_id;
......@@ -359,12 +358,31 @@ class Database {
}
/**
* Decide if the SQL statement has to be logged.If yes, create a timestamp and do the log.
*
* @param $sql
* @param $parameterArray
* @throws \qfq\CodeException
* @param array $parameterArray
* @return string
* @throws \qfq\UserException
*/
private function dbLog($sql, $parameterArray = array()) {
$msg = '[' . date('r') . '][';
$mode = $this->store->getVar(SYSTEM_SQL_LOG_MODE, STORE_SYSTEM);
switch ($mode) {
case SQL_LOG_MODE_ALL:
break;
case SQL_LOG_MODE_MODIFY:
if ($this->isSqlModify($sql)) {
break;
}
// nothing to log.
return;
default:
throw new UserException("Unknown SQL_LOG_MODE: $mode", ERROR_UNKNOWN_SQL_LOG_MODE);
}
$msg = '[' . date('Y.m.d H:i:s O') . '][';
if (count($parameterArray) === 0) {
$msg .= $sql;
......@@ -374,6 +392,10 @@ class Database {
$ii = 0;
foreach ($parameterArray as $value) {
if (isset($sqlArray[$ii])) {
if (is_array($value)) {
$value = OnArray::toString($value);
}
$msg .= $sqlArray[$ii++] . "'" . $value . "'";
} else {
$msg = '?';
......@@ -388,6 +410,24 @@ class Database {
Logger::logMessage($msg, $this->sqlLog);
}
/**
* Check if the given SQL Statement might modify data.
*
* @param $sql
* @return bool true is the statement might modify data, else: false
*/
private function isSqlModify($sql) {
$command = explode(' ', $sql, 2);
switch (strtoupper($command[0])) {
case 'INSERT':
case 'UPDATE':
case 'DELETE':
case 'REPLACE':
return true;
}
return false;
}
/**
* @param $arr
*/
......
......@@ -362,7 +362,7 @@ class QuickFormQuery {
* @return string
*/
private function doReport() {
$report = new Report($this->t3data, $this->store->getVar(SYSTEM_SESSIONNAME, STORE_SYSTEM), $this->phpUnit);
$report = new Report($this->t3data, $this->store->getVar(SYSTEM_SESSION_NAME, STORE_SYSTEM), $this->phpUnit);
$html = $report->process();
......
......@@ -169,6 +169,28 @@ class Store {
$config['SHOW_DEBUG_INFO'] = (isset($GLOBALS["TSFE"]->beUserLogin) && $GLOBALS["TSFE"]->beUserLogin === true) ? 'yes' : 'no';
}
// SYSTEM_PATH_EXT: compute only if not already defined.
if (!isset($config[SYSTEM_PATH_EXT]) || $config[SYSTEM_PATH_EXT] === '' || $config[SYSTEM_PATH_EXT][0] !== '/') {
$relExtDir = '/typo3conf/ext/' . EXT_KEY;
// If we are called through AJAX API (e.g. api/save.php), there is no TYPO3 environment.
if (isset($_SERVER['SCRIPT_FILENAME'])) {
$pos = strpos($_SERVER['SCRIPT_FILENAME'], $relExtDir);
if ($pos === false) {
// probably: index.php - THERE should be a TYPO3 environment.
$config[SYSTEM_PATH_EXT] = dirname($GLOBALS['TYPO3_LOADED_EXT'][EXT_KEY]['ext_localconf.php']);
} else {
$config[SYSTEM_PATH_EXT] = substr($_SERVER['SCRIPT_FILENAME'], 0, $pos + strlen($relExtDir));
}
}
}
// make SQL PATH absolute. This is necessary to work in different directories correctly.
if (isset($config[SYSTEM_SQL_LOG]) && $config[SYSTEM_SQL_LOG][0] !== '/') {
$config[SYSTEM_SQL_LOG] = $config[SYSTEM_PATH_EXT] . '/' . $config[SYSTEM_SQL_LOG];
}
self::setVarArray($config, STORE_SYSTEM, true);
}
......@@ -237,7 +259,7 @@ class Store {
*/
private function fillStoreSip() {
$sessionName = self::getVar(SYSTEM_SESSIONNAME, STORE_SYSTEM);
$sessionName = self::getVar(SYSTEM_SESSION_NAME, STORE_SYSTEM);
self::$sip = new Sip($sessionName);
$s = self::getVar(CLIENT_SIP, STORE_CLIENT);
......
......@@ -59,17 +59,17 @@ abstract class AbstractDatabaseTest extends PHPUnit_Framework_TestCase {
$this->store = \qfq\Store::getInstance();
// SWITCH to TestDB
$this->store->setVar(SYSTEM_DB, $this->store->getVar(SYSTEM_TESTDB, STORE_SYSTEM), STORE_SYSTEM);
$this->store->setVar(SYSTEM_DB_NAME, $this->store->getVar(SYSTEM_DB_NAME_TEST, STORE_SYSTEM), STORE_SYSTEM);
if ($this->db === null) {
$this->db = new \qfq\Database();
}
/// Establish additional mysqli access
$dbserver = $this->store->getVar(SYSTEM_DBSERVER, STORE_SYSTEM);
$dbuser = $this->store->getVar(SYSTEM_DBUSER, STORE_SYSTEM);
$db = $this->store->getVar(SYSTEM_DB, STORE_SYSTEM);
$dbpw = $this->store->getVar(SYSTEM_DBPW, STORE_SYSTEM);
$dbserver = $this->store->getVar(SYSTEM_DB_SERVER, STORE_SYSTEM);
$dbuser = $this->store->getVar(SYSTEM_DB_USER, STORE_SYSTEM);
$db = $this->store->getVar(SYSTEM_DB_NAME, STORE_SYSTEM);
$dbpw = $this->store->getVar(SYSTEM_DB_PASSWORD, STORE_SYSTEM);
if (self::$mysqli === null) {
......
......@@ -67,7 +67,7 @@ class StoreTest extends \PHPUnit_Framework_TestCase {
public function testGetVarStoreSystem() {
// DBUSER in qfq.ini
$dbuser = $this->store->getVar(SYSTEM_DBUSER, STORE_SYSTEM);
$dbuser = $this->store->getVar(SYSTEM_DB_USER, STORE_SYSTEM);
$this->assertTrue($dbuser !== false && $dbuser !== '', "System: DBUSER found in qfq.ini");
}
......@@ -75,12 +75,12 @@ class StoreTest extends \PHPUnit_Framework_TestCase {
public function testSetVarStoreSystem() {
// Sessionname: default value
$this->assertEquals('qfq', $this->store->getVar(SYSTEM_SESSIONNAME, STORE_SYSTEM), "System: SESSIONNAME");
$this->assertEquals('qfq', $this->store->getVar(SYSTEM_SESSION_NAME, STORE_SYSTEM), "System: SESSIONNAME");
// set new Sessionname
$this->store->setVar(SYSTEM_SESSIONNAME, "anothersessionname", STORE_SYSTEM);
$this->store->setVar(SYSTEM_SESSION_NAME, "anothersessionname", STORE_SYSTEM);
$this->assertEquals('anothersessionname', $this->store->getVar(SYSTEM_SESSIONNAME, STORE_SYSTEM), "System: SESSIONNAME");
$this->assertEquals('anothersessionname', $this->store->getVar(SYSTEM_SESSION_NAME, STORE_SYSTEM), "System: SESSIONNAME");
}
......
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