<?php
require_once (PATH_CONTROLLERS . "Common.php") ;
require_once (PATH_MODELS . "admin/UpdateDatabaseClass.php") ;
class Config {
	private $type = "" ;
	private $datas = array() ;
	
	function __construct() {
	}
	function __destruct() {
		unset($this->datas) ;
                
	}
	public function processRequest($params) {
		$this->type = REQ_VIEW ;
		try {
			if (isset($params) && count($params) > 0) {
				if (isset($params['type']))
					$this->type = $params['type'] ;
			}
			switch ($this->type) {
				case REQ_UPDATE:
					$this->saveConfig($params) ;
					break ;
				case REQ_GET:
					$this->getUserKey() ;
					break ;
				case REQ_QUERY:
					$this->testConnection($params) ;
					break ;
				case "UDPATE_DATABASE":
					$this->updateDatabase($params) ;
					break ;
				case REQ_VIEW:
					$this->getView() ;
					break ;
				default:
					$this->sendJsonResponse(Status::Error,"invalid request.","",$this->type) ;
					break ;
			}
			return true ;
		} catch (Exception $e) {
			die ($e->getMessage()) ;
		}
	}
	private function saveConfig($params) {
		try {
			
			$pwd = Util::decryptData($this->getParam($params,'dbpwd',""),$_SESSION['key']) ;
			$config = new ConfigDb(PATH_CODE . 'db.xml') ;
			$config->loadConfig() ;
			$config->setHost($this->getParam($params,'server',""));
			$config->setDbName($this->getParam($params,'dbname',"")) ;
			$config->setUserName($this->getParam($params,'dbuser',"")) ;
			$config->setPassword($pwd);
			$port = $this->getParamInt($params,'dbport',0) ;
			if ($port > 0)
				$config->setPortNo($port) ;
			else
				$config->setPortNo("") ;
			$config->setDbType($this->getParam($params,'dbtype',DbType::MsSql)) ;
            
			$pwd = Util::decryptData($this->getParam($params,'dbpwdsun',""),$_SESSION['key']) ;
			$config->setHostSun($this->getParam($params,'serversun',""));
			$config->setDbNameSun($this->getParam($params,'dbnamesun',"")) ;
			$config->setUserNameSun($this->getParam($params,'dbusersun',"")) ;
			$config->setPasswordSun($pwd);
			$config->setBusinessUnitSun($this->getParam($params,'businessunitsun',"")) ;
			$port = $this->getParamInt($params,'dbportsun',0) ;
			if ($port > 0)
				$config->setPortNoSun($port) ;
			else
				$config->setPortNoSun("") ;
			$config->setDbTypeSun($this->getParam($params,'dbtypesun',DbType::MsSql)) ;

			$pwd = Util::decryptData($this->getParam($params,'dbpwdnotif',""),$_SESSION['key']) ;
			$config->setHostNotif($this->getParam($params,'servernotif',""));
			$config->setDbNameNotif($this->getParam($params,'dbnamenotif',"")) ;
			$config->setUserNameNotif($this->getParam($params,'dbusernotif',"")) ;
			$config->setPasswordNotif($pwd);
			$port = $this->getParamInt($params,'dbportnotif',0) ;
			if ($port > 0)
				$config->setPortNoNotif($port) ;
			else
				$config->setPortNoNotif("") ;
			$config->setDbTypeNotif($this->getParam($params,'dbtypenotif',DbType::MsSql)) ;
			
			$pwd = Util::decryptData($this->getParam($params,'dbpwdrms',""),$_SESSION['key']) ;
			$config->setHostRms($this->getParam($params,'serverrms',""));
			$config->setDbNameRms($this->getParam($params,'dbnamerms',"")) ;
			$config->setUserNameRms($this->getParam($params,'dbuserrms',"")) ;
			$config->setPasswordRms($pwd);
			$port = $this->getParamInt($params,'dbportrms',0) ;
			if ($port > 0)
				$config->setPortNoRms($port) ;
			else
				$config->setPortNoRms("") ;
			$config->setDbTypeRms($this->getParam($params,'dbtyperms',DbType::MsSql)) ;
            
			$config->saveConfig() ;
			$this->sendJsonResponse(Status::Ok,"System Configuration successfully saved.","",$this->type) ;
		} catch (Exception $e) {
			Log::write('[Admin]' . $e->getMessage());
			$this->sendJsonResponse(Status::Error,"Sorry, there is a error in updating system configuration.","",$this->type) ;
		}
		unset($coy) ;
	}
	private function testConnection($params) {
		$status = "" ;
		$mesg = "" ;
		$pwd = Util::decryptData($this->getParam($params,'dbpwd',""),$_SESSION['key']) ;
		$host = $this->getParam($params,'server',"") ;
		$name = $this->getParam($params,'dbname',"") ;
		$user = $this->getParam($params,'dbuser',"") ;
		$port = $this->getParam($params,'dbport',"") ;
		$type = $this->getParam($params,'dbtype',"") ;
	
		if ($type == "") {
			$status = Status::Error ;
			$mesg = "Missing database type info." ;
		}
		if ($status == "") {
			if ($host == "" || $name == "") {
				$status = Status::Error ;
				$mesg = "Missing Server/Database info." ;
			}
		}
		if ($status == "") {
			if ($user == "" || $pwd == "") {
				$status = Status::Error ;
				$mesg = "Missing Database user name/password info." ;
			}
		}
		
		if ($status == "") {
			try {
				if ($type == DbType::MySql) {
					MySqlDb::testConnection($host,$name,$user,$pwd,$port) ;
				} else if ($type == DbType::MySqli) {
					MySqliDb::testConnection($host,$name,$user,$pwd,$port) ;
				} else {
					MsSqlDb::testConnection($host,$name,$user,$pwd,$port) ;
				}
				$mesg = "Database Connection Testing - Ok."  ;
			} catch (Exception $e) {
				$status = Status::Error ;
				$mesg = $e->getMessage() ;
			}
		}
		echo $mesg ;
	}
	private function updateDatabase($params){
		$status = "" ;
		$mesg = "" ;
		$pwd = Util::decryptData($this->getParam($params,'dbpwd',""),$_SESSION['key']) ;
		$host = $this->getParam($params,'server',"") ;
		$name = $this->getParam($params,'dbname',"") ;
		$user = $this->getParam($params,'dbuser',"") ;
		$port = $this->getParam($params,'dbport',"") ;
		$type = $this->getParam($params,'dbtype',"") ;

		if ($type == "") {
			$status = Status::Error ;
			$mesg = "Missing database type info." ;
		}
		if ($status == "") {
			if ($host == "" || $name == "") {
				$status = Status::Error ;
				$mesg = "Missing Server/Database info." ;
			}
		}
		if ($status == "") {
			if ($user == "" || $pwd == "") {
				$status = Status::Error ;
				$mesg = "Missing Database user name/password info." ;
			}
		}

		if ($status == "") {
			try {
                $clsUpdateDabatase = new UpdateDatabaseClass(null);
				if ($type == DbType::MySql) {
					MySqlDb::testConnection($host,$name,$user,$pwd,$port) ;
                    $clsUpdateDabatase = new UpdateDatabaseClass(new MySqlDb($host, $name,$user,$pwd,$port));
				} else if ($type == DbType::MySqli) {
					MySqliDb::testConnection($host,$name,$user,$pwd,$port) ;
                    $clsUpdateDabatase = new UpdateDatabaseClass(new MySqliDb($host, $name,$user,$pwd,$port));
				} else {
					MsSqlDb::testConnection($host,$name,$user,$pwd,$port) ;
                    $clsUpdateDabatase = new UpdateDatabaseClass(new MsSqlDb($host, $name,$user,$pwd,$port));
				}
                
                $res = $clsUpdateDabatase->processUpdateDatabase();
                if ($res == ""){
                    $mesg = "Successful in updating database.";
                } else {
                    $mesg = $res;
                }
			} catch (Exception $e) {
				$status = Status::Error ;
				$mesg = $e->getMessage() ;
			}
		}
		echo $mesg ;
    }
	private function getUserKey() {
		$key = Util::createMD5(time().'planetz',"","",true) ;
		$_SESSION['key'] = $key ;
		$this->sendJsonResponse(Status::Ok,"",$key,$this->type) ;
	}
	private function loadConfig() {
		$config = new ConfigDb(PATH_CODE . 'db.xml') ;
		$config->loadConfig() ;
		$this->datas['server'] = $config->getHost();
		$this->datas['dbname'] = $config->getDbName();
		$this->datas['dbuser'] = $config->getUserName() ;
		$this->datas['dbpwd'] = $config->getPassword();
		$this->datas['dbtype'] = $config->getDbType() ;
		$port = (int)$config->getPortNo() ;
		if ($port > 0)
			$this->datas['dbport'] = $port ;
		else
			$this->datas['dbport'] = "" ;
        
		$this->datas['serversun'] = $config->getHostSun();
		$this->datas['dbnamesun'] = $config->getDbNameSun();
		$this->datas['dbusersun'] = $config->getUserNameSun() ;
		$this->datas['dbpwdsun'] = $config->getPasswordSun();
		$this->datas['dbtypesun'] = $config->getDbTypeSun() ;
		$this->datas['dbbusinessunitsun'] = $config->getBusinessUnitSun() ;
		$port = (int)$config->getPortNoSun() ;
		if ($port > 0)
			$this->datas['dbportsun'] = $port ;
		else
			$this->datas['dbportsun'] = "" ;

		$this->datas['servernotif'] = $config->getHostNotif();
		$this->datas['dbnamenotif'] = $config->getDbNameNotif();
		$this->datas['dbusernotif'] = $config->getUserNameNotif() ;
		$this->datas['dbpwdnotif'] = $config->getPasswordNotif();
		$this->datas['dbtypenotif'] = $config->getDbTypeNotif() ;
		$port = (int)$config->getPortNoNotif() ;
		if ($port > 0)
			$this->datas['dbportnotif'] = $port ;
		else
			$this->datas['dbportnotif'] = "" ;


		$this->datas['serverrms'] = $config->getHostRms();
		$this->datas['dbnamerms'] = $config->getDbNameRms();
		$this->datas['dbuserrms'] = $config->getUserNameRms() ;
		$this->datas['dbpwdrms'] = $config->getPasswordRms();
		$this->datas['dbtyperms'] = $config->getDbTypeRms() ;
		$port = (int)$config->getPortNoRms() ;
		if ($port > 0)
			$this->datas['dbportrms'] = $port ;
		else
			$this->datas['dbportrms'] = "" ;
		unset($config) ;
	}
	private function getDbName() {
		return $this->datas['dbname'] ;
	}
	private function getServer() {
		return $this->datas['server'] ;
	}
	private function getDbUser() {
		return $this->datas['dbuser'] ;
	}
	private function getDbPwd() {
		return $this->datas['dbpwd'] ;
	}
	private function getDbPort() {
		return $this->datas['dbport'] ;
	}
	private function getDbType() {
		return $this->datas['dbtype'] ;
	}
	private function getDbNameSun() {
		return $this->datas['dbnamesun'] ;
	}
	private function getServerSun() {
		return $this->datas['serversun'] ;
	}
	private function getDbUserSun() {
		return $this->datas['dbusersun'] ;
	}
	private function getDbPwdSun() {
		return $this->datas['dbpwdsun'] ;
	}
	private function getDbPortSun() {
		return $this->datas['dbportsun'] ;
	}
	private function getDbTypeSun() {
		return $this->datas['dbtypesun'] ;
	}
	private function getDbBusinessUnitSun() {
		return $this->datas['dbbusinessunitsun'] ;
	}
	private function getDbNameNotif() {
		return $this->datas['dbnamenotif'] ;
	}
	private function getServerNotif() {
		return $this->datas['servernotif'] ;
	}
	private function getDbUserNotif() {
		return $this->datas['dbusernotif'] ;
	}
	private function getDbPwdNotif() {
		return $this->datas['dbpwdnotif'] ;
	}
	private function getDbPortNotif() {
		return $this->datas['dbportnotif'] ;
	}
	private function getTypeListNotif() {
		return $this->datas['dbtypenotif'] ;
	}

