<?php
include (PATH_CONTROLLERS . "Common.php") ;
//Log::write(microtime());
//Util::require_once_encryption(PATH_MODELS . "admin/UserClass-encrypted.php");
require_once (PATH_MODELS . "admin/UserClass.php") ;
require_once (PATH_MODELS . "admin/UserPasswordHistoryClass.php") ;
//Log::write(microtime());
require_once (PATH_LIB . "LicenseFile.php") ;

class Login extends ControllerBase {
	private $user ;
	function __construct() {
		$this->db = $_SESSION[SE_DB] ;
		$this->user = new UserClass($this->db) ;
	}
	function __destruct() {
		unset($this->user) ;
	}
	public function processRequest($params) {
		$username = "" ;
		$pwd = "" ;
		$type = REQ_VIEW ;
		try {
			$this->db->open() ;
			if (isset($params) && count($params) > 0) {
				if (isset($params['type']))
					$type = $params['type'] ;
				if (isset($params['id']))
					$username = $params['id'];
				if (isset($params['pwd'])) {
					$pwd = Util::decryptData($params['pwd'],$_SESSION[SE_USERKEY]) ;
				}
			}

			switch ($type) {
				case REQ_SIGNIN:
					$this->processLogin($username,$pwd) ;
					break ;
				case REQ_SIGNOUT:
					$this->processLogout($username) ;
					break ;
				case REQ_QUERY:
					$this->processQuery($username) ;
					break ;
				case REQ_NEW:
					$this->processLogin($username,$pwd,true) ;
					break ;
				case REQ_VIEW:
					$this->processView() ;
					break ;
				default:
					$this->sendJsonResponse(Status::Error,"invalid request.","") ;
					break ;
			}
			$this->db->close() ;
			return true ;
		} catch (Exception $e) {
			$this->db->close() ;
			die ($e->getMessage()) ;
		}
	}
	 //check app/web version
	private function webVersion2(){

		$result     = "";
        $file         = "version.txt";
        $path         = PATH_CODE.$file;

        if(file_exists($path)){
            $version     = fgets(fopen($path, "r"));
                 $result     = "<a href=\"../code/".$file."\" download=\"changelog_".$version.".txt\">".$version."</a>";
        }

        return $result;
	}
	 //check app/web version
	 private function webVersion(){

		$result     = "";
        $file         = "version.txt";
        $path         = PATH_CODE.$file;

        if(file_exists($path)){
            $version     = fgets(fopen($path, "r"));
                  $result = "<span>".$version."</span>";
        }

        return $result;
	}
	private function getLedgerBuName(){
		$clsSetting 	= new LedgerSettingClass($this->db);
		$setting 		= $clsSetting->getTable();
		if(!is_null($setting) && count($setting)>0) {
			return  $setting[0][LedgerSettingTable::C_NAME];
		} else {
			return "";
		}
	}
	private function getLedgerBuReg(){
		$clsSetting 	= new LedgerSettingClass($this->db);
		$setting 		= $clsSetting->getTable();
		if(!is_null($setting) && count($setting)>0) {
			return  "Reg. ".$setting[0][LedgerSettingTable::C_COMPANY_REG_NO];
		} else {
			return "";
		}
	}
	private function processLogin($username,$pwd,$firsttime=false) {
		$cls = new UserPasswordHistoryClass($this->db);
		$status = "" ;
		$mesg = "" ;
		$data = "" ;
		$id = "" ;
		$orgid = -1 ;
		$dteNow = date("Y-m-d");
		$row = $this->user->getRecordByName(0,$username);
		if (is_null($row) || count($row) == 0) {
			$status = Status::Error ;
			$mesg = "Invalid user name.";
		} else {

			$id = $row[UserTable::C_ID];
			$cnt = $row[UserTable::C_FAIL_COUNT] ;
			$group = $row[UserTable::C_GROUP] ;
			$name = $row[UserTable::C_FULL_NAME] ;
			$orgid = $row[UserTable::C_ORG_ID] ;
			if ($cnt > 2) {
				$status = Status::Error;
				$mesg = "Your account have been disabled. Please contact your system administrator for assistance.";
			}
			if ($status == "") {
				$start = strtotime($row[UserTable::C_START_DATE]) ;
				// if ($row[UserTable::C_START_DATE] > time()) {
				if (date_create($row[UserTable::C_START_DATE])->format('Y-m-d') > date('Y-m-d')) {
					$status = Status::Error;
					$mesg = "You can only login after 1 " . date_create($row[UserTable::C_START_DATE])->format('Y-m-d') ;
				}
			}

			if ($status == "") {
				$dte = date_create($row[UserTable::C_EXPIRY_DATE])->format('Y-m-d') ;
				if ($dte != date_create(MAX_DATE)) {
					// if ($dte <= $dteNow) {
					if (date_create($row[UserTable::C_EXPIRY_DATE])->format('Y-m-d') < date('Y-m-d')) {
						$status = Status::Error ;
						$mesg = "Yours account have already expired. Please contact your system administrator for assistance.";
					}
				}
			}
		}
		if ($status == "") {
			if ($firsttime) {
				$strCheck = Util::checkPasswordComplexity($pwd);
				if($strCheck != ""){
					$status = Status::Error ;
					$mesg = $strCheck ;
				} else{
					if($cls->checkPasswordSimiliarToPrevious($id, $pwd)){
						$cls->insertPasswordHistory($id, $pwd);
						$this->user->changePassword($id,$pwd) ;
					}else{
						$status = Status::Error ;
						$mesg = "New password cannot be same as previous 3 password" ;
					}
				}
			} else {
				//5ef27ddc3a25a0242096718cb7d10071  : 12345 pwd
				if ($this->user->encryptPwd1($pwd) != $row[UserTable::C_PWD1]) {
					$status = Status::Error ;
					$mesg = "Invalid password. Please try again." ;
					$this->user->updateFailCount($id,$cnt + 1) ;
				}
			}
		}
		if ($status == "") {
			$status = Status::Ok ;
			$this->user->updateLogin($id) ;
			$this->setLogin($id,$username,$name,$group,$orgid) ;
			$data = "index.pzx?c=" . Util::convertLink("Main");
		} else {

		}

		// Log::write($status);die();
		$this->sendJsonResponse($status,$mesg,$data);
	}

