ini_acl.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: ini__acl_8php-source.html 675 2008-12-26 00:27:14Z gwoo $ */
00003 /**
00004  * This is core configuration file.
00005  *
00006  * Use it to configure core behaviour ofCake.
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00011  * Copyright 2005-2008, Cake Software Foundation, Inc.
00012  *                              1785 E. Sahara Avenue, Suite 490-204
00013  *                              Las Vegas, Nevada 89104
00014  *
00015  * Licensed under The MIT License
00016  * Redistributions of files must retain the above copyright notice.
00017  *
00018  * @filesource
00019  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00020  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00021  * @package         cake
00022  * @subpackage      cake.cake.libs.controller.componenets.iniacl
00023  * @since           CakePHP(tm) v 0.2.9
00024  * @version         $Revision: 675 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-12-25 16:27:14 -0800 (Thu, 25 Dec 2008) $
00027  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00028  */
00029 /**
00030  * load AclBase
00031  */
00032 uses('controller/components/acl_base');
00033 /**
00034  * In this file you can extend the AclBase.
00035  *
00036  * @package     cake
00037  * @subpackage  cake.cake.libs.controller.componenets.iniacl
00038  */
00039 class INI_ACL extends AclBase {
00040 /**
00041  * Array with configuration, parsed from ini file
00042  *
00043  * @var array
00044  */
00045     var $config = null;
00046 /**
00047  * Constructor
00048  *
00049  */
00050     function __construct() {
00051     }
00052 
00053 /**
00054  * Main ACL check function. Checks to see if the ARO (access request object) has access to the ACO (access control object).
00055  * Looks at the acl.ini.php file for permissions (see instructions in/config/acl.ini.php).
00056  *
00057  * @param string $aro
00058  * @param string $aco
00059  * @return boolean
00060  * @access public
00061  */
00062     function check($aro, $aco, $acoAction = null) {
00063         if ($this->config == null) {
00064             $this->config = $this->readConfigFile(CONFIGS . 'acl.ini.php');
00065         }
00066         $aclConfig = $this->config;
00067 
00068         //First, if the user is specifically denied, then DENY
00069         if (isset($aclConfig[$aro]['deny'])) {
00070             $userDenies = $this->arrayTrim(explode(",", $aclConfig[$aro]['deny']));
00071 
00072             if (array_search($aco, $userDenies)) {
00073                 //echo "User Denied!";
00074                 return false;
00075             }
00076         }
00077 
00078         //Second, if the user is specifically allowed, then ALLOW
00079         if (isset($aclConfig[$aro]['allow'])) {
00080             $userAllows = $this->arrayTrim(explode(",", $aclConfig[$aro]['allow']));
00081 
00082             if (array_search($aco, $userAllows)) {
00083                 //echo "User Allowed!";
00084                 return true;
00085             }
00086         }
00087 
00088         //Check group permissions
00089         if (isset($aclConfig[$aro]['groups'])) {
00090             $userGroups = $this->arrayTrim(explode(",", $aclConfig[$aro]['groups']));
00091 
00092             foreach ($userGroups as $group) {
00093                 //If such a group exists,
00094                 if (array_key_exists($group, $aclConfig)) {
00095                     //If the group is specifically denied, then DENY
00096                     if (isset($aclConfig[$group]['deny'])) {
00097                         $groupDenies=$this->arrayTrim(explode(",", $aclConfig[$group]['deny']));
00098 
00099                         if (array_search($aco, $groupDenies)) {
00100                             //echo("Group Denied!");
00101                             return false;
00102                         }
00103                     }
00104 
00105                     //If the group is specifically allowed, then ALLOW
00106                     if (isset($aclConfig[$group]['allow'])) {
00107                         $groupAllows = $this->arrayTrim(explode(",", $aclConfig[$group]['allow']));
00108 
00109                         if (array_search($aco, $groupAllows)) {
00110                             //echo("Group Allowed!");
00111                             return true;
00112                         }
00113                     }
00114                 }
00115             }
00116         }
00117 
00118         //Default, DENY
00119         //echo("DEFAULT: DENY.");
00120         return false;
00121     }
00122 
00123 /**
00124  * Parses an INI file and returns an array that reflects the INI file's section structure. Double-quote friendly.
00125  *
00126  * @param string $fileName
00127  * @return array
00128  */
00129     function readConfigFile($fileName) {
00130         $fileLineArray = file($fileName);
00131 
00132         foreach ($fileLineArray as $fileLine) {
00133                 $dataLine = trim($fileLine);
00134                 $firstChar = substr($dataLine, 0, 1);
00135 
00136                 if ($firstChar != ';' && $dataLine != '') {
00137                     if ($firstChar == '[' && substr($dataLine, -1, 1) == ']') {
00138                         $sectionName = preg_replace('/[\[\]]/', '', $dataLine);
00139                     } else {
00140                         $delimiter = strpos($dataLine, '=');
00141 
00142                         if ($delimiter > 0) {
00143                             $key = strtolower(trim(substr($dataLine, 0, $delimiter)));
00144                             $value = trim(substr($dataLine, $delimiter + 1));
00145 
00146                             if (substr($value, 0, 1) == '"' && substr($value, -1) == '"') {
00147                                 $value = substr($value, 1, -1);
00148                             }
00149 
00150                             $iniSetting[$sectionName][$key]=stripcslashes($value);
00151                         } else {
00152                             if (!isset($sectionName)) {
00153                                 $sectionName = '';
00154                             }
00155 
00156                             $iniSetting[$sectionName][strtolower(trim($dataLine))]='';
00157                         }
00158                     }
00159                 } else {
00160                 }
00161         }
00162 
00163         return $iniSetting;
00164     }
00165 /**
00166  * Removes trailing spaces on all array elements (to prepare for searching)
00167  *
00168  * @param array $array Array to trim
00169  * @return array Trimmed array
00170  * @access public
00171  */
00172     function arrayTrim($array) {
00173         foreach ($array as $key => $value) {
00174             $array[$key] = trim($value);
00175         }
00176         array_unshift($array, "");
00177         return $array;
00178     }
00179 }
00180 ?>