<?php
class UserRight {
	private  $C_NO_ADMIN 	= 9;
	private  $C_NO_HR	 	= 10;
	private  $C_NO_ACTIVITY	= 10;
	private  $C_NO_PORTAL	= 6;
	private  $C_NO_REPORTS	= 4;
	
	const C_ENABLE = 0 ;
	const C_ADD = 1 ;
	const C_UPDATE = 2;
	const C_DELETE = 3;
	const C_VIEW = 4;
	const C_PRINT = 5 ;
	
	const C_ADMIN = "admin";
	const C_ADMIN_SETTING = 0;
	const C_ADMIN_COY = 1;
	const C_ADMIN_USER = 2;
	const C_ADMIN_GROUP = 3;
	const C_ADMIN_RESET = 4;
	const C_ADMIN_RESET_HISTORY = 5;
	const C_ADMIN_CONTENT_CATEGORY = 6;
	const C_ADMIN_CONTENT_FILES = 7;
	const C_ADMIN_CONTENT_NEWS = 8;
	
	const C_HR = "hr";
	const C_HR_EMP = 0;
	const C_HR_TYPE = 1 ;
	const C_HR_DEPT = 2;
	const C_HR_JOB = 3;
	const C_HR_NAT = 4 ;
	const C_HR_RACE = 5;
	const C_HR_PERMIT = 6;
	const C_HR_EMP_PASSWORD = 7;
	const C_HR_EMP_PURGE = 8;
	const C_HR_TRANS_PURGE = 9;
	
	const C_ACTIVITY = "activity";
	const C_ACTIVITY_TAXES = 0 ;
	const C_ACTIVITY_CUSTOMER = 1 ;
	const C_ACTIVITY_PROJECT = 2 ;
	const C_ACTIVITY_PHASE = 3 ;
	const C_ACTIVITY_EXPENSE = 4 ;
	const C_ACTIVITY_PRODUCT = 5 ;
	const C_ACTIVITY_CURRENCY = 6 ;
	const C_ACTIVITY_CONSULTANT = 7 ;
    const C_ACTIVITY_BRAND = 8 ;
	const C_ACTIVITY_MODEL = 9 ;	
	
	const C_PORTAL = "portal";
	const C_PORTAL_CUSTOMER_MASTER = 0 ;
	const C_PORTAL_SALES_QUOTATION = 1 ;
	const C_PORTAL_JOB_QUOTATION = 2 ;
	const C_PORTAL_JOB_PLAN = 3 ;
	const C_PORTAL_SERVICE = 4 ;
	const C_PORTAL_REPORTS = 5 ;

	const C_REPORTS = "reports";
	const C_REPORTS_JOB_STATUS = 0 ;
	const C_REPORTS_BILLING_STATUS = 1 ;
	const C_REPORTS_ACTUAL_BILLING = 2 ;
	const C_REPORTS_SALES_QUOTATION = 3 ;

	private $hr = array() ;
	private $admin = array() ;
	private $activity = array() ;
	private $portal = array() ;
	private $reports = array() ;
	
	function __construct() {
		$this->initRights() ;
	}
	function __destruct() {
	}
	function getAdminRight() {
		return $this->admin ;
	}
	function getHrRight() {
		return $this->hr ;
	}
	function getActivityRight() {
		return $this->activity ;
	}
	function getPortalRight() {
		return $this->portal ;
	}
	function getReportsRight() {
		return $this->reports ;
	}
	
	function setAdminRight($value) {
		$this->admin = $value ;
	}
	function setHrRight($value) {
		$this->hr = $value ;
	}
	function setActivityRight($value) {
		$this->activity = $value ;
	}
	function setPortalRight($value) {
		$this->portal = $value ;
	}
	function setReportsRight($value) {
		$this->reports = $value ;
	}
    
	function toRights($data) {
		$this->initRights() ;
		if ($data == "")
			return ;
		
		$r = trim(Util::decryptString(pack("H*" , $data),"","")) ;
		//$r = $data ;
		if ($r == "")
			return ;
		$d = explode("|",$r) ;
		$cnt = count($d) ;
		if ($cnt > 0)
			$this->toAdminRight($d[0]) ;
		else 
			return ;
		if ($cnt > 1) 
			$this->toHrRight($d[1]) ;
		else
			return;
		if ($cnt > 2)
			$this->toActivityRight($d[2]) ;
		else
			return;
		if ($cnt > 3)
			$this->toPortalRight($d[3]) ;
		else
			return;
		if ($cnt > 4)
			$this->toReportsRight($d[4]) ;
		else
			return;
	}
	
