<?php

class ConfigDb {

	const TAG_TYPE = "type" ;
	const TAG_HOST = "host" ;
	const TAG_NAME = "dbname" ;
	const TAG_PORT = "port" ;
	const TAG_USER = "username" ;
	const TAG_PWD = "password" ;
	const TAG_PERSIT = "persistent" ;    
    
	const TAG_TYPE_SUN = "typesun" ;
	const TAG_HOST_SUN = "hostsun" ;
	const TAG_NAME_SUN = "dbnamesun" ;
	const TAG_PORT_SUN = "portsun" ;
	const TAG_USER_SUN = "usernamesun" ;
	const TAG_PWD_SUN = "passwordsun" ;
	const TAG_BU_SUN = "businessunitsun" ;

	const TAG_TYPE_NOTIF = "typenotif" ;
	const TAG_HOST_NOTIF = "hostnotif" ;
	const TAG_NAME_NOTIF = "dbnamenotif" ;
	const TAG_PORT_NOTIF = "portnotif" ;
	const TAG_USER_NOTIF = "usernamenotif" ;
	const TAG_PWD_NOTIF = "passwordnotif" ;

	const TAG_TYPE_RMS = "typerms" ;
	const TAG_HOST_RMS = "hostrms" ;
	const TAG_NAME_RMS = "dbnamerms" ;
	const TAG_PORT_RMS = "portrms" ;
	const TAG_USER_RMS = "usernamerms" ;
	const TAG_PWD_RMS = "passwordrms" ;
	
	private $type ;
	private $dbname ;
	private $host ;
	private $username ;
	private $pwd ;
	private $portno ;
	private $persistent ;
	private $file ;
    
	private $typesun ;
	private $dbnamesun ;
	private $hostsun ;
	private $usernamesun ;
	private $pwdsun ;
	private $portnosun ;
	private $businessunitsun ;

	private $typenotif ;
	private $dbnamenotif ;
	private $hostnotif ;
	private $usernamenotif ;
	private $pwdnotif ;
	private $portnonotif ;
	
	private $typerms ;
	private $dbnamerms ;
	private $hostrms ;
	private $usernamerms ;
	private $pwdrms ;
	private $portnorms ;

	function setDbType($value) {
		$this->type = $value ;
	}
	function getDbType() {
		return $this->type ;
	}
	function setDbName($value) {
		$this->dbname = $value ;
	}
	function getDbName() {
		return $this->dbname ;
	}
	function setHost($value) {
		$this->host = $value ;
	}
	function getHost() {
		return $this->host ;
	}
	function setUserName($value) {
		$this->username = $value ;
	}
	function getUserName() {
		return $this->username ;
	}
	function setPassword($value) {
		$this->pwd = $value ;
	}
	function getPassword() {
		return $this->pwd ;
	}
	function setPortNo($value) {
		$this->portno = $value ;
	}
	function getPortNo() {
		return $this->portno ;
	}
	function setPersistent($value) {
		$this->persistent = $value ;
	}
	function getPersistent() {
		return $this->persistent ;
	}
	function setFileName($value) {
		$this->file = $value ;
	}
	function getFileName() {
		return $this->file ;
	}
    
	function setDbTypeSun($value) {
		$this->typesun = $value ;
	}
	function getDbTypeSun() {
		return $this->typesun ;
	}
	function setDbNameSun($value) {
		$this->dbnamesun = $value ;
	}
	function getDbNameSun() {
		return $this->dbnamesun ;
	}
	function setHostSun($value) {
		$this->hostsun = $value ;
	}
	function getHostSun() {
		return $this->hostsun ;
	}
	function setUserNameSun($value) {
		$this->usernamesun = $value ;
	}
	function getUserNameSun() {
		return $this->usernamesun ;
	}
	function setPasswordSun($value) {
		$this->pwdsun = $value ;
	}
	function getPasswordSun() {
		return $this->pwdsun ;
	}
	function setPortNoSun($value) {
		$this->portnosun = $value ;
	}
	function getPortNoSun() {
		return $this->portnosun ;
	}
	function setBusinessUnitSun($value) {
		$this->businessunitsun = $value ;
	}
	function getBusinessUnitSun() {
		return $this->businessunitsun ;
	}

	function setDbTypeNotif($value) {
		$this->typenotif = $value ;
	}
	function getDbTypeNotif() {
		return $this->typenotif ;
	}
	function setDbNameNotif($value) {
		$this->dbnamenotif = $value ;
	}
	function getDbNameNotif() {
		return $this->dbnamenotif ;
	}
	function setHostNotif($value) {
		$this->hostnotif = $value ;
	}
	function getHostNotif() {
		return $this->hostnotif ;
	}
	function setUserNameNotif($value) {
		$this->usernamenotif = $value ;
	}
	function getUserNameNotif() {
		return $this->usernamenotif ;
	}
	function setPasswordNotif($value) {
		$this->pwdnotif = $value ;
	}
	function getPasswordNotif() {
		return $this->pwdnotif ;
	}
	function setPortNoNotif($value) {
		$this->portnonotif = $value ;
	}
	function getPortNoNotif() {
		return $this->portnonotif ;
	}
	
