<?php
	// tomekk
	// http://tomekk.org/
	// GPL - http://www.gnu.org/copyleft/gpl.html
	// last fix: 21-02-2014

	// added: multiple db support
	// changed: hosts definition

	class dbase
	{
		// 0 - errors output with echo ""
		// 1 - errors output with die("")
		// 2 - do not show extra class errors
		var $script_stop_on_error = 1;

		//////////////////////////////////////////////////////////////////////////////////////////////////////
		var $handle;
		var $query;
		var $fetch;
		var $queries_counter;

		function dbase( $conn_array )
		{
			$this->handle = @mysql_connect($conn_array['host'], $conn_array['user'], $conn_array['pass']);

			if (!$this->handle)
			{
				$this->error(mysql_error());
			}
	
			if (!@mysql_select_db($conn_array['db'], $this->handle))
			{
				$this->error(mysql_error());
			}

			if (isset($conn_array['force_enc']))
			{
				if (trim($conn_array['force_enc']) != '')
				{
					$this->query("SET NAMES " . $conn_array['force_enc']);
				}
			}
		}

		function query( $query_string ) 
		{
			$this->query = @mysql_query($query_string, $this->handle);

			if (!$this->query)
			{	
				$this->error(mysql_error() . " => '" . $query_string . "'");
			}

			$this->queries_counter++;

			return $this->query;
		}

		function fetch( $query_string )
		{
			$this->fetch = @mysql_query($query_string, $this->handle);

			if (!$this->fetch)
			{
				$this->error(mysql_error() . " => '" . $query_string . "'");
			}

			$this->queries_counter++;

			$result = mysql_fetch_row($this->fetch);

			return $result[0];
		}

		function insert( $sql_table, $sql_data )
		{
			$insert_table = "INSERT INTO `" . $sql_table . "` ";
		
			$rows = '';
			$values = '';

			foreach($sql_data as $row => $value)
			{
				$rows .= "`" . $row . "`" . ", ";
				if (strtoupper($value) == 'NULL')
				{
					$values .= "NULL, ";
				} else {
					$values .= "'" . $this->resc($value) . "'" . ", ";
				}
			}

			$rows = rtrim($rows, ', ');
			$values = rtrim($values,  ', ');

			$ready_query = $insert_table . "(" . $rows . ")" . " VALUES " . "(" . $values . ")";

			if (($this->query($ready_query)))
			{
				return true;
			} else {
				return false;
			}
		}

		function update( $sql_table, $sql_data, $sql_where )
		{
			$update_table = "UPDATE `" . $sql_table . "` SET ";

			$update_string = '';

			foreach($sql_data as $row => $value)
			{
				if (strtoupper($value) == 'NULL')
				{
					$update_string .= $row . "=" . "NULL, ";
				} else {
					$update_string .= $row . "=" . "'" . $this->resc($value) . "'" . ", ";
				}
			}

			$update_string = rtrim($update_string, ', ');

			$ready_query = $update_table . $update_string . " WHERE " . $sql_where;

			if (($this->query($ready_query)))
			{
				return true;
			} else {
				return false;
			}
		}

		function resc( $string ) 
		{
			return @mysql_real_escape_string($string);
		}

		function affected_rows()
		{
			return @mysql_affected_rows($this->handle);
		}

	        function num_rows()
		{
			return @mysql_num_rows($this->query);
		}
		
		function num_fields()
		{
			return @mysql_num_fields($this->query);
		}

		function seek_back()
                {
                        return @mysql_data_seek($this->query, 0);
                }

		// always +1 for force_enc
		function queries_counter()
		{
			return $this->queries_counter;
		}

		function last_ai_id()
		{

			return @mysql_insert_id($this->handle);
		}

		function close()
		{
			if (!@mysql_close($this->handle))
			{
				$this->error(mysql_error());
			}
		}

		private function error( $err_msg )
		{
			switch($this->script_stop_on_error)
			{
				case 0:
					echo "<font color='#ff0000'>MySQL</font> - <b>" . $err_msg . "</b><br />";
					break;
				case 1:
					die ("<font color='#ff0000'>MySQL</font> - <b>" . $err_msg . "</b><br />");					
					break;
			}
		}
	}
?>
