require ("database.interface.php");
require ("database_error.class.php");
/********************************************
* *
* Deze class is geschreven door Flance, *
* ookwel bekend als Jordi Betting. Deze *
* class mag onbeperkt gebruikt worden, *
* en ook onbeperkt gepubliceerd worden *
* zolang deze notitie blijft staan. *
* *
* Het is niet toegestaan om deze class te *
* verhandelen. *
* *
********************************************/
class db
//class db implements databaseInterface
{
//
// Variables
//
private $rConnect,$bShowError,$sLastQuery,$rLastQuery = NULL;
private $sErrorFile;
private $bConnected = FALSE;
private $iQuery = 0;
private $iExecStart;
//
// Constructor - makes connection - Set default data
//
public function __construct($aConndata,$bError=FALSE)
{
$this->iExecStart = microtime(TRUE);
$this->bShowError = $bError;
$max_parameters = 2; //het maximaal mogelijk aantal parameters
$this->set_errorfile();
try
{
//
// If the connection data is not an array
//
if(!is_array($aConndata))
{
//
// Display error message
//
throw new Foutmelding("DBclass : constructor : Eerste parameter, de database connectie data, moet in een array gegeven worden.",3);
}
//
// If the connection data is invalid
//
if(count($aConndata) != 4)
{
//
// Display error message
//
throw new Foutmelding("DBclass : constructor : Eerste parameter, de database connectie data, heeft een onjuist aantal waarden.",3);
}
//
// If second parameter isn't even a booleaan (noob!)
//
if(!is_bool($bError))
{
//
// Display error message
//
throw new Foutmelding("DBclass : constructor : Tweede parameter moet boolean zijn..",1);
$bError=FALSE;
}
//
// If there is an invalid amount of parameters
//
if(func_num_args() > $max_parameters)
{
throw new Foutmelding("DBclass : constructor : Er zijn te veel parameters meegegeven.",1);
}
$this->rConnect = @mysql_connect($aConndata['host'],$aConndata['user'],$aConndata['pass']);
//
// If connecting failed
//
if(!$this->rConnect)
{
throw new Foutmelding("DBclass : constructor : Kan geen verbinding met database maken.",3);
}
//
// if selecting database failed
//
if(!@mysql_select_db($aConndata['db']))
{
throw new Foutmelding("DBclass : constructor : Kan de database niet selecteren, controleer of de gebruiker de juiste permissies heeft.",3);
}
$this->bConnected = TRUE;
}
catch (Foutmelding $error)
{
echo $error; // het geven van de melding ( __toString() )
//
// If error type is a Warning
//
if ( $error->getCode() == 3 )
{
$error->setShowTrace($this->bShowError);
$error->traceError();
echo 'Het laden is stopgezet wegens een fatale error. Neem contact op met de webmaster en vernoem de bovenstaande error. Bedankt.';
exit;
//
// Stop everything and DIE!
//
}
}
}
//
// Destructor, will be executed when class is destroyed or at the end of the page automaticly, returns an array with stats
//
public function __destruct()
{
$this->is_connected();
@mysql_close($this->rConnect);
$this->bConnected=FALSE;
$this->free_result();
RETURN $this->get_stats();
}
//
// Function to calc the execution time from the beginning of the class till the moment this function is called
//
private function calcExecTime()
{
RETURN round(microtime(TRUE)-$this->iExecStart,6);
}
//
// Function gives class stats in an array
//
public function get_stats()
{
@$status = explode(" ", mysql_stat());
RETURN array($this->iQuery,$this->calcExecTime(),$status);
}
//
// checks if this thing is connected
//
public function is_connected()
{
// in princiepe kan deze functie nooit wat doen aangezien de database connectie al gecheckt word bij het connecten
if ($this->bConnected === FALSE)
{
echo "It's not possible to execute mysql functions if there is no connection.";
exit;
}
RETURN NULL;
}
public function set_errorfile($sFile="classes/error.database.php",$bCheck=FALSE)
{
try
{
//
// If user want to check wheter file exists and is writable
//
if($bCheck === TRUE)
{
//
// Do the check
//
if(file_exists($sFile) && is_writable($sFile))
{
//
// If it is, save settings and return TRUE
//
$this->sErrorFile = $sFile;
RETURN TRUE;
}
else
{
//
// File does not exist or is nog writable - Return FALSE
//
throw new Foutmelding("DBclass : set_errorfile() : Het opgegeven bestand bestaat niet of is niet beschrijfbaar",1);
}
}
else
{
$this->sErrorFile = $sFile;
RETURN TRUE;
}
}
catch (Foutmelding $error)
{
//
// GRML! There is an error
//
echo $error;
if ( $error->getCode() == 3 )
{
$error->setShowTrace($this->bShowError);
$error->traceError();
echo 'Het laden is stopgezet wegens een fatale error. Neem contact op met de webmaster en vernoem de bovenstaande error. Bedankt.';
exit;
//
// Stop everything
//
}
}
}
//
// Function to free last result
//
public function free_result()
{
@mysql_free_result();
$this->rLastQuery = NULL;
RETURN TRUE;
}
//
// Sql_error - shows a nice error and log it to a given file.
//
private function sql_error($sMelding="Geen error vermelding gegeven")
{
//
// Code for a new line and a seperation line
//
$nl ="
";
$lijn="|-*************************************************************************-|";
//
// Push all possible data in a variable
//
$errorlog = $nl.$lijn.$nl.$nl."General Error information:".$nl.$nl;
#$errorlog .= "Info about last query: ".mysql_info($this->rConnect).$nl;
$errorlog .= "MySQL protocol version: ".mysql_get_proto_info().$nl;
$errorlog .= "MySQL host info: ".mysql_get_host_info().$nl;
$errorlog .= "MySQL Client info: ".mysql_get_client_info().$nl;
$errorlog .= $nl;
$errorlog .= "Info about last query:".$nl;
$errorlog .= "Given reason: ".$sMelding.$nl;
$errorlog .= "Query: ".$this->sLastQuery.$nl;
$errorlog .= "MySQL Error: ".mysql_error().$nl;
$errorlog .= "MySQL ErrorNumber: ".mysql_errno().$nl;
$errorlog .= $nl;
$errorlog .= "MySQL Server stats:".$nl;
$mysql_stat = mysql_stat($this->rConnect);
$mysql_stat = $status = explode(' ',$mysql_stat);
foreach($mysql_stat AS $key => $value)
{
$errorlog .= " - ".$value.$nl;
}
$errorlog .= $nl;
@$errorlog .= "User information:".$nl;
@$errorlog .= "Ip Adress: ".$_SERVER['REMOTE_ADDR'].$nl;
@$errorlog .= "Remote host: ".$_SERVER['REMOTE_HOST'].$nl;
@$errorlog .= "Referer: ".$_SERVER['HTTP_REFERER'].$nl;
@$errorlog .= "Browser/OS: ".$_SERVER['HTTP_USER_AGENT'].$nl;
@$errorlog .= "Request page: ".$_SERVER['REQUEST_URI'].$nl;
@$errorlog .= "Request Time: ".time().$nl;
$errorlog .= $nl;
try
{
//
// if log file is not writable
//
if(!is_writable($this->sErrorFile))
{
echo $this->sErrorFile;
//
// Damn, show an error
//
throw new Foutmelding("DBclass : sql_error() : Error bestand is niet beschrijfbaar, geef deze de juiste permissies.",3);
}
//
// Try to write all information to file
//
$fp = fopen($this->sErrorFile, "a");
$fw = fwrite($fp,$errorlog);
fclose($fp);
if($fw===FALSE)
{
//
// Writing didn't work, put error on page
//
throw new Foutmelding("DBclass : sql_error() : Error bestand is wel beschrijfbaar maar de error wegschrijven is mislukt.",3);
}
RETURN TRUE;
}
catch (Foutmelding $error)
{
//
// OOPS, an error occured, print it
//
echo $error;
$error->setShowTrace($this->bShowError);
$error->traceError();
RETURN FALSE;
}
}
}
?>