	function setDbTypeRms($value) {
		$this->typerms = $value ;
	}
	function getDbTypeRms() {
		return $this->typerms ;
	}
	function setDbNameRms($value) {
		$this->dbnamerms = $value ;
	}
	function getDbNameRms() {
		return $this->dbnamerms ;
	}
	function setHostRms($value) {
		$this->hostrms = $value ;
	}
	function getHostRms() {
		return $this->hostrms ;
	}
	function setUserNameRms($value) {
		$this->usernamerms = $value ;
	}
	function getUserNameRms() {
		return $this->usernamerms ;
	}
	function setPasswordRms($value) {
		$this->pwdrms = $value ;
	}
	function getPasswordRms() {
		return $this->pwdrms ;
	}
	function setPortNoRms($value) {
		$this->portnorms = $value ;
	}
	function getPortNoRms() {
		return $this->portnorms ;
	}

	function __construct($filename) {
		$this->initData() ;
		$this->file = $filename ;
		//$fn = $filename ;
		//touch($fn);		//for win server environment bug
		//$this->file = realpath($fn);	//for win server environment bug
	}
	function __destruct() {
	}
	function loadConfig() {
		$this->readSimpleXml() ;
	}
	function saveConfig() {
		$this->saveXml() ;
	}
	private function saveXml() {
		$v = "" ;
		$xw = new XMLWriter(); 
		$xw->openURI($this->file); 
		$xw->startDocument('1.0','utf-8'); 
		$xw->setIndent(4); 
		$xw->startElement("database"); 
        $xw->writeElement(self::TAG_TYPE, $this->type); 
        $xw->writeElement(self::TAG_NAME, $this->dbname); 
        $xw->writeElement(self::TAG_HOST, $this->host); 
        $xw->writeElement(self::TAG_PORT, $this->portno); 
        $xw->writeElement(self::TAG_USER, $this->username);
		$xw->writeElement(self::TAG_PWD, bin2hex(Util::encryptString($this->pwd,"",""))) ;
		$xw->writeElement(self::TAG_PERSIT,$this->persistent) ;
        $xw->writeElement(self::TAG_TYPE_SUN, $this->typesun); 
        $xw->writeElement(self::TAG_NAME_SUN, $this->dbnamesun); 
        $xw->writeElement(self::TAG_HOST_SUN, $this->hostsun); 
        $xw->writeElement(self::TAG_PORT_SUN, $this->portnosun); 
        $xw->writeElement(self::TAG_USER_SUN, $this->usernamesun);
		$xw->writeElement(self::TAG_PWD_SUN, bin2hex(Util::encryptString($this->pwdsun,"",""))) ;
        $xw->writeElement(self::TAG_BU_SUN, $this->businessunitsun);
        $xw->writeElement(self::TAG_TYPE_NOTIF, $this->typenotif); 
        $xw->writeElement(self::TAG_NAME_NOTIF, $this->dbnamenotif); 
        $xw->writeElement(self::TAG_HOST_NOTIF, $this->hostnotif); 
        $xw->writeElement(self::TAG_PORT_NOTIF, $this->portnonotif); 
        $xw->writeElement(self::TAG_USER_NOTIF, $this->usernamenotif);
		$xw->writeElement(self::TAG_PWD_NOTIF, bin2hex(Util::encryptString($this->pwdnotif,"",""))) ;
		$xw->writeElement(self::TAG_TYPE_RMS, $this->typerms); 
        $xw->writeElement(self::TAG_NAME_RMS, $this->dbnamerms); 
        $xw->writeElement(self::TAG_HOST_RMS, $this->hostrms); 
        $xw->writeElement(self::TAG_PORT_RMS, $this->portnorms); 
        $xw->writeElement(self::TAG_USER_RMS, $this->usernamerms);
		$xw->writeElement(self::TAG_PWD_RMS, bin2hex(Util::encryptString($this->pwdrms,"",""))) ;
		$xw->endElement(); 
		$xw->endDocument(); 
		$xw->flush();
		if (isset($_SESSION[SE_DB])) {unset($_SESSION[SE_DB]);} 
		if (isset($_SESSION[SE_DB_SUN])) {unset($_SESSION[SE_DB_SUN]);} 
		if (isset($_SESSION[SE_DB_NOTIF])) {unset($_SESSION[SE_DB_NOTIF]);} 
		if (isset($_SESSION[SE_DB_RMS])) {unset($_SESSION[SE_DB_RMS]);} 
		unset($xw) ;
	}
	private function readXml_unuse() {
		$this->initData() ;
		$tag = "" ;
		$value = "" ;
		if (file_exists($this->file)) {
			$xr = new XMLReader();
			$xr->open($this->file);
			while($xr->read()) {
				if($xr->nodeType == XMLReader::ELEMENT) {
					$tag = $xr->localName ;
					switch ($tag) {
						case self::TAG_TYPE:
							$this->type = $xr->read() ;
							break ;
						case self::TAG_NAME:
							$this->dbname = $xr->read() ;
							break ;
						case self::TAG_HOST:
							$this->host = $xr->read() ;
							break ;
						case self::TAG_PORT:
							$this->portno = $xr->read() ;
							break ;
						case self::TAG_USER:
							$this->username = $xr->read() ;
							break ;
						case self::TAG_PWD:
							$p = $xr->read() ;
							if ($p == "")
								$this->pwd = $p ;
							else
								$this->pwd = trim(Util::decryptString(pack("H*",$p),"","")) ;
							
							break ;
						case self::TAG_PERSIT:
							$this->persistent = $xr->read() ;
							break ;
						default:
					}
				}
			}
			unset($reader) ;
		}
	}
	private function readSimpleXml() {
		//$xmlObject = simplexml_load_string($xml);
		if (file_exists($this->file)) {
			$xr = simplexml_load_file($this->file);
			if ($xr) {
				$this->type = (string) $xr->{self::TAG_TYPE} ;
				$this->dbname = (string) $xr->{self::TAG_NAME} ;
				$this->host = (string) $xr->{self::TAG_HOST} ;
				$this->portno = (string) $xr->{self::TAG_PORT} ;
				$this->username = (string) $xr->{self::TAG_USER} ;
				$this->persistent = (string) $xr->{self::TAG_PERSIT} ;
                
				$this->typesun = (string) $xr->{self::TAG_TYPE_SUN} ;
				$this->dbnamesun = (string) $xr->{self::TAG_NAME_SUN} ;
				$this->hostsun = (string) $xr->{self::TAG_HOST_SUN} ;
				$this->portnosun = (string) $xr->{self::TAG_PORT_SUN} ;
				$this->usernamesun = (string) $xr->{self::TAG_USER_SUN} ;
				$this->businessunitsun = (string) $xr->{self::TAG_BU_SUN} ;
				
				$this->typenotif = (string) $xr->{self::TAG_TYPE_NOTIF} ;
				$this->dbnamenotif = (string) $xr->{self::TAG_NAME_NOTIF} ;
				$this->hostnotif = (string) $xr->{self::TAG_HOST_NOTIF} ;
				$this->portnonotif = (string) $xr->{self::TAG_PORT_NOTIF} ;
				$this->usernamenotif = (string) $xr->{self::TAG_USER_NOTIF} ;

				$this->typerms = (string) $xr->{self::TAG_TYPE_RMS} ;
				$this->dbnamerms = (string) $xr->{self::TAG_NAME_RMS} ;
				$this->hostrms = (string) $xr->{self::TAG_HOST_RMS} ;
				$this->portnorms = (string) $xr->{self::TAG_PORT_RMS} ;
				$this->usernamerms = (string) $xr->{self::TAG_USER_RMS} ;

				$p = (string) $xr->{self::TAG_PWD}  ;
				
				if ($p == "")
					$this->pwd = $p ;
				else
					$this->pwd = trim(Util::decryptString(pack("H*",$p),"","")) ;
				
				$p = (string) $xr->{self::TAG_PWD_SUN}  ;
				
				if ($p == "")
					$this->pwdsun = $p ;
				else
					$this->pwdsun = trim(Util::decryptString(pack("H*",$p),"","")) ;

				$p = (string) $xr->{self::TAG_PWD_NOTIF}  ;
				
				if ($p == "")
					$this->pwdnotif = $p ;
				else
					$this->pwdnotif = trim(Util::decryptString(pack("H*",$p),"","")) ;

				$p = (string) $xr->{self::TAG_PWD_RMS}  ;
				
				if ($p == "")
					$this->pwdrms = $p ;
				else
					$this->pwdrms = trim(Util::decryptString(pack("H*",$p),"","")) ;

			}
			unset($xr) ;
		}
	}
	private function initData() {
		$this->type = DbType::MsSql ;
		$this->dbname = "" ;
		$this->host = "" ;
		$this->portno = "" ;
		$this->username = "" ;
		$this->pwd = "" ;
		$this->persistent = true ;
        
		$this->typesun = DbType::MsSql ;
		$this->dbnamesun = "" ;
		$this->hostsun = "" ;
		$this->portnosun = "" ;
		$this->usernamesun = "" ;
		$this->pwdsun = "" ;
		$this->businessunitsun = "" ;
		
		$this->typenotif = DbType::MsSql ;
		$this->dbnamenotif = "" ;
		$this->hostnotif = "" ;
		$this->portnonotif = "" ;
		$this->usernamenotif = "" ;
		$this->pwdnotif = "" ;

		$this->typerms = DbType::MsSql ;
		$this->dbnamerms = "" ;
		$this->hostrms = "" ;
		$this->portnorms = "" ;
		$this->usernamerms = "" ;
		$this->pwdrms = "" ;
	}
}
?>