<?
 
class utilityClass extends condbClass{

	private $currentAction;   
	private $status;   
	private $task;   
	private $output;   
	private $createdby;
	private $createddate;
	private $updateddate;
	private $updatedby;
	
	public function encodeActivityLog($activityArr){
		$nextId					=	self::getNextId('Id','activity_log');
		$acArr 					=	array();
		$activityArr['Id'] 		=	$nextId;
		$acArr['Id']			=	$activityArr['Id'];
		$acArr['UserType']		=	$activityArr['UserType']; 
		$acArr['Remarks']		=	$activityArr['Remarks'];
		$acArr['Action']		=	$activityArr['Action']; 
		$acArr['ActionType']	=	$activityArr['ActionType']; 
		$acArr['ActionContent']	=	$activityArr['ActionContent'];
		$acArr['ActionBy']		=	$activityArr['ActionBy'];
		$acArr['ActionDate']	=	$activityArr['ActionDate'];
		$acArr['TransId']		=	$activityArr['TransId'];
		$acArr['IpAddress']		=	self::get_client_ip();
		self::insertSingleQuery($acArr,'activity_log');
	}
	
	public function getUserIP()
{
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];

    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }

    return $ip;
}

public function get_client_ip() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
       $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}
	
	public function checkExistField($field,$table,$valueField,$value){
		self::query("SELECT $field FROM $table WHERE $field = :$valueField");
		self::bind(":$valueField", $value); 
		$row = self::resultset();
		$ctrrow = count($row);
		if($ctrrow==0){
			$output = false;
		}else{
			$output = true;
		}
		return $output;
	}

	
	public function getVoucherNo($module,$seqno){
		$fieldArr = array('prefix','maxno');
		$condArr = array('module'=>$module);
		$order = "id DESC LIMIT 1";
		$output = self::retrieveSingleQuery($fieldArr,"seqno",$condArr,$order);
		$prefix = $output['prefix'];
		$maxno = $output['maxno'];
		$prefixCode = $prefix.str_pad($seqno,$maxno,"0",STR_PAD_LEFT);
		return $prefixCode;
	}
	
	public function getFieldId($fieldId,$refId,$refVal,$table){
		$fieldArr = array($fieldId);
		$condArr = array($refId=>$refVal);
		$order = "$fieldId DESC LIMIT 1";
		$output = self::retrieveSingleQuery($fieldArr,$table,$condArr,$order);
		$result = $output[$fieldId]; 
		return $result;
	}
	
	
	public function getSeqNo($module,$seq){
		$fieldArr = array($seq);
		$condArr = array();
		$order  = "id DESC LIMIT 1";
		$output = self::retrieveSingleQuery($fieldArr,$module,$condArr,$order);
		$seqno	= $output[$seq];
		
		//get from seqno table
		$fieldArr2 = array('startno');
		$condArr2 = array('module'=>$module);
		$order2  = "id DESC LIMIT 1";
		$output2 = self::retrieveSingleQuery($fieldArr2,"seqno",$condArr2,$order2);
		if(empty($seqno)){
			$seqno = $output2['startno'];
		}else{
			$seqno += 1;
		}
		return $seqno;
	}
	 
	public function setStatus($status){	$this->status = $status;	}	
	public function getStatus(){	return $this->status;	}
	
	public function setCurrentValue($currentValue){	$this->currentValue = $currentValue;	}	
	public function getCurrentValue(){	return $this->currentValue;	}
	
	public function setCurrentAction($currentAction){	$this->currentAction = $currentAction;	}	
	public function getCurrentAction(){	return $this->currentAction;	}
  
	public function setCreatedDate($createddate){	$this->createddate = $createddate;	}	
	public function getCreatedDate(){	return $this->createddate;	}
	
	public function setCreatedBy($createdby){	$this->createdby = $createdby;	}
	public function getCreatedBy(){	return $this->createdby;	}
	
	public function setUpdatedDate($updateddate){	$this->updateddate = $updateddate;	}	
	public function getUpdatedDate(){	return $this->updateddate;	}
	
	public function setUpdatedBy($updatedby){	$this->updatedby = $updatedby;	}
	public function getUpdatedBy(){	return $this->updatedby;	} 
	 
	
	
	
	
	
	
	
	
	
	
	
	public function encrypt($pure_string, $encryption_key) {
		$iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
		$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
		$encrypted_string = mcrypt_encrypt(MCRYPT_BLOWFISH, $encryption_key, utf8_encode($pure_string), MCRYPT_MODE_ECB, $iv);
		return $encrypted_string;
	}

	public function decrypt($encrypted_string, $encryption_key) {
		$iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
		$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
		$decrypted_string = mcrypt_decrypt(MCRYPT_BLOWFISH, $encryption_key, $encrypted_string, MCRYPT_MODE_ECB, $iv);
		return $decrypted_string;
	}

}