<?php

/**
 * @class hosting
 * @brief Manage, display hostings
 * 
 * @author Vincent Lemoine
 *
 * @modified Benjamin Mercier
 * @date 18/07/2009
 * @version 0.1
*/
class hosting {
	
	/**
	 * @brief Array with information from current hosting for all API.
	 * @brief Null when hosting is not initialized
	*/
	public $information_hosting = null;
	
	/**
	 * @brief Cache variable with total of hostings for the current user for all API
	*/
	private $user_hostings_count = null;
	
	/**
	 * @brief Result test for userInitializeHosting.
	 * @brief Get the result code before be used by userCheckAccess
	*/
	private $result_test = null;

	/** 
	 * @brief initialize the hosting of the user
	 * @return 0 : Hosting deactivated
	 * @return 1 : No hosting specified
	 * @return 2 : Data server is deactivated
	 * @return 3 : MySQL server is deactivated
	 * @return 4 : dns server is deactivated
	 * @return 5 : mail server is deactivated
	 * @return 6 : web server is deactivated
	 * @return 7 : All is okay, information setting
	*/
	public function userInitializeHosting()
	{
		if ( !isset($_SESSION['hosting_infos']) ) {
			if ( !is_null($this->information_hosting) ) throw new myException('Session hosting_information is not initialized but hosting_information is not null');
			$this->result_test = 1;
			return 1;
		} else {
			$hosting_infos = $_SESSION['database']->clearString($_SESSION['hosting_infos']);
			$result = $this->listHostingsByClause(0, 1, "h.id = '$hosting_infos'");
			if ( count($result) == 0 ) throw new myException('Hosting_infos is defined but cannot find the hosting associated');
			if ( $result[0]->user_id != $_SESSION['user']->information_user->userid ) throw new myException('The hosting is not for the specified user');
			if ( $result[0]->hosting_active == 'false' ) {
				$this->result_test = 0;
				return 0;
			} elseif ( ($result[0]->data_active == 'false') or ($result[0]->data_server_active == 'false') ){
				$this->result_test = 2;
				return 2;
			} elseif ( ($result[0]->mysql_active == 'false') or ($result[0]->mysql_server_active == 'false')) {
				$this->result_test = 3;
				return 3;
			} elseif ( ($result[0]->dns1_active == 'false' or ($result[0]->dns1_server_active == 'false'))) {
				$this->result_test = 4;
				return 4;
			} elseif ( ($result[0]->dns2_active == 'false') or ($result[0]->dns2_server_active == 'false')) {
				$this->result_test = 4;
				return 4;
			} elseif ( ($result[0]->smtp_active == 'false') or ($result[0]->smtp_server_active == 'false')) {
				$this->result_test = 5;
				return 5;
			} elseif ( ($result[0]->smtps_active == 'false') or ($result[0]->smtps_server_active == 'false')) {
				$this->result_test = 5;
				return 5;
			} elseif ( ($result[0]->pop_active == 'false') or ($result[0]->pop_server_active == 'false')) {
				$this->result_test = 5;
				return 5;
			} elseif ( ($result[0]->pops_active == 'false') or ($result[0]->pops_server_active == 'false')) {
				$this->result_test = 5;
				return 5;
			} elseif ( ($result[0]->imap_active == 'false') or ($result[0]->imap_server_active == 'false')) {
				$this->result_test = 5;
				return 5;
			} elseif ( ($result[0]->imaps_active == 'false') or ($result[0]->imaps_server_active == 'false')) {
				$this->result_test = 5;
				return 5;
			} elseif ( ($result[0]->http_active == 'false') or ($result[0]->http_server_active == 'false')) {
				$this->result_test = 6;
				return 6;
			}

			$this->information_hosting = $result[0];
			$this->result_test = 7;
			$_SESSION['hosting_infos'] = $result[0]->id;
			return 7;
		}


	} // End of initialiazeHosting
	
	
	/**
	 * @brief Check autorization and redirect or make exception if is not correct.
	 * @return TRUE only, throw or redirect if not correct.
	*/
	public function userCheckAccess()
	{
		if ( is_null($this->result_test) ) throw new myException('Hosting initialization not initialized');
		if ( $this->result_test == 0 ) redirect('error-5.xhtml');
		elseif ( $this->result_test == 1 ) redirect('myhostings.xhtml');
		elseif ( ($this->result_test > 1) and ($this->result_test < 7) ) redirect ('error-6.xhtml');
		elseif ( $this->result_test == 7 ) return true;
		else throw new myException('result_code is unknow');
	} // End of userCheckAccess
	
