cvs: pear /Perm_LiveUser LiveUser.php
| From: | Arnaud Limbourg | Date: | Mon, 07 Jun 2004 15:33:16 +0000 |
| Subject: | cvs: pear /Perm_LiveUser LiveUser.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-20305@lists.php.net to get a copy of this message | ||
arnaud Mon Jun 7 11:33:16 2004 EDT
Modified files:
/pear/Perm_LiveUser LiveUser.php
Log:
* update documentation for upcoming changes relating to bug #1554 regarding custom fields
handling
http://cvs.php.net/diff.php/pear/Perm_LiveUser/LiveUser.php?r1=1.100&r2=1.101&ty=u Index: pear/Perm_LiveUser/LiveUser.php diff -u pear/Perm_LiveUser/LiveUser.php:1.100 pear/Perm_LiveUser/LiveUser.php:1.101 --- pear/Perm_LiveUser/LiveUser.php:1.100 Sat May 22 06:55:56 2004 +++ pear/Perm_LiveUser/LiveUser.php Mon Jun 7 11:33:15 2004 @@ -32,7 +32,8 @@ /** * Include PEAR base class */ -require_once 'PEAR.php'; +require_once 'PEAR/ErrorStack.php'; + /**#@+ * Error related constants definition @@ -46,6 +47,7 @@ define('LIVEUSER_ERROR_MISSING_LOGINFUNCTION', -5); define('LIVEUSER_ERROR_MISSING_LOGOUTFUNCTION', -6); define('LIVEUSER_ERROR_COOKIE', -7); +define('LIVEUSER_ERROR_MISSING_FILE', -8); /**#@-*/ /**#@+ @@ -72,6 +74,17 @@ define('LIVEUSER_MAX_LEVEL', 3); /** + * Debug global. When set to true the + * error stack will be printed to + * a separate window using the Win implementation + * of PEAR::Log (of which PEAR::ErrorStack has builtin + * knowledge). + * + * @var boolean + */ +$GLOBALS['_LIVEUSER_DEBUG'] = false; + +/** * Class LiveUser - Login handling class * * Description: @@ -92,7 +105,8 @@ * it will try to send HTTP headers such as cookies and redirects. * * Requirements: - * - Should run on PHP version 4.1.0 or higher, tested only from 4.2.1 onwards + * - Should run on PHP version 4.2.0 (required for PEAR_Errorstack or higher, + * tested only from 4.2.1 onwards * * Thanks to: * Bjoern Schotte, Kristian Koehntopp, Antonio Guerra @@ -102,10 +116,10 @@ * @author Lukas Smith <smith@backendmedia.com> * @author Pierre-Alain Joye <pajoye@php.net> * @author Arnaud Limbourg <arnaud@php.net> - * @version $Id: LiveUser.php,v 1.100 2004/05/22 10:55:56 arnaud Exp $ + * @version $Id: LiveUser.php,v 1.101 2004/06/07 15:33:15 arnaud Exp $ * @package LiveUser */ -class LiveUser extends PEAR +class LiveUser { /** * LiveUser options set in the configuration file. @@ -201,64 +215,86 @@ * * @access private * @var string - * @see LIVEUSER_STATUS constants + * @see LIVEUSER_STATUS_* constants */ var $status = null; /** - * Whether an error was raised + * Error stack * * @access private - * @var boolean + * @var PEAR_ErrorStack */ - var $_error = false; + var $_stack = null; /** - * Constructor + * PEAR::Log object + * used for error logging + * + * @access private + * @var Log */ - function LiveUser() - { - $this->PEAR(); - } + var $_log = null; /** - * This method is used to communicate an error and invoke error - * callbacks etc. Basically a wrapper for PEAR::raiseError - * without the message string. - * - * @param mixed integer error code, or a PEAR error object (all - * other parameters are ignored if this parameter is - * an object + * Handle used to login * - * @param int error mode, see PEAR_Error docs + * @access private + * @var string + */ + var $_handle = ''; + + /** + * Password used to login * - * @param mixed If error mode is PEAR_ERROR_TRIGGER, this is the - * error level (E_USER_NOTICE etc). If error mode is - * PEAR_ERROR_CALLBACK, this is the callback function, - * either as a function name, or as an array of an - * object and method name. For other error modes this - * parameter is ignored. + * @access private + * @var string + */ + var $_passwd = ''; + + /** + * RememberMe cookie set ? * - * @param string Extra debug information. Defaults to the last - * query and native error code. + * @access private + * @var boolean + */ + var $_remember = false; + + /** + * Error codes to message mapping array * - * @return object a PEAR error object + * @access private + * @var array + */ + var $_errorMessages = array( + LIVEUSER_ERROR => 'Unknown error', + LIVEUSER_ERROR_NOT_SUPPORTED => 'Feature not supported by the container', + LIVEUSER_ERROR_CONFIG => 'There is an errror in the configuration parameters', + LIVEUSER_ERROR_MISSING_DEPS => 'Missing package depedencies: %msg%', + LIVEUSER_ERROR_MISSING_LOGINFUNCTION => 'Login function not found', + LIVEUSER_ERROR_MISSING_LOGOUTFUNCTION => 'Logout function not found', + LIVEUSER_ERROR_COOKIE => 'Remember Me cookie error', + LIVEUSER_ERROR_MISSING_FILE => 'A file is missing: %file%' + ); + + /** + * Constructor * - * @see PEAR_Error + * @acess protected + * @return void */ - function &raiseError($code = null, $mode = null, $options = null, - $userinfo = null) + function LiveUser() { - // The error is yet a LiveUser error object - if (is_object($code)) { - return PEAR::raiseError($code, null, null, null, null, null, true); - } + $this->_stack = &PEAR_ErrorStack::singleton('LiveUser'); + $this->_log = &Log::factory('composite'); + $this->_stack->setLogger($this->_log); - if (empty($code)) { - $code = LIVEUSER_ERROR; + if ($GLOBALS['_LIVEUSER_DEBUG']) { + $this->_log->addChild(Log::factory('win')); } - $msg = LiveUser::errorMessage($code); - return PEAR::raiseError("LiveUser Error: $msg", $code, $mode, $options, $userinfo); + + $this->_stack->setErrorMessageTemplate($this->_errorMessages); + PEAR::pushErrorHandling(PEAR_ERROR_CALLBACK, array(&$this, 'errorCallback')); } /** @@ -298,7 +334,8 @@ * 'lifetime' => 'Cookie lifetime in days', * 'path' => 'Cookie path', * 'domain' => 'Cookie domain', - * 'secret' => 'Secret key used for cookie value encryption' + * 'secret' => 'Secret key used for cookie value encryption', + * 'savedir' => '/absolute/path/to/writeable/directory' // No / at the end ! * ), * 'authContainers' => array( * 'name' => array( @@ -310,20 +347,36 @@ * 'idleTime' => 1800, * 'allowDuplicateHandles' => 0, * 'authTable' => 'liveuser_users', - * 'authTableCols' => array('user_id' => 'auth_user_id', - * 'handle' => 'handle', - * 'passwd' => 'passwd', - * 'lastlogin' => 'lastlogin' - * 'owner_user_id' => 'owner_user_id' - * 'owner_group_id' => 'owner_group_id' - * ) + * 'authTableCols' => array( + * 'required' => array( + * 'auth_user_id' => array('type' => '', 'name' => 'user_id'), + * 'handle' => array('type' => '', 'name' => 'handle'), + * 'passwd' => array('type' => '', 'name' => 'passwd') + * ), + * 'optional' => array( + * 'lastlogin' => array('type' => '', 'name' => 'lastlogin'), + * 'is_active' => array('type' => '', 'name' => 'is_active') + * ), + * 'custom' => array( + * 'myfield1' => array('type' => 'string', 'name' => 'myfield1') + * ) * ) * ), * 'permContainer' => array( * 'type' => 'DB_Complex', * 'connection' => 'db connection object, use this or dsn', * 'dsn' => 'database dsn, use this or connection', - * 'prefix' => 'liveuser_' + * 'prefix' => 'liveuser_', + * 'groupTableCols' => array( + * 'required' => array( + * 'group_id' => array('type' => '', 'name' => 'group_id') + * ), + * 'optional' => array( + * 'group_type' => array('type' => '', 'name' => 'group_type') + * ), + * 'custom' => array( + * 'myaliasforfield1' => array('type' => 'string', 'name' => 'myfield1') + * ) * ) * * </code> @@ -350,27 +403,36 @@ if (!empty($conf)) { $init = $obj->_readConfig($conf, $confName); - if (LiveUser::isError($init)) { - $this->_error = $init; - return $init; - } } - if (!PEAR::isError($obj) && - isset($obj->_options['autoInit']) && + if (isset($obj->_options['autoInit']) && $obj->_options['autoInit'] ) { $init = $obj->init($handle, $passwd, $logout); - if (LiveUser::isError($init)) { - $this->_error = $init; - return $init; - } } return $obj; } /** + * PEAR_Error callback. + * + * It will catch all PEAR_Errors and repackage + * them as LiveUser errors. + * + * @access private + * @param PEAR_Error a reference to a PEAR_Error object + * @return void + */ + function errorCallback(&$error) + { + $this->_stack->push( + LIVEUSER_ERROR, 'error', array('PEAR_Error' => $error), + 'An error occured in a dependant package', false , $error->getBacktrace() + ); + } + + /** * Makes your instance global. * * <b>You MUST call this method with the $var = &LiveUser::singleton() syntax. @@ -396,9 +458,6 @@ $signature = serialize(array($handle, $passwd, $confName)); if (!isset($instances[$signature])) { $obj = &LiveUser::factory($conf, $handle, $passwd, $logout, $remember, $confName); - if(PEAR::isError($obj)) { - return $obj; - } $instances[$signature] =& $obj; } @@ -406,7 +465,7 @@ } /** - * creates an instance of an auth object + * Creates an instance of an auth object * * @access public * @param mixed Name of array containing the configuration. @@ -418,9 +477,8 @@ $filename = 'LiveUser/Auth/Container/' . $conf['type'] . '.php'; @include_once($filename); if (!class_exists($classname)) { - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_NOT_SUPPORTED, null, null, - 'Missing file: ' . $filename, 'LiveUser'); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_NOT_SUPPORTED, 'exception', array('file' => $filename)); + return false; } $auth = &new $classname($conf); return $auth; @@ -439,9 +497,8 @@ $filename = 'LiveUser/Perm/Container/' . $conf['type'] . '.php'; @include_once($filename); if (!class_exists($classname)) { - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_NOT_SUPPORTED, null, null, - 'Missing file: ' . $filename); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_NOT_SUPPORTED, 'exception', array('file' => $filename)); + return false; } $perm = &new $classname($conf); return $perm; @@ -514,16 +571,36 @@ } if (!@include_once($conf)) { - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_CONFIG, null, null, - "Could not read the configuration file in LiveUser::readConfig()"); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Could not read the configuration file'); } if (isset(${$confName}) && is_array(${$confName})) { return $this->_readConfig(${$confName}, $confName); } - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_CONFIG, null, null, - "Configuration array not found in LiveUser::readConfig()"); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Configuration array not found in LiveUser::readConfig()'); + } + + /** + * Add error logger for use by Errorstack. + * + * Be aware that if you need add a log + * at the beginning of your code if you + * want it to be effective. A log will only + * be taken into account after it's added. + * + * Sample usage: + * <code> + * $lu_object = &LiveUser::singleton($conf); + * $logger = &Log::factory('mail', 'bug@example.com', + * 'myapp_debug_mail_log', array('from' => 'application_bug@example.com')); + * $lu_object->addErrorLog($logger); + * </code> + * + * @param Log logger instance + * @return boolean true on success + */ + function addErrorLog(&$log) + { + return $this->_log->addChild($log); } /** @@ -552,9 +629,7 @@ $filename = 'LiveUser/Admin/Auth/Container/' . $container['name'] . '.php'; @include_once($filename); if (!class_exists($classname)) { - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_NOT_SUPPORTED, null, null, - 'Missing file: '.$filename); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Missing file: ' . $filename); } $con = &new $classname($container, array('type' => $container['type'])); } @@ -562,51 +637,12 @@ } /** - * Crypts data using mcrypt or userland if not available - * - * @access private - * @param boolean true to crypt, false to decrypt - * @param string data to crypt - * @return string crypted data - */ - function _cookieCryptMode($crypt, $data) - { - if (function_exists('mcrypt_module_open')) { - $td = mcrypt_module_open('tripledes', '', 'ecb', ''); - $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size ($td), MCRYPT_RAND); - mcrypt_generic_init($td, $this->_options['cookie']['secret'], $iv); - if ($crypt) { - $data = mcrypt_generic($td, $data); - } else { - $data = mdecrypt_generic($td, $data); - } - mcrypt_generic_deinit($td); - mcrypt_module_close($td); - } else { - @include_once 'Crypt/Rc4.php'; - if (!class_exists('Crypt_RC4')) { - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_NOT_SUPPORTED, null, null, - 'Please install Crypt_RC4 to use this feature'); - return $this->_error; - } - $rc4 =& new Crypt_RC4($this->_options['cookie']['secret']); - if ($crypt) { - $rc4->crypt($data); - } else { - $rc4->decrypt($data); - } - } - - return $data; - } - - /** * Sets an option. * * @access public - * @param string option name - * @param mixed value for the option - * @return mixed true or PEAR Error + * @param string option name + * @param mixed value for the option + * @return boolean true * @see LiveUser::_options */ function setOption($option, $value) @@ -615,9 +651,7 @@ $this->options[$option] = $value; return true; } - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_CONFIG, null, null, - "unknown option $option"); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), "unknown option $option"); } /** @@ -632,9 +666,7 @@ if (isset($this->options[$option])) { return $this->options[$option]; } - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_CONFIG, null, null, - "unknown option $option"); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), "unknown option $option"); } /** @@ -648,9 +680,7 @@ if (is_object($this->_auth)) { return $this->_auth; } - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_CONFIG, null, null, - 'authentication container has not yet been instantianted'); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'authentication container has not yet been instantianted'); } /** @@ -664,9 +694,7 @@ if (is_object($this->_perm)) { return $this->_perm; } - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_CONFIG, null, null, - "permission container has not yet been instantianted"); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'permission container has not yet been instantianted'); } /** @@ -681,15 +709,11 @@ * @param string password of the user trying to authenticate * @param boolean set to true if user wants to logout * @param boolean set if remember me is set - * @return mixed true if init process well, false if something - * went wrong or an error object. + * @return boolean true if init process well, false if something + * went wrong. */ function init($handle = '', $passwd = '', $logout = false, $remember = false) { - if ($this->_error) { - return $this->_error; - } - // set session save handler if needed if ($this->_options['session_save_handler'] == true) { session_set_save_handler( @@ -713,11 +737,6 @@ if (!$this->unfreeze()) { if (is_null($this->_auth)) { $lulogin = $this->tryLogin($handle, $passwd); - if (PEAR::isError($lulogin)) { - $this->_error = $lulogin; - $this->status = LIVEUSER_STATUS_PERMINITERROR; - return $lulogin; - } } } else { $lulogin = true; @@ -765,10 +784,98 @@ if (is_null($this->status)) { $this->status = LIVEUSER_STATUS_UNKNOWN; } + return false; } /** + * Handles the rememberMe cookie login. + * + * @access private + * @return boolean true on success, false otherwise + */ + function cookieBasedLogin() + { + $cookieData = unserialize(stripslashes($_COOKIE[$this->_options['cookie']['name']])); + if (count($cookieData) != 3) { + // Delete cookie if it's not valid, keeping it messes up the + // authentication process + setcookie($this->_options['cookie']['name'], '', + time() - 3600, + $this->_options['cookie']['path'], + $this->_options['cookie']['domain'] + ); + $this->_stack->push(LIVEUSER_ERROR_COOKIE, 'error', + 'Wrong data in cookie store in LiveUser::cookieBasedLogin()'); + return false; + } + + $serverData = $this->_readStoreCookiePasswdId($cookieData[0]); + + if ($serverData[0] != $cookieData[2]) { + // Delete cookie if it's not valid, keeping it messes up the + // authentication process + setcookie($this->_options['cookie']['name'], '', + time() - 3600, + $this->_options['cookie']['path'], + $this->_options['cookie']['domain']); + $this->_stack->push(LIVEUSER_ERROR_COOKIE, 'error', + 'Passwords hashes do not match in cookie in LiveUser::cookieBasedLogin()'); + return false; + } + + $this->_handle = $cookieData[1]; + $this->_passwd = $serverData[1]; + return true; + } + + /** + * Handles login from GET, POST or REQUEST. + * + * @access private + * @return boolean true on success, false otherwise + */ + function httpvarBasedLogin() + { + switch (strtolower($this->_options['login']['method'])) { + case 'request': + $httpvar = $_REQUEST; + break; + case 'post': + $httpvar = $_POST; + break; + case 'get': + default: + $httpvar = $_GET; + break; + } + + if (empty($httpvar[$this->_options['login']['username']]) || + empty($httpvar[$this->_options['login']['password']])) + { + if (!$this->_options['login']['force'] && + !empty($this->_options['login']['function']) && + is_callable($this->_options['login']['function'])) + { + call_user_func($this->_options['login']['function'], $this); + } + } else { + $this->_handle = $httpvar[$this->_options['login']['username']]; + $this->_passwd = $httpvar[$this->_options['login']['password']]; + if ($this->_options['login']['remember'] && + isset($httpvar[$this->_options['login']['remember']]) + ) { + $this->_remember = $httpvar[$this->_options['login']['remember']]; + } + if (get_magic_quotes_gpc()) { + $this->_handle = stripslashes($this->_handle); + $this->_passwd = stripslashes($this->_passwd); + } + } + return true; + } + + /** * Tries to log the user in by trying all the Auth containers defined * in the configuration file until there is a success or failure. * @@ -780,106 +887,56 @@ */ function tryLogin($handle = '', $passwd = '', $remember = false) { - if ($this->_error) { - return $this->_error; - } - - // handle and password not directly passed + // handle and password not passed directly if (empty($handle)) { if (isset($this->_options['cookie']) && isset($_COOKIE[$this->_options['cookie']['name']])) { - $cookieData = unserialize(stripslashes($_COOKIE[$this->_options['cookie']['name']])); - if (count($cookieData) != 3) { - $this->_error = true; - // Delete cookie if it's not valid, keeping it messes up the - // authentication process - setcookie($this->_options['cookie']['name'], '', - time() - 3600, - $this->_options['cookie']['path'], - $this->_options['cookie']['domain'] - ); - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_COOKIE, null, null, - "Wrong data in cookie store in LiveUser::tryLogin()"); - return $this->_error; - } - - $serverData = $this->_readStoreCookiePasswdId($cookieData[0]); - - if ($serverData[0] != $cookieData[2]) { - $this->_error = true; - // Delete cookie if it's not valid, keeping it messes up the - // authentication process - setcookie($this->_options['cookie']['name'], '', - time() - 3600, - $this->_options['cookie']['path'], - $this->_options['cookie']['domain']); - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_COOKIE, null, null, - "Passwords hashes do not match in cookie in LiveUser::tryLogin()"); - return $this->_error; - } - - $handle = $cookieData[1]; - $passwd = $serverData[1]; - // $remember + $res = $this->cookieBasedLogin(); } elseif ($this->_options['login']['method']) { - switch (strtolower($this->_options['login']['method'])) { - case 'request': - $httpvar = $_REQUEST; - break; - case 'post': - $httpvar = $_POST; - break; - case 'get': - default: - $httpvar = $_GET; - break; - } + $res = $this->httpvarBasedLogin(); + } - if (empty($httpvar[$this->_options['login']['username']]) || - empty($httpvar[$this->_options['login']['password']])) - { - if (!$this->_options['login']['force'] && - !empty($this->_options['login']['function']) && - is_callable($this->_options['login']['function'])) - { - call_user_func($this->_options['login']['function'], $this); - } - } else { - $handle = $httpvar[$this->_options['login']['username']]; - $passwd = $httpvar[$this->_options['login']['password']]; - if ($this->_options['login']['remember'] && - isset($httpvar[$this->_options['login']['remember']]) - ) { - $remember = $httpvar[$this->_options['login']['remember']]; - } - if (get_magic_quotes_gpc()) { - $handle = stripslashes($handle); - $passwd = stripslashes($passwd); - } - } + if (!$res) { + $this->_stack->push(LIVEUSER_ERROR, 'error', array(), 'cannot find credentials'); + return false; } + } else { + $this->_handle = $handle; + $this->_passwd = $passwd; + $this->_remember = $remember; } - if (empty($handle)) { + if (empty($this->_handle)) { return false; } + $userfound = $this->instantiateAuthContainer(); + + if (!$userfound || !is_object($this->_auth) || !$this->_auth->loggedIn) { + $this->status = LIVEUSER_STATUS_AUTHFAILED; + } + return true; + } + + /** + * Instantiate the auth containers + * + * @acess private + * @return boolean true if success + */ + function instantiateAuthContainer() + { $counter = 0; $userFound = false; $backends = array_keys($this->authContainers); $backend_cnt = count($backends); - + //loop into auth containers while ($userFound == false && $backend_cnt > $counter) { $auth = &$this->authFactory($this->authContainers[$backends[$counter]]); - if (LiveUser::isError($auth)) { - $this->_error = $auth; - $this->status = LIVEUSER_STATUS_AUTHINITERROR; - return $auth; - } - - $auth->login($handle, $passwd, true); + + $auth->login($this->_handle, $this->_passwd, true); if ($auth->loggedIn) { if ($this->_options['login']['regenid'] == true) { @session_regenerate_id(); @@ -888,31 +945,22 @@ $userFound = true; $this->_auth = $auth; $this->_auth->backendArrayIndex = $backends[$counter]; - + // Create permission object if (is_array($this->permContainer)) { $this->_perm =& $this->permFactory($this->permContainer); $res = $this->_perm->init($this->_auth->authUserId); - - if (PEAR::isError($res)) { - $this->_error = $res; - $this->status = LIVEUSER_STATUS_PERMINITERROR; - return $res; - } } - + $this->freeze(); - $this->setRememberCookie($handle, $passwd, $remember); + $this->setRememberCookie($this->_handle, $this->_passwd, $this->_remember); } elseif ($auth->isActive === false) { $this->status = LIVEUSER_STATUS_ISINACTIVE; $userFound = true; } $counter++; } - if (!is_object($this->_auth) || !$this->_auth->loggedIn) { - $this->status = LIVEUSER_STATUS_AUTHFAILED; - } - return true; + return $userFound; } /** @@ -924,10 +972,6 @@ */ function unfreeze() { - if ($this->_error) { - return $this->_error; - } - if (isset($_SESSION[$this->_options['session']['varname']]['auth']) && is_array($_SESSION[$this->_options['session']['varname']]['auth']) && isset($_SESSION[$this->_options['session']['varname']]['auth_name']) @@ -961,10 +1005,6 @@ */ function freeze() { - if ($this->_error) { - return $this->_error; - } - if (is_object($this->_auth) && $this->_auth->loggedIn) { // Bind objects to session $_SESSION[$this->_options['session']['varname']] = array(); @@ -979,10 +1019,7 @@ } return true; } - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR, null, null, - "No data available to store inside session in LiveUser::freeze()"); - return $this->_error; - + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'No data available to store inside session'); } /** @@ -993,10 +1030,6 @@ */ function disconnect() { - if ($this->_error) { - return $this->_error; - } - if (is_object($this->_auth) && $this->_auth->loggedIn) { $this->_auth->disconnect(); $this->_auth = null; @@ -1006,10 +1039,7 @@ } return true; } - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR, null, null, - "No connection to disconnect in LiveUser::disconnect()"); - return $this->_error; - + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'No connection to disconnect in LiveUser::disconnect()'); } /** @@ -1025,10 +1055,6 @@ */ function setRememberCookie($handle, $passwd, $remember) { - if ($this->_error) { - return $this->_error; - } - switch (strtolower($this->_options['login']['method'])) { case 'request': $httpvar = $_REQUEST; @@ -1049,9 +1075,7 @@ $store_id = md5($handle . $passwd); if (!$passwd_id = $this->_storeCookiePasswdId($passwd, $store_id)) { - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_COOKIE, null, null, - "Cannot store cookie data in LiveUser::setRememberCookie()"); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Cannot store cookie data'); } $setcookie = setcookie( @@ -1062,15 +1086,133 @@ $this->_options['cookie']['domain']); if (!$setcookie) { - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR, null, null, - "Unable to set cookie in LiveUser::setRememberCookie()"); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Unable to set cookie'); } } return true; } /** + * Crypts data using mcrypt or userland if not available + * + * @access private + * @param boolean true to crypt, false to decrypt + * @param string data to crypt + * @return string crypted data + */ + function _cookieCryptMode($crypt, $data) + { + if (function_exists('mcrypt_module_open')) { + $td = mcrypt_module_open('tripledes', '', 'ecb', ''); + $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size ($td), MCRYPT_RAND); + mcrypt_generic_init($td, $this->_options['cookie']['secret'], $iv); + if ($crypt) { + $data = mcrypt_generic($td, $data); + } else { + $data = mdecrypt_generic($td, $data); + } + mcrypt_generic_deinit($td); + mcrypt_module_close($td); + } else { + @include_once 'Crypt/Rc4.php'; + if (!class_exists('Crypt_RC4')) { + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Please install Crypt_RC4 to use this feature'); + } + $rc4 =& new Crypt_RC4($this->_options['cookie']['secret']); + if ($crypt) { + $rc4->crypt($data); + } else { + $rc4->decrypt($data); + } + } + + return $data; + } + + /** + * A "store" on the server contains the password and the + * cookie id in an encrypted form. + * + * This method generates a md5 from given password and writes it + * into the "store" along with crypted password. + * + * Cookies are not secure but keeping the password in plain text + * in the cookie is not the best way to go. Since some + * containers like LDAP need the clear text password, we store + * an encrypted version of the password using Crypt_Rc4 which + * provides a simple two-way mechanism. + * + * To do this LiveUser needs access to a writeable directory. + * If you do no have access to the ini_get() function please + * set a constant named LIVEUSER_TMPDIR with an absolute + * path to a writeable directory. + * + * @access private + * @param string the password to store + * @param string file name used as storage + * @return boolean true if success, false otherwise + */ + function _storeCookiePasswdId($passwd, $store) + { + $dir = $this->_options['cookie']['savedir']; + + if (!is_writable($dir . "/$store.lu")) { + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Cannot create file, please check path and permissions'); + } + + if (!$fh = @fopen($dir . "/$store.lu", 'wb')) { + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Cannot open file for writting'); + } + + $data = serialize(array(md5($passwd), $passwd)); + + $crypted_data = $this->_cookieCryptMode(true, $data); + + if (!fwrite($fh, $crypted_data)) { + fclose($fh); + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Cannot save cookie data'); + } + + fclose($fh); + + return true; + } + + /** + * A "store" on the server contains the password and the + * cookie id in an encrypted form. + * + * This method reads the data contained in it. + * + * @access private + * @param string the filename of the store + * @return mixed an array of the data, false otherwise + */ + function _readStoreCookiePasswdId($store) + { + $dir = $this->_options['cookie']['savedir']; + + if (!$fh = @fopen($dir . "/$store.lu", 'rb')) { + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Cannot open file for reading'); + } + + if (!$fields = fread($fh, 4096)) { + fclose($fh); + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Cannot read file'); + } + + fclose($fh); + + $params = unserialize($this->_cookieCryptMode(false, $fields)); + + if (count($params) != 2) { + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Incorrect array structure'); + } + + return $params; + } + + /** * If the logout parameter has been set via GET/POST or the user is * idled/expired, this destroys the session object. * @@ -1083,10 +1225,6 @@ */ function processLogout($logout, $handle = '', $passwd = '') { - if ($this->_error) { - return $this->_error; - } - // logout user if the user is logged in // and new creditionals have been passed to LiveUser if (!$logout && $this->_options['logout']['method']) { @@ -1159,10 +1297,6 @@ */ function logout($direct = true) { - if ($this->_error) { - return $this->_error; - } - if ($this->status != LIVEUSER_STATUS_IDLED || $this->status != LIVEUSER_STATUS_EXPIRED ) { @@ -1235,10 +1369,6 @@ */ function checkRight($rights) { - if ($this->_error) { - return $this->_error; - } - if ($rights === false) { return LIVEUSER_MAX_LEVEL; } @@ -1281,10 +1411,6 @@ */ function checkRightLevel($rights, $owner_user_id, $owner_group_id) { - if ($this->_error) { - return $this->_error; - } - if ($rights === false) { return LIVEUSER_MAX_LEVEL; } @@ -1306,9 +1432,6 @@ */ function isLoggedIn() { - if ($this->_error) { - return $this->_error; - } if (!is_object($this->_auth)) { return false; } @@ -1322,18 +1445,14 @@ * * Use this to check if this was the reason * why a user was not able to login. - * true == Not active - * false == active + * true == user account is NOT active + * false == user account is active * * @access public * @return boolean */ function isInactive() { - if ($this->_error) { - return $this->_error; - } - return $this->status == LIVEUSER_STATUS_ISINACTIVE; } @@ -1347,22 +1466,16 @@ * * @access public * @param string The name of the function to be called. - * @return mixed void or PEAR_Error + * @return void */ function setLoginFunction($functionName) { - if ($this->_error) { - return $this->_error; - } - if (!empty($functionName) && is_callable($functionName)) { $this->_options['login']['function'] = $functionName; return true; } - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_MISSING_LOGINFUNCTION, null, null, - "Login function not found in LiveUser::setLoginFunction()"); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Login function not found'); } /** @@ -1377,23 +1490,17 @@ * * @access public * @param string The name of the function to be called. - * @return mixed void or a PEAR_Error + * @return void * @see LiveUser::_options */ function setLogoutFunction($functionName) { - if ($this->_error) { - return $this->_error; - } - if (!empty($functionName) && is_callable($functionName)) { $this->_options['logout']['function'] = $functionName; return true; } - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_MISSING_LOGOUTFUNCTION, null, null, - "Logout function not found in LiveUser::setLogoutFunction()"); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Logout function not found'); } /** @@ -1407,9 +1514,6 @@ */ function getProperty($what, $container = 'auth') { - if ($this->_error) { - return $this->_error; - } $that = null; if ($container == 'auth' && $this->_auth && $this->_auth->getProperty($what) !== null) { $that = $this->_auth ? $this->_auth->getProperty($what) : null; @@ -1431,149 +1535,6 @@ } /** - * A "store" on the server contains the password and the - * cookie id in an encrypted form. - * - * This method generates a md5 from given - * password and writes it into the "store" along - * with crypted password. - * - * Cookies are not secure but keeping - * the password in plain text in the cookie - * is not the best way to go. Since some - * containers like LDAP need the clear text - * password, we store an encrypted version - * of the password using Crypt_Rc4 which provides - * a simple two-way mechanism. - * - * To do this LiveUser needs access - * to a writeable directory. If you do - * no have access to the ini_get() - * function please set a constant - * named LIVEUSER_TMPDIR with an absolute - * path to a writeable directory. - * - * @access private - * @param string the password to store - * @param string file name used as storage - * @return boolean true if success, false otherwise - */ - function _storeCookiePasswdId($passwd, $store) - { - if (!defined('LIVEUSER_TMPDIR')) { - define('LIVEUSER_TMPDIR', ini_get('session.save_path')); - } - - if (!$fh = fopen(LIVEUSER_TMPDIR . "/$store.lu", 'wb')) { - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_COOKIE, null, null, - "Cannot open file for writting in LiveUser::_storeCookiePasswdId()"); - return $this->_error; - } - - $data = serialize(array(md5($passwd), $passwd)); - - $crypted_data = $this->_cookieCryptMode(true, $data); - - if (!fwrite($fh, $crypted_data)) { - fclose($fh); - $this->_error = LiveUser::raiseError(LIVEUSER_COOKIE_ERROR, null, null, - "Cannot save cookie data in LiveUser::_storeCookiePasswdId()"); - return $this->_error; - } - - fclose($fh); - - return true; - } - - /** - * A "store" on the server contains the password and the - * cookie id in an encrypted form. - * - * This method reads the data contained in it. - * - * @access private - * @param string the filename of the store - * @return mixed an array of the data, false otherwise - */ - function _readStoreCookiePasswdId($store) - { - if (!defined('LIVEUSER_TMPDIR')) { - define('LIVEUSER_TMPDIR', ini_get('session.save_path')); - } - - if (!$fh = fopen(LIVEUSER_TMPDIR . "/$store.lu", 'rb')) { - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_COOKIE, null, null, - "Cannot open file for reading in LiveUser::_readStoreCookiePasswdId()"); - return $this->_error; - } - - if (!$fields = fread($fh, 4096)) { - fclose($fh); - $this->_error = LiveUser::raiseError(LIVEUSER_COOKIE_ERROR, null, null, - "Cannot read file in LiveUser::_readStoreCookiePasswdId()"); - return $this->_error; - } - - fclose($fh); - - $params = unserialize($this->_cookieCryptMode(false, $fields)); - - if (count($params) != 2) { - $this->_error = LiveUser::raiseError(LIVEUSER_COOKIE_ERROR, null, null, - "Incorrect array structure in LiveUser::_readStoreCookiePasswdId()"); - } - - return $params; - } - - /** - * Tell whether a result from a LiveUser method is an error. - * - * @access public - * @param mixed result code - * @return bool whether value is an error - */ - function isError($value) - { - return PEAR::isError($value); - } // end func isError - - /** - * Return a textual error message for a LiveUser error code. - * - * @access public - * @param mixed error code or error object - * @return string error message - */ - function errorMessage($value) - { - // make the variable static so that it only has to do the defining on the first call - static $errorMessages; - - // define the varies error messages - if (!isset($errorMessages)) { - $errorMessages = array( - LIVEUSER_ERROR => 'Unknown error', - LIVEUSER_ERROR_NOT_SUPPORTED => 'Feature not supported', - LIVEUSER_ERROR_CONFIG => 'Config file error', - LIVEUSER_ERROR_MISSING_DEPS => 'Missing package depedencies', - LIVEUSER_ERROR_MISSING_LOGINFUNCTION => 'Login function not found', - LIVEUSER_ERROR_MISSING_LOGOUTFUNCTION => 'Logout function not found', - LIVEUSER_ERROR_COOKIE => 'Remember Me cookie error', - ); - } - - // If this is an error object, then grab the corresponding error code - if (LiveUser::isError($value)) { - $value = $value->getCode(); - } - - // return the textual error message corresponding to the code - return isset($errorMessages[$value]) ? $errorMessages[$value] : $errorMessages[LIVEUSER_ERROR]; - } // end func errorMessage - - /** * Return a textual status message for a LiveUser status code. * * @access public @@ -1588,15 +1549,15 @@ // define the varies error messages if (!isset($statusMessages)) { $statusMessages = array( - LIVEUSER_STATUS_IDLED => 'Maximum idle time is reached', - LIVEUSER_STATUS_EXPIRED => 'User session has expired', - LIVEUSER_STATUS_ISINACTIVE => 'User is set to inactive', - LIVEUSER_STATUS_PERMINITERROR => 'Cannot instantiate permission container', - LIVEUSER_STATUS_AUTHINITERROR => 'Cannot instantiate authentication configuration', - LIVEUSER_STATUS_AUTHNOTFOUND => 'Cannot retrieve Auth object from session', - LIVEUSER_STATUS_UNKNOWN => 'Something went wrong in whatever you were trying to do', - LIVEUSER_STATUS_LOGGEDOUT => 'User was logged out correctly', - LIVEUSER_STATUS_AUTHFAILED => 'No auth container authenticated successfully', + LIVEUSER_STATUS_IDLED => 'Maximum idle time is reached', + LIVEUSER_STATUS_EXPIRED => 'User session has expired', + LIVEUSER_STATUS_ISINACTIVE => 'User is set to inactive', + LIVEUSER_STATUS_PERMINITERROR => 'Cannot instantiate permission container', + LIVEUSER_STATUS_AUTHINITERROR => 'Cannot instantiate authentication configuration', + LIVEUSER_STATUS_AUTHNOTFOUND => 'Cannot retrieve Auth object from session', + LIVEUSER_STATUS_UNKNOWN => 'Something went wrong in whatever you were trying to do', + LIVEUSER_STATUS_LOGGEDOUT => 'User was logged out correctly', + LIVEUSER_STATUS_AUTHFAILED => 'No auth container authenticated successfully', ); }
http://cvs.php.net/diff.php/pear/Perm_LiveUser/LiveUser.php?r1=1.100&r2=1.101&ty=u Index: pear/Perm_LiveUser/LiveUser.php diff -u pear/Perm_LiveUser/LiveUser.php:1.100 pear/Perm_LiveUser/LiveUser.php:1.101 --- pear/Perm_LiveUser/LiveUser.php:1.100 Sat May 22 06:55:56 2004 +++ pear/Perm_LiveUser/LiveUser.php Mon Jun 7 11:33:15 2004 @@ -32,7 +32,8 @@ /** * Include PEAR base class */ -require_once 'PEAR.php'; +require_once 'PEAR/ErrorStack.php'; + /**#@+ * Error related constants definition @@ -46,6 +47,7 @@ define('LIVEUSER_ERROR_MISSING_LOGINFUNCTION', -5); define('LIVEUSER_ERROR_MISSING_LOGOUTFUNCTION', -6); define('LIVEUSER_ERROR_COOKIE', -7); +define('LIVEUSER_ERROR_MISSING_FILE', -8); /**#@-*/ /**#@+ @@ -72,6 +74,17 @@ define('LIVEUSER_MAX_LEVEL', 3); /** + * Debug global. When set to true the + * error stack will be printed to + * a separate window using the Win implementation + * of PEAR::Log (of which PEAR::ErrorStack has builtin + * knowledge). + * + * @var boolean + */ +$GLOBALS['_LIVEUSER_DEBUG'] = false; + +/** * Class LiveUser - Login handling class * * Description: @@ -92,7 +105,8 @@ * it will try to send HTTP headers such as cookies and redirects. * * Requirements: - * - Should run on PHP version 4.1.0 or higher, tested only from 4.2.1 onwards + * - Should run on PHP version 4.2.0 (required for PEAR_Errorstack or higher, + * tested only from 4.2.1 onwards * * Thanks to: * Bjoern Schotte, Kristian Koehntopp, Antonio Guerra @@ -102,10 +116,10 @@ * @author Lukas Smith <smith@backendmedia.com> * @author Pierre-Alain Joye <pajoye@php.net> * @author Arnaud Limbourg <arnaud@php.net> - * @version $Id: LiveUser.php,v 1.100 2004/05/22 10:55:56 arnaud Exp $ + * @version $Id: LiveUser.php,v 1.101 2004/06/07 15:33:15 arnaud Exp $ * @package LiveUser */ -class LiveUser extends PEAR +class LiveUser { /** * LiveUser options set in the configuration file. @@ -201,64 +215,86 @@ * * @access private * @var string - * @see LIVEUSER_STATUS constants + * @see LIVEUSER_STATUS_* constants */ var $status = null; /** - * Whether an error was raised + * Error stack * * @access private - * @var boolean + * @var PEAR_ErrorStack */ - var $_error = false; + var $_stack = null; /** - * Constructor + * PEAR::Log object + * used for error logging + * + * @access private + * @var Log */ - function LiveUser() - { - $this->PEAR(); - } + var $_log = null; /** - * This method is used to communicate an error and invoke error - * callbacks etc. Basically a wrapper for PEAR::raiseError - * without the message string. - * - * @param mixed integer error code, or a PEAR error object (all - * other parameters are ignored if this parameter is - * an object + * Handle used to login * - * @param int error mode, see PEAR_Error docs + * @access private + * @var string + */ + var $_handle = ''; + + /** + * Password used to login * - * @param mixed If error mode is PEAR_ERROR_TRIGGER, this is the - * error level (E_USER_NOTICE etc). If error mode is - * PEAR_ERROR_CALLBACK, this is the callback function, - * either as a function name, or as an array of an - * object and method name. For other error modes this - * parameter is ignored. + * @access private + * @var string + */ + var $_passwd = ''; + + /** + * RememberMe cookie set ? * - * @param string Extra debug information. Defaults to the last - * query and native error code. + * @access private + * @var boolean + */ + var $_remember = false; + + /** + * Error codes to message mapping array * - * @return object a PEAR error object + * @access private + * @var array + */ + var $_errorMessages = array( + LIVEUSER_ERROR => 'Unknown error', + LIVEUSER_ERROR_NOT_SUPPORTED => 'Feature not supported by the container', + LIVEUSER_ERROR_CONFIG => 'There is an errror in the configuration parameters', + LIVEUSER_ERROR_MISSING_DEPS => 'Missing package depedencies: %msg%', + LIVEUSER_ERROR_MISSING_LOGINFUNCTION => 'Login function not found', + LIVEUSER_ERROR_MISSING_LOGOUTFUNCTION => 'Logout function not found', + LIVEUSER_ERROR_COOKIE => 'Remember Me cookie error', + LIVEUSER_ERROR_MISSING_FILE => 'A file is missing: %file%' + ); + + /** + * Constructor * - * @see PEAR_Error + * @acess protected + * @return void */ - function &raiseError($code = null, $mode = null, $options = null, - $userinfo = null) + function LiveUser() { - // The error is yet a LiveUser error object - if (is_object($code)) { - return PEAR::raiseError($code, null, null, null, null, null, true); - } + $this->_stack = &PEAR_ErrorStack::singleton('LiveUser'); + $this->_log = &Log::factory('composite'); + $this->_stack->setLogger($this->_log); - if (empty($code)) { - $code = LIVEUSER_ERROR; + if ($GLOBALS['_LIVEUSER_DEBUG']) { + $this->_log->addChild(Log::factory('win')); } - $msg = LiveUser::errorMessage($code); - return PEAR::raiseError("LiveUser Error: $msg", $code, $mode, $options, $userinfo); + + $this->_stack->setErrorMessageTemplate($this->_errorMessages); + PEAR::pushErrorHandling(PEAR_ERROR_CALLBACK, array(&$this, 'errorCallback')); } /** @@ -298,7 +334,8 @@ * 'lifetime' => 'Cookie lifetime in days', * 'path' => 'Cookie path', * 'domain' => 'Cookie domain', - * 'secret' => 'Secret key used for cookie value encryption' + * 'secret' => 'Secret key used for cookie value encryption', + * 'savedir' => '/absolute/path/to/writeable/directory' // No / at the end ! * ), * 'authContainers' => array( * 'name' => array( @@ -310,20 +347,36 @@ * 'idleTime' => 1800, * 'allowDuplicateHandles' => 0, * 'authTable' => 'liveuser_users', - * 'authTableCols' => array('user_id' => 'auth_user_id', - * 'handle' => 'handle', - * 'passwd' => 'passwd', - * 'lastlogin' => 'lastlogin' - * 'owner_user_id' => 'owner_user_id' - * 'owner_group_id' => 'owner_group_id' - * ) + * 'authTableCols' => array( + * 'required' => array( + * 'auth_user_id' => array('type' => '', 'name' => 'user_id'), + * 'handle' => array('type' => '', 'name' => 'handle'), + * 'passwd' => array('type' => '', 'name' => 'passwd') + * ), + * 'optional' => array( + * 'lastlogin' => array('type' => '', 'name' => 'lastlogin'), + * 'is_active' => array('type' => '', 'name' => 'is_active') + * ), + * 'custom' => array( + * 'myfield1' => array('type' => 'string', 'name' => 'myfield1') + * ) * ) * ), * 'permContainer' => array( * 'type' => 'DB_Complex', * 'connection' => 'db connection object, use this or dsn', * 'dsn' => 'database dsn, use this or connection', - * 'prefix' => 'liveuser_' + * 'prefix' => 'liveuser_', + * 'groupTableCols' => array( + * 'required' => array( + * 'group_id' => array('type' => '', 'name' => 'group_id') + * ), + * 'optional' => array( + * 'group_type' => array('type' => '', 'name' => 'group_type') + * ), + * 'custom' => array( + * 'myaliasforfield1' => array('type' => 'string', 'name' => 'myfield1') + * ) * ) * * </code> @@ -350,27 +403,36 @@ if (!empty($conf)) { $init = $obj->_readConfig($conf, $confName); - if (LiveUser::isError($init)) { - $this->_error = $init; - return $init; - } } - if (!PEAR::isError($obj) && - isset($obj->_options['autoInit']) && + if (isset($obj->_options['autoInit']) && $obj->_options['autoInit'] ) { $init = $obj->init($handle, $passwd, $logout); - if (LiveUser::isError($init)) { - $this->_error = $init; - return $init; - } } return $obj; } /** + * PEAR_Error callback. + * + * It will catch all PEAR_Errors and repackage + * them as LiveUser errors. + * + * @access private + * @param PEAR_Error a reference to a PEAR_Error object + * @return void + */ + function errorCallback(&$error) + { + $this->_stack->push( + LIVEUSER_ERROR, 'error', array('PEAR_Error' => $error), + 'An error occured in a dependant package', false , $error->getBacktrace() + ); + } + + /** * Makes your instance global. * * <b>You MUST call this method with the $var = &LiveUser::singleton() syntax. @@ -396,9 +458,6 @@ $signature = serialize(array($handle, $passwd, $confName)); if (!isset($instances[$signature])) { $obj = &LiveUser::factory($conf, $handle, $passwd, $logout, $remember, $confName); - if(PEAR::isError($obj)) { - return $obj; - } $instances[$signature] =& $obj; } @@ -406,7 +465,7 @@ } /** - * creates an instance of an auth object + * Creates an instance of an auth object * * @access public * @param mixed Name of array containing the configuration. @@ -418,9 +477,8 @@ $filename = 'LiveUser/Auth/Container/' . $conf['type'] . '.php'; @include_once($filename); if (!class_exists($classname)) { - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_NOT_SUPPORTED, null, null, - 'Missing file: ' . $filename, 'LiveUser'); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_NOT_SUPPORTED, 'exception', array('file' => $filename)); + return false; } $auth = &new $classname($conf); return $auth; @@ -439,9 +497,8 @@ $filename = 'LiveUser/Perm/Container/' . $conf['type'] . '.php'; @include_once($filename); if (!class_exists($classname)) { - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_NOT_SUPPORTED, null, null, - 'Missing file: ' . $filename); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_NOT_SUPPORTED, 'exception', array('file' => $filename)); + return false; } $perm = &new $classname($conf); return $perm; @@ -514,16 +571,36 @@ } if (!@include_once($conf)) { - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_CONFIG, null, null, - "Could not read the configuration file in LiveUser::readConfig()"); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Could not read the configuration file'); } if (isset(${$confName}) && is_array(${$confName})) { return $this->_readConfig(${$confName}, $confName); } - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_CONFIG, null, null, - "Configuration array not found in LiveUser::readConfig()"); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Configuration array not found in LiveUser::readConfig()'); + } + + /** + * Add error logger for use by Errorstack. + * + * Be aware that if you need add a log + * at the beginning of your code if you + * want it to be effective. A log will only + * be taken into account after it's added. + * + * Sample usage: + * <code> + * $lu_object = &LiveUser::singleton($conf); + * $logger = &Log::factory('mail', 'bug@example.com', + * 'myapp_debug_mail_log', array('from' => 'application_bug@example.com')); + * $lu_object->addErrorLog($logger); + * </code> + * + * @param Log logger instance + * @return boolean true on success + */ + function addErrorLog(&$log) + { + return $this->_log->addChild($log); } /** @@ -552,9 +629,7 @@ $filename = 'LiveUser/Admin/Auth/Container/' . $container['name'] . '.php'; @include_once($filename); if (!class_exists($classname)) { - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_NOT_SUPPORTED, null, null, - 'Missing file: '.$filename); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Missing file: ' . $filename); } $con = &new $classname($container, array('type' => $container['type'])); } @@ -562,51 +637,12 @@ } /** - * Crypts data using mcrypt or userland if not available - * - * @access private - * @param boolean true to crypt, false to decrypt - * @param string data to crypt - * @return string crypted data - */ - function _cookieCryptMode($crypt, $data) - { - if (function_exists('mcrypt_module_open')) { - $td = mcrypt_module_open('tripledes', '', 'ecb', ''); - $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size ($td), MCRYPT_RAND); - mcrypt_generic_init($td, $this->_options['cookie']['secret'], $iv); - if ($crypt) { - $data = mcrypt_generic($td, $data); - } else { - $data = mdecrypt_generic($td, $data); - } - mcrypt_generic_deinit($td); - mcrypt_module_close($td); - } else { - @include_once 'Crypt/Rc4.php'; - if (!class_exists('Crypt_RC4')) { - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_NOT_SUPPORTED, null, null, - 'Please install Crypt_RC4 to use this feature'); - return $this->_error; - } - $rc4 =& new Crypt_RC4($this->_options['cookie']['secret']); - if ($crypt) { - $rc4->crypt($data); - } else { - $rc4->decrypt($data); - } - } - - return $data; - } - - /** * Sets an option. * * @access public - * @param string option name - * @param mixed value for the option - * @return mixed true or PEAR Error + * @param string option name + * @param mixed value for the option + * @return boolean true * @see LiveUser::_options */ function setOption($option, $value) @@ -615,9 +651,7 @@ $this->options[$option] = $value; return true; } - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_CONFIG, null, null, - "unknown option $option"); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), "unknown option $option"); } /** @@ -632,9 +666,7 @@ if (isset($this->options[$option])) { return $this->options[$option]; } - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_CONFIG, null, null, - "unknown option $option"); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), "unknown option $option"); } /** @@ -648,9 +680,7 @@ if (is_object($this->_auth)) { return $this->_auth; } - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_CONFIG, null, null, - 'authentication container has not yet been instantianted'); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'authentication container has not yet been instantianted'); } /** @@ -664,9 +694,7 @@ if (is_object($this->_perm)) { return $this->_perm; } - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_CONFIG, null, null, - "permission container has not yet been instantianted"); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'permission container has not yet been instantianted'); } /** @@ -681,15 +709,11 @@ * @param string password of the user trying to authenticate * @param boolean set to true if user wants to logout * @param boolean set if remember me is set - * @return mixed true if init process well, false if something - * went wrong or an error object. + * @return boolean true if init process well, false if something + * went wrong. */ function init($handle = '', $passwd = '', $logout = false, $remember = false) { - if ($this->_error) { - return $this->_error; - } - // set session save handler if needed if ($this->_options['session_save_handler'] == true) { session_set_save_handler( @@ -713,11 +737,6 @@ if (!$this->unfreeze()) { if (is_null($this->_auth)) { $lulogin = $this->tryLogin($handle, $passwd); - if (PEAR::isError($lulogin)) { - $this->_error = $lulogin; - $this->status = LIVEUSER_STATUS_PERMINITERROR; - return $lulogin; - } } } else { $lulogin = true; @@ -765,10 +784,98 @@ if (is_null($this->status)) { $this->status = LIVEUSER_STATUS_UNKNOWN; } + return false; } /** + * Handles the rememberMe cookie login. + * + * @access private + * @return boolean true on success, false otherwise + */ + function cookieBasedLogin() + { + $cookieData = unserialize(stripslashes($_COOKIE[$this->_options['cookie']['name']])); + if (count($cookieData) != 3) { + // Delete cookie if it's not valid, keeping it messes up the + // authentication process + setcookie($this->_options['cookie']['name'], '', + time() - 3600, + $this->_options['cookie']['path'], + $this->_options['cookie']['domain'] + ); + $this->_stack->push(LIVEUSER_ERROR_COOKIE, 'error', + 'Wrong data in cookie store in LiveUser::cookieBasedLogin()'); + return false; + } + + $serverData = $this->_readStoreCookiePasswdId($cookieData[0]); + + if ($serverData[0] != $cookieData[2]) { + // Delete cookie if it's not valid, keeping it messes up the + // authentication process + setcookie($this->_options['cookie']['name'], '', + time() - 3600, + $this->_options['cookie']['path'], + $this->_options['cookie']['domain']); + $this->_stack->push(LIVEUSER_ERROR_COOKIE, 'error', + 'Passwords hashes do not match in cookie in LiveUser::cookieBasedLogin()'); + return false; + } + + $this->_handle = $cookieData[1]; + $this->_passwd = $serverData[1]; + return true; + } + + /** + * Handles login from GET, POST or REQUEST. + * + * @access private + * @return boolean true on success, false otherwise + */ + function httpvarBasedLogin() + { + switch (strtolower($this->_options['login']['method'])) { + case 'request': + $httpvar = $_REQUEST; + break; + case 'post': + $httpvar = $_POST; + break; + case 'get': + default: + $httpvar = $_GET; + break; + } + + if (empty($httpvar[$this->_options['login']['username']]) || + empty($httpvar[$this->_options['login']['password']])) + { + if (!$this->_options['login']['force'] && + !empty($this->_options['login']['function']) && + is_callable($this->_options['login']['function'])) + { + call_user_func($this->_options['login']['function'], $this); + } + } else { + $this->_handle = $httpvar[$this->_options['login']['username']]; + $this->_passwd = $httpvar[$this->_options['login']['password']]; + if ($this->_options['login']['remember'] && + isset($httpvar[$this->_options['login']['remember']]) + ) { + $this->_remember = $httpvar[$this->_options['login']['remember']]; + } + if (get_magic_quotes_gpc()) { + $this->_handle = stripslashes($this->_handle); + $this->_passwd = stripslashes($this->_passwd); + } + } + return true; + } + + /** * Tries to log the user in by trying all the Auth containers defined * in the configuration file until there is a success or failure. * @@ -780,106 +887,56 @@ */ function tryLogin($handle = '', $passwd = '', $remember = false) { - if ($this->_error) { - return $this->_error; - } - - // handle and password not directly passed + // handle and password not passed directly if (empty($handle)) { if (isset($this->_options['cookie']) && isset($_COOKIE[$this->_options['cookie']['name']])) { - $cookieData = unserialize(stripslashes($_COOKIE[$this->_options['cookie']['name']])); - if (count($cookieData) != 3) { - $this->_error = true; - // Delete cookie if it's not valid, keeping it messes up the - // authentication process - setcookie($this->_options['cookie']['name'], '', - time() - 3600, - $this->_options['cookie']['path'], - $this->_options['cookie']['domain'] - ); - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_COOKIE, null, null, - "Wrong data in cookie store in LiveUser::tryLogin()"); - return $this->_error; - } - - $serverData = $this->_readStoreCookiePasswdId($cookieData[0]); - - if ($serverData[0] != $cookieData[2]) { - $this->_error = true; - // Delete cookie if it's not valid, keeping it messes up the - // authentication process - setcookie($this->_options['cookie']['name'], '', - time() - 3600, - $this->_options['cookie']['path'], - $this->_options['cookie']['domain']); - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_COOKIE, null, null, - "Passwords hashes do not match in cookie in LiveUser::tryLogin()"); - return $this->_error; - } - - $handle = $cookieData[1]; - $passwd = $serverData[1]; - // $remember + $res = $this->cookieBasedLogin(); } elseif ($this->_options['login']['method']) { - switch (strtolower($this->_options['login']['method'])) { - case 'request': - $httpvar = $_REQUEST; - break; - case 'post': - $httpvar = $_POST; - break; - case 'get': - default: - $httpvar = $_GET; - break; - } + $res = $this->httpvarBasedLogin(); + } - if (empty($httpvar[$this->_options['login']['username']]) || - empty($httpvar[$this->_options['login']['password']])) - { - if (!$this->_options['login']['force'] && - !empty($this->_options['login']['function']) && - is_callable($this->_options['login']['function'])) - { - call_user_func($this->_options['login']['function'], $this); - } - } else { - $handle = $httpvar[$this->_options['login']['username']]; - $passwd = $httpvar[$this->_options['login']['password']]; - if ($this->_options['login']['remember'] && - isset($httpvar[$this->_options['login']['remember']]) - ) { - $remember = $httpvar[$this->_options['login']['remember']]; - } - if (get_magic_quotes_gpc()) { - $handle = stripslashes($handle); - $passwd = stripslashes($passwd); - } - } + if (!$res) { + $this->_stack->push(LIVEUSER_ERROR, 'error', array(), 'cannot find credentials'); + return false; } + } else { + $this->_handle = $handle; + $this->_passwd = $passwd; + $this->_remember = $remember; } - if (empty($handle)) { + if (empty($this->_handle)) { return false; } + $userfound = $this->instantiateAuthContainer(); + + if (!$userfound || !is_object($this->_auth) || !$this->_auth->loggedIn) { + $this->status = LIVEUSER_STATUS_AUTHFAILED; + } + return true; + } + + /** + * Instantiate the auth containers + * + * @acess private + * @return boolean true if success + */ + function instantiateAuthContainer() + { $counter = 0; $userFound = false; $backends = array_keys($this->authContainers); $backend_cnt = count($backends); - + //loop into auth containers while ($userFound == false && $backend_cnt > $counter) { $auth = &$this->authFactory($this->authContainers[$backends[$counter]]); - if (LiveUser::isError($auth)) { - $this->_error = $auth; - $this->status = LIVEUSER_STATUS_AUTHINITERROR; - return $auth; - } - - $auth->login($handle, $passwd, true); + + $auth->login($this->_handle, $this->_passwd, true); if ($auth->loggedIn) { if ($this->_options['login']['regenid'] == true) { @session_regenerate_id(); @@ -888,31 +945,22 @@ $userFound = true; $this->_auth = $auth; $this->_auth->backendArrayIndex = $backends[$counter]; - + // Create permission object if (is_array($this->permContainer)) { $this->_perm =& $this->permFactory($this->permContainer); $res = $this->_perm->init($this->_auth->authUserId); - - if (PEAR::isError($res)) { - $this->_error = $res; - $this->status = LIVEUSER_STATUS_PERMINITERROR; - return $res; - } } - + $this->freeze(); - $this->setRememberCookie($handle, $passwd, $remember); + $this->setRememberCookie($this->_handle, $this->_passwd, $this->_remember); } elseif ($auth->isActive === false) { $this->status = LIVEUSER_STATUS_ISINACTIVE; $userFound = true; } $counter++; } - if (!is_object($this->_auth) || !$this->_auth->loggedIn) { - $this->status = LIVEUSER_STATUS_AUTHFAILED; - } - return true; + return $userFound; } /** @@ -924,10 +972,6 @@ */ function unfreeze() { - if ($this->_error) { - return $this->_error; - } - if (isset($_SESSION[$this->_options['session']['varname']]['auth']) && is_array($_SESSION[$this->_options['session']['varname']]['auth']) && isset($_SESSION[$this->_options['session']['varname']]['auth_name']) @@ -961,10 +1005,6 @@ */ function freeze() { - if ($this->_error) { - return $this->_error; - } - if (is_object($this->_auth) && $this->_auth->loggedIn) { // Bind objects to session $_SESSION[$this->_options['session']['varname']] = array(); @@ -979,10 +1019,7 @@ } return true; } - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR, null, null, - "No data available to store inside session in LiveUser::freeze()"); - return $this->_error; - + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'No data available to store inside session'); } /** @@ -993,10 +1030,6 @@ */ function disconnect() { - if ($this->_error) { - return $this->_error; - } - if (is_object($this->_auth) && $this->_auth->loggedIn) { $this->_auth->disconnect(); $this->_auth = null; @@ -1006,10 +1039,7 @@ } return true; } - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR, null, null, - "No connection to disconnect in LiveUser::disconnect()"); - return $this->_error; - + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'No connection to disconnect in LiveUser::disconnect()'); } /** @@ -1025,10 +1055,6 @@ */ function setRememberCookie($handle, $passwd, $remember) { - if ($this->_error) { - return $this->_error; - } - switch (strtolower($this->_options['login']['method'])) { case 'request': $httpvar = $_REQUEST; @@ -1049,9 +1075,7 @@ $store_id = md5($handle . $passwd); if (!$passwd_id = $this->_storeCookiePasswdId($passwd, $store_id)) { - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_COOKIE, null, null, - "Cannot store cookie data in LiveUser::setRememberCookie()"); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Cannot store cookie data'); } $setcookie = setcookie( @@ -1062,15 +1086,133 @@ $this->_options['cookie']['domain']); if (!$setcookie) { - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR, null, null, - "Unable to set cookie in LiveUser::setRememberCookie()"); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Unable to set cookie'); } } return true; } /** + * Crypts data using mcrypt or userland if not available + * + * @access private + * @param boolean true to crypt, false to decrypt + * @param string data to crypt + * @return string crypted data + */ + function _cookieCryptMode($crypt, $data) + { + if (function_exists('mcrypt_module_open')) { + $td = mcrypt_module_open('tripledes', '', 'ecb', ''); + $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size ($td), MCRYPT_RAND); + mcrypt_generic_init($td, $this->_options['cookie']['secret'], $iv); + if ($crypt) { + $data = mcrypt_generic($td, $data); + } else { + $data = mdecrypt_generic($td, $data); + } + mcrypt_generic_deinit($td); + mcrypt_module_close($td); + } else { + @include_once 'Crypt/Rc4.php'; + if (!class_exists('Crypt_RC4')) { + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Please install Crypt_RC4 to use this feature'); + } + $rc4 =& new Crypt_RC4($this->_options['cookie']['secret']); + if ($crypt) { + $rc4->crypt($data); + } else { + $rc4->decrypt($data); + } + } + + return $data; + } + + /** + * A "store" on the server contains the password and the + * cookie id in an encrypted form. + * + * This method generates a md5 from given password and writes it + * into the "store" along with crypted password. + * + * Cookies are not secure but keeping the password in plain text + * in the cookie is not the best way to go. Since some + * containers like LDAP need the clear text password, we store + * an encrypted version of the password using Crypt_Rc4 which + * provides a simple two-way mechanism. + * + * To do this LiveUser needs access to a writeable directory. + * If you do no have access to the ini_get() function please + * set a constant named LIVEUSER_TMPDIR with an absolute + * path to a writeable directory. + * + * @access private + * @param string the password to store + * @param string file name used as storage + * @return boolean true if success, false otherwise + */ + function _storeCookiePasswdId($passwd, $store) + { + $dir = $this->_options['cookie']['savedir']; + + if (!is_writable($dir . "/$store.lu")) { + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Cannot create file, please check path and permissions'); + } + + if (!$fh = @fopen($dir . "/$store.lu", 'wb')) { + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Cannot open file for writting'); + } + + $data = serialize(array(md5($passwd), $passwd)); + + $crypted_data = $this->_cookieCryptMode(true, $data); + + if (!fwrite($fh, $crypted_data)) { + fclose($fh); + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Cannot save cookie data'); + } + + fclose($fh); + + return true; + } + + /** + * A "store" on the server contains the password and the + * cookie id in an encrypted form. + * + * This method reads the data contained in it. + * + * @access private + * @param string the filename of the store + * @return mixed an array of the data, false otherwise + */ + function _readStoreCookiePasswdId($store) + { + $dir = $this->_options['cookie']['savedir']; + + if (!$fh = @fopen($dir . "/$store.lu", 'rb')) { + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Cannot open file for reading'); + } + + if (!$fields = fread($fh, 4096)) { + fclose($fh); + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Cannot read file'); + } + + fclose($fh); + + $params = unserialize($this->_cookieCryptMode(false, $fields)); + + if (count($params) != 2) { + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Incorrect array structure'); + } + + return $params; + } + + /** * If the logout parameter has been set via GET/POST or the user is * idled/expired, this destroys the session object. * @@ -1083,10 +1225,6 @@ */ function processLogout($logout, $handle = '', $passwd = '') { - if ($this->_error) { - return $this->_error; - } - // logout user if the user is logged in // and new creditionals have been passed to LiveUser if (!$logout && $this->_options['logout']['method']) { @@ -1159,10 +1297,6 @@ */ function logout($direct = true) { - if ($this->_error) { - return $this->_error; - } - if ($this->status != LIVEUSER_STATUS_IDLED || $this->status != LIVEUSER_STATUS_EXPIRED ) { @@ -1235,10 +1369,6 @@ */ function checkRight($rights) { - if ($this->_error) { - return $this->_error; - } - if ($rights === false) { return LIVEUSER_MAX_LEVEL; } @@ -1281,10 +1411,6 @@ */ function checkRightLevel($rights, $owner_user_id, $owner_group_id) { - if ($this->_error) { - return $this->_error; - } - if ($rights === false) { return LIVEUSER_MAX_LEVEL; } @@ -1306,9 +1432,6 @@ */ function isLoggedIn() { - if ($this->_error) { - return $this->_error; - } if (!is_object($this->_auth)) { return false; } @@ -1322,18 +1445,14 @@ * * Use this to check if this was the reason * why a user was not able to login. - * true == Not active - * false == active + * true == user account is NOT active + * false == user account is active * * @access public * @return boolean */ function isInactive() { - if ($this->_error) { - return $this->_error; - } - return $this->status == LIVEUSER_STATUS_ISINACTIVE; } @@ -1347,22 +1466,16 @@ * * @access public * @param string The name of the function to be called. - * @return mixed void or PEAR_Error + * @return void */ function setLoginFunction($functionName) { - if ($this->_error) { - return $this->_error; - } - if (!empty($functionName) && is_callable($functionName)) { $this->_options['login']['function'] = $functionName; return true; } - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_MISSING_LOGINFUNCTION, null, null, - "Login function not found in LiveUser::setLoginFunction()"); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Login function not found'); } /** @@ -1377,23 +1490,17 @@ * * @access public * @param string The name of the function to be called. - * @return mixed void or a PEAR_Error + * @return void * @see LiveUser::_options */ function setLogoutFunction($functionName) { - if ($this->_error) { - return $this->_error; - } - if (!empty($functionName) && is_callable($functionName)) { $this->_options['logout']['function'] = $functionName; return true; } - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_MISSING_LOGOUTFUNCTION, null, null, - "Logout function not found in LiveUser::setLogoutFunction()"); - return $this->_error; + $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Logout function not found'); } /** @@ -1407,9 +1514,6 @@ */ function getProperty($what, $container = 'auth') { - if ($this->_error) { - return $this->_error; - } $that = null; if ($container == 'auth' && $this->_auth && $this->_auth->getProperty($what) !== null) { $that = $this->_auth ? $this->_auth->getProperty($what) : null; @@ -1431,149 +1535,6 @@ } /** - * A "store" on the server contains the password and the - * cookie id in an encrypted form. - * - * This method generates a md5 from given - * password and writes it into the "store" along - * with crypted password. - * - * Cookies are not secure but keeping - * the password in plain text in the cookie - * is not the best way to go. Since some - * containers like LDAP need the clear text - * password, we store an encrypted version - * of the password using Crypt_Rc4 which provides - * a simple two-way mechanism. - * - * To do this LiveUser needs access - * to a writeable directory. If you do - * no have access to the ini_get() - * function please set a constant - * named LIVEUSER_TMPDIR with an absolute - * path to a writeable directory. - * - * @access private - * @param string the password to store - * @param string file name used as storage - * @return boolean true if success, false otherwise - */ - function _storeCookiePasswdId($passwd, $store) - { - if (!defined('LIVEUSER_TMPDIR')) { - define('LIVEUSER_TMPDIR', ini_get('session.save_path')); - } - - if (!$fh = fopen(LIVEUSER_TMPDIR . "/$store.lu", 'wb')) { - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_COOKIE, null, null, - "Cannot open file for writting in LiveUser::_storeCookiePasswdId()"); - return $this->_error; - } - - $data = serialize(array(md5($passwd), $passwd)); - - $crypted_data = $this->_cookieCryptMode(true, $data); - - if (!fwrite($fh, $crypted_data)) { - fclose($fh); - $this->_error = LiveUser::raiseError(LIVEUSER_COOKIE_ERROR, null, null, - "Cannot save cookie data in LiveUser::_storeCookiePasswdId()"); - return $this->_error; - } - - fclose($fh); - - return true; - } - - /** - * A "store" on the server contains the password and the - * cookie id in an encrypted form. - * - * This method reads the data contained in it. - * - * @access private - * @param string the filename of the store - * @return mixed an array of the data, false otherwise - */ - function _readStoreCookiePasswdId($store) - { - if (!defined('LIVEUSER_TMPDIR')) { - define('LIVEUSER_TMPDIR', ini_get('session.save_path')); - } - - if (!$fh = fopen(LIVEUSER_TMPDIR . "/$store.lu", 'rb')) { - $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_COOKIE, null, null, - "Cannot open file for reading in LiveUser::_readStoreCookiePasswdId()"); - return $this->_error; - } - - if (!$fields = fread($fh, 4096)) { - fclose($fh); - $this->_error = LiveUser::raiseError(LIVEUSER_COOKIE_ERROR, null, null, - "Cannot read file in LiveUser::_readStoreCookiePasswdId()"); - return $this->_error; - } - - fclose($fh); - - $params = unserialize($this->_cookieCryptMode(false, $fields)); - - if (count($params) != 2) { - $this->_error = LiveUser::raiseError(LIVEUSER_COOKIE_ERROR, null, null, - "Incorrect array structure in LiveUser::_readStoreCookiePasswdId()"); - } - - return $params; - } - - /** - * Tell whether a result from a LiveUser method is an error. - * - * @access public - * @param mixed result code - * @return bool whether value is an error - */ - function isError($value) - { - return PEAR::isError($value); - } // end func isError - - /** - * Return a textual error message for a LiveUser error code. - * - * @access public - * @param mixed error code or error object - * @return string error message - */ - function errorMessage($value) - { - // make the variable static so that it only has to do the defining on the first call - static $errorMessages; - - // define the varies error messages - if (!isset($errorMessages)) { - $errorMessages = array( - LIVEUSER_ERROR => 'Unknown error', - LIVEUSER_ERROR_NOT_SUPPORTED => 'Feature not supported', - LIVEUSER_ERROR_CONFIG => 'Config file error', - LIVEUSER_ERROR_MISSING_DEPS => 'Missing package depedencies', - LIVEUSER_ERROR_MISSING_LOGINFUNCTION => 'Login function not found', - LIVEUSER_ERROR_MISSING_LOGOUTFUNCTION => 'Logout function not found', - LIVEUSER_ERROR_COOKIE => 'Remember Me cookie error', - ); - } - - // If this is an error object, then grab the corresponding error code - if (LiveUser::isError($value)) { - $value = $value->getCode(); - } - - // return the textual error message corresponding to the code - return isset($errorMessages[$value]) ? $errorMessages[$value] : $errorMessages[LIVEUSER_ERROR]; - } // end func errorMessage - - /** * Return a textual status message for a LiveUser status code. * * @access public @@ -1588,15 +1549,15 @@ // define the varies error messages if (!isset($statusMessages)) { $statusMessages = array( - LIVEUSER_STATUS_IDLED => 'Maximum idle time is reached', - LIVEUSER_STATUS_EXPIRED => 'User session has expired', - LIVEUSER_STATUS_ISINACTIVE => 'User is set to inactive', - LIVEUSER_STATUS_PERMINITERROR => 'Cannot instantiate permission container', - LIVEUSER_STATUS_AUTHINITERROR => 'Cannot instantiate authentication configuration', - LIVEUSER_STATUS_AUTHNOTFOUND => 'Cannot retrieve Auth object from session', - LIVEUSER_STATUS_UNKNOWN => 'Something went wrong in whatever you were trying to do', - LIVEUSER_STATUS_LOGGEDOUT => 'User was logged out correctly', - LIVEUSER_STATUS_AUTHFAILED => 'No auth container authenticated successfully', + LIVEUSER_STATUS_IDLED => 'Maximum idle time is reached', + LIVEUSER_STATUS_EXPIRED => 'User session has expired', + LIVEUSER_STATUS_ISINACTIVE => 'User is set to inactive', + LIVEUSER_STATUS_PERMINITERROR => 'Cannot instantiate permission container', + LIVEUSER_STATUS_AUTHINITERROR => 'Cannot instantiate authentication configuration', + LIVEUSER_STATUS_AUTHNOTFOUND => 'Cannot retrieve Auth object from session', + LIVEUSER_STATUS_UNKNOWN => 'Something went wrong in whatever you were trying to do', + LIVEUSER_STATUS_LOGGEDOUT => 'User was logged out correctly', + LIVEUSER_STATUS_AUTHFAILED => 'No auth container authenticated successfully', ); }