<?php
/**
 * Database class
 *
 * Controls the MySQL Database connection, and MySQL Tasks
 *
 * @package    DamaPlus
 * @revision   $Revision: 3 $
 * @author     Vincent M. <admin@damaplus.org>
 * @license    http://www.gnu.org/licenses/gpl-3.0-standalone.html
 * @link       http://damaplus.org/downloads.php
 */
class Database
{
	/**
	 * @private $connection The database connection
	 */
	private $connection;
	
	/**
	 * @private $query The last preformed MySQL Query
	 */
	private $query;
	
	/**
	 * Constructs the MySQL connection, connecting to the database
	 */
	function __construct()
	{
		/**
		 * Not sure...
		 *if ($this->connection) {
		 *	mysql_close($this->connection);
		 *}
		 */
		if (DATABASE_TYPE == 'mysql') {
			$this->connection = mysql_connect(DATABASE_HOST . ':' . DATABASE_PORT, DATABASE_USER, DATABASE_PASS);
		} else {
			$this->connection = new mysqli(DATABASE_HOST, DATABASE_USER, DATABASE_PASS, NULL, DATABASE_PORT);
		}
		
		if (DATABASE_TYPE == 'mysql' && !$this->connection) {
			die("Unable to connect to MySQL Server.\n<!--\nMySQL Said:\n" . mysql_error() . "\n-->\n");
		}
		
		if (DATABASE_TYPE == 'mysqli' && $this->connection->connect_error) {
			die("Unable to connect to MySQL Server.\n<!--\nMySQLi Said:\n" . $this->connection->connect_error . "\n-->\n");
		}
		
		if (!$this->select_db()) {
			die("Unable to select database.");
		}
		
	}
	
	/**
	 * Selects the database we're going to use for DamaPlus
	 *
	 * @return boolean
	 */
	private function select_db()
	{
		return DATABASE_TYPE == 'mysql' ? mysql_select_db(DATABASE_NAME, $this->connection) : $this->connection->select_db(DATABASE_NAME);
	}
	
	/**
	 * Executes a simple MySQL Query
	 *
	 * @param   string $query The MySQL Query to execute
	 * @return  boolean
	 */
	function query($query)
	{
		$this->query = $query;
		$result      = "";
		$debug_text  = "";
		if (DATABASE_TYPE == 'mysql') {
			if ($result = mysql_query($query, $this->connection)) {
				return $result;
			} else {
				if (DEBUG) {
					$debug_text .= "\n<!--\nMySQL Query Error:\n";
					$debug_text .= "Error number: " . mysql_errno($this->connection) . "\n";
					$debug_text .= "Error: " . mysql_error($this->connection) . "\n";
					$debug_text .= "Query: $this->query\n-->\n";
					echo $debug_text;
					return false;
				}
			}
		} else {
			if ($result = $this->connection->query($query)) {
				return $result;
			} else {
				if (DEBUG) {
					$debug_text .= "\n<!--\nMySQL Query Error:\n";
					$debug_text .= "Error number: " . $this->connection->errno . "\n";
					$debug_text .= "Error: " . $this->connection->error . "\n";
					$debug_text .= "Query: $this->query\n-->\n";
					echo $debug_text;
					return false;
				}
			}
		}
	}
	
	/**
	 * Fetches data from a MySQL result
	 *
	 * @param   mysql $result The MySQL Query result
	 * @param   int    $row    The row number to retrieve
	 * @param   string $field  The field to fetch
	 * @return  mixed
	 */
	function result($result, $row, $field) 
	{
		if (DATABASE_TYPE == 'mysql') {
			return mysql_result($result, $row, $field);
		} else {
			$result->data_seek($row);
			$assoc  = $this->fetch_assoc($result);
			return $assoc[$field];
		}
		return null;
	}
	
	/**
	 * Fetches an associative array corresponding to the row
	 *
	 * @param  mysql $result The MySQL Query result
	 * @return mixed
	 */
	function fetch_assoc($result)
	{
		return DATABASE_TYPE == 'mysql' ? mysql_fetch_assoc($result) : $result->fetch_assoc();
	}
	
	/**
	 * Fetches an array of strings corresponding to the row
	 *
	 * @param  mysql $result The MySQL Query result
	 * @return mixed
	 */
	function fetch_array($result)
	{
		$array = array();
		while ($row = DATABASE_TYPE == 'mysql' ? mysql_fetch_array($result) : $result->fetch_array()) {
			$array[] = $row;
		}
		return $array;
	}
	
	/**
	 * Returns the count of rows from a result
	 *
	 * @param  mysql $result The MySQL Query result
	 * @return mixed
	 */
	function num_rows($result)
	{
		return DATABASE_TYPE == 'mysql' ? mysql_num_rows($result) : $result->num_rows;
	}
	
	/**
	 * Escapes a string for MySQL
	 * 
	 * @param  mixed $string The string to be escaped
	 * @return mixed
	 */
	function escape_string($string)
	{
		if (DATABASE_TYPE == 'mysql') {
			if (is_array($string)) {
				foreach($string as $key => $value) {
					$string[$key] = get_magic_quotes_gpc() ? mysql_real_escape_string(stripslashes($value), $this->connection) : mysql_real_escape_string($value, $this->connection);
				}
				return $string;
			} else {
				return get_magic_quotes_gpc() ? mysql_real_escape_string(stripslashes($string), $this->connection) : mysql_real_escape_string($string, $this->connection);
			}
		} else {
			if (is_array($string)) {
				foreach($string as $key => $value) {
					$string[$key] = get_magic_quotes_gpc() ? $this->connection->escape_string(stripslashes($value)) : $this->connection->escape_string($value);
				}
				return $string;
			} else {
				return get_magic_quotes_gpc() ? $this->connection->escape_string(stripslashes($string)) : $this->connection->escape_string($string);
			}
		}
	}
}
$db = new Database;