connect() ) genMySQLErr( 'Database connect failed' ); if( !$MySQL->select_db() ) genMySQLErr( 'Database select failed' ); db_res( "SET NAMES 'utf8'" ); $cacheParam = array(); class CMySQL { var $host = DATABASE_HOST; var $sock = DATABASE_SOCK; var $port = DATABASE_PORT; var $user = DATABASE_USER; var $passwd = DATABASE_PASS; var $db = DATABASE_NAME; var $connected = false; var $link = null; function connect() { if ( $this->connected ) return; if( strlen($this->port) ) $this->port = ":".$this->port; if ( strlen($this->sock) ) $this->sock = ":".$this->sock; $this->link = @mysql_pconnect( $this->host . $this->port . $this->sock, $this->user, $this->passwd ); if ( $this->link ) $this->connected = true; return $this->connected; } function select_db() { return @mysql_select_db( $this->db ); } } function db_list_tables( $error_checking = true ) { global $MySQL; $res = mysql_list_tables($MySQL->db); if ( $error_checking && !$res ) genMySQLErr( 'Database list tables failed' ); return $res; } function db_get_encoding ( $error_checking = true ) { global $MySQL; $res = mysql_client_encoding($MySQL->link); if ( $error_checking && !$res ) genMySQLErr( 'Database get encoding error' ); return $res; } function db_res( $query, $error_checking = true ) { global $MySQL; $res = mysql_query( $query, $MySQL->link ); if ( $error_checking && !$res ) genMySQLErr( 'Database query error', $query ); return $res; } function db_arr( $query, $error_checking = true ) { $res = db_res( $query, $error_checking ); if( !$res ) return false; $arr = mysql_fetch_array( $res ); return $arr; } function db_assoc_arr( $query, $error_checking = true ) { $res = db_res( $query, $error_checking ); if( !$res ) return false; $arr = mysql_fetch_assoc( $res ); return $arr; } function db_value( $query, $error_checking = true, $index = 0 ) { $arr = db_arr( $query, $error_checking ); $val = $arr[$index]; return $val; } function fill_array( $res ) { global $MySQL; if (!$res) return false; $i = 0; $arr = array(); while( $r = mysql_fetch_array( $res ) ) $arr[$i++] = $r; return $arr; } function fill_assoc_array( $res ) { global $MySQL; if (!$res) return false; $i = 0; $arr = array(); while( $r = mysql_fetch_assoc( $res ) ) $arr[$i++] = $r; return $arr; } function getParam( $param_name, $use_cache = true ) { global $cacheParam; if ( $use_cache && isset($cacheParam[$param_name]) ) return $cacheParam[$param_name]; elseif ( !$line = db_assoc_arr( "SELECT `VALUE` FROM `GlParams` WHERE `Name` = '$param_name'" ) ) return false; $cacheParam[$param_name] = $line['VALUE']; return $line['VALUE']; } function getParamDesc( $param_name ) { if ( !$line = db_assoc_arr( "SELECT `desc` FROM `GlParams` WHERE `Name` = '$param_name'" ) ) return false; return $line['desc']; } function setParam( $param_name, $param_val ) { global $cacheParam; if ( !$res = db_res( "UPDATE `GlParams` SET `VALUE` = '".process_db_input($param_val)."' WHERE `Name` = '$param_name'" ) ) return false; $cacheParam[$param_name] = $line[$param_val]; return true; } function mysqlErrorReport() { mail( $site['bugReportMail'], "Error", "Error in $_SERVER[PHP_SELF]: " . mysql_error() . "\nQuery: '$query'" ); } function genMySQLErr( $out, $query ='' ) { global $site; $aBackTrace = debug_backtrace(); unset( $aBackTrace[0] ); if( $query ) { //try help to find error $aFoundError = array(); foreach( $aBackTrace as $aCall ) { foreach( $aCall['args'] as $argNum => $argVal ) { if( is_string($argVal) and strcmp( $argVal, $query ) == 0 ) { $aFoundError['file'] = $aCall['file']; $aFoundError['line'] = $aCall['line']; $aFoundError['function'] = $aCall['function']; $aFoundError['arg'] = $argNum; } } } if( $aFoundError ) { $sFoundError = <<Found error in file {$aFoundError['file']}
at line {$aFoundError['line']}. Called {$aFoundError['function']} function with erroneous argument #{$aFoundError['arg']}

EOJ; } } if( DB_FULL_VISUAL_PROCESSING ) { ?>
Error
Query:
{$query}
"; echo '
Mysql error:
'.mysql_error().'
'; echo '
'; echo $sFoundError; echo "Debug backtrace:
"; echoDbg( $aBackTrace ); echo "Called script: {$_SERVER['PHP_SELF']}
"; echo "Request parameters:
"; echoDbg( $_REQUEST ); echo '
'; } ?> \n"; if( strlen( $query ) ) $sMailBody .= "Query:\n{$query}\n\n"; $sMailBody .= "Mysql error:\n" . mysql_error() . "\n\n"; $sMailBody .= strip_tags( $sFoundError ); $sMailBody .= "Debug backtrace:\n" . print_r( $aBackTrace, true ) . "\n\n"; $sMailBody .= "Called script: {$_SERVER['PHP_SELF']}\n\n"; $sMailBody .= "Request parameters:\n" . print_r( $_REQUEST, true ) . "\n\n"; $sMailBody .= "--\nAuto-report system\n"; sendMail( $site['bugReportMail'], "Database error in ", $sMailBody ); } exit; } ?>