"Fossies" - the Fresh Open Source Software Archive

Member "icinga-web-1.14.1/app/modules/Api/lib/config/AccessConfig.class.php" (19 Dec 2017, 6128 Bytes) of package /linux/misc/old/icinga-web-1.14.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 // {{{ICINGA_LICENSE_CODE}}}
    3 // -----------------------------------------------------------------------------
    4 // This file is part of icinga-web.
    5 //
    6 // Copyright (c) 2009-2015 Icinga Developer Team.
    7 // All rights reserved.
    8 //
    9 // icinga-web is free software: you can redistribute it and/or modify
   10 // it under the terms of the GNU General Public License as published by
   11 // the Free Software Foundation, either version 3 of the License, or
   12 // (at your option) any later version.
   13 //
   14 // icinga-web is distributed in the hope that it will be useful,
   15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
   16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17 // GNU General Public License for more details.
   18 //
   19 // You should have received a copy of the GNU General Public License
   20 // along with icinga-web.  If not, see <http://www.gnu.org/licenses/>.
   21 // -----------------------------------------------------------------------------
   22 // {{{ICINGA_LICENSE_CODE}}}
   23 
   24 /**
   25 * Gives access to the access.xml values.
   26 * Please make sure to not use the AgaviConfig base class for static access, as php < 5.3 does not
   27 * support late static binding and therefore you will access the methdods of the baseclass
   28 **/
   29 class ApiUnknownHostException extends AppKitException {};
   30 class ApiUnknownInstanceException extends AppKitException {};
   31 
   32 final class AccessConfig {
   33     /**
   34      * @var        array
   35      */
   36     private static $config = array();
   37     private static $configLoaded = false;
   38     private static $path = null;
   39     public static function loadConfig() {
   40         if(self::$configLoaded)
   41             return;
   42         self::$config = include AgaviConfigCache::checkConfig(AgaviToolkit::expandDirectives('%core.module_dir%/Api/config/access.xml'));
   43         self::$configLoaded = true;
   44     }
   45 
   46     public static function getDefaultHost() {
   47         self::loadConfig();
   48         return self::getHostByName(self::$config["defaultHost"]);
   49     }
   50 
   51     public static function getDefaultHostname() {
   52         self::loadConfig();
   53         return self::$config["defaultHost"];
   54     }
   55 
   56     public static function getHostByName($hostname) {
   57         self::loadConfig();
   58         if(is_array($hostname)) {
   59            $hosts = array();
   60            foreach($hostname as $hostEntry) {
   61                if(isset(self::$config["hosts"][$hostEntry]))
   62                     $hosts[$hostEntry] = self::$config["hosts"][$hostEntry];
   63            }
   64            return $hosts;
   65         }
   66         if(!is_string($hostname))
   67             return $hostname;
   68         if(isset(self::$config["hosts"][$hostname]))
   69             return self::$config["hosts"][$hostname];
   70         throw new ApiUnknownHostException("Unknown host ".$hostname.". You must first define it in your access.xml if you want to use it");
   71     }
   72 
   73     public static function getHostnameByInstance($instance) {
   74         self::loadConfig();
   75         if(isset(self::$config["instances"][$instance]))
   76             return self::$config["instances"][$instance];
   77         throw new ApiUnknownInstanceException("Unknown instance ".$instance.". You must first define it in your access.xml if you want to use it");
   78     }
   79 
   80     public static function getHostByInstance($instance) {
   81         self::loadConfig();
   82         return self::getHostByName(self::getHostnameByInstance($instance));
   83     }
   84 
   85     public static function canRead($file,$host) {
   86         self::loadConfig();
   87         $hosts = self::getHostByName($host);
   88         if(isset($hosts["auth"]))
   89             $hosts = array($hosts);
   90         foreach($hosts as $host) {
   91             if(isset($host["r"][$file]) || isset($host["rw"][$file]))
   92                 continue;
   93             $basedir = preg_replace("/([^\\\]) .*/","$1",$file);
   94             if(in_array($file,$host["r"]) || in_array($file,$host["rw"])
   95                 || in_array(dirname($basedir)."/*",$host["r"]) || in_array(dirname($basedir)."/*",$host["rw"]))
   96                 continue;
   97             return false;
   98         }
   99         return true;
  100     }
  101 
  102     public static function canWrite($file,$host) {
  103 
  104         $hosts = self::getHostByName($host);
  105         if(isset($hosts["auth"]))
  106             $hosts = array($hosts);
  107         foreach($hosts as $host) {
  108             if(isset($host["w"][$file]) || isset($host["rw"][$file]))
  109                 continue;
  110             $basedir = preg_replace("/([^\\\]) .*/","$1",$file);
  111             if (in_array($file,$host["w"]) || in_array($file,$host["rw"])
  112                 || in_array(dirname($basedir)."/*",$host["w"]) || in_array(dirname($basedir)."/*",$host["rw"]))
  113                 continue;
  114             return false;
  115         }
  116         return true;
  117     }
  118 
  119     public static function canExecute($file,$host) {
  120         $hosts = self::getHostByName($host);
  121         if(isset($hosts["auth"]))
  122             $hosts = array($hosts);
  123         foreach($hosts as $host) {
  124             if(isset($host["x"][$file]))
  125                 continue;
  126             $basedir = preg_replace("/([^\\\]) .*/","$1",$file);
  127             if(in_array($file,$host["x"]) || in_array(dirname($basedir)."/*",$host["x"]))
  128                 continue;
  129             return false;
  130         }
  131         return true;
  132     }
  133 
  134     public static function expandSymbol($file,$type,$host) {
  135         $host = self::getHostByName($host);
  136 
  137         if(isset($host[$type][$file]))
  138             $file = $host[$type][$file];
  139         if(is_array($file))
  140             $file = $file[0];
  141         return $file;
  142     }
  143 
  144     public static function fromArray(array $data)
  145     {
  146         // array_merge would reindex numeric keys, so we use the + operator
  147         // mind the operand order: keys that exist in the left one aren't overridden
  148         self::$config = $data + self::$config;
  149     }
  150     public static function getAvailableHosts() {
  151         self::loadConfig();
  152         $hostnames = array();
  153         foreach(self::$config["hosts"] as $name=>$host) {
  154             $hostnames[] = $name;
  155         }
  156         return $hostnames;
  157 
  158     }
  159 
  160     public static function toArray()
  161     {
  162         self::loadConfig();
  163         return self::$config;
  164     }
  165 
  166     public static function clear()
  167     {
  168         // doesn't apply as everything is read-only per default
  169 
  170     }
  171 
  172 
  173 }