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

Source for file XML.php

Documentation is available at XML.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * A framework for authentication and authorization in PHP applications
  6.  *
  7.  * LiveUser is an authentication/permission framework designed
  8.  * to be flexible and easily extendable.
  9.  *
  10.  * Since it is impossible to have a
  11.  * "one size fits all" it takes a container
  12.  * approach which should enable it to
  13.  * be versatile enough to meet most needs.
  14.  *
  15.  * PHP version 4 and 5
  16.  *
  17.  * LICENSE: This library is free software; you can redistribute it and/or
  18.  * modify it under the terms of the GNU Lesser General Public
  19.  * License as published by the Free Software Foundation; either
  20.  * version 2.1 of the License, or (at your option) any later version.
  21.  *
  22.  * This library is distributed in the hope that it will be useful,
  23.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  25.  * Lesser General Public License for more details.
  26.  *
  27.  * You should have received a copy of the GNU Lesser General Public
  28.  * License along with this library; if not, write to the Free Software
  29.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30.  * MA  02111-1307  USA
  31.  *
  32.  *
  33.  * @category authentication
  34.  * @package LiveUser
  35.  * @author  Markus Wolff <wolff@21st.de>
  36.  * @author  Helgi Þormar Þorbjörnsson <dufuz@php.net>
  37.  * @author  Lukas Smith <smith@pooteeweet.org>
  38.  * @author  Arnaud Limbourg <arnaud@php.net>
  39.  * @author  Pierre-Alain Joye <pajoye@php.net>
  40.  * @author  Bjoern Kraus <krausbn@php.net>
  41.  * @copyright 2002-2006 Markus Wolff
  42.  * @license http://www.gnu.org/licenses/lgpl.txt
  43.  * @version CVS: $Id: XML.php,v 1.26 2006/03/14 13:10:04 lsmith Exp $
  44.  * @link http://pear.php.net/LiveUser
  45.  */
  46.  
  47. /**
  48.  * Require parent class definition.
  49.  */
  50. require_once 'LiveUser/Perm/Storage.php';
  51. require_once 'XML/Tree.php';
  52.  
  53. /**
  54.  * XML container for permission handling
  55.  *
  56.  * This is a XML backend driver for the LiveUser class.
  57.  *
  58.  * Requirements:
  59.  * - File "Liveuser.php" (contains the parent class "LiveUser")
  60.  * - XML_Parser
  61.  *
  62.  * @category authentication
  63.  * @package LiveUser
  64.  * @author  Lukas Smith <smith@pooteeweet.org>
  65.  * @author  Bjoern Kraus <krausbn@php.net>
  66.  * @copyright 2002-2006 Markus Wolff
  67.  * @license http://www.gnu.org/licenses/lgpl.txt
  68.  * @version Release: @package_version@
  69.  * @link http://pear.php.net/LiveUser
  70.  */
  71. {
  72.     /**
  73.      * XML file in which the auth data is stored.
  74.      *
  75.      * @var string 
  76.      * @access private
  77.      */
  78.     var $file '';
  79.  
  80.     /**
  81.      * XML::Tree object.
  82.      *
  83.      * @var    XML_Tree 
  84.      * @access private
  85.      */
  86.     var $tree = null;
  87.  
  88.     /**
  89.      * XML::Tree object of the user logged in.
  90.      *
  91.      * @var    XML_Tree 
  92.      *
  93.      * @access private
  94.      * @see    readUserData()
  95.      */
  96.     var $userObj = null;
  97.  
  98.     /**
  99.      * Initialize the storage container
  100.      *
  101.      * @param array Array with the storage configuration
  102.      * @return bool true on success, false on failure.
  103.      *
  104.      * @access public
  105.      */
  106.     function init(&$storageConf)
  107.     {
  108.         parent::init($storageConf);
  109.  
  110.         if (!is_file($this->file)) {
  111.             if (!is_file(getenv('DOCUMENT_ROOT'$this->file)) {
  112.                 $this->stack->push(LIVEUSER_ERROR_MISSING_DEPS'exception'array(),
  113.                     "Perm initialisation failed. Can't find xml file.");
  114.                 return false;
  115.             }
  116.             $this->file getenv('DOCUMENT_ROOT'$this->file;
  117.         }
  118.  
  119.         $tree =new XML_Tree($this->file);
  120.         $err =$tree->getTreeFromFile();
  121.         if (PEAR::isError($err)) {
  122.             $this->stack->push(LIVEUSER_ERROR'exception'array(),
  123.                 "Perm initialisation failed. Can't get tree from file");
  124.             return false;
  125.         }
  126.         $this->tree =$tree;
  127.  
  128.         if (!is_a($this->tree'xml_tree')) {
  129.             $this->stack->push(LIVEUSER_ERROR_INIT_ERROR'error',
  130.                 array('container' => 'storage layer configuration missing'));
  131.             return false;
  132.         }
  133.  
  134.         return true;
  135.     }
  136.  
  137.     /**
  138.      * map an auth user to a perm user
  139.      *
  140.      * @param int $auth_user_id 
  141.      * @param string $containerName 
  142.      * @return array requested data or false on failure
  143.      *
  144.      * @access public
  145.      */
  146.     function mapUser($auth_user_id$containerName)
  147.     {
  148.         $nodeIndex = 0;
  149.         $userIndex = 0;
  150.  
  151.         if (isset($this->tree->root->children&& is_array($this->tree->root->children)) {
  152.             foreach ($this->tree->root->children as $node{
  153.                 if ($node->name == 'users'{
  154.                     foreach ($node->children as $user{
  155.                         if ($user->name == 'user'
  156.                             && $auth_user_id == $user->attributes['authUserId']
  157.                             && $containerName == $user->attributes['authContainerName']
  158.                         {
  159.                             $result['perm_user_id'$user->attributes['userId'];
  160.                             $result['perm_type'$user->attributes['type'];
  161.                             $this->userObj =$this->tree->root->getElement(array($nodeIndex$userIndex));
  162.                             return $result;
  163.                         }
  164.                         $userIndex++;
  165.                     }
  166.                 }
  167.                 $nodeIndex++;
  168.             }
  169.         }
  170.  
  171.         return false;
  172.     }
  173.  
  174.     /**
  175.      * Reads all rights of current user into a
  176.      * two-dimensional associative array, having the
  177.      * area names as the key of the 1st dimension.
  178.      * Group rights and invididual rights are being merged
  179.      * in the process.
  180.      *
  181.      * @param int perm user id
  182.      * @return array requested data or false on failure
  183.      *
  184.      * @access public
  185.      */
  186.     function readUserRights($perm_user_id)
  187.     {
  188.         $result = array();
  189.  
  190.         foreach ($this->userObj->children as $node{
  191.             if ($node->name == 'rights'{
  192.                 $tmp explode(','$node->content);
  193.                 foreach ($tmp as $value{
  194.                     $level LIVEUSER_MAX_LEVEL;
  195.                     // level syntax: 10(2) => right id 10 at level 2
  196.                     $match = array();
  197.                     if (preg_match('/(\d+)\((\d+)\)/'$value$match)) {
  198.                         $value $match[1];
  199.                         $level $match[2];
  200.                     }
  201.                     $result[$value$level;
  202.                 }
  203.             }
  204.         }
  205.  
  206.         return $result;
  207.     }
  208.  
  209.     /**
  210.      * properly disconnect from resources
  211.      *
  212.      * @return bool true on success and false on failure
  213.      *
  214.      * @access public
  215.      */
  216.     function disconnect()
  217.     {
  218.         $this->tree = null;
  219.         $this->userObj = null;
  220.         return true;
  221.     }
  222. }
  223. ?>

Documentation generated on Mon, 28 Jan 2008 03:30:34 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.