From c1dca671765e5338fbe308e67285e695815f9ada Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Thu, 5 Mar 2015 16:59:05 +0900 Subject: [PATCH] Update to core classes, see detail below - config.inc: add define for show/not show all errors when parsning through Error.Handling.inc with SHOW_ALL_ERRORS - Error.Handling.inc: check php error level and do not show ones that are not flagged unless SHOW_ALL_ERRORS is set to true - db_pgsql.inc for fetch array, call the internal wrapper method, not the pg method directly - db_pgsql_pdo.inc: test insert for alternative with pdo lib instead of php internal postgresql interface - Class.DB.IO.inc: on prepared check if cursor exist before returing inserted id in INSERT queries. fail if no insert id could be aquired if there was no cursor (or other error) - Class.Basic.inc: rewrite Time to string method for speed up and removal of old php microtime format --- www/admin/class_test.php | 3 ++ www/configs/config.inc | 1 + www/libs/Class.Basic.inc | 29 +++++++-------- www/libs/Class.DB.IO.inc | 9 ++++- www/libs/Error.Handling.inc | 4 +++ www/libs/db_pgsql.inc | 2 +- www/libs/db_pgsql_pdo.inc | 71 ++++++++++++++++++++++++++----------- 7 files changed, 80 insertions(+), 39 deletions(-) diff --git a/www/admin/class_test.php b/www/admin/class_test.php index fb2d9101..59b32e1c 100644 --- a/www/admin/class_test.php +++ b/www/admin/class_test.php @@ -146,6 +146,9 @@ $status = $basic->db_exec("INSERT INTO test.schema_test (contents, id) VALUES ('TIME: ".time()."', ".rand(1, 10).")"); print "OTHER SCHEMA INSERT STATUS: ".$status." | PK NAME: ".$basic->pk_name.", PRIMARY KEY: ".$basic->insert_id."
"; + // time string thest + print "TIME STRING TEST: ".$basic->TimeStringFormat(5887998.33445)."
"; + // magic links test print $basic->magic_links('user@bubu.at').'
'; print $basic->magic_links('http://test.com/foo/bar.php?foo=1').'
'; diff --git a/www/configs/config.inc b/www/configs/config.inc index ca16d322..0995bca9 100644 --- a/www/configs/config.inc +++ b/www/configs/config.inc @@ -197,6 +197,7 @@ // DEFINE('EXPORT_SCRIPT', $PATHS[TARGET]['perl_bin']); // DEFINE('REDIRECT_URL', $PATHS[TARGET]['redirect_url']); DEFINE('DEBUG', $DEBUG_FLAG[$HOST_NAME]); + DEFINE('SHOW_ALL_ERRORS', false); // show all errors if debug_all & show_error_handling are enabled /************* GENERAL PAGE TITLE ********/ $G_TITLE = ''; diff --git a/www/libs/Class.Basic.inc b/www/libs/Class.Basic.inc index 3a20c500..72c2f035 100644 --- a/www/libs/Class.Basic.inc +++ b/www/libs/Class.Basic.inc @@ -1036,32 +1036,27 @@ // PARAMS: seconds // RETURN: formated time string // DESC: formats a timestamp into time from. not a date - public static function TimeStringFormat($timestamp, $show_micro = 1) + public static function TimeStringFormat($timestamp, $show_micro = true) { // check if the timestamp has any h/m/s/ms inside, if yes skip if (!preg_match("/(h|m|s|ms)/", $timestamp)) { $ms = 0; - // work on ms timestamp - if (strstr($timestamp, " ")) - { - list ($ms, $timestamp) = explode(" ", $timestamp); - if (is_numeric($ms)) // special case round - $ms = substr(round($ms, 4), 2); - } - else // if ms is as float - { - list ($timestamp, $ms) = explode('.', round($timestamp, 4)); - } - $timegroups = array ("86400", "3600", "60", "1"); - $output = array (); + list ($timestamp, $ms) = explode('.', round($timestamp, 4)); + $timegroups = array ('86400', '3600', '60', '1'); + $labels = array ('d', 'h', 'm', 's'); + $time_string = ''; for ($i = 0; $i < count($timegroups); $i ++) { - array_push($output, floor($timestamp / $timegroups[$i])); + $output = floor($timestamp / $timegroups[$i]); $timestamp = $timestamp % $timegroups[$i]; + // output has days|hours|min|sec + if ($output || $time_string) + $time_string .= $output.$labels[$i].(($i + 1) != count($timegroups) ? ' ' : ''); } - # output has days|hours|min|sec(|ms) - $time_string = (($output[0]) ? $output[0]."d " : "").(($output[1] || $output[0]) ? $output[1]."h " : "").(($output[2] ||$output[1] || $output[0]) ? $output[2]."m " : "").$output[3]."s".(($show_micro) ? " ".((!$ms) ? 0 : $ms)."ms" : ""); + // add ms if there + if ($show_micro) + $time_string .= ' '.(!$ms ? 0 : $ms).'ms'; } else { diff --git a/www/libs/Class.DB.IO.inc b/www/libs/Class.DB.IO.inc index 932d012b..e9316759 100644 --- a/www/libs/Class.DB.IO.inc +++ b/www/libs/Class.DB.IO.inc @@ -339,6 +339,7 @@ $this->error_string['23'] = 'Query Execute failed, data array does not match placeholders'; $this->error_string['24'] = 'Missing prepared query entry for execute.'; $this->error_string['30'] = 'Query call in a possible endless loop. Was called more than '.$this->MAX_QUERY_CALL.' times'; + $this->error_string['31'] = 'Could not fetch PK after query insert'; $this->error_string['40'] = 'Query async call failed.'; $this->error_string['41'] = 'Connection is busy with a different query. Cannot execute.'; $this->error_string['42'] = 'Cannot check for async query, none has been started yet.'; @@ -1415,7 +1416,7 @@ $this->debug('ExecuteData', 'ERROR in STM['.$stm_name.'|'.$this->prepare_cursor[ { if (!$this->prepare_cursor[$stm_name]['returning_id']) $this->insert_id = $this->db_functions->_db_insert_id($this->prepare_cursor[$stm_name]['query'], $this->prepare_cursor[$stm_name]['pk_name']); - else + elseif ($code) $this->insert_id = $this->db_functions->_db_fetch_array($code)[$this->prepare_cursor[$stm_name]['pk_name']]; // this error handling is only for pgsql if (is_array($this->insert_id)) @@ -1425,6 +1426,12 @@ $this->debug('ExecuteData', 'ERROR in STM['.$stm_name.'|'.$this->prepare_cursor[ $this->_db_debug('db', 'DB-Warning '.$stm_name.': Could not get insert id', 'DB_WARNING'); unset($this->insert_id); } + elseif (!$this->insert_id) + { + $this->warning_id = 31; + $this->_db_error(); + $this->_db_debug('db', 'DB-Warning '.$stm_name.': Could not get insert id', 'DB_WARNING'); + } } return $code; } diff --git a/www/libs/Error.Handling.inc b/www/libs/Error.Handling.inc index 81dc0f06..75bb9183 100644 --- a/www/libs/Error.Handling.inc +++ b/www/libs/Error.Handling.inc @@ -21,6 +21,10 @@ // if this fails, it will print the data to the window via echo function MyErrorHandler ($type, $message, $file, $line, $context) { + if (!(error_reporting() & $type) && !SHOW_ALL_ERRORS) { + // This error code is not included in error_reporting + return; + } // ERROR LEVEL $error_level = array ( 1 => 'E_ERROR', diff --git a/www/libs/db_pgsql.inc b/www/libs/db_pgsql.inc index 3b5ba3c7..4df6eaca 100644 --- a/www/libs/db_pgsql.inc +++ b/www/libs/db_pgsql.inc @@ -205,7 +205,7 @@ // I have to do manually or I overwrite the original insert internal vars ... if ($q = $this->_db_query($q)) { - list($id) = pg_fetch_array($q); + list($id) = $this->_db_fetch_array($q); } else { diff --git a/www/libs/db_pgsql_pdo.inc b/www/libs/db_pgsql_pdo.inc index 4ac014ad..5c6d0e42 100644 --- a/www/libs/db_pgsql_pdo.inc +++ b/www/libs/db_pgsql_pdo.inc @@ -16,7 +16,7 @@ { private $last_error_query; private $dbh; - private $cursor; + private $cursor = array(); // METHOD: __construct // PARAMS: none @@ -36,8 +36,8 @@ // METHOD: _db_query // PARAMS: query - // RETURN: query result - // DESC : wrapper for gp_query, catches error and stores it in class var + // RETURN: cursor + // DESC : was wrapper for pg_query, now it runs pepare and execute in one set. uses the query md5 as the cursor name public function _db_query($query) { $this->last_error_query = ''; @@ -45,7 +45,20 @@ $result = @pg_query($this->dbh, $query); if (!$result) $this->last_error_query = $query; */ - return $result; + $cursor = $this->_db_prepare(md5($query), $query); + $result = $this->_db_execute(md5($query), array ()); + if (!$result) + $this->last_error_query = $query; + return $cursor; + } + + // METHOD: _db_query_result + // PARAMS: query + // RETURN: result from query + // DESC : only valid for the pdo version here. use with care + public function _db_query_result($query) + { + return $this->dbh->query($query); } // METHOD: _db_send_query @@ -76,8 +89,14 @@ // DESC : wrapper for pg_close public function _db_close() { - $this->cursor->closeCursor; - $this->cursor = null; + if (is_array($this->cursor)) + { + foreach ($this->cursor as $key => $data) + { + $this->cursor[$key]->closeCursor; + $this->cursor[$key] = null; + } + } $this->dbh = null; } @@ -88,6 +107,8 @@ public function _db_prepare($name, $query) { // return @pg_prepare($this->dbh, $name, $query); + $this->cursor[$name] = $this->dbh->prepare($query); + return $this->cursor[$name]; } // METHOD: _db_execute @@ -97,6 +118,7 @@ public function _db_execute($name, $data) { // return @pg_execute($this->dbh, $name, $data); + return $this->cursor[$name]->execute($data); } // METHOD: _db_num_rows @@ -106,6 +128,7 @@ public function _db_num_rows($cursor) { // return pg_num_rows($cursor); + return $cusor->rowCount(); } // METHOD: _db_num_fields @@ -115,6 +138,7 @@ public function _db_num_fields($cursor) { // return pg_num_fields($cursor); + return $cursor->columnCount(); } // METHOD: _db_field_name @@ -133,6 +157,7 @@ public function _db_fetch_array($cursor) { // return pg_fetch_array($cursor); + return $cursor->fetch(); } // METHOD: _db_affected_ros @@ -142,6 +167,7 @@ public function _db_affected_rows($cursor) { // return pg_affected_rows($cursor); + return $cusor->rowCount(); } // METHOD: _db_insert_id @@ -168,7 +194,7 @@ // no PK name given at all if (!$pk_name) { - // if name is plurar, make it singular + // if name is plural, make it singular // if (preg_match("/.*s$/i", $table)) // $table = substr($table, 0, -1); // set pk_name to "id" @@ -176,11 +202,11 @@ } $seq = (($schema) ? $schema.'.' : '').$table."_".$pk_name."_seq"; $q = "SELECT CURRVAL('$seq') AS insert_id"; -// $this->currval_query = $q; // I have to do manually or I overwrite the original insert internal vars ... - if ($q = $this->_db_query($q)) + $row = $this->_db_query_result($q); + if ($row['insert_id']) { - list($id) = pg_fetch_array($q); + $id = $row['insert_id']; } else { @@ -203,8 +229,9 @@ if ($schema) { $q = "SHOW search_path"; - $cursor = $this->_db_query($q); - $search_path = $this->_db_fetch_array($cursor)['search_path']; +// $cursor = $this->_db_query($q); +// $search_path = $this->_db_fetch_array($cursor)['search_path']; + $search_path = $this->_db_query_result($q)['search_path']; if ($search_path != $schema) { $table_prefix = $schema.'.'; @@ -228,11 +255,11 @@ $q .= "pg_attribute.attrelid = pg_class.oid AND "; $q .= "pg_attribute.attnum = any(pg_index.indkey) "; $q .= "AND indisprimary"; - $cursor = $this->_db_query($q); - if ($cursor) - return $this->_db_fetch_array($cursor)['column_name']; - else + $row = $this->_db_query_result($q); + if ($row === FALSE) return false; + else + return $row['column_name']; } else { @@ -251,11 +278,15 @@ { $db_port = 5432; } -/* $this->dbh = @pg_connect("host=".$db_host." port=".$db_port." user=".$db_user." password=".$db_pass." dbname=".$db_name." sslmode=".$db_ssl); - if (!$this->dbh) + try { - die(""); - } */ + $this->dbh = new PDO('pgsql:host='.$db_host.';dbname='.$db_name.';port='.$db_port.';sslmode='.$db_ssl, $db_user, $db_pass); + } + catch (PDOException $e) + { + print "Error!: ".$e->getMessage()."\n"; + die(""); + } return $this->dbh; }