<?
class class_db {	

	var $ErrorCode;//0 no Error, 1 Halt, 2 No Halt 

	var $config;

	var $page;

	function __construct($config, $page){ 

		$this->page = $page;
		$this->config = $config;

		$this->ErrorCode = 0;

		$connect=@mysql_connect(
					$this->config->getValue("database","host"),
					$this->config->getValue("database","user"),
					$this->config->getValue("database","password"));
		if ($connect){	
		  if (@mysql_select_db($this->config->getValue("database","db"))){
		    if (!@mysql_query("set names 'latin2'",$connect)){
		      $this->reportError(1,"", $this->config->getValue("database","setname_error_message"));
		    }
		  }else{
		    $this->reportError(1,"", $this->config->getValue("database","dbselect_error_message"));
		  }		
		}else{
		  $this->reportError(1,"", $this->config->getValue("database","dbconnect_error_message"));
		}
	}
	
	function reportError($errorCode, $query, $errorInfo) {
		$this->ErrorCode = $errorCode;	
		
		$this->page->contents = date("Y.m.d H:i:s")." ".$this->config->getValue("database","error_message")." ".$errorInfo;

		if ($this->config->getValue("database","debug_printerrors")) {			
			$this->page->setTag("title", $this->config->getValue("database","error_message"));
			$this->page->AppendTag("content", $this->page->getAppropriateTemplate($this->config->getValue("templates_path","")."/error.tmpl.php"));
			$this->page->out();
		}

		if ($this->config->getValue("database","debug_mailerrors")){

		    $pg_db_mail=new class_mail($this->config);
		    $pg_db_mail->sendMail("database",$this->modultype,$this->page->contents);

		}
		file_put_contents($this->config->getValue("log_path","")."/database_".date("Y-m-d").".log", $this->page->contents."\n", FILE_APPEND);
	}
	
	function getErrorCode() {
		return $this->ErrorCode;
	}

	function getOne($sql) {

		$query = @mysql_query($sql) ;

		if (!$query){
		  return false;
		}else{
		  if (@mysql_num_rows($query)>0){
		      $result = @mysql_fetch_array($query);
		      return $result;
		  }else{
		      return false;
		  }
		}	  
	}

	function insertOne($sql) {

		$query = @mysql_query($sql) ;

		if ($query){
		  return mysql_insert_id();
		}else{  
		  return false;  
		}	  
	}

	function insertMore($sql) {

		$query = @mysql_query($sql) ;

		if ($query){
		  return mysql_insert_id();
		}else{  
		  return false;  
		}	  
	}

	function deleteOne($sql) {

		$query = @mysql_query($sql) ;

		if ($query){
		  return true;
		}else{  
		  return false;  
		}	  
	}

	function updateOne($sql) {

		$query = @mysql_query($sql) ;

		if ($query){
		  return true;
		}else{  
		  return false;  
		}	  
	}

	function getAll($sql) {

		$query = @mysql_query($sql) ;

		if (!$query){
		  return false;
		}else{
		  if (@mysql_num_rows($query)>0){
		      $i=0;
		      while ($result = @mysql_fetch_array($query)){
			$resultAll[$i] = $result;
			$i++;
		      }
		      return $resultAll;
		  }else{
		      return false;
		  }
		}	  
	}

}

?>