<?php

class install extends API
{
   protected $content = null;
   protected $MySQL   = null;
   
   public function Init()
   {
    
   }
   
   public function CreateInstance()
   {
         $this->getInstance()->setConfig();
		 $this->getInstance()->setIndexContent('index');
	     $this->getInstance()->setIndexDefault();
		 $this->getInstance()->setTemplateDir('templates/install/');
		 $this->getInstance()->setLogin();
		 $this->getInstance()->getLogin()->start();
		 $this->getInstance()->setSQL();
		 $this->getInstance()->setPlaceHolder();
		 $this->getInstance()->setContent($this->getInstance()->getTempDir(),$this->getInstance()->getTempError());
		 
		 $this->content  = $this->getInstance()->getContent();
   }
   
   
   public function CreateFile($file)
   {
      $this->content->ReadFile($file.$this->getInstance()->getContentFormat());
   }
   
   public function placeholder_fill($array)
   {
     $this->getInstance()->getPlaceHolder()->fill($array);
   }
   
   public function ShowFile()
   {
     $this->content->replace($this->getInstance()->getPlaceHolder()->output());
     $this->content->Show();
   }
   
   public function MySQLConnect()
   {
              $this->getInstance()->setConfig();
			  $config_db = $this->getInstance()->getConfig();
              $mysql_array =  array( $config_db->getServer(),
									 $config_db->getUser(),
									 $config_db->getPass(),
									 $config_db->getDB(),
									 $config_db->isConnect()
							       );   
              /* Datenbank Verbindung herstellen */							  
		      $MySQL = new MySQL($mysql_array,false);
			  $MySQL->Connect();
			  $this->MySQL = $MySQL;
   }
   
   public function CreateDatabase()
   {
      /* Database erstellen */
	 $SQL    = $this->getInstance()->getSQL();
	 $syntax = $SQL->CreateDatabase($config_db->getDB());
	 $SQL->query($syntax);
   }
   
   public function Update($file)
   {
    /* Neu erstellte Database select... */
	    $this->MySQL->select_db();
			  
	/* Auktionshaus SQL Daten hochladen..*/
		$this->MySQL->sql_file_update($file); //_mca.sql
   }
   
   public function WebSiteRefresh()
   {
     $this->getInstance()->getLogin()->refresh();
   }
   
   
   public function getContent()
   {
      return $this->content;
   }
   
   
   
   
   
     public function IncomingPost()
	 {
	    if(isset($_POST['server'])){
		   if(!empty($_POST['server']) && !empty($_POST['port'])&& !empty($_POST['name']) && !empty($_POST['password']) && !empty($_POST['datenbank'])){		   
		      return true;
		   } 
		}
	 
	   return false;
	 }
	 
	 public function CreateConfigFile(){
	  
	  $config = null;
	  
		if($this->IncomingPost()){
	  
			$Server = $_POST['server'];
			$Name   = $_POST['name'];
			$Pass   = $_POST['password'];
			$DB     = $_POST['datenbank'];
		 
		    $config .= '			
<?php
/**
 * Open Source - Content Managment System
 * Next Generation v2
 * @Coder:  Mutzii
 * @copyright  Copyright (c) 2012-2013 by Mutzii
 * File: config.inc.php
 */
			';
			$config .= '$server="'.$Server.'";';
			$config .= '$user="'.$Name.'";';
			$config .= '$pass="'.$Pass.'";';
			$config .= '$db ="'.$DB.'";';
			$config .= '$connect = true;';
			$config .= '?>';
	 
            
		}
		
	  return $config;
	 }
	 
	 
	 public function CreateConfigYmlFile()
	 {
	   $config = null;  
		if($this->IncomingPost()){
		
			$Server = $_POST['server'];
			$Port   = $_POST['port'];
			$Name   = $_POST['name'];
			$Pass   = $_POST['password'];
			$DB     = $_POST['datenbank'];
		 
		    $config .= 'MySQLServer: '.$Server.'';
			$config .= "\r\n";
			$config .= 'MySQLPort: '.$Port.'';
			$config .= "\r\n";
			$config .= 'MySQLUser: '.$Name.'';
			$config .= "\r\n";
			$config .= 'MySQLPass: '.$Pass.'';
			$config .= "\r\n";
			$config .= 'MySQLDB: '.$DB.'';
			$config .= "\r\n";
		
		}
		
		return $config;
	 }
	 
	 
	 public function isConfig(){
	     if($this->CreateConfigFile() == null){
		    return false;
		 }else{
		    return true;
		 }
	 }

   
}
?>