<?php
/**
 * Open Source - Content Managment System
 * Next Generation v2
 * @Coder:  Mutzii
 * @copyright  Copyright (c) 2012-2013 by Mutzii
 * File: boot.inc.php 
 * @Description:  
      Class CMS ist das Herz vom Content Managment System.
      Man kann mit allen klassen die im includes Ordner stehn
	  miteinander verknüpfen. Sozusagen als CMS API.
      Die Methode Boot führt alles aus.  
 */ 

class CMS
{

  protected $VERSION = '0.5';
  protected static $_instance;
  protected $tempdir       = null;
  protected $temperror     = null;
  protected $placeholder   = null;
  protected $config        = null;
  protected $login         = null;
  protected $SQL           = null;
  protected $content       = null;
  protected $setIndex      = null;
  protected $IndexContent  = 'index';
  protected $IndexDefault  = null;
  protected $ContentFormat = '.html';
  protected $ajax          = null;
  
 
  public static function CreateInstance()
  {
      if(null == self::$_instance)
	  {
	     self::$_instance = new self();
		 return self::getInstance();
	  }
  }
  
  public static function getInstance()
  {
      if(null != self::$_instance)
	  {
	      return self::$_instance;
	  }
  }
  
  public function getVersion()
  {
    $ph = $this->getPlaceHolder();

	$array = array(
	  "{version}" => $this->VERSION
	  );
	
	 $ph->fill($array);
  }
  
  public function file_exist($ordner,$file)
  {
     $filename = ROOT.'/'.$ordner.'/'.$file;
     return file_exists($filename);
  }
  
  
  public function load_inc()
  { 
      if($this->file_exist("includes","includes.inc.php")){
		        require_once ROOT .'/includes/includes.inc.php';
				new Includes();
	  }
  } 

  public function DataBase_Connect()
  {
        $config = self::getInstance()->getConfig();
        $mysql_array =  array( $config->getServer(),
		                       $config->getUser(),
							   $config->getPass(),
							   $config->getDB(),
							   $config->isConnect()
							  );
        
		new MySQL($mysql_array,true);
  }  
  
  public function getPlaceHolder()
  {
    return $this->placeholder;
  }
  
  public function setPlaceHolder()
  {
     $this->placeholder = new placeholder();
  }
  
  public function setSQL()
  {
    $this->SQL = new SQL();
  }
  
  public function getSQL()
  {
    return $this->SQL;
  }
  
  public function setLogin()
  {
    $this->login = new login();
  }
  
  public function getLogin()
  {
    return $this->login;
  }
  
  public function setConfig()
  {
    $this->config = new ConfigLoader();
  }
  
  public function getConfig()
  {
     return $this->config;	 
  }
 
  public function setTemplateDir($TempDir)
  {
     $this->tempdir = $TempDir;
  }
  
  public function setTempError($temp_error_file_pfad)
  {
     $this->temperror = $temp_error_file_pfad;
  } 
  
  public function setContent($tempdir,$temperror)
  {
    $this->content = new Content($tempdir,$temperror);
  }
  
  public function getContent()
  {
     return $this->content;
  }
  
  public function getErrorList()
  {
     return new ErrorList();
  }
  
  
  public function getTempDir()
  {
    return  $this->tempdir;
  }
  
  public function getTempError()
  {
    return $this->temperror;
  }
  
  public function setIndexDefault()
  {
     $this->IndexDefault = $this->setIndex;
  }
  
  public function getIndexDefault()
  {
    return $this->IndexDefault;
  }
  
  
  public function setContentFormat($format)
  {
    $this->ContentFormat = $format;
  }
  
  public function getContentFormat()
  {
    return $this->ContentFormat;
  }
  
  public function setIndexContent($inhalt)
  {
    $this->setIndex = $inhalt;
  }
  
  public function getIndexContent()
  {     
	  if(!empty($_GET['overlay'])){
	       if($_GET['overlay'] == 'logout' || $_GET['overlay'] == 'index'){
		      return $this->getIndexDefault();   
		   }
		     
			if($this->setIndex != $this->getIndexDefault()){
			        $this->IndexContent = $this->setIndex;
					$this->setIndexContent($this->getIndexDefault());
			}else{
			        $this->IndexContent = $_GET['overlay'];	 
			}
		   
			$this->ContentSecurity();	   
	        return $this->IndexContent;
	  }else{
	     return $this->getIndexDefault();
	  }
	  
  }
  