	private function getLicensing(){
		$licenseFile = new LicenseFile();
		$licenseFile->read(PATH_CODE . "sbg.lic");

		$dtLicense = new DateTime($licenseFile->getExpiryDate());
		$dtNow = new DateTime();
		if($dtLicense < $dtNow){
			return 1;
		}

		$dtdiff = $dtLicense->diff($dtNow);
		if($dtdiff->days < 60){
			return 2;
		}

		return 0;
	}
	private function getLicenseDate(){
		$licenseFile = new LicenseFile();
		$licenseFile->read(PATH_CODE . "sbg.lic");

		$dtLicense = new DateTime($licenseFile->getExpiryDate());

		return $dtLicense->format("d/m/Y");
	}
	private function processQuery($username) {
		$status = "" ;
		$mesg = "" ;
		$data = "" ;
		$id = "" ;
		$chngpwd = false ;
		$key = "" ;
		$dteNow = date("Y-m-d H:i:s");
		$row = $this->user->getRecordByName(0,$username);
		if (is_null($row) || count($row) == 0) {
			$status = Status::Error ;
			$mesg = "Invalid user name.";
		} else {
			$id = $row[UserTable::C_ID];
			$cnt = $row[UserTable::C_FAIL_COUNT] ;
			$group = $row[UserTable::C_GROUP] ;
			$name = $row[UserTable::C_NAME] ;
			$chngpwd = $row[UserTable::C_CHANGE_PWD] ;
			if ($cnt > 2) {
				$status = Status::Error;
				$mesg = "Your account have been disabled. Please contact your system administrator for assistance.";
			}
			if ($status == "") {
				$start = strtotime($row[UserTable::C_START_DATE]) ;
				// if ($row[UserTable::C_START_DATE] > time()) {
					if (date_create($row[UserTable::C_START_DATE])->format('Y-m-d') > date('Y-m-d')) {
					$status = Status::Error;
					$mesg = "You can only login after 2 " . date_create($row[UserTable::C_START_DATE])->format('Y-m-d') ;
				}
			}
			if ($status == "") {
				$dte = date_create($row[UserTable::C_EXPIRY_DATE]) ;

				if ($dte != date_create(MAX_DATE)) {
					// if ($dte <= $dteNow) {
					if (date_create($row[UserTable::C_EXPIRY_DATE])->format('Y-m-d') < date('Y-m-d')) {

						$status = Status::Error ;
						$mesg = "Yours account have already expired. Please contact your system administrator for assistance.";
					}
				}
			}
		}
		if ($status == "") {
			$mesg = "" ;
			$key = Util::createMD5(time().$username,"","",true) ;
			$_SESSION[SE_USERKEY] = $key ;
			$data = $key ;
			if (
			$chngpwd == 1) {
				$status = Status::Info ;
			} else {
				$status = Status::Confirm ;
			}
		}

					//Log::write($status);

		$this->sendJsonResponse($status,$mesg,$data);
	}
	private function processLogout($params) {
		$this->setLogin("","","","",-1) ;
		$_SESSION[SE_USERKEY] = "" ;
		$this->sendJsonResponse(Status::Ok,"","index.pzx") ;
	}
	private function setLogin($userid,$username,$fullname,$usergroup,$orgid) {
		$_SESSION[SE_USERID] = $userid ;
		$_SESSION[SE_USERNAME] = $username ;
		$_SESSION[SE_USERGROUP] = $usergroup ;
		$_SESSION[SE_FULLNAME] = $fullname ;
		$_SESSION[SE_MENU] = "";
		$_SESSION[SE_ORGID] = $orgid ;
		$_SESSION[SE_ORGNAME] = "";
	}
	private function processView() {
		ob_start() ;
		include (PATH_VIEWS . "web/general/LoginView.php") ;
		echo Util::minifyHtml(ob_get_clean()) ;
	}
	private function createMenu($usergroup) {
		//$menu = new Menu() ;
		//return $menu->Render() ;
		return "" ;
	}
}
?>