<?php

/*
	This file is part of Phoenix PHP Script.

    Phoenix PHP is free script: 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 3 of the License, or
    (at your option) any later version.

    Phoenix PHP 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
     	
	Author : Samer Moustafa
	Author Email : sam.dev@live.com
	Author URL : http://www.samdev.co.cc
*/



// Include Required Files That have the DataBase Info
require_once($_SERVER['DOCUMENT_ROOT'].'/proj_1/include.php');

	class database {
		
		//init Required Properties
		protected $host;
		protected $mysql_user;
		protected $mysql_password;
		protected $database;
		protected $connection;
		public $results;
		public $results_num;
		
		
		//Class Construction , used to establish Connction To database
		public function __construct() {
			$this->host = DATABASE_HOST;
			$this->mysql_user = DATABASE_USER;
			$this->mysql_password = DATABASE_PASSWORD;
			$this->database = DATABASE_NAME;
			$this->keep_alive();
			return $this->connection;
		}
		
		
		//َQuery Method For Send a Query To DataBase
		public function submit_query($query) {
			$this->results = mysql_query($query);
			if(is_bool($this->results)) {
				$this->results_num = mysql_affected_rows();
			}else {
				$this->results_num = mysql_num_rows($this->results);
				
			}
			return $this->results;
		}
		
		
		// Safe The Query Variables
		public function mysql_safe($var = false,$rejected = '') {
			//Call $this->keep_alive to keep connect to use mysql escape
			$this->keep_alive();
			if(!$var) {
				$_POST = array_map('mysql_real_escape_string',$_POST);
				$_GET = array_map('mysql_real_escape_string',$_GET);
			} else {
				if(is_array($var)) {
					array_map('mysql_real_escape_string',$var);
				} else {
					$var = mysql_real_escape_string($var);
					$var = addcslashes($var,$rejected); 
				}
			return $var;
			}
		}
		
		
		//Protected Method For Check Connection Status
		protected function check_connection() {
			if(!$this->connection)return false;
			else return true;
		}
		
		
		//Protected Method for Keep Connection Alive If Died
		protected function keep_alive() {
				if(!$this->check_connection()) {
					try {
						$this->connection =@ mysql_connect($this->host,$this->mysql_user,$this->mysql_password);
						if(!$this->connection) throw new Exception('Cannot Connect The Server');
						$this->db_connection =@ mysql_select_db($this->database,$this->connection);
						if(!$this->db_connection) throw new Exception('Database Not Found , Please Check it!');
					} catch (Exception $e) {
						echo '<h3>'.$e->getMessage()."</h3><br />\n";
						exit;
					  }
				}
		}
		
		
		public function close_it($con) {
			if(is_resource($con)) {
				mysql_close($this->connection);
			}
		}
		
		// Auto Tasks on Object Destruction for free memory
		public function __destruct() {
			if(is_resource($this->results)) {
				mysql_free_result($this->results);
				
			}
			
		}
		 
	}
	
?>
