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.44 2006/08/22 17:11:43 lsmith Exp $
  44.  * @link http://pear.php.net/LiveUser
  45.  */
  46.  
  47. /**
  48.  * Require parent class definition and XML::Tree class.
  49.  */
  50. require_once 'LiveUser/Auth/Common.php';
  51. require_once 'XML/Tree.php';
  52.  
  53. /**
  54.  * XML driver for authentication
  55.  *
  56.  * This is a XML backend driver for the LiveUser class.
  57.  *
  58.  * @category authentication
  59.  * @package LiveUser
  60.  * @author  Björn Kraus <krausbn@php.net>
  61.  * @copyright 2002-2006 Markus Wolff
  62.  * @license http://www.gnu.org/licenses/lgpl.txt
  63.  * @version Release: @package_version@
  64.  * @link http://pear.php.net/LiveUser
  65.  */
  66. {
  67.     /**
  68.      * XML file in which the auth data is stored.
  69.      *
  70.      * @var    string 
  71.      * @access private
  72.      */
  73.     var $file '';
  74.  
  75.     /**
  76.      * XML::Tree object.
  77.      *
  78.      * @var    XML_Tree 
  79.      * @access private
  80.      */
  81.     var $tree = false;
  82.  
  83.     /**
  84.      * XML::Tree object of the user logged in.
  85.      *
  86.      * @var    XML_Tree 
  87.      * @access private
  88.      * @see    readUserData()
  89.      */
  90.     var $userObj = null;
  91.  
  92.     /**
  93.      * Load the storage container
  94.      *
  95.      * @param   array  array containing the configuration.
  96.      * @param string  name of the container that should be used
  97.      * @return bool true on success or false on failure
  98.      *
  99.      * @access public
  100.      */
  101.     function init(&$conf$containerName)
  102.     {
  103.         parent::init($conf$containerName);
  104.  
  105.         if (!is_file($this->file)) {
  106.             if (!is_file(getenv('DOCUMENT_ROOT'$this->file)) {
  107.                 $this->stack->push(LIVEUSER_ERROR_MISSING_DEPS'exception'array(),
  108.                     "Perm initialisation failed. Can't find xml file.");
  109.                 return false;
  110.             }
  111.             $this->file getenv('DOCUMENT_ROOT'$this->file;
  112.         }
  113.  
  114.         $tree =new XML_Tree($this->file);
  115.         $err =$tree->getTreeFromFile();
  116.         if (PEAR::isError($err)) {
  117.             $this->stack->push(LIVEUSER_ERROR'exception'array(),
  118.                 "Perm initialisation failed. Can't get tree from file");
  119.             return false;
  120.         }
  121.         $this->tree =$tree;
  122.  
  123.         if (!is_a($this->tree'xml_tree')) {
  124.             $this->stack->push(LIVEUSER_ERROR_INIT_ERROR'error',
  125.                 array('container' => 'storage layer configuration missing'));
  126.             return false;
  127.         }
  128.  
  129.         return true;
  130.     }
  131.  
  132.     /**
  133.      * Writes current values for user back to the database.
  134.      *
  135.      * @return bool true on success or false on failure
  136.      *
  137.      * @access private
  138.      */
  139.     function _updateUserData()
  140.     {
  141.         if (!array_key_exists('lastlogin'$this->tables['users']['fields'])) {
  142.             return true;
  143.         }
  144.  
  145.         $index = 0;
  146.         foreach ($this->userObj->children as $value{
  147.             if ($value->name == $this->alias['lastlogin']{
  148.                 $el =$this->userObj->getElement(array($index));
  149.                 $el->setContent($this->currentLogin);
  150.             }
  151.             $index++;
  152.         }
  153.  
  154.         $success = false;
  155.         do {
  156.           if (!is_writable($this->file)) {
  157.               $errorMsg 'Auth freeze failure. Cannot write to the xml file';
  158.               break;
  159.           }
  160.           $fp fopen($this->file'wb');
  161.           if (!$fp{
  162.               $errorMsg "Auth freeze failure. Failed to open the xml file.";
  163.               break;
  164.           }
  165.           if (!flock($fpLOCK_EX)) {
  166.               $errorMsg "Auth freeze failure. Couldn't get an exclusive lock on the file.";
  167.               break;
  168.           }
  169.           if (!fwrite($fp$this->tree->get())) {
  170.               $errorMsg "Auth freeze failure. Write error when writing back the file.";
  171.               break;
  172.           }
  173.           @fflush($fp);
  174.           $success = true;
  175.         while (false);
  176.  
  177.         @flock($fpLOCK_UN);
  178.         @fclose($fp);
  179.  
  180.         if (!$success{
  181.             $this->stack->push(LIVEUSER_ERROR'exception',
  182.                 array()'Cannot read XML Auth file: '.$errorMsg);
  183.         }
  184.  
  185.         return $success;
  186.     }
  187.  
  188.     /**
  189.      * Reads user data from the given data source
  190.      * If only $handle is given, it will read the data
  191.      * from the first user with that handle and return
  192.      * true on success.
  193.      * If $handle and $passwd are given, it will try to
  194.      * find the first user with both handle and password
  195.      * matching and return true on success (this allows
  196.      * multiple users having the same handle but different
  197.      * passwords - yep, some people want this).
  198.      * if only an auth_user_id is passed it will try to read the data based on the id
  199.      * If no match is found, false is being returned.
  200.      *
  201.      * @param  string user handle
  202.      * @param  string user password
  203.      * @param  bool|intif the user data should be read using the auth user id
  204.      * @return bool true on success or false on failure
  205.      *
  206.      * @access public
  207.      */
  208.     function readUserData($handle ''$passwd ''$auth_user_id = false)
  209.     {
  210.         $success = false;
  211.         $index = 0;
  212.  
  213.         foreach ($this->tree->root->children as $user{
  214.             $result = array();
  215.             $names array_flip($this->alias);
  216.             foreach ($user->children as $value{
  217.                 if (array_key_exists($value->name$names)) {
  218.                     $result[$names[$value->name]] $value->content;
  219.                 }
  220.             }
  221.  
  222.             if ($auth_user_id{
  223.                 if (array_key_exists('auth_user_id'$result)
  224.                     && $auth_user_id === $result['auth_user_id']
  225.                 {
  226.                     $success = true;
  227.                     break;
  228.                 }
  229.             elseif (array_key_exists('handle'$result&& $handle === $result['handle']{
  230.                 if (!is_null($this->tables['users']['fields']['passwd'])) {
  231.                     if (array_key_exists('passwd'$result)
  232.                         && $this->encryptPW($passwd=== $result['passwd']
  233.                     {
  234.                         $success = true;
  235.                         break;
  236.                     elseif (is_string($this->tables['users']['fields']['handle'])) {
  237.                         // dont look for any further matching handles
  238.                         break;
  239.                     }
  240.                 else {
  241.                     $success = true;
  242.                     break;
  243.                 }
  244.             }
  245.  
  246.             $index++;
  247.         }
  248.  
  249.         if (!$success{
  250.             return null;
  251.         }
  252.  
  253.         $this->propertyValues = $result;
  254.  
  255.         $this->userObj      =$this->tree->root->getElement(array($index));
  256.  
  257.         return true;
  258.     }
  259.  
  260.     /**
  261.      * Properly disconnect from resources
  262.      *
  263.      * @return bool true on success or false on failure
  264.      *
  265.      * @access public
  266.      */
  267.     function disconnect()
  268.     {
  269.         $this->tree = false;
  270.         $this->userObj = null;
  271.         return true;
  272.     }
  273. }
  274. ?>

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