cvs: pear /Perm_LiveUser/Auth/Container XML.php

From: Date: Sat, 15 Nov 2003 15:37:07 +0000
Subject: cvs: pear /Perm_LiveUser/Auth/Container XML.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-14285@lists.php.net to get a copy of this message
krausbn Sat Nov 15 10:37:07 2003 EDT Modified files: /pear/Perm_LiveUser/Auth/Container XML.php Log: Made username case insensitive

Index: pear/Perm_LiveUser/Auth/Container/XML.php diff -u pear/Perm_LiveUser/Auth/Container/XML.php:1.9 pear/Perm_LiveUser/Auth/Container/XML.php:1.10 --- pear/Perm_LiveUser/Auth/Container/XML.php:1.9 Fri Sep 26 09:29:45 2003 +++ pear/Perm_LiveUser/Auth/Container/XML.php Sat Nov 15 10:37:07 2003 @@ -1,285 +1,285 @@ -<?php -// LiveUser: A framework for authentication and authorization in PHP applications -// Copyright (C) 2002-2003 Markus Wolff -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 2.1 of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -/** - * XML driver for authentication - * - * @package LiveUser - * @category authentication - */ - -/** - * Require parent class definition and XML::Tree class. - */ -require_once 'LiveUser/Auth/Common.php'; -require_once 'XML/Tree.php'; - -/** - * Class LiveUser_Auth_Container_XML - * - * Description: - * This is a XML backend driver for the LiveUser class. - * - * @author Björn Kraus <krausbn@php.net> - * @version $Id: XML.php,v 1.9 2003/09/26 13:29:45 lsmith Exp $ - * @package LiveUser - * @category authentication - */ -class LiveUser_Auth_Container_XML extends LiveUser_Auth_Common -{ - /** - * XML file in which the auth data is stored. - * @var string - * @access private - */ - var $file = ''; - - /** - * XML::Tree object. - * @var object - * @access private - */ - var $tree = null; - - /** - * XML::Tree object of the user logged in. - * @var object - * @access private - * @see readUserData() - */ - var $userObj = null; - - /** - * Indicates whether the auth data in the XML file has changed and - * needs to be stored - * @var boolean - * @access public - * @see freeze() - */ - var $dataChanged = false; - - /** - * Indicates if backend module initialized correctly. If yes, - * true, if not false. Backend module won't initialize if the - * init value (usually an object or resource handle that - * identifies the backend to be used) is not of the required - * type. - * @var boolean - * @access public - */ - var $init_ok = false; - - /** - * LiveUser_Auth_Container_XML::LiveUser_Auth_Container_XML() - * - * Class constructor. - * - * @param mixed - */ - function &LiveUser_Auth_Container_XML(&$connectOptions) - { - if (is_array($connectOptions)) { - foreach ($connectOptions as $key => $value) { - if (isset($this->$key)) { - $this->$key = $value; - } - } - if (!is_file($this->file)) { - if (is_file(getenv('DOCUMENT_ROOT') . $this->file)) { - $this->file = getenv('DOCUMENT_ROOT') . $this->file; - } else { - return PEAR::raiseError(null, LIVEUSER_MISSING_DEPS, - "Auth initialisation failed. Can't find xml file.", 'LiveUser', true); - } - } - if ($this->file) { - if (class_exists('XML_Tree')) { - $tree =& new XML_Tree($this->file); - $err =& $tree->getTreeFromFile(); - if (PEAR::isError($err)) { - return $err; - } else { - $this->tree = $tree; - $this->init_ok = true; - } - } else { - $this->_error = true; - return PEAR::raiseError(null, LIVEUSER_MISSING_DEPS, - "Auth initialisation failed. Can't find XML_Tree class.", 'LiveUser', true); - } - } else { - return PEAR::raiseError(null, LIVEUSER_MISSING_DEPS, - "Auth initialisation failed. Can't find xml file.", 'LiveUser', true); - } - } - } - - /** - * LiveUser_Auth_Container_XML::unfreeze() - * - * Reinitializes database connection after the object had been - * retrieved from the session - * - * @param mixed $connectOptions - * @access public - */ - function unfreeze(&$connectOptions) - { - $this->LiveUser_Auth_Container_XML($connectOptions); - } - - /** - * LiveUser_Auth_Container_XML::freeze() - * - * Prepares the object for being stored in a session. - * If auth data has changed, the function stores the xml file. - * - * @access public - */ - function freeze() - { - if ($this->dataChanged) { - $success = false; - do { - if (!($fp = fopen($this->file, 'wb'))) { - $errorMsg = "Auth freeze failure. Failed to open the xml file."; - break; - } - if (!flock($fp, LOCK_EX)) { - $errorMsg = "Auth freeze failure. Couldn't get an exclusive lock on the file."; - break; - } - if (!fwrite($fp, $this->tree->get())) { - $errorMsg = "Auth freeze failure. Write error when writing back the file."; - break; - } - @fflush($fp); - $success = true; - } while (false); - - @flock($fp, LOCK_UN); - @fclose($fp); - - if (!$success) { - return PEAR::raiseError(null, LIVEUSER_ERROR, null, E_USER_WARNING, $errorMsg, 'LiveUser', true); - } - } - $this->tree = null; - $this->userObj = null; - } - - /** - * LiveUser_Auth_Container_XML::updateUserData() - * - * Writes current values for user back to the database. - * This method does nothing in the base class and is supposed to - * be overridden in subclasses according to the supported backend. - * - * @access private - */ - function updateUserData() - { - if (!$this->init_ok || !$this->userObj) { - return false; - } - - $data = array('lastLogin' => $this->currentLogin); - - $index = 0; - foreach ($this->userObj->children as $value) { - if (in_array($value->name, array_keys($data))) { - $el =& $this->userObj->getElement(array($index)); - $el->setContent($data[$value->name]); - } - $index++; - } - - $this->dataChanged = true; - } - - /** - * LiveUser_Auth_Container_XML::readUserData() - * - * Reads auth_user_id, password from the xml file - * If only $handle is given, it will read the data - * from the first user with that handle and return - * true on success. - * If $handle and $passwd are given, it will try to - * find the first user with both handle and passwd - * matching and return true on success (this allows - * multiple users having the same handle but different - * passwords - yep, some people want this). - * If no match is found, false is being returned. - * - * @param string $userHandle Handle of the current user. - * @param mixed $userPasswd Can be a string with an - * unencrypted pwd or false. - * @return boolean - */ - function readUserData($userHandle, $userPasswd = false) - { - if (!$this->init_ok) { - return false; - } - $success = false; - $index = 0; - - foreach ($this->tree->root->children as $user) { - $userId = ''; - $handle = ''; - $password = ''; - $lastLogin = 0; - $isActive = ''; - - foreach ($user->children as $value) { - if (isset(${$value->name})) { - ${$value->name} = $value->content; - } - } - - if ($userHandle == $handle) { - if ($userPasswd !== false) { - if ($this->encryptPW($userPasswd) == $password) { - $success = true; - break; - } - } else { - $success = true; - break; - } - } - - $index++; - } - - - // If a user was found, read data into class variables and save - // the tree object for faster access in the other functions. - if ($success) { - $this->handle = $handle; - $this->passwd = $this->decryptPW($password); - $this->isActive = ($isActive == 'Y' ? true : false); - $this->authUserId = $userId; - $this->lastLogin = (!empty($lastLogin) ? $lastLogin : 0); - $this->userObj =& $this->tree->root->getElement(array($index)); - } - - return $success; - } -} +<?php +// LiveUser: A framework for authentication and authorization in PHP applications +// Copyright (C) 2002-2003 Markus Wolff +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +/** + * XML driver for authentication + * + * @package LiveUser + * @category authentication + */ + +/** + * Require parent class definition and XML::Tree class. + */ +require_once 'LiveUser/Auth/Common.php'; +require_once 'XML/Tree.php'; + +/** + * Class LiveUser_Auth_Container_XML + * + * Description: + * This is a XML backend driver for the LiveUser class. + * + * @author Björn Kraus <krausbn@php.net> + * @version $Id: XML.php,v 1.10 2003/11/15 15:37:07 krausbn Exp $ + * @package LiveUser + * @category authentication + */ +class LiveUser_Auth_Container_XML extends LiveUser_Auth_Common +{ + /** + * XML file in which the auth data is stored. + * @var string + * @access private + */ + var $file = ''; + + /** + * XML::Tree object. + * @var object + * @access private + */ + var $tree = null; + + /** + * XML::Tree object of the user logged in. + * @var object + * @access private + * @see readUserData() + */ + var $userObj = null; + + /** + * Indicates whether the auth data in the XML file has changed and + * needs to be stored + * @var boolean + * @access public + * @see freeze() + */ + var $dataChanged = false; + + /** + * Indicates if backend module initialized correctly. If yes, + * true, if not false. Backend module won't initialize if the + * init value (usually an object or resource handle that + * identifies the backend to be used) is not of the required + * type. + * @var boolean + * @access public + */ + var $init_ok = false; + + /** + * LiveUser_Auth_Container_XML::LiveUser_Auth_Container_XML() + * + * Class constructor. + * + * @param mixed + */ + function &LiveUser_Auth_Container_XML(&$connectOptions) + { + if (is_array($connectOptions)) { + foreach ($connectOptions as $key => $value) { + if (isset($this->$key)) { + $this->$key = $value; + } + } + if (!is_file($this->file)) { + if (is_file(getenv('DOCUMENT_ROOT') . $this->file)) { + $this->file = getenv('DOCUMENT_ROOT') . $this->file; + } else { + return PEAR::raiseError(null, LIVEUSER_MISSING_DEPS, + "Auth initialisation failed. Can't find xml file.", 'LiveUser', true); + } + } + if ($this->file) { + if (class_exists('XML_Tree')) { + $tree =& new XML_Tree($this->file); + $err =& $tree->getTreeFromFile(); + if (PEAR::isError($err)) { + return $err; + } else { + $this->tree = $tree; + $this->init_ok = true; + } + } else { + $this->_error = true; + return PEAR::raiseError(null, LIVEUSER_MISSING_DEPS, + "Auth initialisation failed. Can't find XML_Tree class.", 'LiveUser', true); + } + } else { + return PEAR::raiseError(null, LIVEUSER_MISSING_DEPS, + "Auth initialisation failed. Can't find xml file.", 'LiveUser', true); + } + } + } + + /** + * LiveUser_Auth_Container_XML::unfreeze() + * + * Reinitializes database connection after the object had been + * retrieved from the session + * + * @param mixed $connectOptions + * @access public + */ + function unfreeze(&$connectOptions) + { + $this->LiveUser_Auth_Container_XML($connectOptions); + } + + /** + * LiveUser_Auth_Container_XML::freeze() + * + * Prepares the object for being stored in a session. + * If auth data has changed, the function stores the xml file. + * + * @access public + */ + function freeze() + { + if ($this->dataChanged) { + $success = false; + do { + if (!($fp = fopen($this->file, 'wb'))) { + $errorMsg = "Auth freeze failure. Failed to open the xml file."; + break; + } + if (!flock($fp, LOCK_EX)) { + $errorMsg = "Auth freeze failure. Couldn't get an exclusive lock on the file."; + break; + } + if (!fwrite($fp, $this->tree->get())) { + $errorMsg = "Auth freeze failure. Write error when writing back the file."; + break; + } + @fflush($fp); + $success = true; + } while (false); + + @flock($fp, LOCK_UN); + @fclose($fp); + + if (!$success) { + return PEAR::raiseError(null, LIVEUSER_ERROR, null, E_USER_WARNING, $errorMsg, 'LiveUser', true); + } + } + $this->tree = null; + $this->userObj = null; + } + + /** + * LiveUser_Auth_Container_XML::updateUserData() + * + * Writes current values for user back to the database. + * This method does nothing in the base class and is supposed to + * be overridden in subclasses according to the supported backend. + * + * @access private + */ + function updateUserData() + { + if (!$this->init_ok || !$this->userObj) { + return false; + } + + $data = array('lastLogin' => $this->currentLogin); + + $index = 0; + foreach ($this->userObj->children as $value) { + if (in_array($value->name, array_keys($data))) { + $el =& $this->userObj->getElement(array($index)); + $el->setContent($data[$value->name]); + } + $index++; + } + + $this->dataChanged = true; + } + + /** + * LiveUser_Auth_Container_XML::readUserData() + * + * Reads auth_user_id, password from the xml file + * If only $handle is given, it will read the data + * from the first user with that handle and return + * true on success. + * If $handle and $passwd are given, it will try to + * find the first user with both handle and passwd + * matching and return true on success (this allows + * multiple users having the same handle but different + * passwords - yep, some people want this). + * If no match is found, false is being returned. + * + * @param string $userHandle Handle of the current user. + * @param mixed $userPasswd Can be a string with an + * unencrypted pwd or false. + * @return boolean + */ + function readUserData($userHandle, $userPasswd = false) + { + if (!$this->init_ok) { + return false; + } + $success = false; + $index = 0; + + foreach ($this->tree->root->children as $user) { + $userId = ''; + $handle = ''; + $password = ''; + $lastLogin = 0; + $isActive = ''; + + foreach ($user->children as $value) { + if (isset(${$value->name})) { + ${$value->name} = $value->content; + } + } + + if (strtoupper($userHandle) == strtoupper($handle)) { + if ($userPasswd !== false) { + if ($this->encryptPW($userPasswd) == $password) { + $success = true; + break; + } + } else { + $success = true; + break; + } + } + + $index++; + } + + + // If a user was found, read data into class variables and save + // the tree object for faster access in the other functions. + if ($success) { + $this->handle = $handle; + $this->passwd = $this->decryptPW($password); + $this->isActive = ($isActive == 'Y' ? true : false); + $this->authUserId = $userId; + $this->lastLogin = (!empty($lastLogin) ? $lastLogin : 0); + $this->userObj =& $this->tree->root->getElement(array($index)); + } + + return $success; + } +} ?>
« previous php.pear.cvs (#14285) next »