  public function ContentSecurity()
  {
        $security = array('/','\\','\/','.','<','>','?','(',')',';','header','footer','php','html','xml','css','exe','script','alert','"','$','echo');
		$this->IndexContent = str_replace($security,"", $this->IndexContent);
		if($this->IndexContent == ""){ $this->IndexContent = $this->getIndexDefault();}
  }
  
  public function LoginStatus()
  {
     return $this->getLogin()->check_login();
  }
  
 
  public function Templates()
  {
	$_content = new Content($this->getTempDir(),$this->getTempError());		
		
	$_content->ReadFile('header'.$this->getContentFormat());
	$_content->ReadFile($this->getIndexContent().$this->getContentFormat());
	$_content->ReadFile('footer'.$this->getContentFormat());
	
    $_content->replace($this->getPlaceHolder()->output());

    $_content->Show();	
  }
  
  
  public function loadingModule()
  {
      $ordner = ROOT.'/module/';
	  $handle = opendir($ordner);
	  
	  if($handle != false){
	     while ($file = readdir ($handle)) {
		     if($file != "." && $file != "..") {
			    
				$file = str_replace(".modul.php", "", $file);
				
                $Modul = new ModulLoader();
                $Modul->ClassLoad($file);
                $Modul->setCMSInstance($this->getInstance());				
				$Modul->getClassInstance();
                $Modul->Run();				
				
		     }
	     }
	  }
 
      closedir($handle); 
  }
  
  public function getAPI($name)
  {
      $API    = new APILoader();
      $API->ClassLoad($name);
      $API->setCMSInstance($this->getInstance());
	  $API->Activ();
		 
	return $API->getClassInstance();
  }
  
  public function getAuktionAPI()
  {
  
		 $API    = new APILoader();
		 $API->ClassLoad("auktion");
		 $API->setCMSInstance($this->getInstance());
		 $API->Activ();
		 
	return $API->getClassInstance();
  }
  
  public function getAjaxAPI()
  {
         $API    = new APILoader();
		 $API->ClassLoad("ajax");
		 $API->setCMSInstance($this->getInstance());
		 $API->Activ();
		 
	return $API->getClassInstance(); 
  }
  
   public function getInstallAPI()
  {
         $API    = new APILoader();
		 $API->ClassLoad("install");
		 $API->setCMSInstance($this->getInstance());
		 $API->Activ();
		 
	return $API->getClassInstance(); 
  }
  
