<?php

	//Security
	if( ! defined( 'IS_INIT' ) )
	{
		die( 'Unauthorized Access');
	}

class theme 
{
	private $Handler;                            //The Handler variable
	private $indexes = array();                   //Array for the template values
	private $language;
	
	//Construct
	public function __construct()
	{
		global $Handler;                         //Sets global
		$this->Handler = $Handler;               //Sets this Handler variable to the global variable, easier to call
		$this->SetValue( 'TITLE' , WebsiteTitle );
		$this->SetValue( "GAME_IP" , GAME_IP );
	}
	
	public function Listen()
	{
		if( isset( $_GET['ajax' ] ) )
		{
			include( PAGE_PATH . 'ajax.php' );
		}
		else
		{
			//Parse header
			$this->Parse( 'header.html' );
			
			//If a page was requested
			if( isset( $_GET['view'] ) )
			{
				if( $_GET['view'] == null )
				{
					$this->SetValue( 'ERROR' , $this->GetValue( 'ERR_PAGE_NULL' ) );
					$this->SetValue( 'GO_BACK' , $this->GetValue( 'GO_BACK' ) );
					$this->SetValue( 'RED_URL' , MAIN_URL );
					$this->Parse( 'error.html' );
				}
				else
				{
					$page = $_GET['view'];
					
					//Get the available pages
					$pages = explode( ',' , PAGES );
					
					//Check if the requested page is in pages
					if( in_array( strtolower( $page ) ,  $pages ) )
					{
						//Then check if the file exists
						if( file_exists( PAGE_PATH . $page . ".php" ) )
		            	{
		                	include( PAGE_PATH . $page . ".php" );
		            	}
		            	//If not, output error
		            	else
		            	{
		            		echo "Please make sure the page you added has an existing file.";
		            	}
		            	
					}
					//Else output error
					else
					{
						echo "The page does not exist.";
					}
				}
			}
			//Else parse the main page
			else
			{
				include( PAGE_PATH . 'main' . ".php" );
			}
			
			//Parse footer
			$this->Parse( 'footer.html' );
		}
	}
	
	public function Parse( $file , $type = null )
	{			
		if( $type == null )
		{
			$file = THEME_PATH . $file;
		}
                else
		{
			$file = ADM_THEME . $file;
		}
        //If the file doesn't exist echo it
        if( ! file_exists( $file ) )
        {
        	echo 'Failed to load template file - ' . $file . "<br />";
        }
        //Else parse it
        else
        {
            //Get the contents
            $output = file_get_contents( $file );
            
            //Replace the values
            foreach( $this->indexes as $key => $value )
            {
                $replace = '{' . $key . '}';
                $output = str_replace( $replace , $value , $output );
            }
            
            //Echo it
            echo $output;
        }
	}
	
	public function QuietParse( $file )
	{
		$file = THEME_PATH . $file;
		//If the file doesn't exist echo it
		if( ! file_exists( $file ) )
		{
			echo 'Failed to load template file - ' . $file;
		}
		//Else parse it
		else
		{
			//Get the contents
			$output = file_get_contents( $file );
	
			//Replace the values
			foreach( $this->indexes as $key => $value )
			{
				$replace = '{' . $key . '}';
				$output = str_replace( $replace , $value , $output );
			}
	
			//Echo it
			return $output;
		}
	}
	
	public function SetValue( $key , $value) //Sets value for the template
	{
		$this->indexes[$key] = $value;
	}
	
	public function GetValue( $key ) //Returns value from the requested key
	{
		//Set default variable
		$contents = null;
	
		//Gets language variable
		$language = $this->language;
	
		//Includes language file
		include( LIB_PATH . "lang/" . $language . ".php" );
	
		//Returns requested key from the language file
		return $contents[$key];
	}
	
	public function SetLanguage( $language )
	{
		//Values variable
		$contents = null;
		
		//Set the language
		$this->language = $language;
		
		//Set file path
		$file = LIB_PATH . "lang/" . $language . ".php";
		
		//Check if file exists
		if( file_exists( $file ) )
		{
			include( $file );
		}
		//Else include english file be default
		else
		{
			include( LIB_PATH . "lang/english.php" );
		}
		
		//Set the values
		foreach( $contents as $key => $value )
		{
			$this->SetValue( $key , $value );
		}
	}
	
	public function ListenAdmin( $logged = 0 )
	{
            if( $logged == 1 )
            {
                //Parse header
		$this->Parse( 'header.html' , 1 );
                
                
                include( ADM_PAGE_PATH . 'login' . ".php" );
                
                
                //Parse footer
		$this->Parse( 'footer.html' , 1 );
            }
            else
            {
		//Parse header
		$this->Parse( 'header.html' , 1 );
		
		//If a page was requested
		if( isset( $_GET['view'] ) )
		{
			if( $_GET['view'] == null )
			{
				$this->SetValue( 'ERROR' , $this->GetValue( 'ERR_PAGE_NULL' ) );
				$this->SetValue( 'GO_BACK' , $this->GetValue( 'GO_BACK' ) );
				$this->SetValue( 'RED_URL' , MAIN_URL );
				$this->Parse( 'error.html' );
			}
			else
			{
				$page = $_GET['view'];
				
				//Get the available pages
				$pages = explode( ',' , PAGES );
				
				//Check if the requested page is in pages
				if( in_array( strtolower( $page ) ,  $pages ) )
				{
					//Then check if the file exists
					if( file_exists( ADM_PAGE_PATH . $page . ".php" ) )
					{
                                                
						include( ADM_PAGE_PATH . $page . ".php" );
					}
					//If not, output error
					else
					{
						echo "Please make sure the page you added has an existing file.";
					}
					
				}
				//Else output error
				else
				{
					echo "The page does not exist.";
				}
			}
		}
		//Else parse the main page
		else
		{
			include( ADM_PAGE_PATH . 'main' . ".php" );
		}
		
		//Parse footer
		$this->Parse( 'footer.html' , 1 );
            }
        }
}

?>