<?php

class DatabaseConnection {
	
	static private $instance=null;

	public static function getInstance(){
		if(self::$instance==null) self::$instance = new DatabaseConnection();
		return self::$instance;
	}	 	

	function __construct(){}

	function delete($d){
	 
		$a = $this->getFileObject();
		
		foreach($a as $key=>$value){
		 	if($value->name==$d->name) {
				array_splice($a, $key, 1);
				if(!$this->setFileObject($a)){
					Response::sendResponse(false, null, "database_connection", "cannot_write");
				}
			  	Response::sendOKResponse((object) array("success"=>true));
			}
	
		}
		
		Response::sendOKResponse((object) array("success"=>false));
		
	}
	function edit($d){
	 	  
		$a = $this->getFileObject();
		
		foreach($a as $key=>$value){
		 	if($value->name==$d->name) {
				$password = !empty($d->dbpassword) ? $d->dbpassword : $value->dbpassword;
			
				if(($success=$this->testConnection($d->dbhost, $d->dbname, $d->dbusername, $password))!==true){
					Response::sendErrorResponse("database", $success, $data=null);
				}
				
				$a[$key] = $d;
				$a[$key]->dbpassword = $password;
				if(!$this->setFileObject($a)){
					Response::sendResponse(false, null, "database_connection", "cannot_write");
				}
			  	Response::sendOKResponse((object) array("success"=>true));
			}
	
		}
		
		Response::sendOKResponse((object) array("success"=>false));
	}

	function add($d){
	
		$a = $this->getFileObject();
		
		//$maxId=1;
		$isUnique=true;
		foreach($a as $key=>$value){
		 	//if($value->id>$maxId) $maxId = $value->id;
		 	$isUnique = $isUnique && $value->name!=$d->name;
		}
		
		if(!$isUnique){
			Response::sendResponse(false, null, "database_connection", "name_not_unique");
		}
		
		if(($success=$this->testConnection($d->dbhost, $d->dbname, $d->dbusername, $d->dbpassword))!==true){
			Response::sendErrorResponse("database", $success, $data=null);
		}
		 
		//$d->id = $maxId + 1;
		$a[] = $d;
		
		
		if(!$this->setFileObject($a)){
			Response::sendResponse(false, null, "database_connection", "cannot_write");
		}
		
		$retObj = (object) array();
		$retObj->success = true;
		
		Response::sendOKResponse($retObj);
		
	}
	function load(){
		
		$retObj = (object) array();
	
		$a = null;
		
		$txtFilePath = Util::readFile(Config::get("private_system_path") ."/config/system/databaseConnections.txt");
	 	if($txtFilePath!==false){
	 		$a = DataSerializer::deserialize($txtFilePath);
			foreach($a as $k=>$v){
				unset($a[$k]->dbpassword);
			}
	 	}
	 		
	 	$retObj = $a;	

		Response::sendOKResponse($retObj);
	
	}
	
	private function getFileObject(){
	
		$txtFilePath = Util::readFile(Config::get("private_system_path") ."/config/system/databaseConnections.txt");
	 	if($txtFilePath!==false){
	 		return DataSerializer::deserialize($txtFilePath);
		}
		
		return null;
	}
	
	private function setFileObject($obj){
	
		return  Util::writeFile(Config::get("private_system_path") ."/config/system/databaseConnections.txt", DataSerializer::serialize($obj));
		
	}

	private function testConnection($dbhost, $dbname, $dbusername, $dbpassword){
	
		$link = mysql_connect($dbhost, $dbusername, $dbpassword);
		if (!$link) {
		  	return mysql_error();
		}
		
		$db_selected = mysql_select_db($dbname, $link);
		if (!$db_selected) {
		    return  mysql_error();
		}
				
		mysql_close($link);
		
		return true;
		
	}
	
}

?>