<?php
class Database{
	
	var $_sql = '';
	var $_errorNum = 0;
	var $_errorMsg = '';
	var $_table_prefix = '';
	var $_resource = '';
	var $_cursor = null;
	var $_debug = 0;
	var $_log = array();
	var $_null_date = '0000-00-00 00:00:00';
	
	function Database($db_host='localhost', $db_user, $db_pass, $db_name, $db_prefix){
		if(!$this->_resource = @mysql_connect($db_host, $db_user, $db_pass))
			die("Error connecting to database : ".mysql_error());
		if(!mysql_select_db($db_name))
			die("Error selecting database : ".mysql_error());
		$this->_tablePrefix = $db_prefix;
	}
	
		
	function setBareQuery($sql){
		$this->_sql = $sql;
	}
	
	function setQuery($sql, $prefix='#__' ){
		$this->setBareQuery ($this->replacePrefix($sql, $prefix));
	}
	
	function replacePrefix($sql, $prefix='#__'){
		$done = '';
		while (strlen($sql)) {
			$single = preg_match("/\'([^\\\']|\\.)*'/", $sql,$matches_single,PREG_OFFSET_CAPTURE);
			if ($double = preg_match('/\"([^\\\"]|\\.)*"/', $sql,$matches_double,PREG_OFFSET_CAPTURE) OR $single) {
				if ($single == 0 OR ($double AND $matches_double[0][1] < $matches_single[0][1])) {
					$done .= str_replace($prefix, $this->_table_prefix, substr($sql,0,$matches_double[0][1])).$matches_double[0][0];
					$sql = substr($sql,$matches_double[0][1]+strlen($matches_double[0][0]));
				}
				else {
					$done .= str_replace($prefix, $this->_table_prefix, substr($sql,0,$matches_single[0][1])).$matches_single[0][0];
					$sql = substr($sql,$matches_single[0][1]+strlen($matches_single[0][0]));
				}
			}
			else return $done.str_replace($prefix, $this->_table_prefix,$sql);
		}
		return $done;
	}
	
	function query($sql = ''){
		if ($sql == '') $sql = $this->_sql;
		if ($this->_cursor = mysql_query($sql, $this->_resource)){
			$this->_errorNum = 0;
			$this->_errorMsg = '';
			return $this->_cursor;
		}
		else {
			$this->_errorNum = mysql_errno( $this->_resource );
			$this->_errorMsg = mysql_error( $this->_resource )." SQL=$sql";
			return false;
		}
	}
	
	function &retrieveResults ($key='', $max=0, $result_type='row') {
		$results = array();
		$sql_function = 'mysql_fetch_'.$result_type;
		if ($cur = $this->query()) {
			while ($row = $sql_function($cur)) {
				if ($key != '') $results[$row->$key] = $row;
				else $results[] = $row;
				if ($max AND count($results) >= $max) break;
			}
			mysql_free_result($cur);
		}
		return $results;
	}
	
	function loadResult() {
		$results =& $this->retrieveResults('', 1, 'row');
		if (count($results)) return $results[0][0];
		else return null;
	}
	
	function loadResultArray($numinarray = 0) {
		$results =& $this->retrieveResults('', 0, 'row');
		$values = array();
		foreach ($results as $result) $values[] = $result[$numinarray];
		if (count($values)) return $values;
		else return null;
	}
	
	function getErrorMsg(){
		return $_errorMsg;
	}
	
	function make_safe($variable) {
    	$variable = mysql_real_escape_string(trim($variable));
    	return $variable;
   	}
}

class DatabaseKontakti{
	
	var $_sql = '';
	var $_errorNum = 0;
	var $_errorMsg = '';
	var $_table_prefix = '';
	var $_resource = '';
	var $_cursor = null;
	var $_debug = 0;
	var $_log = array();
	var $_null_date = '0000-00-00 00:00:00';
	
	function Database($db_host='localhost', $db_user, $db_pass, $db_name, $db_prefix){
		if(!$this->_resource = @mysql_connect($db_host, $db_user, $db_pass))
			die("Error connecting to database : ".mysql_error());
		if(!mysql_select_db($db_name))
			die("Error selecting database : ".mysql_error());
		$this->_tablePrefix = $db_prefix;
	}
	
		
	function setBareQuery($sql){
		$this->_sql = $sql;
	}
	
	function setQuery($sql, $prefix='#__' ){
		$this->setBareQuery ($this->replacePrefix($sql, $prefix));
	}
	
	function replacePrefix($sql, $prefix='#__'){
		$done = '';
		while (strlen($sql)) {
			$single = preg_match("/\'([^\\\']|\\.)*'/", $sql,$matches_single,PREG_OFFSET_CAPTURE);
			if ($double = preg_match('/\"([^\\\"]|\\.)*"/', $sql,$matches_double,PREG_OFFSET_CAPTURE) OR $single) {
				if ($single == 0 OR ($double AND $matches_double[0][1] < $matches_single[0][1])) {
					$done .= str_replace($prefix, $this->_table_prefix, substr($sql,0,$matches_double[0][1])).$matches_double[0][0];
					$sql = substr($sql,$matches_double[0][1]+strlen($matches_double[0][0]));
				}
				else {
					$done .= str_replace($prefix, $this->_table_prefix, substr($sql,0,$matches_single[0][1])).$matches_single[0][0];
					$sql = substr($sql,$matches_single[0][1]+strlen($matches_single[0][0]));
				}
			}
			else return $done.str_replace($prefix, $this->_table_prefix,$sql);
		}
		return $done;
	}
	
	function query($sql = ''){
		if ($sql == '') $sql = $this->_sql;
		if ($this->_cursor = mysql_query($sql, $this->_resource)){
			$this->_errorNum = 0;
			$this->_errorMsg = '';
			return $this->_cursor;
		}
		else {
			$this->_errorNum = mysql_errno( $this->_resource );
			$this->_errorMsg = mysql_error( $this->_resource )." SQL=$sql";
			return false;
		}
	}
	
	function &retrieveResults ($key='', $max=0, $result_type='row') {
		$results = array();
		$sql_function = 'mysql_fetch_'.$result_type;
		if ($cur = $this->query()) {
			while ($row = $sql_function($cur)) {
				if ($key != '') $results[$row->$key] = $row;
				else $results[] = $row;
				if ($max AND count($results) >= $max) break;
			}
			mysql_free_result($cur);
		}
		return $results;
	}
	
	function loadResult() {
		$results =& $this->retrieveResults('', 1, 'row');
		if (count($results)) return $results[0][0];
		else return null;
	}
	
	function loadResultArray($numinarray = 0) {
		$results =& $this->retrieveResults('', 0, 'row');
		$values = array();
		foreach ($results as $result) $values[] = $result[$numinarray];
		if (count($values)) return $values;
		else return null;
	}
	
	function getErrorMsg(){
		return $_errorMsg;
	}
	
	function make_safe($variable) {
    	$variable = mysql_real_escape_string(trim($variable));
    	return $variable;
   	}
}

class DatabaseConnect extends Database{
	
	function DatabaseConnect(){
		$db_host = Ultraviolet::get('db_host');
		$db_user = Ultraviolet::get('db_user');
		$db_pass = Ultraviolet::get('db_pass');
		$db_name = Ultraviolet::get('db_name');
		$db_prefix = Ultraviolet::get('db_prefix');
		parent::Database($db_host, $db_user, $db_pass, $db_name, $db_prefix);
	}
	
	function &getInstance(){
		static $instance;
		if(!is_object($instance))
			$instance = new DatabaseConnect();
		return $instance;
	}

}

?>