	function toString() {
		$s = "" ;
		for ($i=0;$i<$this->C_NO_ADMIN;$i++) {
			$s .= $this->access2Hex($this->admin[$i]) ;
		}
		$s .= "|" ;
		for ($i=0;$i<$this->C_NO_HR;$i++) {
			$s .= $this->access2Hex($this->hr[$i]) ;
		}
		$s .= "|" ;
		for ($i=0;$i<$this->C_NO_ACTIVITY;$i++) {
			$s .= $this->access2Hex($this->activity[$i]) ;
		}
		$s .= "|" ;
		for ($i=0;$i<$this->C_NO_PORTAL;$i++) {
			$s .= $this->access2Hex($this->portal[$i]) ;
		}
		$s .= "|" ;
		for ($i=0;$i<$this->C_NO_REPORTS;$i++) {
			$s .= $this->access2Hex($this->reports[$i]) ;
		}
		return bin2hex(Util::encryptString($s,"","")) ;
		//return $s ;
	}
	function initRights() {
		$r  = '00';
		for ($i=0 ;$i < $this->C_NO_ADMIN;$i++) {
			$this->admin[$i] = $this->hex2Access($r) ;
		}
		for ($i= 0 ;$i < $this->C_NO_HR;$i++) {
			$this->hr[$i] = $this->hex2Access($r) ;
		}
		for ($i=0;$i<$this->C_NO_ACTIVITY;$i++) {
			$this->activity[$i] = $this->hex2Access($r) ;
		}
		for ($i=0;$i<$this->C_NO_PORTAL;$i++) {
			$this->portal[$i] = $this->hex2Access($r) ;
		}
		for ($i=0;$i<$this->C_NO_REPORTS;$i++) {
			$this->reports[$i] = $this->hex2Access($r) ;
		}
		
	}
	private function toAdminRight($data) {
		if (trim($data) == "")
			return ;

		$len = strlen($data) ;
		if ($len < 2)
			return ;
			
		if ($len > $this->C_NO_ADMIN)
			$len = $this->C_NO_ADMIN ;
		$idx = 0 ;
		for ($i=0;$i<$len;$i++) {
			$this->admin[$i] = $this->hex2Access(substr($data,$idx,2)) ;
			$idx += 2;
		}
	}

	private function toHrRight($data) {
		if (trim($data) == "")
			return ;

		$len = strlen($data) ;
		if ($len < 2) 
			return ;
			
		if ($len > $this->C_NO_HR)
			$len = $this->C_NO_HR ;
		$idx = 0 ;
		for ($i=0;$i<$len;$i++) {
			$this->hr[$i] = $this->hex2Access(substr($data,$idx,2)) ;
			$idx += 2;
		}
	}
	
	private function toActivityRight($data) {
		if (trim($data) == "")
			return ;
	
		$len = strlen($data) ;
		if ($len < 2)
			return ;
			
		if ($len > $this->C_NO_ACTIVITY)
			$len = $this->C_NO_ACTIVITY ;
		$idx = 0 ;
		for ($i=0;$i<$len;$i++) {
			$this->activity[$i] = $this->hex2Access(substr($data,$idx,2)) ;
			$idx += 2;
		}
	}

	private function toPortalRight($data) {
		if (trim($data) == "")
			return ;
	
		$len = strlen($data) ;
		if ($len < 2)
			return ;
			
		if ($len > $this->C_NO_PORTAL)
			$len = $this->C_NO_PORTAL ;
		$idx = 0 ;
		for ($i=0;$i<$len;$i++) {
			$this->portal[$i] = $this->hex2Access(substr($data,$idx,2)) ;
			$idx += 2;
		}
	}

	private function toReportsRight($data) {
		if (trim($data) == "")
			return ;
	
		$len = strlen($data) ;
		if ($len < 2)
			return ;
			
		if ($len > $this->C_NO_REPORTS)
			$len = $this->C_NO_REPORTS ;
		$idx = 0 ;
		for ($i=0;$i<$len;$i++) {
			$this->reports[$i] = $this->hex2Access(substr($data,$idx,2)) ;
			$idx += 2;
		}
	}
	
	private function access2Hex($access) {
		$data = "00000000";
		if (!is_null($access) && count($access) > 0) {
			$cnt = count($access) ;
			if ($cnt > 8)
				$cnt = 8 ;
			for ($i=0;$i < $cnt ;$i++) {
				if ($access[$i])
					$data[$i] = "1";
			}
		} 
		return str_pad(dechex(bindec($data)),2,"0",STR_PAD_LEFT) ;
	}
	private function hex2Access($hex) {
		$access = array();
		for ($i=0;$i<8;$i++) {
			$access[$i] = false ;
		}
		$r = str_pad(decbin(hexdec($hex)),8,"0",STR_PAD_LEFT) ;
		for ($i=0;$i<8;$i++) {
			if ($r[$i] == "1")
				$access[$i] = true ;
		}
		return $access ;
	}
	function bin2Access($binary) {
		$access = array() ;
		for ($i=0;$i < 8;$i++) {
			$access[$i] = false ;
		}
		$len = strlen($binary) ;
		if ($len > 8)
			$len = 8 ;
		for ($i=0;$i < $len; $i++) {
			if ($binary[$i] == 1) {
				$access[$i] = true ;
			}
		}
		return $access ;
	}
}
?>