	/**
	 * @brief Initialize hosting by ID
	 * @brief ID of the hosting
	 * @return true : Hosting added
	*/
	public function userSetCurrentHosting($id)
	{
		$id = $_SESSION['database']->clearString($id);
		$result = $this->listHostingsByClause(NULL, NULL, "h.id = '$id'");
		if ( count($result) == 0 ) throw new myException('Hosting selected is not found');
		elseif ( $result[0]->user_id != $_SESSION['user']->information_user->userid ) throw new myException('Hosting is not allowed to this user');
		$_SESSION['hosting_infos'] = $result[0]->id;
		return true;
	}

    /**
	 * @brief List all hostings of the current member
	 * @param start -> Record of starting listing
	 * @param extract -> Number of records to extract
	 * @return array with information (nul if empty)
	*/
    public function userListHostings($start = NULL, $extract = NULL)
    {
        $user_id=$_SESSION['user']->information_user->userid;
        
        return $this->listHostingsByClause($start, $extract, "h.user_id = $user_id");
    } // End of userListHostings
    
	/**
	 * @brief List all hostings with a given clause
	 * @param start -> Record of starting listing
	 * @param extract -> Number of records to extract
	 * @return array with information (nul if empty)
	*/
	private function listHostingsByClause($start = NULL, $extract = NULL, $clause = NULL) 
	{
		if ( !is_null($start) and !is_null($extract) ) {
			$limit = "LIMIT $start, $extract";
		} else $limit = NULL;
	
		if ( !is_null($clause) ) {
				$clause = "WHERE $clause";
		}
		
		$req = "SELECT
					h.id AS id,
					h.base_name,
					UNIX_TIMESTAMP(h.start_date) AS start_date,
					UNIX_TIMESTAMP(h.end_date) AS end_date,
					h.is_active AS hosting_active,
					h.user_id,
					h.offer_id,
					dns1.id AS dns1_id,
					dns1.web_ip AS dns1_ip,
					dns1.port AS dns1_port,
					dns1.is_active AS dns1_active,
					dns1_s.is_active AS dns1_server_active,
					dns2.id AS dns1_id,
					dns2.web_ip AS dns2_ip,
					dns2.port AS dns2_port,
					dns2.is_active AS dns2_active,				
					dns2_s.is_active AS dns2_server_active,
					data.id AS data_id,
					data.web_ip AS data_ip,
					data.port AS data_port,
					data.is_active AS data_active,
					data_s.is_active AS data_server_active,
					http.id AS http_id,
					http.web_ip AS http_ip,
					http.port AS http_port,
					http.is_active AS http_active,
					http_s.is_active AS http_server_active,
					smtp.id AS smtp_id,
					smtp.web_ip AS smtp_ip,
					smtp.port AS smtp_port,
					smtp.is_active AS smtp_active,
					smtp_s.is_active AS smtp_server_active,
					smtps.id AS smtps_id,
					smtps.web_ip AS smtps_ip,
					smtps.port AS smtps_port,
					smtps.is_active AS smtps_active,
					smtps_s.is_active AS smtps_server_active,
					pop.id AS pop_id,
					pop.web_ip AS pop_ip,
					pop.port AS pop_port,
					pop.is_active AS pop_active,
					pop_s.is_active AS pop_server_active,
					pops.id AS pops_id,
					pops.web_ip AS pops_ip,
					pops.port AS pops_port,
					pops.is_active AS pops_active,
					pops_s.is_active AS pops_server_active,
					imap.id AS imap_id,
					imap.web_ip AS imap_ip,
					imap.port AS imap_port,
					imap.is_active AS imap_active,
					imap_s.is_active AS imap_server_active,
					imaps.id AS imaps_id,
					imaps.web_ip AS imaps_ip,
					imaps.port AS imaps_port,
					imaps.is_active AS imaps_active,
					imaps_s.is_active AS imaps_server_active,
					mysql.id AS mysql_id,
					mysql.web_ip AS mysql_ip,
					mysql.port AS mysql_port,
					mysql.is_active AS mysql_active,
					mysql_s.is_active AS mysql_server_active,
					o.name AS offer_name,
					o.is_active AS offer_active,
					o.databases_number AS offer_databases_number,
					o.domains_number AS offer_domains_number,
					o.crons_number AS offer_crons_number,
					o.dns_domains_number AS offer_dns_domains_number,
					o.virtualhosts_number AS offer_virtualhosts_number,
					o.email_accounts_number AS offer_email_accounts_number,
					o.email_accounts_space AS offer_email_accounts_space,
					o.email_alias_number AS offer_email_alias_number,
					o.space_limit AS offer_space_limit,
					o.trafic_limit AS offer_trafic_limit,
					o.service_smtp AS offer_service_smtp,
					o.service_smtps AS offer_service_smtps,
					o.service_pop AS offer_service_pop,
					o.service_pops AS offer_service_pops,
					o.service_imap AS offer_service_imap,
					o.service_imaps AS offer_service_imaps,
					o.service_mysql AS offer_service_mysql
				FROM hostings AS h
				LEFT JOIN services AS dns1
				ON dns1.id = h.service_dns1
				LEFT JOIN servers AS dns1_s
				ON dns1_s.id = dns1.servers_id
				LEFT JOIN services AS dns2
				ON dns2.id = h.service_dns2
				LEFT JOIN servers AS dns2_s
				ON dns2_s.id = dns2.servers_id
				LEFT JOIN services AS data
				ON data.id = h.service_data
				LEFT JOIN servers AS data_s
				ON data_s.id = data.servers_id
				LEFT JOIN services AS http
				ON http.id = h.service_http
				LEFT JOIN servers AS http_s
				ON http_s.id = http.servers_id
				LEFT JOIN services AS smtp
				ON smtp.id = h.service_smtp
				LEFT JOIN servers AS smtp_s
				ON smtp_s.id = smtp.servers_id
				LEFT JOIN services AS smtps
				ON smtps.id = h.service_smtps
				LEFT JOIN servers AS smtps_s
				ON smtps_s.id = smtps.servers_id
				LEFT JOIN services AS pop
				ON pop.id = h.service_pop
				LEFT JOIN servers AS pop_s
				ON pop_s.id = pop.servers_id
				LEFT JOIN services AS pops
				ON pops.id = h.service_pops
				LEFT JOIN servers AS pops_s
				ON pops_s.id = pops.servers_id
				LEFT JOIN services AS imap
				ON imap.id = h.service_imap
				LEFT JOIN servers AS imap_s
				ON imap_s.id = imap.servers_id
				LEFT JOIN services AS imaps
				ON imaps.id = h.service_imaps
				LEFT JOIN servers AS imaps_s
				ON imaps_s.id = imaps.servers_id
				LEFT JOIN services AS mysql
				ON mysql.id = h.service_mysql
				LEFT JOIN servers AS mysql_s
				ON mysql_s.id = mysql.servers_id
				LEFT JOIN offers AS o
				ON h.offer_id = o.id
			$clause $limit";
		$result = $_SESSION['database']->fetchObject($req);
		return $result;
	} // End of listHostingsByClause
	
	/**
	 * @brief Get number of hostings for the current user
	 * @return number of hostings for the user
	*/
	public function userCountHostings() 
	{
		// Try to get the value from the cache
		if( !is_null($this->user_hostings_count) ) {
			return $this->user_hostings_count;
		}
		
		$user_id = $_SESSION['user']->information_user->userid;
		$req = "SELECT COUNT(id) AS total FROM hostings WHERE user_id='$user_id'";
		$result = $_SESSION['database']->fetchObject($req);
		$this->user_hostings_count = $result[0]->total;
		
		return $this->user_hostings_count;
	}
	
	/**
	 * @brief Get number of hosting pages, regarding the total count of records and the count of items to be shown per page
	 * @return number of pages availables
	*/
	public function userCountTotalPages() 
	{
		$items_count = $this->userCountHostings();
		$pages_count = intval($items_count / RECORD_BY_PAGE);
		if ( ($items_count % RECORD_BY_PAGE) != 0 ) {
			$pages_count++;
		}
		return $pages_count;
	}
	 

} // End of class
?>