	private function getDbNameRms() {
		return $this->datas['dbnamerms'] ;
	}
	private function getServerRms() {
		return $this->datas['serverrms'] ;
	}
	private function getDbUserRms() {
		return $this->datas['dbuserrms'] ;
	}
	private function getDbPwdRms() {
		return $this->datas['dbpwdrms'] ;
	}
	private function getDbPortRms() {
		return $this->datas['dbportrms'] ;
	}
	private function getTypeListRms() {
		return $this->datas['dbtyperms'] ;
	}

	private function getView() {
		$this->loadConfig();
		ob_start() ;
		include (PATH_VIEWS . "admin/ConfigView.php") ;
		echo Util::minifyHtml(ob_get_clean()) ;
	}
	private function getTypeList() {
		$arr = array() ;
		$arr[] = array ('code'=>DbType::MsSql,'desc'=>'Microsoft SQL Server' );
		$arr[] = array ('code'=>DbType::MySql,'desc'=>'MySql' ) ;
		$arr[] = array ('code'=>DbType::PostgreSql,'desc'=>'Postgres Sql' ) ;
		$arr[] = array ('code'=>DbType::MySqli,'desc'=>'MySqli' ) ;
		return Util::createOptionValue($arr,$this->getDbType()) ;
	}
	private function sendJsonResponse($status="",$mesg="",$data="",$type="") {
		header('Content-type: application/json');
		$arr = array(FIELD_STATUS => $status, FIELD_MESG => $mesg, FIELD_DATA => $data, FIELD_TYPE => $type);
		echo json_encode($arr) ;
	}
	private function getParam($params,$key,$default) {
		if (!isset($params[$key]) || empty($params[$key])) {
			return $default ;
		} else {
			return $params[$key] ;
		}
	}
	private function getParamInt($params,$key,$default) {
		$result = $default ;
		if (isset($params[$key]) && $params[$key] != "") {
			if (is_numeric($params[$key]))
				$result = $params[$key] ;
		}
		return $result ;
	}
}
?>