  public static function Boot()
  {
    /* CMS Instance */
	 $instance = self::CreateInstance();
	 
	/* Loading include class files */
	 $instance->load_inc();
	 
	 $ajax = $instance->getAPI("ajax");
	 /* Ajax Deaktivieren.
	    $ajax->getAjaxDisable();
	 */
	 
	 
	 if($ajax->isAjax("buy")){
	      if($ajax->AjaxInstance(3)){
		        
				 $API = $instance->getAPI("auktion");		 
				 if(isset($_GET["buyId"])){
                     if(!empty($_GET["buyId"]) || $_GET["buyId"] != ""){
                    
					   if(!empty($_GET['move'])){
					      $move = $_GET['move'];
					   }else{
					      $move = 1; // Default Postfach
					   }
					      
                           $Output = $API->Buy($_GET["buyId"],$move);
						   echo $Output;
						   
                     }
                 } 	
		  
		  }
	 }
	 
	 if($ajax->isAjax("true")){
	    if($ajax->AjaxInstance(3)){
	        
			if($ajax->isAjaxPara()){$ajax->Warenkorb();}
				   
	    }
	 }
	 
	 if($ajax->isAjax("nav1")){
	    if($ajax->AjaxInstance(3)){
	        
			if($ajax->isAjaxPara()){$ajax->AjaxListe("auktion");}
		}
	}

	 if($ajax->isAjax("nav2")){
	    if($ajax->AjaxInstance(3)){
	        	
			    if($ajax->isAjaxPara()){$ajax->AjaxListe("lagerhaus");}
		}
	}

     if($ajax->isAjax("nav3")){
	    if($ajax->AjaxInstance(3)){
	        
			    if($ajax->isAjaxPara()){$ajax->AjaxListe("postfach");}
		}
	}  
	
	
	 if($ajax->isAjax("createItem")){
	    if($ajax->AjaxInstance(3)){
	        
			    if($ajax->isAjaxPara()){
				
				     $money = $_GET['money'];
					 $mode  = $_GET['mode'];
					 $IDs   = $ajax->CAPTA($_GET['parameter']);
					 
					 if(strcmp($mode,"nav2") == false){
					    
						  $size = count($IDs);
					      $SQL = $instance->getSQL();
					      $API = $instance->getAPI("auktion");	

                          for($x = 0; $x < $size; $x++){
                              
                                $result = $API->LagerDaten($IDs[$x]);
								$daten_array = array();
                                while($daten = mysql_fetch_object($result)){

								 $daten_array = $API->toArray($daten_array,$API->getLoginUser());    
								 $daten_array = $API->toArray($daten_array,$daten->ItemID);     
								 $daten_array = $API->toArray($daten_array,$daten->ItemStack);  
								 $daten_array = $API->toArray($daten_array,$money);  
								 $daten_array = $API->toArray($daten_array,$daten->ItemBytes);  
								 $daten_array = $API->toArray($daten_array,$daten->EnchString); 
								 $daten_array = $API->toArray($daten_array,$daten->EnchInt);
								 $daten_array = $API->toArray($daten_array,$daten->Damage); 	

                                }
								
							 if($API->isLoginPlayer("lagerhaus",$IDs[$x])){
							 
	                              $API->InsertToAuktion($daten_array);
								  $API->RemoveEntry("lagerhaus",$IDs[$x]);
							 }
                          }						  
					     	 
					 }else{
					    echo $ajax->error->getMessage(3);
					 }
					 
				
				}
		}
	}  

     if($ajax->isAjax("delete")){
	    if($ajax->AjaxInstance(3)){
			    if($ajax->isAjaxPara()){
				     
					 $mode  = $_GET['mode'];
					 $IDs   = $ajax->CAPTA($_GET['parameter']);
				     $size  = count($IDs);
					 $API   = $instance->getAPI("auktion");
					 $items = false;
				      
					   for($x = 0; $x < $size; $x++){
                           if(strcmp($mode,"nav1") == false){ 
                                if($API->isLoginPlayer("auktion",$IDs[$x])){					   
                                      $API->RemoveEntry("auktion",$IDs[$x]);
									  $items = true;
							    } 
						   }
						   
						   if(strcmp($mode,"nav2") == false){
								if($API->isLoginPlayer("lagerhaus",$IDs[$x])){
                                      $API->RemoveEntry("lagerhaus",$IDs[$x]);
									  $items = true;
							    }
						   }
						   
						   if(strcmp($mode,"nav3") == false){  
								if($API->isLoginPlayer("postfach",$IDs[$x])){
                                     $API->RemoveEntry("postfach",$IDs[$x]);
									 $items = true;
								}
						   }
                       }
 
                       if($items){
						  echo $ajax->error->getMessage(4);
                       }					   
				
				}		
		} 
	 }
	
	 if($ajax->isAjax("moveToPost")){
	    if($ajax->AjaxInstance(3)){
			    if($ajax->isAjaxPara()){
				
				       $mode  = $_GET['mode'];
					   $IDs   = $ajax->CAPTA($_GET['parameter']);
				       $size  = count($IDs);
					   $API   = $instance->getAPI("auktion");
					   $move  = false;
					   
					   for($x = 0; $x < $size; $x++){
					   
					          if(strcmp($mode,"nav1") == false){
							    if($API->isLoginPlayer("auktion",$IDs[$x])){
							        $result = $API->AuktionsDaten($IDs[$x]);
                                    $array  = $API->DatabaseArray($result);
                                  
                                    $API->InsertToPostfach($array);
                                    $API->RemoveEntry("auktion",$IDs[$x]);
									$move = true;
                                }									
							  }
							  
							  if(strcmp($mode,"nav2") == false){
							     if($API->isLoginPlayer("lagerhaus",$IDs[$x])){
							        $result = $API->LagerDaten($IDs[$x]);
                                    $array  = $API->DatabaseArray($result);
                                  
                                    $API->InsertToPostfach($array);
                                    $API->RemoveEntry("lagerhaus",$IDs[$x]);
                                    $move = true;									
								}
							  }
								   
					   }
					   
					   if(strcmp($mode,"nav3") == false){
						   echo $ajax->error->getMessage(3);
					   }
					   
					   if($move){
						   echo $ajax->error->getMessage(5);
					   }
					   
			
				}
		}		
	}
	
	 if($ajax->isAjax("moveToLager")){
	    if($ajax->AjaxInstance(3)){
			    if($ajax->isAjaxPara()){
				
				   
				       $mode  = $_GET['mode'];
					   $IDs   = $ajax->CAPTA($_GET['parameter']);
				       $size  = count($IDs);
					   $API   = $instance->getAPI("auktion");
					   $move  = false;
					   
					   for($x = 0; $x < $size; $x++){
					   
					          if(strcmp($mode,"nav1") == false){
							    if($API->isLoginPlayer("auktion",$IDs[$x])){
							        $result = $API->AuktionsDaten($IDs[$x]);
                                    $array  = $API->DatabaseArray($result);
                                  
                                    $API->InsertToLager($array);
                                    $API->RemoveEntry("auktion",$IDs[$x]);
									$move = true;
                                }									
							  }
							  
							  if(strcmp($mode,"nav3") == false){
							     if($API->isLoginPlayer("postfach",$IDs[$x])){
							        $result = $API->PostDaten($IDs[$x]);
                                    $array  = $API->DatabaseArray($result);
                                  
                                    $API->InsertToLager($array);
                                    $API->RemoveEntry("postfach",$IDs[$x]);	
									$move = true;
								}
							  }
								   
					   }
					   
					   if(strcmp($mode,"nav2") == false){
						   echo $ajax->error->getMessage(3);
					   }
					   
					   if($move){
						  echo $ajax->error->getMessage(6);
					   }
					   
				
				
				}
		}		
	}
	
	
	 
	 if($ajax->getAjaxMode("default")){
			$_content = new Content("",$instance->getTempError());	
			if($_content->isReadFile("config.inc.php"))
			{
					$instance->System();
		
			}else{
					/* If config.inc.php Empty is...*/
					$install  = $instance->getAPI("install");
					$install->CreateInstance();
					$install->CreateFile("install");
					
					$array = array(
					
					   "{css}" => $instance->getTempDir()."css/"
					 
					 );		 
					$install->placeholder_fill($array);
					
					
					if($install->isConfig()){
									
                            $configfile = $install->CreateConfigFile();
							$ymlfile    = $install->CreateConfigYmlFile();
							$install->getContent()->setFile("config.yml",$ymlfile);
							$install->getContent()->setFile("config.inc.php",$config);
							
							$install->MySQLConnect();
							$install->CreateDatabase();
							$install->Update('_mca.sql');
							
							$install->WebSiteRefresh();
													
					}
					
					$install->ShowFile();			
			}
	 }
	 
  
  }
   
