cvs: pear /Perm_LiveUser LiveUser.php

From: Date: Mon, 07 Jun 2004 18:29:40 +0000
Subject: cvs: pear /Perm_LiveUser LiveUser.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-20316@lists.php.net to get a copy of this message
arnaud Mon Jun 7 14:29:40 2004 EDT Modified files: /pear/Perm_LiveUser LiveUser.php Log: * reverting commit

http://cvs.php.net/diff.php/pear/Perm_LiveUser/LiveUser.php?r1=1.101&r2=1.102&ty=u Index: pear/Perm_LiveUser/LiveUser.php diff -u pear/Perm_LiveUser/LiveUser.php:1.101 pear/Perm_LiveUser/LiveUser.php:1.102 --- pear/Perm_LiveUser/LiveUser.php:1.101 Mon Jun 7 11:33:15 2004 +++ pear/Perm_LiveUser/LiveUser.php Mon Jun 7 14:29:40 2004 @@ -32,8 +32,7 @@ /** * Include PEAR base class */ -require_once 'PEAR/ErrorStack.php'; - +require_once 'PEAR.php'; /**#@+ * Error related constants definition @@ -47,7 +46,6 @@ define('LIVEUSER_ERROR_MISSING_LOGINFUNCTION', -5); define('LIVEUSER_ERROR_MISSING_LOGOUTFUNCTION', -6); define('LIVEUSER_ERROR_COOKIE', -7); -define('LIVEUSER_ERROR_MISSING_FILE', -8); /**#@-*/ /**#@+ @@ -74,17 +72,6 @@ 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: @@ -105,8 +92,7 @@ * it will try to send HTTP headers such as cookies and redirects. * * Requirements: - * - Should run on PHP version 4.2.0 (required for PEAR_Errorstack or higher, - * tested only from 4.2.1 onwards + * - Should run on PHP version 4.1.0 or higher, tested only from 4.2.1 onwards * * Thanks to: * Bjoern Schotte, Kristian Koehntopp, Antonio Guerra @@ -116,10 +102,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.101 2004/06/07 15:33:15 arnaud Exp $ + * @version $Id: LiveUser.php,v 1.102 2004/06/07 18:29:40 arnaud Exp $ * @package LiveUser */ -class LiveUser +class LiveUser extends PEAR { /** * LiveUser options set in the configuration file. @@ -215,86 +201,64 @@ * * @access private * @var string - * @see LIVEUSER_STATUS_* constants + * @see LIVEUSER_STATUS constants */ var $status = null; /** - * Error stack + * Whether an error was raised * * @access private - * @var PEAR_ErrorStack + * @var boolean */ - var $_stack = null; + var $_error = false; /** - * PEAR::Log object - * used for error logging - * - * @access private - * @var Log + * Constructor */ - var $_log = null; + function LiveUser() + { + $this->PEAR(); + } /** - * Handle used to login + * This method is used to communicate an error and invoke error + * callbacks etc. Basically a wrapper for PEAR::raiseError + * without the message string. * - * @access private - * @var string - */ - var $_handle = ''; - - /** - * Password used to login + * @param mixed integer error code, or a PEAR error object (all + * other parameters are ignored if this parameter is + * an object * - * @access private - * @var string - */ - var $_passwd = ''; - - /** - * RememberMe cookie set ? + * @param int error mode, see PEAR_Error docs * - * @access private - * @var boolean - */ - var $_remember = false; - - /** - * Error codes to message mapping array + * @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 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 + * @param string Extra debug information. Defaults to the last + * query and native error code. * - * @acess protected - * @return void + * @return object a PEAR error object + * + * @see PEAR_Error */ - function LiveUser() + function &raiseError($code = null, $mode = null, $options = null, + $userinfo = null) { - $this->_stack = &PEAR_ErrorStack::singleton('LiveUser'); - $this->_log = &Log::factory('composite'); - $this->_stack->setLogger($this->_log); - - if ($GLOBALS['_LIVEUSER_DEBUG']) { - $this->_log->addChild(Log::factory('win')); + // The error is yet a LiveUser error object + if (is_object($code)) { + return PEAR::raiseError($code, null, null, null, null, null, true); } - $this->_stack->setErrorMessageTemplate($this->_errorMessages); - PEAR::pushErrorHandling(PEAR_ERROR_CALLBACK, array(&$this, 'errorCallback')); + if (empty($code)) { + $code = LIVEUSER_ERROR; + } + $msg = LiveUser::errorMessage($code); + return PEAR::raiseError("LiveUser Error: $msg", $code, $mode, $options, $userinfo); } /** @@ -334,8 +298,7 @@ * 'lifetime' => 'Cookie lifetime in days', * 'path' => 'Cookie path', * 'domain' => 'Cookie domain', - * 'secret' => 'Secret key used for cookie value encryption', - * 'savedir' => '/absolute/path/to/writeable/directory' // No / at the end ! + * 'secret' => 'Secret key used for cookie value encryption' * ), * 'authContainers' => array( * 'name' => array( @@ -403,36 +366,27 @@ if (!empty($conf)) { $init = $obj->_readConfig($conf, $confName); + if (LiveUser::isError($init)) { + $this->_error = $init; + return $init; + } } - if (isset($obj->_options['autoInit']) && + if (!PEAR::isError($obj) && + 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. @@ -458,6 +412,9 @@ $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; } @@ -465,7 +422,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. @@ -477,8 +434,9 @@ $filename = 'LiveUser/Auth/Container/' . $conf['type'] . '.php'; @include_once($filename); if (!class_exists($classname)) { - $this->_stack->push(LIVEUSER_ERROR_NOT_SUPPORTED, 'exception', array('file' => $filename)); - return false; + $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_NOT_SUPPORTED, null, null, + 'Missing file: ' . $filename, 'LiveUser'); + return $this->_error; } $auth = &new $classname($conf); return $auth; @@ -497,8 +455,9 @@ $filename = 'LiveUser/Perm/Container/' . $conf['type'] . '.php'; @include_once($filename); if (!class_exists($classname)) { - $this->_stack->push(LIVEUSER_ERROR_NOT_SUPPORTED, 'exception', array('file' => $filename)); - return false; + $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_NOT_SUPPORTED, null, null, + 'Missing file: ' . $filename); + return $this->_error; } $perm = &new $classname($conf); return $perm; @@ -571,36 +530,16 @@ } if (!@include_once($conf)) { - $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Could not read the configuration file'); + $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_CONFIG, null, null, + "Could not read the configuration file in LiveUser::readConfig()"); + return $this->_error; } if (isset(${$confName}) && is_array(${$confName})) { return $this->_readConfig(${$confName}, $confName); } - $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); + $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_CONFIG, null, null, + "Configuration array not found in LiveUser::readConfig()"); + return $this->_error; } /** @@ -629,7 +568,9 @@ $filename = 'LiveUser/Admin/Auth/Container/' . $container['name'] . '.php'; @include_once($filename); if (!class_exists($classname)) { - $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Missing file: ' . $filename); + $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_NOT_SUPPORTED, null, null, + 'Missing file: '.$filename); + return $this->_error; } $con = &new $classname($container, array('type' => $container['type'])); } @@ -637,12 +578,51 @@ } /** + * 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 boolean true + * @param string option name + * @param mixed value for the option + * @return mixed true or PEAR Error * @see LiveUser::_options */ function setOption($option, $value) @@ -651,7 +631,9 @@ $this->options[$option] = $value; return true; } - $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), "unknown option $option"); + $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_CONFIG, null, null, + "unknown option $option"); + return $this->_error; } /** @@ -666,7 +648,9 @@ if (isset($this->options[$option])) { return $this->options[$option]; } - $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), "unknown option $option"); + $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_CONFIG, null, null, + "unknown option $option"); + return $this->_error; } /** @@ -680,7 +664,9 @@ if (is_object($this->_auth)) { return $this->_auth; } - $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'authentication container has not yet been instantianted'); + $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_CONFIG, null, null, + 'authentication container has not yet been instantianted'); + return $this->_error; } /** @@ -694,7 +680,9 @@ if (is_object($this->_perm)) { return $this->_perm; } - $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'permission container has not yet been instantianted'); + $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_CONFIG, null, null, + "permission container has not yet been instantianted"); + return $this->_error; } /** @@ -709,11 +697,15 @@ * @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 boolean true if init process well, false if something - * went wrong. + * @return mixed true if init process well, false if something + * went wrong or an error object. */ 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( @@ -737,6 +729,11 @@ 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; @@ -784,98 +781,10 @@ 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. * @@ -887,56 +796,106 @@ */ function tryLogin($handle = '', $passwd = '', $remember = false) { - // handle and password not passed directly + if ($this->_error) { + return $this->_error; + } + + // handle and password not directly passed if (empty($handle)) { if (isset($this->_options['cookie']) && isset($_COOKIE[$this->_options['cookie']['name']])) { - $res = $this->cookieBasedLogin(); + $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 } elseif ($this->_options['login']['method']) { - $res = $this->httpvarBasedLogin(); - } + switch (strtolower($this->_options['login']['method'])) { + case 'request': + $httpvar = $_REQUEST; + break; + case 'post': + $httpvar = $_POST; + break; + case 'get': + default: + $httpvar = $_GET; + break; + } - if (!$res) { - $this->_stack->push(LIVEUSER_ERROR, 'error', array(), 'cannot find credentials'); - return false; + 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); + } + } } - } else { - $this->_handle = $handle; - $this->_passwd = $passwd; - $this->_remember = $remember; } - if (empty($this->_handle)) { + if (empty($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]]); - - $auth->login($this->_handle, $this->_passwd, true); + if (LiveUser::isError($auth)) { + $this->_error = $auth; + $this->status = LIVEUSER_STATUS_AUTHINITERROR; + return $auth; + } + + $auth->login($handle, $passwd, true); if ($auth->loggedIn) { if ($this->_options['login']['regenid'] == true) { @session_regenerate_id(); @@ -945,22 +904,31 @@ $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($this->_handle, $this->_passwd, $this->_remember); + $this->setRememberCookie($handle, $passwd, $remember); } elseif ($auth->isActive === false) { $this->status = LIVEUSER_STATUS_ISINACTIVE; $userFound = true; } $counter++; } - return $userFound; + if (!is_object($this->_auth) || !$this->_auth->loggedIn) { + $this->status = LIVEUSER_STATUS_AUTHFAILED; + } + return true; } /** @@ -972,6 +940,10 @@ */ 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']) @@ -1005,6 +977,10 @@ */ 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(); @@ -1019,7 +995,10 @@ } return true; } - $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'No data available to store inside session'); + $this->_error = LiveUser::raiseError(LIVEUSER_ERROR, null, null, + "No data available to store inside session in LiveUser::freeze()"); + return $this->_error; + } /** @@ -1030,6 +1009,10 @@ */ function disconnect() { + if ($this->_error) { + return $this->_error; + } + if (is_object($this->_auth) && $this->_auth->loggedIn) { $this->_auth->disconnect(); $this->_auth = null; @@ -1039,7 +1022,10 @@ } return true; } - $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'No connection to disconnect in LiveUser::disconnect()'); + $this->_error = LiveUser::raiseError(LIVEUSER_ERROR, null, null, + "No connection to disconnect in LiveUser::disconnect()"); + return $this->_error; + } /** @@ -1055,6 +1041,10 @@ */ function setRememberCookie($handle, $passwd, $remember) { + if ($this->_error) { + return $this->_error; + } + switch (strtolower($this->_options['login']['method'])) { case 'request': $httpvar = $_REQUEST; @@ -1075,7 +1065,9 @@ $store_id = md5($handle . $passwd); if (!$passwd_id = $this->_storeCookiePasswdId($passwd, $store_id)) { - $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Cannot store cookie data'); + $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_COOKIE, null, null, + "Cannot store cookie data in LiveUser::setRememberCookie()"); + return $this->_error; } $setcookie = setcookie( @@ -1086,133 +1078,15 @@ $this->_options['cookie']['domain']); if (!$setcookie) { - $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Unable to set cookie'); + $this->_error = LiveUser::raiseError(LIVEUSER_ERROR, null, null, + "Unable to set cookie in LiveUser::setRememberCookie()"); + return $this->_error; } } 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. * @@ -1225,6 +1099,10 @@ */ 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']) { @@ -1297,6 +1175,10 @@ */ function logout($direct = true) { + if ($this->_error) { + return $this->_error; + } + if ($this->status != LIVEUSER_STATUS_IDLED || $this->status != LIVEUSER_STATUS_EXPIRED ) { @@ -1369,6 +1251,10 @@ */ function checkRight($rights) { + if ($this->_error) { + return $this->_error; + } + if ($rights === false) { return LIVEUSER_MAX_LEVEL; } @@ -1411,6 +1297,10 @@ */ function checkRightLevel($rights, $owner_user_id, $owner_group_id) { + if ($this->_error) { + return $this->_error; + } + if ($rights === false) { return LIVEUSER_MAX_LEVEL; } @@ -1432,6 +1322,9 @@ */ function isLoggedIn() { + if ($this->_error) { + return $this->_error; + } if (!is_object($this->_auth)) { return false; } @@ -1445,14 +1338,18 @@ * * Use this to check if this was the reason * why a user was not able to login. - * true == user account is NOT active - * false == user account is active + * true == Not active + * false == active * * @access public * @return boolean */ function isInactive() { + if ($this->_error) { + return $this->_error; + } + return $this->status == LIVEUSER_STATUS_ISINACTIVE; } @@ -1466,16 +1363,22 @@ * * @access public * @param string The name of the function to be called. - * @return void + * @return mixed void or PEAR_Error */ function setLoginFunction($functionName) { + if ($this->_error) { + return $this->_error; + } + if (!empty($functionName) && is_callable($functionName)) { $this->_options['login']['function'] = $functionName; return true; } - $this->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Login function not found'); + $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_MISSING_LOGINFUNCTION, null, null, + "Login function not found in LiveUser::setLoginFunction()"); + return $this->_error; } /** @@ -1490,17 +1393,23 @@ * * @access public * @param string The name of the function to be called. - * @return void + * @return mixed void or a PEAR_Error * @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->_stack->push(LIVEUSER_ERROR_CONFIG, 'exception', array(), 'Logout function not found'); + $this->_error = LiveUser::raiseError(LIVEUSER_ERROR_MISSING_LOGOUTFUNCTION, null, null, + "Logout function not found in LiveUser::setLogoutFunction()"); + return $this->_error; } /** @@ -1514,6 +1423,9 @@ */ 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; @@ -1535,6 +1447,149 @@ } /** + * 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 @@ -1549,15 +1604,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', ); }
« previous php.pear.cvs (#20316) next »