<?php

// -----------------------------------------------------------------------
// This file is part of Igloo
// 
// Copyright (C) 2003, 2006 Barnraiser
// http://www.barnraiser.org/
// info@barnraiser.org
// 
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option) any
// later version.
// 
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with program; see the file COPYING. If not, write to the Free
// Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
// 02110-1301, USA.
// -----------------------------------------------------------------------

class Acs {
	// CLASS FUNCTIONS
	// Acs
	// insertPermission
	// deletePermissions
	// getPermission
	// checkPermission
	// getAllPermissions
	// findRoleIndex
	// getResources
	
	
	
	// Acs --------------------------------------------------------------------
	// the constuctor
	// erik sundelof - 29th Nov 2004
	//
	function Acs($role_id=null, $db=null){
		$this->role_id     = $role_id;
		$this->db          = $db;
	}//EO Acs
	
	
	
	// insertPermission --------------------------------------------------------------------
	// insert permission
	// erik sundelof - 4th Nov 2004
	//
	function insertPermission($role_id, $resource_id, $privilege_id) {
		
		$rec = array();
		$rec['role_id'] = $role_id;
		$rec['resource_id'] = $resource_id;
		$rec['privilege_id'] = $privilege_id;
		
		$table = $this->db->prefix . "_acs_permission";
		
		$query = $this->db->GetInsertSQL($table, $rec, get_magic_quotes_gpc());
		
		$result = $this->db->Execute($query);
	}//EO insertPermission
	
	
	
	// deletePermissions --------------------------------------------------------------------
	// delete single group role
	// erik sundelof - 29th Nov 2004
	//
	function deletePermissions() {
	
		$query = "
			DELETE FROM " . $this->db->prefix . "_acs_permission "
		;
		
		if (isset($this->role_id)) {
			$query .= "
				WHERE 
				role_id=" . $this->role_id
			;
		}
		
		$result = $this->db->Execute($query);
	}//EO deletePermissions
	
	
	
	// getPermission --------------------------------------------------------------------
	// get permission
	// tom calthrop - 4th Nov 2004
	//
	function getPermission($role_id, $resource_id, $privilege_id) {
		$query = "
			SELECT rp.*
			FROM " . $this->db->prefix . "_acs_permission rp
			where
			rp.role_id=" . $role_id . " and 
			rp.resource_id=" . $resource_id . " and 
			rp.privilege_id=" . $privilege_id
		;
		
		$result = $this->db->Execute($query);
			
		if (isset($result->fields['role_id'])) {
			return 1;
		}
		else{
			return 0;
		}
	} //EO getPermission
	
	
	
	// checkPermission --------------------------------------------------------------------
	// check permission for a given resource and privilege ID
	// erik sundelof - 29th Nov 2004
	//
	function checkPermission($resource_id, $privilege_id) {
		
		$tmp = $this->getPermission($this->role_id, $resource_id, $privilege_id);
		
		if(!empty($tmp)) {
			return true;
		}
		else {
			return false;
		}
	}// EO checkPermission
	
	
	
	// getAllPermissions --------------------------------------------------------------------
	// get all permissions for a given section
	// erik sundelof - 29th Nov 2004
	//
	function getAllPermissions() {
		$query = "
			SELECT rp.*
			FROM " . $this->db->prefix . "_acs_permission rp"
		;
		
		$result = $this->db->Execute($query);
	
		if (isset($result->fields)) {

			$temp = array();

			while (!$result->EOF) {
				
				array_push($temp, $result->fields);
				$result->MoveNext();
			}

			$result->Close();
			return $temp;
		}
	}// EO getALLPermissions
	
	
	
	// findRoleIndex --------------------------------------------------------------------
	// find array with index that corresponds to a given role
	// erik sundelof - 29th Nov 2004
	//
	function findRoleIndex($role_id, $output_roles){
		foreach($output_roles as $key => $roles){
			if($roles["role_id"]==$role_id)
				return $key;
		}
	}//EO findRoleIndex
	
	
	
	// getResources --------------------------------------------------------------------
	// get resources and create structure
	// erik sundelof - 30th Nov 2004
	//
	function getResources($config_resources, $resource_privilege, $arr_acs, $output_roles){
	
		if (!empty($config_resources)) {
			$output_resources = array();
			
			//loop through the array and get the name, desc from the language file
			foreach($config_resources as $key => $res):
			
				if(!is_array($arr_acs['resource'][$res])){
					$temp1['resource_name'] = $arr_acs['resource'][$res];
					$temp1['resource_id'] = $res;
					$temp1['resource_desc'] = $arr_acs['resource_desc'][$res];
				}
				else{
					$temp1['resource_name'] = $arr_acs['resource'][$res][0]." (sub-resources)";
					$temp1['resource_id'] = $res;
					$temp1['resource_desc'] = $arr_acs['resource_desc'][$res];
				}
				
				//get privileges attached to each resource
				$config_privileges = $resource_privilege[$res];
				
				$temp1['privileges'] = array();
				
				if (!empty($config_privileges)) {
					//loop through the array and get the name from the language file
					foreach($config_privileges as $key1 => $priv):
						$temp2['privilege_name'] = $arr_acs['privilege'][$priv];
						$temp2['privilege_id'] = $priv;
						
						//for each privilege we now see if the permission is set against the role
						if (!empty($output_roles)) {
							$temp2['permissions'] = array();
							
							//loop through the array and check the permission
							foreach($output_roles as $key2 => $role):
								
								if ($this->getPermission($role['role_id'], $res, $priv)) {
									$temp2['permissions'][] = 1;
								}
								else {
									$temp2['permissions'][] = 0;
								}
							endforeach;
						}
						array_push($temp1['privileges'], $temp2);
					endforeach;
				}
				//push this resource info to the main array
				array_push($output_resources, $temp1);
			endforeach;
		}
		else {
			$output_resources = array();
		}
		
		return $output_resources;

	}//EO getResources
}

?>