  public function System()
  {
     /* CMS Instance */
	 $instance = $this->getInstance();
		 
	 /* Config instance || Connect to Database || Startseite || Template Directory */
	 $instance->setConfig();
	 $instance->DataBase_Connect();
	 $instance->setIndexContent('index');
	 $instance->setIndexDefault();
	 $instance->setTemplateDir('templates/default/');
	 
	 /* Login instance */
	 $instance->setLogin();
	 $instance->getLogin()->start();
	 
	 /* SQL instance*/
	 $instance->setSQL();
	 	 
	 /* set PlaceHolder */
	 $instance->setPlaceHolder();
	 
	 
	  /* Module */
	 $instance->loadingModule();
	 
	 /* Language */
	 $lang      = $instance->getAPI("language");
	 $lang_ID   = $lang->getUserIdLanguage($instance->getAPI("auktion")->getLoginUser());
	 $user_lang = $lang->getUserLanguage($lang_ID);
	 $lang->Fill_Message_List($user_lang);
	 $lang->setPlaceHolders();
      
     /* Fill PlaceHolder from Database.*/
	 $ph_instance    = $instance->getPlaceHolder();
	 $database_array = $ph_instance->Loading('placeholder',$instance->getConfig()->isConnect(),$instance->getTempDir());
	 $ph_instance->fill($database_array);
	 
	 $instance->getVersion();
	 	 
	 /* Templates Output*/ 
	 $instance->Templates();
	 	   
  }
  
  
  



}

?>
