phpPoA2
[ class tree: phpPoA2 ] [ index: phpPoA2 ] [ all elements ]

Source for file QueryFilterAuthzEngine.php

Documentation is available at QueryFilterAuthzEngine.php

  1. <?php
  2. /**
  3.  * @copyright Copyright 2005-2010 RedIRIS, http://www.rediris.es/
  4.  *
  5.  *  This file is part of phpPoA2.
  6.  *
  7.  *  phpPoA2 is free software: you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation, either version 3 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  phpPoA2 is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with phpPoA2. If not, see <http://www.gnu.org/licenses/>.
  19.  *
  20.  * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
  21.  * @version 2.0
  22.  * @author Jaime Perez <jaime.perez@rediris.es>
  23.  * @filesource
  24.  */
  25.  
  26. /**
  27.  * This hook is executed right after retrieving the current URI, the params (both GET and POST) and
  28.  * the arrays of allowed and denied patterns that will be checked inmediately.
  29.  * It can be used to alter parameters and the URL, and also to configure the filters on runtime.
  30.  * The hook receives the URI string, an array of parameters, the allowed and the denied patterns.
  31.  * Functions for this hook must be defined like this:
  32.  *
  33.  * function queryBeforeFilterHook(&$uri, &$params, &$allowed, &$denied);
  34.  *
  35.  * Please bear in mind that hooks must return TRUE or they'll keep other hooks from executing.
  36.  */
  37. define("QUERY_BEFORE_FILTERS""QUERY_BEFORE_FILTERS");
  38.  
  39. /**
  40.  * Authorization engine that works by checking the query string of the request.
  41.  * PLEASE NOTE THAT THIS ENGINE SILENTLY IGNORES BOTH USER AND ATTRIBUTES.
  42.  * PLEASE NOTE THAT THIS ENGINE WORKS ONLY FOR WEB-BASED APPLICATIONS.
  43.  * @package phpPoA2
  44.  * @subpackage QueryFilterAuthorizationEngine
  45.  */
  46.  
  47.     protected $valid_hooks = array(QUERY_BEFORE_FILTERS);
  48.  
  49.     /**
  50.      * PLEASE NOTE THAT THIS ENGINE SILENTLY IGNORES BOTH USER AND ATTRIBUTES.
  51.      */
  52.     public function isAuthorized($user$attrs{
  53.         $params $this->getQueryParams();
  54.         $default $this->cfg->getDefaultBehaviour();
  55.         $allowed $this->cfg->getAllowedPatterns();
  56.         $denied  $this->cfg->getDeniedPatterns();
  57.  
  58.         // run hook before checking patterns
  59.         $args array($params$allowed$denied);
  60.         $this->runHooks(QUERY_BEFORE_FILTERS$args);
  61.         $params $args[0];
  62.         $allowed $args[1];
  63.         $denied $args[2];
  64.  
  65.         $allowed_match $this->matches($params$allowed);
  66.         $denied_match  $this->matches($params$denied);
  67.  
  68.         // check matches giving priority to the default setting
  69.         $order array($default!$default);
  70.         foreach ($order as $option{
  71.             if ($option// check allowed parameters
  72.                 if ($allowed_match{
  73.                     trigger_error(PoAUtils::msg('allowed-param-match'array($allowed_match))E_USER_WARNING);
  74.                     return true;
  75.                 }
  76.             else // check denied parameters
  77.                 if ($denied_match{
  78.                     trigger_error(PoAUtils::msg('denied-param-match'array($denied_match))E_USER_WARNING);
  79.                     return false;
  80.                 }
  81.             }
  82.         }
  83.  
  84.         // default response
  85.         trigger_error(PoAUtils::msg('authz-default-fallback')E_USER_NOTICE);
  86.         return $default;
  87.     }
  88.  
  89.     public function getAuthorizedList({
  90.         $this->registerHandler();
  91.         $list $this->cfg->getAllowedPatterns();
  92.         $this->clean();
  93.         return $list;
  94.     }
  95.  
  96.     public function authorize($user$attrs$ref$expires 0{
  97.         return false;
  98.     }
  99.  
  100.     public function revoke($mail{
  101.         return false;
  102.     }
  103.  
  104.     /**
  105.      * Get all the input received for the current request.
  106.      * @return data An array containing all parameters received as input.
  107.      */
  108.     private function getQueryParams({
  109.         // Full URI
  110.         $uri $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
  111.  
  112.         // GET queries
  113.         $data explode("&"$_SERVER['QUERY_STRING']);
  114.  
  115.         // POST queries
  116.         if ($_SERVER['REQUEST_METHOD'=== "POST"{
  117.             $str file_get_contents("php://input");
  118.  
  119.             $post array();
  120.             if ($str{
  121.                 $post explode("&"$str);
  122.             }
  123.  
  124.             $data array_merge($data$post);
  125.         }
  126.  
  127.         // convert to associative array
  128.         $result['&REQUEST_URI'$uri;
  129.         foreach ($data as $item{
  130.             @list($key$valueexplode("="$item);
  131.             $result[$key$value;
  132.         }
  133.  
  134.         return $result;
  135.     }
  136.  
  137.     /**
  138.      * Returns the parameter (or parameters) that matched a list of patterns.
  139.      * @param params An array of parameters to check.
  140.      * @param patterns An array of patterns to be matched with.
  141.      * @return The names of the parameters matched, comma separated if more than one.
  142.      *  False otherwise.
  143.      */
  144.     private function matches($params$patterns{
  145.         $match false;
  146.         foreach ($patterns as $key => $value{
  147.             if (is_numeric($key&& is_array($value)) // must match a bunch of options
  148.                 $partial_match true;
  149.                 $matches array();
  150.                 foreach ($value as $name => $pattern{
  151.                     if (!isset($params[$name])) // parameter not set, skip this option
  152.                         $partial_match false;
  153.                         break;
  154.                     }
  155.  
  156.                     // convert parameter to array for easy handling
  157.                     $param $params[$name];
  158.                     if (!is_array($params[$name])) {
  159.                         $param array($params[$name]);
  160.                     }
  161.  
  162.                     // convert pattern to array for easy handling
  163.                     $pats $pattern;
  164.                     if (!is_array($pattern)) {
  165.                         $pats array($pattern);
  166.                     }
  167.  
  168.                     // check if any of the possible values match
  169.                     $some_val_matches false;
  170.                     foreach ($param as $item{
  171.                         foreach ($pats as $pat{
  172.                             if (preg_match('/'.$pat.'/'$item)) {
  173.                                 // parameter matches, continue
  174.                                 $some_val_matches true;
  175.                                 break;
  176.                             }
  177.                         }
  178.                         if ($some_val_matchesbreak;
  179.                     }
  180.                     if (!$some_val_matches{
  181.                         $partial_match false;
  182.                         break;
  183.                     }
  184.  
  185.                     $matches[$name;
  186.                 }
  187.                 if ($partial_match{
  188.                     $match implode(","$matches);
  189.                 }
  190.  
  191.             else // match just one parameter
  192.                 if (is_numeric($key))
  193.                     $key "&REQUEST_URI"// try to match the complete URI
  194.  
  195.                 if (!isset($params[$key])) // parameter not set, skip this option
  196.                     continue;
  197.  
  198.                 // convert parameter to array for easy handling
  199.                 $param $params[$key];
  200.                 if (!is_array($params[$key])) {
  201.                     $param array($params[$key]);
  202.                 }
  203.  
  204.                 // convert pattern to array for easy handling
  205.                 $pats $value;
  206.                 if (!is_array($value)) {
  207.                     $pats array($value);
  208.                 }
  209.  
  210.                 // check if any of the possible values match
  211.                 foreach ($param as $item{
  212.                     foreach ($pats as $pattern{
  213.                         if (preg_match('/'.$pattern.'/'$item)) {
  214.                             // parameter matches, stop searching
  215.                             $match $key;
  216.                             break;
  217.                         }
  218.                     }
  219.                     if ($matchbreak;
  220.                 }
  221.             }
  222.         }
  223.         return $match;
  224.     }
  225.  
  226. }
  227.  
  228. ?>

Documentation generated on Mon, 20 Feb 2012 12:07:17 +0100 by phpDocumentor 1.4.3