<?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_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_BACKUP_FILENAME = "backupfilename";

	private $type ;
	private $dbname ;
	private $host ;
	private $username ;
	private $pwd ;
	private $portno ;
	private $persistent ;
	private $file ;

	private $typenotif ;
	private $dbnamenotif ;
	private $hostnotif ;
	private $usernamenotif ;
	private $pwdnotif ;
	private $portnonotif ;

	private $backupfilename;

	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 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 setBackupFilename($value) {
		$this->backupfilename = $value ;
	}
	function getBackupFilename() {
		return $this->backupfilename ;
	}

	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_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_BACKUP_FILENAME, $this->backupfilename);
		$xw->endElement(); 
		$xw->endDocument(); 
		$xw->flush(); 
		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->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->backupfilename = (string) $xr->{self::TAG_BACKUP_FILENAME} ;

				$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_NOTIF}  ;
				if ($p == "")
					$this->pwdnotif = $p ;
				else
					$this->pwdnotif = 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->backupfilename = "";
	}
}
?>