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

Report*: initially copied files to QFQ. Report is not running.

parent d78513ea
<?php
/***************************************************************
* Copyright notice
*
* (c) 2010 Glowbase GmbH <support@glowbase.com>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
namespace qfq;
//use qfq;
require_once(__DIR__ . '/Define.php');
require_once(__DIR__ . '/Error.php');
require_once(__DIR__ . '/Log.php');
class Db {
public $t3_typo_db_host = "";
/**
* @var string
*/
private $lastUsedDB = "";
/**
* @var array
*/
private $arrDB = array();
/**
* @var
*/
private $t3_typo_db_username, $t3_typo_db, $t3_typo_db_password;
/**
* @var Log
*/
private $log;
// Emulate global variable: will be set much earlier in other functions. Will be shown in error messages.
private $fr_error;
/**
* Constructor:
*
* @param Log fully created for logging.
*
*/
public function __construct($log) {
// CR 25.4.11: require_once does not work here. No idea why
require(PATH_typo3conf . 'localconf.php');
$this->t3_typo_db_host = $typo_db_host;
$this->t3_typo_db_username = $typo_db_username;
$this->t3_typo_db = $typo_db;
$this->t3_typo_db_password = $typo_db_password;
$this->log = $log;
}
/**
* Set Array fr_error: setter function to set most recent values, especially fr_erro['row'].
* Will be shown in error messages.
*
* @param array $fr_error uid, pid, row, column_idx, full_level
*/
public function set_fr_error(array $fr_error) {
$this->fr_error = $fr_error;
}
/**
* doQueryKeys: See doQuery
* Difference: fake Array for $keys
*
* @param string $dbAlias
* @param string $sql
* @param array $result
* @param string string $expect
* @param string $merge
* @return bool
* @throws CodeReportException
* @throws SqlReportException
* @throws SyntaxReportException
*/
public function doQuery($dbAlias, $sql, array &$result, $expect = EXPECT_GE_0, $merge = MERGE_NONE) {
return ($this->doQueryKeys($dbAlias, $sql, $result, $fake, $expect, $merge, MYSQL_ASSOC));
}
/**
* doQueryKeys: fires a show, select, insert, update or delete and collects result.
* insert, update and delete will produce a log entry.
* If: $expect==EXPECT_SQL_OR_STRING, '$sql' can be anything which won't be fired if it's not a SQL statement.
*
* @param string $dbAlias Name of Database to be used.
* @param string $sql Select Query
* @param array $result content depends on $sql.
* $sql='insert ...': mysql_last_insert_id will be returned in $result.
* $sql='update ...' or 'delete ----': mysql_affected_rows will be returned in $result.
* $sql='select ...': all selected rows will be returned in $result.
* $result will be formatted like specified in $merge.
* Attention: with EXPECT_1|EXPECT_0_1 '$result' is a one dimensional array, else a two dimensional array.
* @param array $keys
* @param string $expect
* @param string $merge Applies different modes of merging - MERGE_NONE, MERGE_ROW, MERGE_ALL
* @param int $arrayMode
* @return bool true: all ok
* false: a) Number of rows don't match $expect
* b) $expect==EXPECT_SQL_OR_STRING and $sql is not an SQL expression (instead it's a regular string) - this is not bad, just to indicate that there was no query.
* @throws CodeReportException
* @throws SqlReportException
* @throws SyntaxReportException
*/
public function doQueryKeys($dbAlias, $sql, array &$result, array &$keys, $expect = EXPECT_GE_0, $merge = MERGE_NONE, $arrayMode = MYSQL_NUM) {
$result = "";
$tmp = "";
$action = "";
$this->selectDB($dbAlias);
if ($this->fr_error["debug_level"] >= DEBUG_SQL) {
// T3 function: debug()
// debug(array('SQL' => $sql));
}
// Extract first parameter to check if it is a SQL statement
$tmp = explode(" ", trim($sql), 2);
$action = strtolower($tmp[0]);
switch ($action) {
case "show" :
case "select":
case "insert":
case "update":
case "delete":
break; // SQL Statement: go further
default:
if ($expect == EXPECT_SQL_OR_STRING) {
$result = $sql;
return (FALSE); // nothing bad, just to indicate $sql was not a SQL statement.
} else
throw new SyntaxReportException ("Unexpected SQL Statement: '$action'", "", __FILE__, __LINE__, array("DB:$dbAlias", "SQL:$sql"), $this->fr_error);
}
// Fire SQL statement
if (!($res = mysql_query($sql))) {
// Escape query if in AJAX mode
$sql = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? addslashes($sql) : $sql;
throw new SqlReportException ("Did not get result for query.", $sql, __FILE__, __LINE__, $this->fr_error);
}
switch ($action) {
case "show" :
case "select":
$action = QUERY; // aggregate 'SLELECT' and 'SHOW' and ...
$num_rows = mysql_num_rows($res);
break;
case "insert":
$num_rows = mysql_affected_rows();
$result = mysql_insert_id();
break;
case "update":
case "delete":
$num_rows = mysql_affected_rows();
$result = $num_rows;
break;
default:
throw new CodeReportException ("This error should be catched 20 lines above.", __FILE__, __LINE__); // can't be happen, should already be detected earlier.
}
// Logging
if ($action != QUERY) {
// Not a query: write log and go home.
$this->log->log_sql('db', '', $num_rows, $result, $sql);
return (TRUE);
} else {
// Logging if localconf log_level >=D2
$this->log->log_do('error', 'D2', '-', $sql);
}
// Check $expect against real result.
switch ($expect) {
case EXPECT_0:
return ($num_rows == 0);
break;
case EXPECT_1:
if ($num_rows != 1) return (FALSE);
break;
case EXPECT_0_1:
if ($num_rows > 1) return (FALSE);
break;
case EXPECT_GE_0:
break;
case EXPECT_GE_1:
if ($num_rows == 0) return (FALSE);
break;
case EXPECT_SQL_OR_STRING:
break;
default:
throw new CodeReportException ("Unknown 'expect' qualifier: $expect", __FILE__, __LINE__);
break;
}
// Preparation to fetch all rows
$tmp = "";
$fieldCount = 0;
// Fetch all rows:
while ($row = mysql_fetch_array($res, $arrayMode)) {
foreach ($row as $key => $value) {
$row[$key] = stripslashes($value);
}
switch ($merge) {
case MERGE_NONE:
$tmp[] = $row;
break;
case MERGE_ROW:
$tmp[] = implode($row);
break;
case MERGE_ALL:
$tmp .= implode($row);
break;
default:
throw new CodeReportException ("Unknown 'merge' qualifier: $merge", __FILE__, __LINE__);
break;
}
}
// Collect 'keys'
if ($merge == MERGE_NONE) {
$keys = array();
$numberfields = mysql_num_fields($res);
for ($i = 0; $i < $numberfields; $i++) {
$keys[] = mysql_field_name($res, $i);
}
}
// adjust Result Array: one or two dimensions.
switch ($expect) {
case EXPECT_0:
break;
case EXPECT_1:
case EXPECT_0_1:
case EXPECT_SQL_OR_STRING:
$result = $tmp[0];
break;
default:
$result = $tmp;
}
if ($merge == MERGE_ALL)
$result = $tmp;
mysql_free_result($res);
return (TRUE);
} // doQueryKeys()
/**
* select DB
*
* @param string $dbAlias : Name of the dbname
*
* @return bool TRUE if ok, else exception.
*/
public function selectDB($dbAlias) {
if (!$dbAlias)
throw new CodeReportException ("Failed: empty dbAlias", __FILE__, __LINE__);
// If the db is still selected: do nothing.
if ($dbAlias == $this->lastUsedDB)
return true;
// if the db is already open - just select it.
if (isset($this->arrDB[$dbAlias]['link'])) {
if (!mysql_select_db($this->arrDB[$dbAlias]['db']))
throw new SqlReportException ("Failed: mysql_select_db($this->arrDB[$dbAlias]['db'])", "", __FILE__, __LINE__, $this->fr_error);
return true;
}
$this->openDB($dbAlias);
return true;
} // openDB()
/**
* Open specified DB
*
* @param string $dbAlias Name of database to be opened
* @throws SqlReportException
*/
public function openDB($dbAlias) {
// TYPO3 globale Variablen
// global $typo_db_host,$typo_db_username,$typo_db,$typo_db_password;
// Du sollst kein global verwenden!!
if ($dbAlias == T3) {
$host = $this->t3_typo_db_host;
$username = $this->t3_typo_db_username;
$db = $this->t3_typo_db;
$password = $this->t3_typo_db_password;
} else {
// require(PATH_typo3conf.'ext/formreport/ext_localconf.php');
$host = $GLOBALS['TYPO3_CONF_VARS'][FORMREPORT][$dbAlias]['host'];
$username = $GLOBALS['TYPO3_CONF_VARS'][FORMREPORT][$dbAlias]['username'];
$db = $GLOBALS['TYPO3_CONF_VARS'][FORMREPORT][$dbAlias]['name'];
$password = $GLOBALS['TYPO3_CONF_VARS'][FORMREPORT][$dbAlias]['password'];
}
// If 't3' is specified or the custom DB is not fully specified, take credentials from localconf.php
# $host = $host ? $host : $typo_db_host;
# $username = $username ? $username : $typo_db_username;
# $db = $db ? $db : $typo_db;
# $password = $password ? $password : $typo_db_password;
// MySQL Connect
if (!($link = mysql_connect($host, $username, $password)))
throw new SqlReportException ("mysql_connect($host, $username)", "", __FILE__, __LINE__, $this->fr_error);
// Set connection charset
if (!mysql_set_charset('utf8', $link))
throw new SqlReportException ("mysql_set_charset('utf8', $link)", "", __FILE__, __LINE__, $this->fr_error);
// MySQL select
if (!mysql_select_db($db, $link))
throw new SqlReportException ("mysql_select_db($db)", "", __FILE__, __LINE__, $this->fr_error);
// Remember that this DB has been opened.
$this->arrDB[$dbAlias]['link'] = $link;
$this->arrDB[$dbAlias]['db'] = $db;
// Remember the new DB as 'last used'
$this->lastUsedDB = $dbAlias;
} // selectDB()
/**
* closeAllDB
*
* @return void
*/
public function closeAllDB() {
foreach ($this->arrDB as $key => $value) {
if ($key != T3) {
mysql_close($value);
$arrDB[$key] = null;
}
}
} // closeAllDB()
}
<?php
define("EXTKEY", "formreport");
define("FORMREPORT", "formreport");
define("LENGTH_HASH", 32);
// dbalias of Extension DB
define("DB", "db");
define("T3", "t3");
// Constants for tx_form_pi1.php
define("FR_FORM", "tx_formreport_form");
define("FR_FORMELEMENT", "tx_formreport_formelement");
define("FR_LOCK", "tx_formreport_lock");
define("URL_HASH", "S_hash");
define("URL_RECORD_ID", "N_r");
define("URL_FORMNAME", "S_form");
define("URL_FORM", "URL");
// Definitions for doQuery()
define("EXPECT_0", "expect_0");
define("EXPECT_1", "expect_1");
define("EXPECT_0_1", "expect_0_1");
define("EXPECT_GE_0", "expect_ge_0");
define("EXPECT_GE_1", "expect_ge_1");
define("EXPECT_SQL_OR_STRING", "expect_sql_or_string");
define("MERGE_NONE", "merge_none");
define("MERGE_ROW", "merge_row");
define("MERGE_ALL", "merge_all");
define("QUERY", "query");
// Definitions for sanatize() strip_tags
define("TAGS_NONE", "none");
define("TAGS_MARKUP", "markup");
define("TAGS_CUSTOM", "custom");
define("TAGS_ALL", "all");
// Definitions for sanatize() reaction on changed variables by sanatize them
define("TAGS_EXCEPTION", "exception");
define("TAGS_SANATIZE", "sanatize");
define("TAGS_IGNORE", "ignore");
// define("LIST_MARKUP_TAGS","<br><p><em><strong><code><samp><kbd><var><cite><dfn><abbr><acronym><q>");
define("LIST_MARKUP_TAGS", "<br><p><em><strong><font><b><u><i><span><div><ol><ul><li><code><samp><kbd><var><cite><dfn><abbr><acronym><q>");
define("DEBUG_SQL", "1");
define("DEBUG_BASIC", "2");
define("DEBUG_VERBOSE", "3");
define("DEBUG_EXTREME", "4");
// Set default values - overridden by ext_localconf.php
define("LOCK_RECORDS_INTERVAL", "300");
define("DFLT_UPLOAD_BASE_DIR", "fileadmin");
define("DFLT_UPLOAD_TMP_DIR", "fileadmin/tempfiles");
define("DFLT_UPLOAD_TMP_TTL", "300");
define("PATH_ICONS", "typo3conf/ext/" . FORMREPORT . "/icons/");
// Definitions to allow successfull include of ext_localconf.
//define( 'TYPO3_MODE', '1' );
//define( 'FORMREPORT', '1' );
<?php
/***************************************************************
* Copyright notice
*
* (c) 2010 Glowbase GmbH <support@glowbase.com>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
namespace qfq;
//use qfq;
/**
* syntaxException: Webmaster made a mistake in tt_content record or form definition
*
* @param string $message
* @param string $code
* @param string $file
* @param string $line
* @param string|array $customMessage =array() a) string: "key: value", b) array("key1: value1","key2: value2", ...) - all items will be displayed on an own line in the error message.
*/
class SyntaxReportException extends \Exception {
private $fr_error;
public function __construct($message, $code, $file, $line, $customMessage = array(), $fr_error = array()) {
parent::__construct($message);
$this->code = $code;
$this->file = $file;
$this->line = $line;
$this->customMessage = $customMessage;
$this->fr_error = $fr_error;
}
public function errorMessage() {
// global $BE_USER; TA: Du sollst kein global verwenden!!
// If '$this->customMessage' is a string, convert it to an array.
if ($this->customMessage && !is_array($this->customMessage)) $this->customMessage = array($this->customMessage);
//error message
// if ($BE_USER->user["uid"]>0) { TA: Du sollst kein global verwenden!!
if ($GLOBALS['BE_USER']->user["uid"] > 0 || $_SESSION[FORMREPORT]['be_user_uid'] > 0) {
$errorMsg = nl2br("<hr />Error: <strong>" . htmlentities($this->getMessage()) . "</strong><br />");
$errorMsg .= "Formreport statement: <strong>" . htmlentities($this->fr_error["row"]) . "</strong><br />";
// Print custom messages.
foreach ($this->customMessage as $value) {
$tmparr = explode(":", $value, 2);
$errorMsg .= $tmparr[0] . ":<strong>" . htmlentities($tmparr[1]) . "</strong><br />";
}
$errorMsg .= "T3 Page pid: <strong>" . $this->fr_error["pid"] . "</strong><br />";
$errorMsg .= "ttcontent record uid: <strong>" . $this->fr_error["uid"] . "</strong><br />";
$errorMsg .= "File: <strong>" . $this->file . "</strong><br />Line: <strong>" . $this->line . "</strong><br />MSG: <strong>" . $this->code . "</strong><br />";
$errorMsg .= "StackTrace<pre>" . nl2br($this->getTraceAsString()) . "</pre><hr />";
} else {
$errorMsg = "<hr />Error: <strong>" . htmlentities($this->getMessage()) . "</strong><hr />";
}
return $errorMsg;
} // errorMessage()
} // class syntaxException
/**
* sqlException: An SQL-Query returned an unexpected result
*
* @param string $message
* @param string $sql
* @param string $file
* @param string $line
*/
class SqlReportException extends \Exception {
private $fr_error;
public function __construct($message, $sql, $file, $line, $fr_error = array()) {
parent::__construct($message);
$this->file = $file;
$this->line = $line;
$this->sql = $sql;
$this->fr_error = $fr_error;
}
public function errorMessage() {
// global $BE_USER; TA: Du sollst kein global verwenden!!
//error message
// if ($BE_USER->user["uid"]>0) { TA: Du sollst kein global verwenden!!
if ($GLOBALS['BE_USER']->user["uid"] > 0 || $_SESSION[FORMREPORT]['be_user_uid'] > 0) {
$errorMsg = nl2br("<hr />Error: <strong>" . htmlentities($this->getMessage()) . "</strong><br />MySQL: <strong>" . mysql_error() . "</strong><hr />");
$errorMsg .= "SQL: <strong>" . htmlentities($this->sql) . "</strong><hr />";
$errorMsg .= "Formreport: <strong>" . $this->fr_error["row"] . "</strong><hr />";
$errorMsg .= "T3 Page pid: <strong>" . $this->fr_error["pid"] . "</strong><br>";
$errorMsg .= "ttcontent record uid: <strong>" . $this->fr_error["uid"] . "</strong><br />";
$errorMsg .= "Line: <strong>" . $this->line . "</strong><br />File: <strong>" . $this->file . "</strong><hr />";
$errorMsg .= "StackTrace<pre>" . nl2br($this->getTraceAsString()) . "</pre><hr />";
} else {
$errorMsg = "<hr />Error: <strong>" . htmlentities($this->getMessage()) . "</strong><hr />";
}
return $errorMsg;
} // errorMessage()
} // class sqlException
/**
* codeException: Error in formreport code
*
* @param string $message
* @param string $file
* @param string $line
*/
class CodeReportException extends \Exception {
protected $file;
protected $line;
public function __construct($message, $file = "undefined", $line = "undefined") {
parent::__construct($message);
$this->file = $file;
$this->line = $line;
}
public function errorMessage() {
// global $BE_USER; TA: Du sollst kein global verwenden!!
// if ($BE_USER->user["uid"]>0) { TA: Du sollst kein global verwenden!!
if ($GLOBALS['BE_USER']->user["uid"] > 0 || $_SESSION[FORMREPORT]['be_user_uid'] > 0) {
$errorMsg = nl2br("<hr />Error: <strong>" . $this->getMessage() . "</strong><br />File: <strong>" . $this->file . "</strong><br />Line: <strong>" . $this->line . "</strong><hr />");
$errorMsg .= "StackTrace<pre>" . nl2br($this->getTraceAsString()) . "</pre><hr />";
} else {
$errorMsg = "<hr />Error: <strong>" . $this->getMessage() . "</strong><hr />";
}
return $errorMsg;
} // errorMessage()
} // class codeException
/**
* userException: Exception on user-level: Session expired, invalid form submission, no authorization, invalid request, etc.
*
* @param string $message
* @param string $file
* @param string $line
*/
class UserReportException extends \Exception {
protected $file;
protected $line;
public function __construct($message, $file = "undefined", $line = "undefined") {
parent::__construct($message);
$this->file = $file;
$this->line = $line;
}
public function errorMessage() {
if ($GLOBALS['BE_USER']->user["uid"] > 0 || $_SESSION[FORMREPORT]['be_user_uid'] > 0) {
$errorMsg = nl2br("<hr />Error: <strong>" . $this->getMessage() . "</strong><br />File: <strong>" . $this->file . "</strong><br />Line: <strong>" . $this->line . "</strong><hr />");
$errorMsg .= "StackTrace<pre>" . nl2br($this->getTraceAsString()) . "</pre><hr />";
} else {
$errorMsg = "<hr />Error: <strong>" . $this->getMessage() . "</strong><hr />";
}
return $errorMsg;
} // errorMessage()
} // class userException
This diff is collapsed.