<?php
/**
* db controller class
*/
class DB
{
var $_handle; // mysql handle
var $_qcount; // query count
var $_last_handle; // last mysql handle
/**
* constructor
*
* @param resource $handle MySQL connection
*/
function DB($handle)
{
$this->_handle = $handle;
$this->_qcount = 0;
}
/**
* get server version
*
* @return string
*/
function GetServerVersion()
{
return(mysql_get_server_info($this->_handle));
}
/**
* escape a string for use in SQL query
*
* @param string $str String
* @return string
*/
function Escape($str)
{
if(function_exists('mysql_real_escape_string'))
return(mysql_real_escape_string($str, $this->_handle));
else if(function_exists('mysql_escape_string'))
return(mysql_escape_string($str));
else
return(addslashes($str));
}
/**
* execute safe query
*
* @param string $query
* @return DB_Result
*/
function Query($query)
{
global $bm_modules, $mysql;
// replace {pre} with prefix
$query = str_replace('{pre}', $mysql['prefix'], $query);
// insert escaped values, if any
if(func_num_args() > 1)
{
$args = func_get_args();
$pos = 0;
for($i=1; $i<func_num_args(); $i++)
{
$pos = strpos($query, '?', $pos);
if($pos === false)
{
$szUsername = $args[$i];
break;
}
else
{
if(is_string($args[$i]) && (strcmp($args[$i], '#NULL#') == 0))
{
$intxt = 'NULL';
}
else if(is_array($args[$i]))
{
$intxt = '';
foreach($args[$i] as $val)
$intxt .= ',\'' . $this->Escape($val) . '\'';
$intxt = '(' . substr($intxt, 1) . ')';
if($intxt == '()')
$intxt = '(0)';
}
else
{
$intxt = '\'' . $this->Escape($args[$i]) . '\'';
}
$query = substr_replace($query, $intxt, $pos, 1);
$pos += strlen($intxt);
}
}
}
// has a module a better handle?
$handle = $this->_handle;
if(is_array($bm_modules))
foreach($bm_modules as $mKey=>$module)
if($bm_modules[$mKey]->Features('MySQLHandle'))
$handle = $bm_modules[$mKey]->MySQLHandle($query, $handle, $szUsername);
$ok = ($result = mysql_query($query, $handle));
// increment query count
$this->_qcount++;
// set last handle
$this->_last_handle = $handle;
// return new MySQL_Result object if query was successful
if($ok)
{
return(isset($result) ? new DB_Result($handle, $result, $query) : false);
}
else
{
if(strpos(strtolower($query), 'insert into ' . strtolower($mysql['prefix']) . 'logs') !== false)
{
// log table corrupt -> error page! (would end up in endless loop otherwise)
DisplayError(0x05, 'Log table error', 'Failed to write log entry to ' . $mysql['prefix'] . 'logs-table. Please check and repair the table.',
sprintf("Process:\n%s\n\nError number:\n%d\n\nError description:\n%s",
'Query',
mysql_errno(),
mysql_error()),
__FILE__,
__LINE__);
die();
}
else if(DEBUG)
{
// debug mode -> error page!
DisplayError(0x09, 'MySQL error', 'Failed to execute MySQL query.',
sprintf("Process:\n%s\n\nQuery:\n%s\n\nError number:\n%d\n\nError description:\n%s",
'Query',
$query,
mysql_errno(),
mysql_error()),
__FILE__,
__LINE__);
}
PutLog("MySQL-Error at '" . $_SERVER['SCRIPT_NAME'] . "': '" . mysql_error($handle) . "', tried to execute '" . $query . "'", PRIO_ERROR, __FILE__, __LINE__);
if(DEBUG)
die();
return(false);
}
}
/**
* get insert id
*
* @return int
*/
function InsertId()
{
return(mysql_insert_id($this->_last_handle));
}
/**
* get number of affected rows
*
* @return int
*/
function AffectedRows()
{
return(mysql_affected_rows($this->_last_handle));
}
}
/**
* db result class
*/
class DB_Result
{
var $_handle; // mysql handle
var $_result; // mysql result
var $_query;
/**
* constructor
*
* @param resource $handle
* @param resource $result
* @return DB_Result
*/
function DB_Result($handle, $result, $query = '')
{
$this->_handle = $handle;
$this->_result = $result;
$this->_query = $query;
}
/**
* fetch a row as array
*
* @return array
*/
function FetchArray($resultType = MYSQL_BOTH)
{
return(mysql_fetch_array($this->_result, $resultType));
}
/**
* fetch a row as object
*
* @return object
*/
function FetchObject()
{
return(mysql_fetch_object($this->_result));
}
/**
* get count of rows in result set
*
* @return int
*/
function RowCount()
{
return(mysql_num_rows($this->_result));
}
/**
* get field count
*
* @return int
*/
function FieldCount()
{
return(mysql_num_fields($this->_result));
}
/**
* get field name
*
* @param int $index Index
* @return string
*/
function FieldName($index)
{
return(mysql_field_name($this->_result, $index));
}
/**
* free result
*
*/
function Free()
{
if(!@mysql_free_result($this->_result))
PutLog(sprintf('mysql_free_result failed for <%s>',
$this->_query),
PRIO_DEBUG,
__FILE__,
__LINE__);
unset($this);
}
/**
* export result set as CSV
*
* @param string $lineBreakChar Line break character
* @param string $quoteChar Quoting character
* @param string $sepChar Seperator character
*/
function ExportCSV($lineBreakChar = "\n", $quoteChar = '"', $sepChar = ';')
{
// get fields
$fields = array();
for($i=0; $i<$this->FieldCount(); $i++)
$fields[] = $this->FieldName($i);
// print field list
$fieldList = '';
foreach($fields as $field)
$fieldList .= $sepChar . $quoteChar . addslashes($field) . $quoteChar;
$fieldList = substr($fieldList, 1) . $lineBreakChar;
echo $fieldList;
// print data
while($row = $this->FetchArray(MYSQL_ASSOC))
{
$columnList = '';
foreach($fields as $field)
$columnList .= $sepChar . $quoteChar . addslashes($row[$field]) . $quoteChar;
$columnList = substr($columnList, 1) . $lineBreakChar;
echo $columnList;
}
}
}
?>