<?php

class Logger {
 
 	static $logFile;

	function Logger(){
		
	}
	
	public function getFullLog($d){
	 
	 	$line = $d->logRow;
	 
	 	$a = explode(";", "$line");
	 	$date = substr($line, 0, 10);
	 	$filename =  substr($line, strrpos($line, "/")+1); 
	 	
	 	$filePath = trim(Config::get("private_system_logs_path")."/".$a[1]."/".$date."/".$filename);
	
		// security
		$is_secure = FileSecurity::getInstance()->systemPathCheck($filePath , array("txt"), Config::get("private_system_logs_path"));
		if(!$is_secure){
			Response::sendResponse(false, null, "system", "terminated_by_system_path_check");
		}

		if(($jsonString = Util::readFile($filePath))===false){
			Response::sendResponse(false, null, "logger", "log_file_doesnot_exist");
		}
		

	 	Response::sendOKResponse($jsonString);
	 	
	}

	public function deleteAllLogs(){

		// delete logs dir
		Util::deleteDirectoryAndAllItsContent(Config::get("private_system_logs_path"), Config::get("private_system_logs_path"));
		
		// createlogs dirs
		Util::createDirectoryIfNotExist(Config::get("private_system_logs_path"));
		Util::createDirectoryIfNotExist(Config::get("private_system_logs_index_path"));
		Util::createDirectoryIfNotExist(Config::get("private_system_logs_index_path")."/date");
		Util::createDirectoryIfNotExist(Config::get("private_system_logs_index_path")."/user");
		Util::createDirectoryIfNotExist(Config::get("private_system_logs_index_path")."/application");
		Util::createDirectoryIfNotExist(Config::get("private_system_logs_index_path")."/method");

		Response::sendOKResponse();
		
	}
	
	public function saveLogsConfig($d){
	 	
	 	$obj = (object) array();
	 	$obj->saveLog = ($d->saveLog==true);
	 	$obj->saveFullLog = ($d->saveFullLog==true);
	 
	 	$sysConfigFilepath = Config::get("private_system_config_path")."/system/systemConfig.txt";
		JSONUtil::updateObject($sysConfigFilepath, $obj);
	 
	 	Response::sendOKResponse();
	 	
	}
	
	public function getLogsConfig(){
		$retObj = (object) array();
		$retObj->saveLog = Config::get("saveLog", "system");
		$retObj->saveFullLog = Config::get("saveFullLog", "system");
		Response::sendOKResponse($retObj);
	}
	 
	public function getLogsView($d){
	 
	 	$ud = new UD();
	 	
	 	$groupsArr = array("user", "date", "application", "method");
	 	
	 	// group name check (provide security to)
		if(!in_array($d->group, $groupsArr)){
	 		Response::sendResponse(false, null, "logger", "group_doesnot_exist");
	 	}
	 	
	 	$FaF = $ud->getFoldersAndFiles(Config::get("private_system_logs_index_path")."/".$d->group);
	 	if($FaF<0){
	 		Response::sendErrorResponse($ud->getErrorID($FaF), $ud->getErrorMsg($FaF));
		}
		$FaF = Util::getArrayFromObjectArray($FaF, "name");
		
		if($FaF==null){
		 	Response::sendOKResponse((object) array());
		}
		
		sort($FaF);
		$FaF = array_reverse($FaF);

		Response::sendOKResponse($FaF);
	 
	}
	
	public 	function getLogs($d){
	 	
	 	$pathToFile = Config::get("private_system_logs_index_path")."/".$d->indexGroup."/".$d->fileName;
		
		// security
		$is_secure = FileSecurity::getInstance()->systemPathCheck($pathToFile , array("txt"), Config::get("private_system_logs_index_path"));
		if(!$is_secure){
			Response::sendResponse(false, null, "system", "terminated_by_system_path_check");
		}
		
		$fileArr = Util::readFileArray($pathToFile);
		if($fileArr==false){
		 	Response::sendResponse(false, null, "logger", "log_file_doesnot_exist");
		}
		
		// TODO: find better solution
		$fileArrLen = count($fileArr);
		if(count($fileArr)>500){
			$fileArrLen = count($fileArr)-2;
		}
		
		$retArr = array();
		$toNum = $d->fromRow+$d->numRows-1;
		if($toNum>$fileArrLen) $toNum=$fileArrLen;
		for( $i=$d->fromRow-1; $i<$toNum; $i++ ){
			$retArr[] = $fileArr[$i];
		}
		
		$retObj = (object) array();
	 	$retObj->data = $retArr;
		$retObj->allRowsNum = $fileArrLen;
		$retObj->startRow = $d->fromRow;
		$retObj->rowsPerPage = $d->numRows;
		//print_r($fileArr);
		Response::sendOKResponse($retObj);
	 			
	}
	
	
	static function saveRequest($obj){

	 	$logObj = (object) array();
	 	$logObj->reguest=$obj;
	 	
		$userInfo = User::GetInstance()->getUserInfo();
	 	$rand_10 = substr(md5(microtime()),0,10);
	 	
	 	// create user log directory
	 	$logDir = Config::get("private_system_logs_path")."/".$userInfo->username;
		if(!Util::createDirectoryIfNotExist($logDir)) return;
		
		// create date log directory
	 	$logDir = Config::get("private_system_logs_path")."/".$userInfo->username."/".Util::getDateForFile();
	 	if(!Util::createDirectoryIfNotExist($logDir)) return;
	 	
	 	//write file
	 	$logFile = "";
	 	if(Config::get("saveFullLog", "system")){
	 		$logFile = $logDir."/".Util::getTimeForFile()."_".$rand_10.".txt";
	 		Util::writeFile($logFile, DataSerializer::serialize($logObj));
	 	}
	 	
		if(Config::get("saveLog", "system")){

		 	//create index
			$row = Util::getDateTimeForFile().";".$userInfo->username.";".$obj->application.";".$obj->action.";".$logFile;
			
			// create date index
			$filename = Config::get("private_system_logs_index_path")."/date/".Util::getDateForFile().".txt";
			Util::writeLineToFileBeginning($filename, $row, Config::get("logger_index_max_filesize"));
			
			// create user index
			$filename = Config::get("private_system_logs_index_path")."/user/".$userInfo->username.".txt";
			Util::writeLineToFileBeginning($filename, $row, Config::get("logger_index_max_filesize"));
		
			// create application index
			$filename = Config::get("private_system_logs_index_path")."/application/".$obj->application.".txt";
			Util::writeLineToFileBeginning($filename, $row, Config::get("logger_index_max_filesize"));
			
			// create application index
			$filename = Config::get("private_system_logs_index_path")."/method/".str_replace(":",".", $obj->action).".txt";
			Util::writeLineToFileBeginning($filename, $row, Config::get("logger_index_max_filesize"));
		 	
		}
		
		self::$logFile = $logFile;

	}
	
	static function saveResponse($obj){
	 	
	 	$logFile = self::$logFile;
	 	
	 	if(empty($logFile)) return;
	 	
	 	$logObj = DataSerializer::deserialize(Util::readFile($logFile));
	 	$logObj->response = $obj;

	 	Util::writeFile($logFile,  DataSerializer::serialize($logObj));
	 	
	}


}