cvs: pear /Perm_LiveUser/Auth DB.php MDB.php MDB2.php PEARAuth.php XML.php
| From: | Lukas Smith | Date: | Mon, 08 Nov 2004 15:51:16 +0000 |
| Subject: | cvs: pear /Perm_LiveUser/Auth DB.php MDB.php MDB2.php PEARAuth.php XML.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-25777@lists.php.net to get a copy of this message | ||
lsmith Mon Nov 8 10:51:16 2004 EDT
Added files:
/pear/Perm_LiveUser/Auth DB.php MDB.php MDB2.php PEARAuth.php
XML.php
Log:
removed container dir
http://cvs.php.net/co.php/pear/Perm_LiveUser/Auth/DB.php?r=1.1&p=1 Index: pear/Perm_LiveUser/Auth/DB.php +++ pear/Perm_LiveUser/Auth/DB.php <?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 /** * DB container for Authentication * * @package LiveUser * @category authentication */ /** * Require parent class definition and PEAR::DB class. */ require_once 'LiveUser/Auth/Common.php'; require_once 'DB.php'; /** * This is a PEAR::DB backend driver for the LiveUser class. * A PEAR::DB connection object can be passed to the constructor to reuse an * existing connection. Alternatively, a DSN can be passed to open a new one. * * Requirements: * - File "LoginManager.php" (contains the parent class "LiveUser") * - Array of connection options or a PEAR::DB connection object must be * passed to the constructor. * Example: array('dsn' => 'mysql://user:pass@host/db_name', * 'connection => &$conn, # PEAR::DB connection object * 'loginTimeout' => 0, * 'allowDuplicateHandles' => 1); * * @author Markus Wolff <wolff@21st.de> * @version $Id: DB.php,v 1.1 2004/11/08 15:51:14 lsmith Exp $ * @package LiveUser * @category authentication */ class LiveUser_Auth_DB extends LiveUser_Auth_Common { /** * dsn to connect to * * @access private * @var string */ var $dsn = null; /** * disconnect * * @access private * @var boolean */ var $disconnect = false; /** * PEAR::DB connection object * * @access private * @var DB */ var $dbc = null; /** * Auth table * Table where the auth data is stored. * * @access public * @var string */ var $authTable = 'liveuser_users'; /** * Columns of the auth table. * Associative array with the names of the auth table columns. * The 'auth_user_id', 'handle' and 'passwd' fields have to be set. * 'lastlogin' and 'is_active' are optional. * It doesn't make sense to set only one of the time columns without the * other. * * The type attribute is only useful when using MDB or MDB2. * * @access public * @var array */ var $authTableCols = array( 'required' => array( 'auth_user_id' => array('name' => 'auth_user_id', 'type' => ''), 'handle' => array('name' => 'handle', 'type' => ''), 'passwd' => array('name' => 'passwd', 'type' => ''), ), 'optional' => array( 'lastlogin' => array('name' => 'lastlogin', 'type' => ''), 'is_active' => array('name' => 'is_active', 'type' => '') ) ); /** * Class constructor. * * @access protected * @param array configuration array * @return void */ function LiveUser_Auth_DB(&$connectOptions) { $this->LiveUser_Auth_Common($connectOptions); if (is_array($connectOptions)) { $this->LiveUser_Auth_Common($connectOptions); if (isset($connectOptions['connection']) && DB::isConnection($connectOptions['connection']) ) { $this->dbc = &$connectOptions['connection']; $this->init_ok = true; } elseif (isset($connectOptions['dsn'])) { $this->dsn = $connectOptions['dsn']; $options = null; if (isset($connectOptions['options'])) { $options = $connectOptions['options']; } $options['portability'] = DB_PORTABILITY_ALL; $this->dbc =& DB::connect($connectOptions['dsn'], $options); if (!DB::isError($this->dbc)) { $this->init_ok = true; } } } } /** * Properly disconnect from database * * @access public * @return void */ function disconnect() { if ($this->disconnect) { $this->dbc->disconnect(); $this->dbc = null; $this->init_ok = null; } } /** * 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 * @return boolean true on success or false on failure */ function _updateUserData() { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); return false; } if (isset($this->authTableCols['optional']['lastlogin'])) { $sql = 'UPDATE ' . $this->authTable.' SET ' . $this->authTableCols['optional']['lastlogin']['name'] . '=' . $this->dbc->quoteSmart(date('Y-m-d H:i:s', $this->currentLogin)) . ' WHERE ' . $this->authTableCols['required']['auth_user_id']['name'] . '=' . $this->dbc->quoteSmart($this->authUserId); $res = $this->dbc->query($sql); if (DB::isError($res)) { return false; } return true; } else { return false; } } /** * Reads auth_user_id, passwd, is_active flag * lastlogin timestamp from the database * 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 password * 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. * * @access private * @param string user handle * @param boolean user password * @return boolean true upon success or false on failure */ function _readUserData($handle, $passwd = false) { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); return false; } $success = false; $fields = array(); foreach ($this->authTableCols as $key => $value) { if (sizeof($value) > 0) { foreach ($value as $alias => $field_data) { $fields[] = $field_data['name'] . ' AS ' . $alias; } } } // Setting the default sql query. $sql = 'SELECT ' . implode(',', $fields) . ' FROM ' . $this->authTable.' WHERE ' . $this->authTableCols['required']['handle']['name'] . '=' . $this->dbc->quoteSmart($handle); if ($passwd !== false) { // If $passwd is set, try to find the first user with the given // handle and password. $sql .= 'AND ' . $this->authTableCols['required']['passwd']['name'] . '=' . $this->dbc->quoteSmart($this->encryptPW($passwd)); } // Query database $result = $this->dbc->getRow($sql, null, DB_FETCHMODE_ASSOC); // If a user was found, read data into class variables and set // return value to true if (!DB::isError($result) && is_array($result)) { $this->handle = $result['handle']; $this->passwd = $this->decryptPW($result['passwd']); $this->isActive = ((!isset($result['is_active']) || $result['is_active'] == 'Y') ? true : false); $this->authUserId = $result['auth_user_id']; $this->lastLogin = !empty($result['lastlogin']) ? strtotime($result['lastlogin']) : ''; $this->ownerUserId = isset($result['owner_user_id']) ? $result['owner_user_id'] : null; $this->ownerGroupid = isset($result['owner_group_id']) ? $result['owner_group_id'] : null; if (isset($this->authTableCols['custom'])) { foreach ($this->authTableCols['custom'] as $alias => $value) { $alias = strtolower($alias); $this->propertyValues['custom'][$alias] = $result[$alias]; } } $success = true; } return $success; } /** * Helper function that checks if there is a user in * the database who's matching the given parameters. * If $checkHandle is given and $checkPW is set to * false, it only checks if a user with that handle * exists. If only $checkPW is given and $checkHandle * is set to false, it will check if there exists a * user with that password. If both values are set to * anything but false, it will find the first user in * the database with both values matching. * Please note: * - If no match was found, the return value is false * - If a match was found, the auth_user_id from the database * is being returned * Whatever is returned, please keep in mind that this * function only searches for the _first_ occurence * of the search values in the database. So when you * have multiple users with the same handle, only the * ID of the first one is returned. Same goes for * passwords. Searching for both password and handle * should be pretty safe, though - having more than * one user with the same handle/password combination * in the database would be pretty stupid anyway. * * @param boolean The handle (username) to search * @param boolean The password to check against * @return mixed true or false if the user does not exist */ function userExists($checkHandle = false,$checkPW = false) { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); return false; } $sql = 'SELECT ' . $this->authTableCols['required']['auth_user_id'] . ' FROM ' . $this->authTable; if ($checkHandle !== false && $checkPW === false) { // only search for the first user with the given handle $sql .= 'WHERE ' . $this->authTableCols['required']['handle'] . '=' . $this->dbc->quoteSmart($checkHandle); } elseif ($checkHandle === false && $checkPW !== false) { // only search for the first user with the given password $sql .= 'WHERE ' . $this->authTableCols['required']['passwd'] . '=' . $this->dbc->quoteSmart($this->encryptPW($checkPW)); } else { // check for a user with both handle and password matching $sql .= 'WHERE ' . $this->authTableCols['required']['handle'] . '=' . $this->dbc->quoteSmart($checkHandle) . ' AND ' . $this->authTableCols['required']['passwd'] . '=' . $this->dbc->quoteSmart($this->encryptPW($checkPW)); } $result = $this->dbc->getOne($sql); if (DB::isError($result)) { return false; } if (is_null($result)) { return false; } return true; } } ?> http://cvs.php.net/co.php/pear/Perm_LiveUser/Auth/MDB.php?r=1.1&p=1 Index: pear/Perm_LiveUser/Auth/MDB.php +++ pear/Perm_LiveUser/Auth/MDB.php <?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 /** * MDB container for Authentication * * @package LiveUser * @category authentication */ /** * Require parent class definition and PEAR::MDB class. */ require_once 'LiveUser/Auth/Common.php'; require_once 'MDB.php'; MDB::loadFile('Date'); /** * This is a PEAR::MDB backend driver for the LiveUser class. * A PEAR::MDB connection object can be passed to the constructor to reuse an * existing connection. Alternatively, a DSN can be passed to open a new one. * * Requirements: * - File "LoginManager.php" (contains the parent class "LiveUser") * - Array of connection options or a PEAR::MDB connection object must be * passed to the constructor. * Example: array('dsn' => 'mysql://user:pass@host/db_name', * 'connection => &$conn, # PEAR::MDB connection object * 'loginTimeout' => 0, * 'allowDuplicateHandles' => 1); * * @author Markus Wolff <wolff@21st.de> * @version $Id: MDB.php,v 1.1 2004/11/08 15:51:14 lsmith Exp $ * @package LiveUser * @category authentication */ class LiveUser_Auth_MDB extends LiveUser_Auth_Common { /** * dsn to connect to * * @access private * @var string */ var $dsn = null; /** * disconnect * * @access private * @var boolean */ var $disconnect = false; /** * PEAR::MDB connection object * * @access private * @var MDB */ var $dbc = null; /** * Auth table * Table where the auth data is stored. * * @access public * @var string */ var $authTable = 'liveuser_users'; /** * Columns of the auth table. * Associative array with the names of the auth table columns. * The 'auth_user_id', 'handle' and 'passwd' fields have to be set. * 'lastlogin' and 'is_active' are optional. * It doesn't make sense to set only one of the time columns without the * other. * * The type attribute is only useful when using MDB or MDB2. * * @access public * @var array */ var $authTableCols = array( 'required' => array( 'auth_user_id' => array('name' => 'auth_user_id', 'type' => ''), 'handle' => array('name' => 'handle', 'type' => ''), 'passwd' => array('name' => 'passwd', 'type' => ''), ), 'optional' => array( 'lastlogin' => array('name' => 'lastlogin', 'type' => ''), 'is_active' => array('name' => 'is_active', 'type' => '') ) ); /** * Class constructor. * * @access protected * @param array configuration array * @return void */ function LiveUser_Auth_MDB(&$connectOptions) { $this->LiveUser_Auth_Common($connectOptions); if (is_array($connectOptions)) { $this->LiveUser_Auth_Common($connectOptions); if (isset($connectOptions['connection']) && MDB::isConnection($connectOptions['connection']) ) { $this->dbc = &$connectOptions['connection']; $this->init_ok = true; } elseif (isset($connectOptions['dsn'])) { $this->dsn = $connectOptions['dsn']; $function = null; if (isset($connectOptions['function'])) { $function = $connectOptions['function']; } $options = null; if (isset($connectOptions['options'])) { $options = $connectOptions['options']; } $options['optimize'] = 'portability'; if ($function == 'singleton') { $this->dbc =& MDB::singleton($connectOptions['dsn'], $options); } else { $this->dbc =& MDB::connect($connectOptions['dsn'], $options); } if (!MDB::isError($this->dbc)) { $this->init_ok = true; } } } } /** * Properly disconnect from database * * @access public * @return void */ function disconnect() { if ($this->disconnect) { $this->dbc->disconnect(); $this->dbc = null; $this->init_ok = null; } } /** * 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 * @return boolean true on success or false on failure */ function _updateUserData() { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); return false; } if (isset($this->authTableCols['optional']['lastlogin'])) { $sql = 'UPDATE ' . $this->authTable.' SET ' . $this->authTableCols['optional']['lastlogin']['name'] .'=' . $this->dbc->getValue($this->authTableCols['optional']['lastlogin']['type'], MDB_Date::unix2Mdbstamp($this->currentLogin)) . ' WHERE ' . $this->authTableCols['required']['auth_user_id']['name'] .'=' . $this->dbc->getValue($this->authTableCols['required']['auth_user_id']['type'], $this->authUserId); $res = $this->dbc->query($sql); if (MDB::isError($res)) { return false; } return true; } else { return false; } } /** * Reads auth_user_id, passwd, is_active flag * lastlogin timestamp from the database * 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 password * 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. * * @access private * @param string user handle * @param boolean user password * @return boolean true upon success or false on failure */ function _readUserData($handle, $passwd = false) { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); return false; } $success = false; $fields = array(); foreach ($this->authTableCols as $key => $value) { if (sizeof($value) > 0) { foreach ($value as $alias => $field_data) { $fields[] = $field_data['name'] . ' AS ' . $alias; $types[] = $field_data['type']; } } } // Setting the default query. $sql = 'SELECT ' . implode(',', $fields) . ' FROM ' . $this->authTable . ' WHERE ' . $this->authTableCols['required']['handle']['name'] . '=' . $this->dbc->getValue($this->authTableCols['required']['handle']['type'], $handle); if ($passwd !== false) { // If $passwd is set, try to find the first user with the given // handle and password. $sql .= ' AND ' . $this->authTableCols['required']['passwd']['name'] . '=' . $this->dbc->getValue($this->authTableCols['required']['passwd']['type'], $this->encryptPW($passwd)); } // Query database $result = $this->dbc->queryRow($sql, $types, MDB_FETCHMODE_ASSOC); // If a user was found, read data into class variables and set // return value to true if (!MDB::isError($result) && is_array($result)) { $this->handle = $result['handle']; $this->passwd = $this->decryptPW($result['passwd']); $this->isActive = ((!isset($result['is_active']) || $result['is_active']) ? true : false); $this->authUserId = $result['auth_user_id']; $this->lastLogin = isset($result['lastlogin'])? MDB_Date::mdbstamp2Unix($result['lastlogin']):''; $this->ownerUserId = isset($result['owner_user_id']) ? $result['owner_user_id'] : null; $this->ownerGroupid = isset($result['owner_group_id']) ? $result['owner_group_id'] : null; if (isset($this->authTableCols['custom'])) { foreach ($this->authTableCols['custom'] as $alias => $value) { $alias = strtolower($alias); $this->propertyValues['custom'][$alias] = $result[$alias]; } } $success = true; } return $success; } /** * Helper function that checks if there is a user in * the database who's matching the given parameters. * If $checkHandle is given and $checkPW is set to * false, it only checks if a user with that handle * exists. If only $checkPW is given and $checkHandle * is set to false, it will check if there exists a * user with that password. If both values are set to * anything but false, it will find the first user in * the database with both values matching. * Please note: * - If no match was found, the return value is false * - If a match was found, the auth_user_id from the database * is being returned * Whatever is returned, please keep in mind that this * function only searches for the _first_ occurence * of the search values in the database. So when you * have multiple users with the same handle, only the * ID of the first one is returned. Same goes for * passwords. Searching for both password and handle * should be pretty safe, though - having more than * one user with the same handle/password combination * in the database would be pretty stupid anyway. * * @param boolean The handle (username) to search * @param boolean The password to check against * @return mixed true or false if the user does not exist */ function userExists($checkHandle = false, $checkPW = false) { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); return false; } $sql = 'SELECT ' . $this->authTableCols['required']['auth_user_id']['name'] . ' FROM ' . $this->authTable; if ($checkHandle !== false && $checkPW === false) { // only search for the first user with the given handle $sql .= 'WHERE ' . $this->authTableCols['required']['handle']['name'] . '=' . $this->dbc->getValue($this->authTableCols['required']['handle']['type'], $checkHandle); } elseif ($checkHandle === false && $checkPW !== false) { // only search for the first user with the given password $sql .= 'WHERE ' . $this->authTableCols['required']['passwd']['name'] . '=' . $this->dbc->getValue($this->authTableCols['required']['passwd']['type'], $this->encryptPW($checkPW)); } else { // check for a user with both handle and password matching $sql .= 'WHERE ' . $this->authTableCols['required']['handle']['name'] . '=' . $this->dbc->getValue($this->authTableCols['required']['handle']['type'], $checkHandle) . ' AND ' . $this->authTableCols['required']['passwd']['name'] . '=' . $this->dbc->getValue($this->authTableCols['required']['passwd']['type'], $this->encryptPW($checkPW)); } $result = $this->dbc->queryOne($sql, $this->authTableCols['required']['auth_user_id']['type']); if (MDB::isError($result)) { return false; } if (is_null($result)) { return false; } return true; } } ?> http://cvs.php.net/co.php/pear/Perm_LiveUser/Auth/MDB2.php?r=1.1&p=1 Index: pear/Perm_LiveUser/Auth/MDB2.php +++ pear/Perm_LiveUser/Auth/MDB2.php <?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 /** * MDB2 container for Authentication * * @package LiveUser * @category authentication */ /** * Require parent class definition and PEAR::MDB2 class. */ require_once 'LiveUser/Auth/Common.php'; require_once 'MDB2.php'; MDB2::loadFile('Date'); /** * This is a PEAR::MDB2 backend driver for the LiveUser class. * A PEAR::MDB2 connection object can be passed to the constructor to reuse an * existing connection. Alternatively, a DSN can be passed to open a new one. * * Requirements: * - File "LoginManager.php" (contains the parent class "LiveUser") * - Array of connection options or a PEAR::MDB2 connection object must be * passed to the constructor. * Example: array('dsn' => 'mysql://user:pass@host/db_name', * 'connection => &$conn, # PEAR::MDB2 connection object * 'loginTimeout' => 0, * 'allowDuplicateHandles' => 1); * * @author Markus Wolff <wolff@21st.de> * @version $Id: MDB2.php,v 1.1 2004/11/08 15:51:14 lsmith Exp $ * @package LiveUser * @category authentication */ class LiveUser_Auth_MDB2 extends LiveUser_Auth_Common { /** * dsn to connect to * * @access private * @var string */ var $dsn = null; /** * disconnect * * @access private * @var boolean */ var $disconnect = false; /** * PEAR::MDB2 connection object * * @access private * @var MDB2 */ var $dbc = null; /** * Auth table * Table where the auth data is stored. * * @access public * @var string */ var $authTable = 'liveuser_users'; /** * Columns of the auth table. * Associative array with the names of the auth table columns. * The 'auth_user_id', 'handle' and 'passwd' fields have to be set. * 'lastlogin' and 'is_active' are optional. * It doesn't make sense to set only one of the time columns without the * other. * * The type attribute is only useful when using MDB or MDB2. * * @access public * @var array */ var $authTableCols = array( 'required' => array( 'auth_user_id' => array('name' => 'auth_user_id', 'type' => ''), 'handle' => array('name' => 'handle', 'type' => ''), 'passwd' => array('name' => 'passwd', 'type' => ''), ), 'optional' => array( 'lastlogin' => array('name' => 'lastlogin', 'type' => ''), 'is_active' => array('name' => 'is_active', 'type' => '') ) ); /** * Class constructor. * * @access protected * @param array configuration array * @return void */ function LiveUser_Auth_MDB2(&$connectOptions) { $this->LiveUser_Auth_Common($connectOptions); if (is_array($connectOptions)) { $this->LiveUser_Auth_Common($connectOptions); if (isset($connectOptions['connection']) && MDB2::isConnection($connectOptions['connection']) ) { $this->dbc = &$connectOptions['connection']; $this->init_ok = true; } elseif (isset($connectOptions['dsn'])) { $this->dsn = $connectOptions['dsn']; $function = null; if (isset($connectOptions['function'])) { $function = $connectOptions['function']; } $options = null; if (isset($connectOptions['options'])) { $options = $connectOptions['options']; } $options['portability'] = MDB2_PORTABILITY_ALL; if ($function == 'singleton') { $this->dbc =& MDB2::singleton($connectOptions['dsn'], $options); } else { $this->dbc =& MDB2::connect($connectOptions['dsn'], $options); } if (!MDB2::isError($this->dbc)) { $this->init_ok = true; } } } } /** * properly disconnect from database * * @access public * @return void */ function disconnect() { if ($this->disconnect) { $this->dbc->disconnect(); $this->dbc = null; $this->init_ok = null; } } /** * 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 * @return boolean true on success or false on failure */ function _updateUserData() { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); return false; } if (isset($this->authTableCols['optional']['lastlogin'])) { $sql = 'UPDATE ' . $this->authTable.' SET ' . $this->authTableCols['optional']['lastlogin']['name'] .'=' . $this->dbc->quote(MDB2_Date::unix2Mdbstamp($this->currentLogin), $this->authTableCols['optional']['lastlogin']['type']) . ' WHERE ' . $this->authTableCols['required']['auth_user_id']['name'] .'=' . $this->dbc->quote($this->authUserId, $this->authTableCols['required']['auth_user_id']['type']); $res = $this->dbc->query($sql); if (MDB2::isError($res)) { return false; } return true; } else { return false; } } /** * Reads auth_user_id, passwd, is_active flag * lastlogin timestamp from the database * 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 password * 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. * * @access private * @param string user handle * @param boolean user password * @return boolean true on success or false on failure */ function _readUserData($handle, $passwd = false) { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); return false; } $success = false; $fields = array(); foreach ($this->authTableCols as $key => $value) { if (sizeof($value) > 0) { foreach ($value as $alias => $field_data) { $fields[] = $field_data['name'] . ' AS ' . $alias; $types[] = $field_data['type']; } } } // Setting the default query. $sql = 'SELECT ' . implode(',', $fields) . ' FROM ' . $this->authTable . ' WHERE ' . $this->authTableCols['required']['handle']['name'] . '=' . $this->dbc->quote($handle, $this->authTableCols['required']['handle']['type']); if ($passwd !== false) { // If $passwd is set, try to find the first user with the given // handle and password. $sql .= ' AND ' . $this->authTableCols['required']['passwd']['name'] . '=' . $this->dbc->quote($this->encryptPW($passwd), $this->authTableCols['required']['passwd']['type']); } // Query database $result = $this->dbc->queryRow($sql, $types, MDB2_FETCHMODE_ASSOC); // If a user was found, read data into class variables and set // return value to true if (!MDB2::isError($result) && is_array($result)) { $this->handle = $result['handle']; $this->passwd = $this->decryptPW($result['passwd']); $this->isActive = ((!isset($result['is_active']) || $result['is_active']) ? true : false); $this->authUserId = $result['auth_user_id']; $this->lastLogin = isset($result['lastlogin'])? MDB2_Date::mdbstamp2Unix($result['lastlogin']):''; $this->ownerUserId = isset($result['owner_user_id']) ? $result['owner_user_id'] : null; $this->ownerGroupid = isset($result['owner_group_id']) ? $result['owner_group_id'] : null; if (isset($this->authTableCols['custom'])) { foreach ($this->authTableCols['custom'] as $alias => $value) { $alias = strtolower($alias); $this->propertyValues['custom'][$alias] = $result[$alias]; } } $success = true; } return $success; } /** * Helper function that checks if there is a user in * the database who's matching the given parameters. * If $checkHandle is given and $checkPW is set to * false, it only checks if a user with that handle * exists. If only $checkPW is given and $checkHandle * is set to false, it will check if there exists a * user with that password. If both values are set to * anything but false, it will find the first user in * the database with both values matching. * Please note: * - If no match was found, the return value is false * - If a match was found, the auth_user_id from the database * is being returned * Whatever is returned, please keep in mind that this * function only searches for the _first_ occurence * of the search values in the database. So when you * have multiple users with the same handle, only the * ID of the first one is returned. Same goes for * passwords. Searching for both password and handle * should be pretty safe, though - having more than * one user with the same handle/password combination * in the database would be pretty stupid anyway. * * @param boolean The handle (username) to search * @param boolean The password to check against * @return mixed true or false if the user does not exist */ function userExists($checkHandle = false, $checkPW = false) { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); return false; } $sql = 'SELECT ' . $this->authTableCols['required']['auth_user_id']['name'] . ' FROM ' . $this->authTable; if ($checkHandle !== false && $checkPW === false) { // only search for the first user with the given handle $sql .= 'WHERE ' . $this->authTableCols['required']['handle']['name'] . '=' . $this->dbc->quote($checkHandle, $this->authTableCols['required']['handle']['type']); } elseif ($checkHandle === false && $checkPW !== false) { // only search for the first user with the given password $sql .= 'WHERE ' . $this->authTableCols['required']['passwd']['name'] . '=' . $this->dbc->quote($this->encryptPW($checkPW), $this->authTableCols['required']['passwd']['type']); } else { // check for a user with both handle and password matching $sql .= 'WHERE ' . $this->authTableCols['required']['handle']['name'] . '=' . $this->dbc->quote($checkHandle, $this->authTableCols['required']['handle']['type']) . ' AND ' . $this->authTableCols['required']['passwd']['name'] . '=' . $this->dbc->quote($this->encryptPW($checkPW), $this->authTableCols['required']['passwd']['type']); } $result = $this->dbc->queryOne($sql, $this->authTableCols['required']['auth_user_id']['type']); if (MDB2::isError($result)) { return false; } if (is_null($result)) { return false; } return true; } } ?> http://cvs.php.net/co.php/pear/Perm_LiveUser/Auth/PEARAuth.php?r=1.1&p=1 Index: pear/Perm_LiveUser/Auth/PEARAuth.php +++ pear/Perm_LiveUser/Auth/PEARAuth.php <?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 /** * PEAR_Auth container for Authentication * * @package LiveUser * @category authentication */ /** * Require parent class definition and PEAR::Auth class. */ require_once 'LiveUser/Auth/Common.php'; require_once 'Auth/Auth.php'; /** * ==================== !!! WARNING !!! ======================================== * * THIS CONTAINER IS UNDER HEAVY DEVELOPMENT. IT'S STILL IN EXPERIMENTAL * STAGE. USE IT AT YOUR OWN RISK. * * ============================================================================= * This is a PEAR::Auth backend driver for the LiveUser class. * The general options to setup the PEAR::Auth class can be passed to the constructor. * To choose the right auth container, you have to add the 'pearAuthContainer' var to * the options array. * * Requirements: * - File "LiveUser.php" (contains the parent class "LiveUser") * - PEAR::Auth must be installed in your PEAR directory * - Array of setup options must be passed to the constructor. * * @author Bjoern Kraus <krausbn@php.net> * @version $Id: PEARAuth.php,v 1.1 2004/11/08 15:51:14 lsmith Exp $ * @package LiveUser * @category authentication */ class LiveUser_Auth_PEARAuth extends LiveUser_Auth_Common { /** * Contains the PEAR::Auth object. * * @access private * @var Auth */ var $pearAuth = null; /** * Class constructor. * * @access protected * @param array configuration array * @return void */ function LiveUser_Auth_PEARAuth(&$connectOptions) { require_once 'Auth.php'; $this->LiveUser_Auth_Common($connectOptions); if (!is_object($this->pearAuth)) { $this->pearAuth = &new Auth($connectOptions['pearAuthContainer'], $connectOptions['pearAuthOptions'], '', false); } } /** * Starts and verifies the PEAR::Auth login process * * @access private * @return boolean true upon success or false on failure */ function _readUserData() { $this->pearAuth->start(); $this->pearAuth->login(); $success = false; // If a user was found, read data into class variables and set // return value to true if ($this->pearAuth->getAuth()) { $this->handle = $this->pearAuth->getUsername(); $this->passwd = $this->encryptPW($this->pearAuth->password); $this->isActive = true; $this->authUserId = $this->pearAuth->getUsername(); $this->lastLogin = ''; $success = true; } return $success; } /** * not yet implemented * * @return mixed true or false if the user does not exist */ function userExists() { return true; } } ?> http://cvs.php.net/co.php/pear/Perm_LiveUser/Auth/XML.php?r=1.1&p=1 Index: pear/Perm_LiveUser/Auth/XML.php +++ pear/Perm_LiveUser/Auth/XML.php <?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'; /** * This is a XML backend driver for the LiveUser class. * * @author Björn Kraus <krausbn@php.net> * @version $Id: XML.php,v 1.1 2004/11/08 15:51:14 lsmith Exp $ * @package LiveUser * @category authentication */ class LiveUser_Auth_XML extends LiveUser_Auth_Common { /** * XML file in which the auth data is stored. * * @access private * @var string */ var $file = ''; /** * XML::Tree object. * * @access private * @var XML_Tree */ var $tree = null; /** * XML::Tree object of the user logged in. * * @access private * @var XML_Tree * @see _readUserData() */ var $userObj = null; /** * Class constructor. * * @access protected * @param array configuration array * @return void */ function LiveUser_Auth_XML(&$connectOptions) { if (is_array($connectOptions)) { $this->LiveUser_Auth_Common($connectOptions); if (!is_file($this->file)) { if (is_file(getenv('DOCUMENT_ROOT') . $this->file)) { $this->file = getenv('DOCUMENT_ROOT') . $this->file; } else { $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_MISSING_DEPS, null, null, "Auth initialisation failed. Can't find xml file."); } } 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 = LiveUser::raiseError(LIVEUSER_ERROR_MISSING_DEPS, null, null, "Auth initialisation failed. Can't find XML_Tree class."); } } else { $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_MISSING_DEPS, null, null, "Auth initialisation failed. Can't find xml file."); } } } /** * Properly disconnect from resources * * @access public * @return void */ function disconnect() { $this->tree = null; $this->userObj = null; $this->init_ok = null; } /** * 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 * @return boolean true on success or false on failure */ function _updateUserData() { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); 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++; } $success = false; do { $fp = fopen($this->file, 'wb'); if (!$fp) { $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) { $this->_stack->push(LIVEUSER_ERROR, 'exception', array(), 'Cannot read XML Auth file'); } return $success; } /** * * 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 Handle of the current user. * @param mixed Can be a string with an * unencrypted pwd or false. * @return boolean true on success or false on failure */ function _readUserData($userHandle, $userPasswd = false) { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); 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; } /** * Helper function that checks if there is a user in * the database who's matching the given parameters. * If $checkHandle is given and $checkPW is set to * false, it only checks if a user with that handle * exists. If only $checkPW is given and $checkHandle * is set to false, it will check if there exists a * user with that password. If both values are set to * anything but false, it will find the first user in * the database with both values matching. * Please note: * - If no match was found, the return value is false * - If a match was found, the auth_user_id from the database * is being returned * Whatever is returned, please keep in mind that this * function only searches for the _first_ occurence * of the search values in the database. So when you * have multiple users with the same handle, only the * ID of the first one is returned. Same goes for * passwords. Searching for both password and handle * should be pretty safe, though - having more than * one user with the same handle/password combination * in the database would be pretty stupid anyway. * * @param boolean The handle (username) to search * @param boolean The password to check against * @return mixed true or false if the user does not exist */ function userExists($checkHandle=false,$checkPW=false) { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); return false; } foreach ($this->tree->root->children as $user) { $handle = ''; $password = ''; foreach ($user->children as $value) { if (isset(${$value->name})) { ${$value->name} = $value->content; } } if ($checkHandle !== false && $checkPW === false) { // only search for the first user with the given handle if ($checkHandle == $handle) { return true; } } elseif ($checkHandle === false && $checkPW !== false) { // only search for the first user with the given password if ($this->encryptPW($checkPW) == $password) { return true; } } else { // check for a user with both handle and password matching if ($checkHandle == $handle) { if ($this->encryptPW($checkPW) == $password) { return true; } } } } return false; } } ?>
http://cvs.php.net/co.php/pear/Perm_LiveUser/Auth/DB.php?r=1.1&p=1 Index: pear/Perm_LiveUser/Auth/DB.php +++ pear/Perm_LiveUser/Auth/DB.php <?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 /** * DB container for Authentication * * @package LiveUser * @category authentication */ /** * Require parent class definition and PEAR::DB class. */ require_once 'LiveUser/Auth/Common.php'; require_once 'DB.php'; /** * This is a PEAR::DB backend driver for the LiveUser class. * A PEAR::DB connection object can be passed to the constructor to reuse an * existing connection. Alternatively, a DSN can be passed to open a new one. * * Requirements: * - File "LoginManager.php" (contains the parent class "LiveUser") * - Array of connection options or a PEAR::DB connection object must be * passed to the constructor. * Example: array('dsn' => 'mysql://user:pass@host/db_name', * 'connection => &$conn, # PEAR::DB connection object * 'loginTimeout' => 0, * 'allowDuplicateHandles' => 1); * * @author Markus Wolff <wolff@21st.de> * @version $Id: DB.php,v 1.1 2004/11/08 15:51:14 lsmith Exp $ * @package LiveUser * @category authentication */ class LiveUser_Auth_DB extends LiveUser_Auth_Common { /** * dsn to connect to * * @access private * @var string */ var $dsn = null; /** * disconnect * * @access private * @var boolean */ var $disconnect = false; /** * PEAR::DB connection object * * @access private * @var DB */ var $dbc = null; /** * Auth table * Table where the auth data is stored. * * @access public * @var string */ var $authTable = 'liveuser_users'; /** * Columns of the auth table. * Associative array with the names of the auth table columns. * The 'auth_user_id', 'handle' and 'passwd' fields have to be set. * 'lastlogin' and 'is_active' are optional. * It doesn't make sense to set only one of the time columns without the * other. * * The type attribute is only useful when using MDB or MDB2. * * @access public * @var array */ var $authTableCols = array( 'required' => array( 'auth_user_id' => array('name' => 'auth_user_id', 'type' => ''), 'handle' => array('name' => 'handle', 'type' => ''), 'passwd' => array('name' => 'passwd', 'type' => ''), ), 'optional' => array( 'lastlogin' => array('name' => 'lastlogin', 'type' => ''), 'is_active' => array('name' => 'is_active', 'type' => '') ) ); /** * Class constructor. * * @access protected * @param array configuration array * @return void */ function LiveUser_Auth_DB(&$connectOptions) { $this->LiveUser_Auth_Common($connectOptions); if (is_array($connectOptions)) { $this->LiveUser_Auth_Common($connectOptions); if (isset($connectOptions['connection']) && DB::isConnection($connectOptions['connection']) ) { $this->dbc = &$connectOptions['connection']; $this->init_ok = true; } elseif (isset($connectOptions['dsn'])) { $this->dsn = $connectOptions['dsn']; $options = null; if (isset($connectOptions['options'])) { $options = $connectOptions['options']; } $options['portability'] = DB_PORTABILITY_ALL; $this->dbc =& DB::connect($connectOptions['dsn'], $options); if (!DB::isError($this->dbc)) { $this->init_ok = true; } } } } /** * Properly disconnect from database * * @access public * @return void */ function disconnect() { if ($this->disconnect) { $this->dbc->disconnect(); $this->dbc = null; $this->init_ok = null; } } /** * 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 * @return boolean true on success or false on failure */ function _updateUserData() { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); return false; } if (isset($this->authTableCols['optional']['lastlogin'])) { $sql = 'UPDATE ' . $this->authTable.' SET ' . $this->authTableCols['optional']['lastlogin']['name'] . '=' . $this->dbc->quoteSmart(date('Y-m-d H:i:s', $this->currentLogin)) . ' WHERE ' . $this->authTableCols['required']['auth_user_id']['name'] . '=' . $this->dbc->quoteSmart($this->authUserId); $res = $this->dbc->query($sql); if (DB::isError($res)) { return false; } return true; } else { return false; } } /** * Reads auth_user_id, passwd, is_active flag * lastlogin timestamp from the database * 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 password * 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. * * @access private * @param string user handle * @param boolean user password * @return boolean true upon success or false on failure */ function _readUserData($handle, $passwd = false) { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); return false; } $success = false; $fields = array(); foreach ($this->authTableCols as $key => $value) { if (sizeof($value) > 0) { foreach ($value as $alias => $field_data) { $fields[] = $field_data['name'] . ' AS ' . $alias; } } } // Setting the default sql query. $sql = 'SELECT ' . implode(',', $fields) . ' FROM ' . $this->authTable.' WHERE ' . $this->authTableCols['required']['handle']['name'] . '=' . $this->dbc->quoteSmart($handle); if ($passwd !== false) { // If $passwd is set, try to find the first user with the given // handle and password. $sql .= 'AND ' . $this->authTableCols['required']['passwd']['name'] . '=' . $this->dbc->quoteSmart($this->encryptPW($passwd)); } // Query database $result = $this->dbc->getRow($sql, null, DB_FETCHMODE_ASSOC); // If a user was found, read data into class variables and set // return value to true if (!DB::isError($result) && is_array($result)) { $this->handle = $result['handle']; $this->passwd = $this->decryptPW($result['passwd']); $this->isActive = ((!isset($result['is_active']) || $result['is_active'] == 'Y') ? true : false); $this->authUserId = $result['auth_user_id']; $this->lastLogin = !empty($result['lastlogin']) ? strtotime($result['lastlogin']) : ''; $this->ownerUserId = isset($result['owner_user_id']) ? $result['owner_user_id'] : null; $this->ownerGroupid = isset($result['owner_group_id']) ? $result['owner_group_id'] : null; if (isset($this->authTableCols['custom'])) { foreach ($this->authTableCols['custom'] as $alias => $value) { $alias = strtolower($alias); $this->propertyValues['custom'][$alias] = $result[$alias]; } } $success = true; } return $success; } /** * Helper function that checks if there is a user in * the database who's matching the given parameters. * If $checkHandle is given and $checkPW is set to * false, it only checks if a user with that handle * exists. If only $checkPW is given and $checkHandle * is set to false, it will check if there exists a * user with that password. If both values are set to * anything but false, it will find the first user in * the database with both values matching. * Please note: * - If no match was found, the return value is false * - If a match was found, the auth_user_id from the database * is being returned * Whatever is returned, please keep in mind that this * function only searches for the _first_ occurence * of the search values in the database. So when you * have multiple users with the same handle, only the * ID of the first one is returned. Same goes for * passwords. Searching for both password and handle * should be pretty safe, though - having more than * one user with the same handle/password combination * in the database would be pretty stupid anyway. * * @param boolean The handle (username) to search * @param boolean The password to check against * @return mixed true or false if the user does not exist */ function userExists($checkHandle = false,$checkPW = false) { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); return false; } $sql = 'SELECT ' . $this->authTableCols['required']['auth_user_id'] . ' FROM ' . $this->authTable; if ($checkHandle !== false && $checkPW === false) { // only search for the first user with the given handle $sql .= 'WHERE ' . $this->authTableCols['required']['handle'] . '=' . $this->dbc->quoteSmart($checkHandle); } elseif ($checkHandle === false && $checkPW !== false) { // only search for the first user with the given password $sql .= 'WHERE ' . $this->authTableCols['required']['passwd'] . '=' . $this->dbc->quoteSmart($this->encryptPW($checkPW)); } else { // check for a user with both handle and password matching $sql .= 'WHERE ' . $this->authTableCols['required']['handle'] . '=' . $this->dbc->quoteSmart($checkHandle) . ' AND ' . $this->authTableCols['required']['passwd'] . '=' . $this->dbc->quoteSmart($this->encryptPW($checkPW)); } $result = $this->dbc->getOne($sql); if (DB::isError($result)) { return false; } if (is_null($result)) { return false; } return true; } } ?> http://cvs.php.net/co.php/pear/Perm_LiveUser/Auth/MDB.php?r=1.1&p=1 Index: pear/Perm_LiveUser/Auth/MDB.php +++ pear/Perm_LiveUser/Auth/MDB.php <?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 /** * MDB container for Authentication * * @package LiveUser * @category authentication */ /** * Require parent class definition and PEAR::MDB class. */ require_once 'LiveUser/Auth/Common.php'; require_once 'MDB.php'; MDB::loadFile('Date'); /** * This is a PEAR::MDB backend driver for the LiveUser class. * A PEAR::MDB connection object can be passed to the constructor to reuse an * existing connection. Alternatively, a DSN can be passed to open a new one. * * Requirements: * - File "LoginManager.php" (contains the parent class "LiveUser") * - Array of connection options or a PEAR::MDB connection object must be * passed to the constructor. * Example: array('dsn' => 'mysql://user:pass@host/db_name', * 'connection => &$conn, # PEAR::MDB connection object * 'loginTimeout' => 0, * 'allowDuplicateHandles' => 1); * * @author Markus Wolff <wolff@21st.de> * @version $Id: MDB.php,v 1.1 2004/11/08 15:51:14 lsmith Exp $ * @package LiveUser * @category authentication */ class LiveUser_Auth_MDB extends LiveUser_Auth_Common { /** * dsn to connect to * * @access private * @var string */ var $dsn = null; /** * disconnect * * @access private * @var boolean */ var $disconnect = false; /** * PEAR::MDB connection object * * @access private * @var MDB */ var $dbc = null; /** * Auth table * Table where the auth data is stored. * * @access public * @var string */ var $authTable = 'liveuser_users'; /** * Columns of the auth table. * Associative array with the names of the auth table columns. * The 'auth_user_id', 'handle' and 'passwd' fields have to be set. * 'lastlogin' and 'is_active' are optional. * It doesn't make sense to set only one of the time columns without the * other. * * The type attribute is only useful when using MDB or MDB2. * * @access public * @var array */ var $authTableCols = array( 'required' => array( 'auth_user_id' => array('name' => 'auth_user_id', 'type' => ''), 'handle' => array('name' => 'handle', 'type' => ''), 'passwd' => array('name' => 'passwd', 'type' => ''), ), 'optional' => array( 'lastlogin' => array('name' => 'lastlogin', 'type' => ''), 'is_active' => array('name' => 'is_active', 'type' => '') ) ); /** * Class constructor. * * @access protected * @param array configuration array * @return void */ function LiveUser_Auth_MDB(&$connectOptions) { $this->LiveUser_Auth_Common($connectOptions); if (is_array($connectOptions)) { $this->LiveUser_Auth_Common($connectOptions); if (isset($connectOptions['connection']) && MDB::isConnection($connectOptions['connection']) ) { $this->dbc = &$connectOptions['connection']; $this->init_ok = true; } elseif (isset($connectOptions['dsn'])) { $this->dsn = $connectOptions['dsn']; $function = null; if (isset($connectOptions['function'])) { $function = $connectOptions['function']; } $options = null; if (isset($connectOptions['options'])) { $options = $connectOptions['options']; } $options['optimize'] = 'portability'; if ($function == 'singleton') { $this->dbc =& MDB::singleton($connectOptions['dsn'], $options); } else { $this->dbc =& MDB::connect($connectOptions['dsn'], $options); } if (!MDB::isError($this->dbc)) { $this->init_ok = true; } } } } /** * Properly disconnect from database * * @access public * @return void */ function disconnect() { if ($this->disconnect) { $this->dbc->disconnect(); $this->dbc = null; $this->init_ok = null; } } /** * 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 * @return boolean true on success or false on failure */ function _updateUserData() { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); return false; } if (isset($this->authTableCols['optional']['lastlogin'])) { $sql = 'UPDATE ' . $this->authTable.' SET ' . $this->authTableCols['optional']['lastlogin']['name'] .'=' . $this->dbc->getValue($this->authTableCols['optional']['lastlogin']['type'], MDB_Date::unix2Mdbstamp($this->currentLogin)) . ' WHERE ' . $this->authTableCols['required']['auth_user_id']['name'] .'=' . $this->dbc->getValue($this->authTableCols['required']['auth_user_id']['type'], $this->authUserId); $res = $this->dbc->query($sql); if (MDB::isError($res)) { return false; } return true; } else { return false; } } /** * Reads auth_user_id, passwd, is_active flag * lastlogin timestamp from the database * 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 password * 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. * * @access private * @param string user handle * @param boolean user password * @return boolean true upon success or false on failure */ function _readUserData($handle, $passwd = false) { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); return false; } $success = false; $fields = array(); foreach ($this->authTableCols as $key => $value) { if (sizeof($value) > 0) { foreach ($value as $alias => $field_data) { $fields[] = $field_data['name'] . ' AS ' . $alias; $types[] = $field_data['type']; } } } // Setting the default query. $sql = 'SELECT ' . implode(',', $fields) . ' FROM ' . $this->authTable . ' WHERE ' . $this->authTableCols['required']['handle']['name'] . '=' . $this->dbc->getValue($this->authTableCols['required']['handle']['type'], $handle); if ($passwd !== false) { // If $passwd is set, try to find the first user with the given // handle and password. $sql .= ' AND ' . $this->authTableCols['required']['passwd']['name'] . '=' . $this->dbc->getValue($this->authTableCols['required']['passwd']['type'], $this->encryptPW($passwd)); } // Query database $result = $this->dbc->queryRow($sql, $types, MDB_FETCHMODE_ASSOC); // If a user was found, read data into class variables and set // return value to true if (!MDB::isError($result) && is_array($result)) { $this->handle = $result['handle']; $this->passwd = $this->decryptPW($result['passwd']); $this->isActive = ((!isset($result['is_active']) || $result['is_active']) ? true : false); $this->authUserId = $result['auth_user_id']; $this->lastLogin = isset($result['lastlogin'])? MDB_Date::mdbstamp2Unix($result['lastlogin']):''; $this->ownerUserId = isset($result['owner_user_id']) ? $result['owner_user_id'] : null; $this->ownerGroupid = isset($result['owner_group_id']) ? $result['owner_group_id'] : null; if (isset($this->authTableCols['custom'])) { foreach ($this->authTableCols['custom'] as $alias => $value) { $alias = strtolower($alias); $this->propertyValues['custom'][$alias] = $result[$alias]; } } $success = true; } return $success; } /** * Helper function that checks if there is a user in * the database who's matching the given parameters. * If $checkHandle is given and $checkPW is set to * false, it only checks if a user with that handle * exists. If only $checkPW is given and $checkHandle * is set to false, it will check if there exists a * user with that password. If both values are set to * anything but false, it will find the first user in * the database with both values matching. * Please note: * - If no match was found, the return value is false * - If a match was found, the auth_user_id from the database * is being returned * Whatever is returned, please keep in mind that this * function only searches for the _first_ occurence * of the search values in the database. So when you * have multiple users with the same handle, only the * ID of the first one is returned. Same goes for * passwords. Searching for both password and handle * should be pretty safe, though - having more than * one user with the same handle/password combination * in the database would be pretty stupid anyway. * * @param boolean The handle (username) to search * @param boolean The password to check against * @return mixed true or false if the user does not exist */ function userExists($checkHandle = false, $checkPW = false) { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); return false; } $sql = 'SELECT ' . $this->authTableCols['required']['auth_user_id']['name'] . ' FROM ' . $this->authTable; if ($checkHandle !== false && $checkPW === false) { // only search for the first user with the given handle $sql .= 'WHERE ' . $this->authTableCols['required']['handle']['name'] . '=' . $this->dbc->getValue($this->authTableCols['required']['handle']['type'], $checkHandle); } elseif ($checkHandle === false && $checkPW !== false) { // only search for the first user with the given password $sql .= 'WHERE ' . $this->authTableCols['required']['passwd']['name'] . '=' . $this->dbc->getValue($this->authTableCols['required']['passwd']['type'], $this->encryptPW($checkPW)); } else { // check for a user with both handle and password matching $sql .= 'WHERE ' . $this->authTableCols['required']['handle']['name'] . '=' . $this->dbc->getValue($this->authTableCols['required']['handle']['type'], $checkHandle) . ' AND ' . $this->authTableCols['required']['passwd']['name'] . '=' . $this->dbc->getValue($this->authTableCols['required']['passwd']['type'], $this->encryptPW($checkPW)); } $result = $this->dbc->queryOne($sql, $this->authTableCols['required']['auth_user_id']['type']); if (MDB::isError($result)) { return false; } if (is_null($result)) { return false; } return true; } } ?> http://cvs.php.net/co.php/pear/Perm_LiveUser/Auth/MDB2.php?r=1.1&p=1 Index: pear/Perm_LiveUser/Auth/MDB2.php +++ pear/Perm_LiveUser/Auth/MDB2.php <?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 /** * MDB2 container for Authentication * * @package LiveUser * @category authentication */ /** * Require parent class definition and PEAR::MDB2 class. */ require_once 'LiveUser/Auth/Common.php'; require_once 'MDB2.php'; MDB2::loadFile('Date'); /** * This is a PEAR::MDB2 backend driver for the LiveUser class. * A PEAR::MDB2 connection object can be passed to the constructor to reuse an * existing connection. Alternatively, a DSN can be passed to open a new one. * * Requirements: * - File "LoginManager.php" (contains the parent class "LiveUser") * - Array of connection options or a PEAR::MDB2 connection object must be * passed to the constructor. * Example: array('dsn' => 'mysql://user:pass@host/db_name', * 'connection => &$conn, # PEAR::MDB2 connection object * 'loginTimeout' => 0, * 'allowDuplicateHandles' => 1); * * @author Markus Wolff <wolff@21st.de> * @version $Id: MDB2.php,v 1.1 2004/11/08 15:51:14 lsmith Exp $ * @package LiveUser * @category authentication */ class LiveUser_Auth_MDB2 extends LiveUser_Auth_Common { /** * dsn to connect to * * @access private * @var string */ var $dsn = null; /** * disconnect * * @access private * @var boolean */ var $disconnect = false; /** * PEAR::MDB2 connection object * * @access private * @var MDB2 */ var $dbc = null; /** * Auth table * Table where the auth data is stored. * * @access public * @var string */ var $authTable = 'liveuser_users'; /** * Columns of the auth table. * Associative array with the names of the auth table columns. * The 'auth_user_id', 'handle' and 'passwd' fields have to be set. * 'lastlogin' and 'is_active' are optional. * It doesn't make sense to set only one of the time columns without the * other. * * The type attribute is only useful when using MDB or MDB2. * * @access public * @var array */ var $authTableCols = array( 'required' => array( 'auth_user_id' => array('name' => 'auth_user_id', 'type' => ''), 'handle' => array('name' => 'handle', 'type' => ''), 'passwd' => array('name' => 'passwd', 'type' => ''), ), 'optional' => array( 'lastlogin' => array('name' => 'lastlogin', 'type' => ''), 'is_active' => array('name' => 'is_active', 'type' => '') ) ); /** * Class constructor. * * @access protected * @param array configuration array * @return void */ function LiveUser_Auth_MDB2(&$connectOptions) { $this->LiveUser_Auth_Common($connectOptions); if (is_array($connectOptions)) { $this->LiveUser_Auth_Common($connectOptions); if (isset($connectOptions['connection']) && MDB2::isConnection($connectOptions['connection']) ) { $this->dbc = &$connectOptions['connection']; $this->init_ok = true; } elseif (isset($connectOptions['dsn'])) { $this->dsn = $connectOptions['dsn']; $function = null; if (isset($connectOptions['function'])) { $function = $connectOptions['function']; } $options = null; if (isset($connectOptions['options'])) { $options = $connectOptions['options']; } $options['portability'] = MDB2_PORTABILITY_ALL; if ($function == 'singleton') { $this->dbc =& MDB2::singleton($connectOptions['dsn'], $options); } else { $this->dbc =& MDB2::connect($connectOptions['dsn'], $options); } if (!MDB2::isError($this->dbc)) { $this->init_ok = true; } } } } /** * properly disconnect from database * * @access public * @return void */ function disconnect() { if ($this->disconnect) { $this->dbc->disconnect(); $this->dbc = null; $this->init_ok = null; } } /** * 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 * @return boolean true on success or false on failure */ function _updateUserData() { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); return false; } if (isset($this->authTableCols['optional']['lastlogin'])) { $sql = 'UPDATE ' . $this->authTable.' SET ' . $this->authTableCols['optional']['lastlogin']['name'] .'=' . $this->dbc->quote(MDB2_Date::unix2Mdbstamp($this->currentLogin), $this->authTableCols['optional']['lastlogin']['type']) . ' WHERE ' . $this->authTableCols['required']['auth_user_id']['name'] .'=' . $this->dbc->quote($this->authUserId, $this->authTableCols['required']['auth_user_id']['type']); $res = $this->dbc->query($sql); if (MDB2::isError($res)) { return false; } return true; } else { return false; } } /** * Reads auth_user_id, passwd, is_active flag * lastlogin timestamp from the database * 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 password * 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. * * @access private * @param string user handle * @param boolean user password * @return boolean true on success or false on failure */ function _readUserData($handle, $passwd = false) { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); return false; } $success = false; $fields = array(); foreach ($this->authTableCols as $key => $value) { if (sizeof($value) > 0) { foreach ($value as $alias => $field_data) { $fields[] = $field_data['name'] . ' AS ' . $alias; $types[] = $field_data['type']; } } } // Setting the default query. $sql = 'SELECT ' . implode(',', $fields) . ' FROM ' . $this->authTable . ' WHERE ' . $this->authTableCols['required']['handle']['name'] . '=' . $this->dbc->quote($handle, $this->authTableCols['required']['handle']['type']); if ($passwd !== false) { // If $passwd is set, try to find the first user with the given // handle and password. $sql .= ' AND ' . $this->authTableCols['required']['passwd']['name'] . '=' . $this->dbc->quote($this->encryptPW($passwd), $this->authTableCols['required']['passwd']['type']); } // Query database $result = $this->dbc->queryRow($sql, $types, MDB2_FETCHMODE_ASSOC); // If a user was found, read data into class variables and set // return value to true if (!MDB2::isError($result) && is_array($result)) { $this->handle = $result['handle']; $this->passwd = $this->decryptPW($result['passwd']); $this->isActive = ((!isset($result['is_active']) || $result['is_active']) ? true : false); $this->authUserId = $result['auth_user_id']; $this->lastLogin = isset($result['lastlogin'])? MDB2_Date::mdbstamp2Unix($result['lastlogin']):''; $this->ownerUserId = isset($result['owner_user_id']) ? $result['owner_user_id'] : null; $this->ownerGroupid = isset($result['owner_group_id']) ? $result['owner_group_id'] : null; if (isset($this->authTableCols['custom'])) { foreach ($this->authTableCols['custom'] as $alias => $value) { $alias = strtolower($alias); $this->propertyValues['custom'][$alias] = $result[$alias]; } } $success = true; } return $success; } /** * Helper function that checks if there is a user in * the database who's matching the given parameters. * If $checkHandle is given and $checkPW is set to * false, it only checks if a user with that handle * exists. If only $checkPW is given and $checkHandle * is set to false, it will check if there exists a * user with that password. If both values are set to * anything but false, it will find the first user in * the database with both values matching. * Please note: * - If no match was found, the return value is false * - If a match was found, the auth_user_id from the database * is being returned * Whatever is returned, please keep in mind that this * function only searches for the _first_ occurence * of the search values in the database. So when you * have multiple users with the same handle, only the * ID of the first one is returned. Same goes for * passwords. Searching for both password and handle * should be pretty safe, though - having more than * one user with the same handle/password combination * in the database would be pretty stupid anyway. * * @param boolean The handle (username) to search * @param boolean The password to check against * @return mixed true or false if the user does not exist */ function userExists($checkHandle = false, $checkPW = false) { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); return false; } $sql = 'SELECT ' . $this->authTableCols['required']['auth_user_id']['name'] . ' FROM ' . $this->authTable; if ($checkHandle !== false && $checkPW === false) { // only search for the first user with the given handle $sql .= 'WHERE ' . $this->authTableCols['required']['handle']['name'] . '=' . $this->dbc->quote($checkHandle, $this->authTableCols['required']['handle']['type']); } elseif ($checkHandle === false && $checkPW !== false) { // only search for the first user with the given password $sql .= 'WHERE ' . $this->authTableCols['required']['passwd']['name'] . '=' . $this->dbc->quote($this->encryptPW($checkPW), $this->authTableCols['required']['passwd']['type']); } else { // check for a user with both handle and password matching $sql .= 'WHERE ' . $this->authTableCols['required']['handle']['name'] . '=' . $this->dbc->quote($checkHandle, $this->authTableCols['required']['handle']['type']) . ' AND ' . $this->authTableCols['required']['passwd']['name'] . '=' . $this->dbc->quote($this->encryptPW($checkPW), $this->authTableCols['required']['passwd']['type']); } $result = $this->dbc->queryOne($sql, $this->authTableCols['required']['auth_user_id']['type']); if (MDB2::isError($result)) { return false; } if (is_null($result)) { return false; } return true; } } ?> http://cvs.php.net/co.php/pear/Perm_LiveUser/Auth/PEARAuth.php?r=1.1&p=1 Index: pear/Perm_LiveUser/Auth/PEARAuth.php +++ pear/Perm_LiveUser/Auth/PEARAuth.php <?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 /** * PEAR_Auth container for Authentication * * @package LiveUser * @category authentication */ /** * Require parent class definition and PEAR::Auth class. */ require_once 'LiveUser/Auth/Common.php'; require_once 'Auth/Auth.php'; /** * ==================== !!! WARNING !!! ======================================== * * THIS CONTAINER IS UNDER HEAVY DEVELOPMENT. IT'S STILL IN EXPERIMENTAL * STAGE. USE IT AT YOUR OWN RISK. * * ============================================================================= * This is a PEAR::Auth backend driver for the LiveUser class. * The general options to setup the PEAR::Auth class can be passed to the constructor. * To choose the right auth container, you have to add the 'pearAuthContainer' var to * the options array. * * Requirements: * - File "LiveUser.php" (contains the parent class "LiveUser") * - PEAR::Auth must be installed in your PEAR directory * - Array of setup options must be passed to the constructor. * * @author Bjoern Kraus <krausbn@php.net> * @version $Id: PEARAuth.php,v 1.1 2004/11/08 15:51:14 lsmith Exp $ * @package LiveUser * @category authentication */ class LiveUser_Auth_PEARAuth extends LiveUser_Auth_Common { /** * Contains the PEAR::Auth object. * * @access private * @var Auth */ var $pearAuth = null; /** * Class constructor. * * @access protected * @param array configuration array * @return void */ function LiveUser_Auth_PEARAuth(&$connectOptions) { require_once 'Auth.php'; $this->LiveUser_Auth_Common($connectOptions); if (!is_object($this->pearAuth)) { $this->pearAuth = &new Auth($connectOptions['pearAuthContainer'], $connectOptions['pearAuthOptions'], '', false); } } /** * Starts and verifies the PEAR::Auth login process * * @access private * @return boolean true upon success or false on failure */ function _readUserData() { $this->pearAuth->start(); $this->pearAuth->login(); $success = false; // If a user was found, read data into class variables and set // return value to true if ($this->pearAuth->getAuth()) { $this->handle = $this->pearAuth->getUsername(); $this->passwd = $this->encryptPW($this->pearAuth->password); $this->isActive = true; $this->authUserId = $this->pearAuth->getUsername(); $this->lastLogin = ''; $success = true; } return $success; } /** * not yet implemented * * @return mixed true or false if the user does not exist */ function userExists() { return true; } } ?> http://cvs.php.net/co.php/pear/Perm_LiveUser/Auth/XML.php?r=1.1&p=1 Index: pear/Perm_LiveUser/Auth/XML.php +++ pear/Perm_LiveUser/Auth/XML.php <?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'; /** * This is a XML backend driver for the LiveUser class. * * @author Björn Kraus <krausbn@php.net> * @version $Id: XML.php,v 1.1 2004/11/08 15:51:14 lsmith Exp $ * @package LiveUser * @category authentication */ class LiveUser_Auth_XML extends LiveUser_Auth_Common { /** * XML file in which the auth data is stored. * * @access private * @var string */ var $file = ''; /** * XML::Tree object. * * @access private * @var XML_Tree */ var $tree = null; /** * XML::Tree object of the user logged in. * * @access private * @var XML_Tree * @see _readUserData() */ var $userObj = null; /** * Class constructor. * * @access protected * @param array configuration array * @return void */ function LiveUser_Auth_XML(&$connectOptions) { if (is_array($connectOptions)) { $this->LiveUser_Auth_Common($connectOptions); if (!is_file($this->file)) { if (is_file(getenv('DOCUMENT_ROOT') . $this->file)) { $this->file = getenv('DOCUMENT_ROOT') . $this->file; } else { $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_MISSING_DEPS, null, null, "Auth initialisation failed. Can't find xml file."); } } 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 = LiveUser::raiseError(LIVEUSER_ERROR_MISSING_DEPS, null, null, "Auth initialisation failed. Can't find XML_Tree class."); } } else { $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_MISSING_DEPS, null, null, "Auth initialisation failed. Can't find xml file."); } } } /** * Properly disconnect from resources * * @access public * @return void */ function disconnect() { $this->tree = null; $this->userObj = null; $this->init_ok = null; } /** * 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 * @return boolean true on success or false on failure */ function _updateUserData() { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); 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++; } $success = false; do { $fp = fopen($this->file, 'wb'); if (!$fp) { $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) { $this->_stack->push(LIVEUSER_ERROR, 'exception', array(), 'Cannot read XML Auth file'); } return $success; } /** * * 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 Handle of the current user. * @param mixed Can be a string with an * unencrypted pwd or false. * @return boolean true on success or false on failure */ function _readUserData($userHandle, $userPasswd = false) { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); 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; } /** * Helper function that checks if there is a user in * the database who's matching the given parameters. * If $checkHandle is given and $checkPW is set to * false, it only checks if a user with that handle * exists. If only $checkPW is given and $checkHandle * is set to false, it will check if there exists a * user with that password. If both values are set to * anything but false, it will find the first user in * the database with both values matching. * Please note: * - If no match was found, the return value is false * - If a match was found, the auth_user_id from the database * is being returned * Whatever is returned, please keep in mind that this * function only searches for the _first_ occurence * of the search values in the database. So when you * have multiple users with the same handle, only the * ID of the first one is returned. Same goes for * passwords. Searching for both password and handle * should be pretty safe, though - having more than * one user with the same handle/password combination * in the database would be pretty stupid anyway. * * @param boolean The handle (username) to search * @param boolean The password to check against * @return mixed true or false if the user does not exist */ function userExists($checkHandle=false,$checkPW=false) { if (!$this->init_ok) { $this->_stack->push(LIVEUSER_ERROR_INIT_ERROR, 'error', array('container' => get_class($this))); return false; } foreach ($this->tree->root->children as $user) { $handle = ''; $password = ''; foreach ($user->children as $value) { if (isset(${$value->name})) { ${$value->name} = $value->content; } } if ($checkHandle !== false && $checkPW === false) { // only search for the first user with the given handle if ($checkHandle == $handle) { return true; } } elseif ($checkHandle === false && $checkPW !== false) { // only search for the first user with the given password if ($this->encryptPW($checkPW) == $password) { return true; } } else { // check for a user with both handle and password matching if ($checkHandle == $handle) { if ($this->encryptPW($checkPW) == $password) { return true; } } } } return false; } } ?>