<?php

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your opinion) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

if (stristr(htmlentities($_SERVER['PHP_SELF']), "inc_database.php")) {
	die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Error:</b> This file cannot be opened directly!</td></tr></table>");
}

//Hooks
require_once("inc_hooks.php");

class database {
	var $dblink;
	var $host = "localhost";
	var $user = "root";
	var $pass;
	var $name;
	
	//PHP5 Constructor
	function __construct() {
		global $db_host,$db_user,$db_pass,$db_name;
			
		$this->host = $db_host;
		$this->user = $db_user;
		$this->pass = $db_pass;
		$this->name = $db_name;
	}
	//PHP4 Constructor
	function database() {
		global $db_host,$db_user,$db_pass,$db_name;
		
		$this->host = $db_host;
		$this->user = $db_user;
		$this->pass = $db_pass;
		$this->name = $db_name;
	}

	function connect() {
		$this->dblink = @mysql_connect($this->host,$this->user,$this->pass) or die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Database error:</b> Cannot establish connection.</td></tr></table>");
		@mysql_select_db($this->name,$this->dblink) or die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Database error:</b> Cannot select database.</td></tr></table>");
	}
	
	function disconnect() {
		@mysql_close($this->dblink);
	}
	
	function ping() {
		if (!mysql_ping($this->dblink)) {
			$this->disconnect();
			$this->connect();
		}
	}
	
	function query($query) {
		$result = mysql_query($query,$this->dblink);

		return $result;
	}
	
	function get_row($query) {
		$result = $this->query($query);
		$returned = mysql_fetch_assoc($result);
		@mysql_free_result($result);
		return $returned;
	}
	
	function get_list($query) {
		$returned = array();
		
		$result = $this->query($query);
		while ($row = mysql_fetch_assoc($result)) {
			$returned[] = $row;
		}
		
		@mysql_free_result($result);
		return $returned;
	}
	
	function get_num($query) {
		$result = $this->query($query);
		$num = @mysql_num_rows($result);
		@mysql_free_result($result);
		return $num;
	}
	
	//Do not print errors
	function get_num_noerr($query) {
		$result = @$this->query($query);
		$num = @mysql_num_rows($result);
		@mysql_free_result($result);
		return $num;
	}
	
	function optimize($gfx=false) {
		global $db_name;
		
		if ($gfx) { echo "<b>"._OPTIMIZINGDATABASE_."</b><br><br>"; }
		$result = mysql_list_tables($db_name);
		$i = 0;
		$toopt = array();
		while ($i < mysql_num_rows($result)) {
			$name_table = mysql_table_name($result, $i);
			if ($gfx) { echo "<img src='images/check-green.gif' title='"._TABLE_." $name_table "._OPTIMIZED_."' border='0' alt='Ok'> $name_table<br>"; }
			$toopt[] = $name_table;
			$i++;
		}
		$sql = "OPTIMIZE TABLE ".implode(",",$toopt);
		if ($result_set = @mysql_query($sql)) {
			if ($gfx) { echo "<br><b>"._OPTIMIZINGDATABASEFINISHED_."</b>"; }
			return true;
		} else {
			return false;
		}
		mysql_free_result($result_set);
	}
	
	function affected_rows(){
		return mysql_affected_rows();
	}
}


?>