<?php	// Copyright by Mateusz Rusin © 2004 // 1.32
require_once ('class.php');
class DB extends DBdriver	// Copyright by Mateusz Rusin © 2004 // 1.32
{
	function Select ($table, $conditions = '', $fields = '*', $order = '', $dir = '', $limit = false, $offset = '')	// Copyright by Mateusz Rusin © 2004 // 1.30
	{
		if ($conditions)
			$conditions = $this -> build_conditions ($conditions);
		if (!is_array ($fields))
			$fields = explode (',', $fields);
		array_walk (&$fields, array (&$this, 'sql_trim'));
		$fields = implode (', ', $fields);
		if ($order)
		{
			$order = ' order by ' . $this -> sql_trim ($order);
			if ($dir)
				$order .= " $dir";
		}
		if ($limit)
		{
			$limit = ' limit ' . $limit;
			if ($offset)
				$limit .= ' offset ' . $offset;
		}
		else 
			$limit = '';
		return $this -> Query ('select ' . $fields . ' from ' . $this -> sql_trim ($table) . $conditions . $order . $limit);
	}
	function Delete ($table, $conditions = '', $order = '', $limit = '')	// Copyright by Mateusz Rusin © 2004 // 1.30
	{
		if ($conditions)
			$conditions = $this -> build_conditions ($conditions);
		if ($order)
			$order = ' order by ' . $this -> sql_trim ($order);
		if ($limit)
			$limit = ' limit ' . $limit;
		return $this -> Query ('delete from ' . $this -> sql_trim ($table) . $conditions . $order . $limit);
	}
	function Update ($table, $conditions, $fields)	// Copyright by Mateusz Rusin © 2004 // 1.30
	{
		array_walk (&$fields, array (&$this, 'sql_trim_key'));
		return $this -> Query ('update ' . $this -> sql_trim ($table) . ' set ' . implode (', ', $fields) . $this -> build_conditions ($conditions));
	}
	function Insert ($table, $fields)	// Copyright by Mateusz Rusin © 2004 // 1.30
	{
		array_walk (&$fields, array (&$this, 'sql_trim_key'));
		return $this -> Query ('insert ' . $this -> sql_trim ($table) . ' set ' . implode (', ', $fields));
	}
	function sql_trim (&$value, $key = null)	// Copyright by Mateusz Rusin © 2004 // 1.01 for fuckin' wizja.net
	{
		$value = trim ($value);
		if (!strpos ($value, ' ') && $value != '*')
			$value = $this -> fucking_wizja ()
			?
				'`' . str_replace ('`', '', $value) . '`'
			:
				'`' . trim ($value, '`') . '`';
		return $value; 
	}
	function sql_trim_key (&$value, $key)	// Copyright by Mateusz Rusin © 2004 // 1.02
	{
		$value = $this -> sql_trim ($key) . ' = ' . 
			(
				(is_string ($value) && $value != 'null' && $value != 'true' && $value != 'false')
				?
					"'" . (get_magic_quotes_gpc () ? trim ($value) : mysql_real_escape_string (trim ($value))) . "'"
				:
					(is_array ($value) ? "'" . (get_magic_quotes_gpc () ? trim (implode (',', $value)) : mysql_real_escape_string (trim (implode (',', $value)))) . "'" : $value)
			);
	}
	function build_conditions ($conditions)	// Copyright by Mateusz Rusin © 2004 // 1.00
	{
		if (is_array ($conditions))
		{
			array_walk (&$conditions, array (&$this, 'sql_trim_key'));
			$conditions = implode (' and ', $conditions);
		}
		else
			if (strpos ($conditions, ' ') === false)
				$conditions = $this -> sql_trim ($conditions);
		return ' where ' . $conditions;
	}
	function fucking_wizja ()
	{
		return intval (substr (str_replace (array ('.', '_'), '', phpversion ()), 0, 2)) < 41;
	}
}
//<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
?>