Source for file Auth.php
Documentation is available at Auth.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Martin Jansen <mj@php.net> |
// +----------------------------------------------------------------------+
// $Id: Auth.php,v 1.67 2003/10/20 06:36:34 yavo Exp $
define('AUTH_WRONG_LOGIN', -3 );
* The PEAR::Auth class provides methods for creating an
* authentication system using PHP.
* @author Martin Jansen <mj@php.net>
* @version $Revision: 1.67 $
* Auth lifetime in seconds
* If this variable is set to 0, auth never expires
* @see setExpire(), checkAuth()
* Has the auth session expired?
* @see checkAuth(), drawLogin()
* Maximum time of idleness in seconds
* The difference to $expire is, that the idletime gets
* refreshed each time, checkAuth() is called. If this
* variable is set to 0, idle time is never checked.
* @see setIdle(), checkAuth()
* Is the maximum idletime over?
* @see checkAuth(), drawLogin();
* @see Auth(), validateLogin()
* Function defined by the user, that creates the login screen
* Should the login form be displayed?
* Current authentication status
* Login callback function name
* @see setLoginCallback()
* Failed Login callback function name
* @see setLoginFailedCallback()
* Logout callback function name
* @see setLogoutCallback()
* Auth session-array name
var $_sessionName = '_authsession';
* Set up the storage driver.
* @param string Type of the storage driver
* @param mixed Additional options for the storage driver
* (example: if you are using DB as the storage
* driver, you have to pass the dsn string here)
* @param string Name of the function that creates the login form
* @param boolean Should the login form be displayed if neccessary?
function Auth($storageDriver, $options = '', $loginFunction = '', $showLogin = true )
if (!empty ($options['sessionName'])) {
$this->_sessionName = $options['sessionName'];
unset ($options['sessionName']);
if ($loginFunction != '' && is_callable($loginFunction)) {
$this->storage = $this->_factory ($storageDriver, $options);
// Pass a reference to auth to the container, ugly but works
// this is used by the DB container to use method setAuthData not staticaly.
$this->storage->_auth_obj = & $this;
* Return a storage driver based on $driver and $options
* @param string $driver Type of storage class to return
* @param string $options Optional parameters for the storage class
* @return object Object Storage object
function _factory ($driver, $options = '')
$storage_path = 'Auth/Container/' . $driver . '.php';
$storage_class = 'Auth_Container_' . $driver;
require_once $storage_path;
return new $storage_class($options);
* Assign data from login form to internal values
* This function takes the values for username and password
* from $HTTP_POST_VARS and assigns them to internal variables.
* If you wish to use another source apart from $HTTP_POST_VARS,
* you have to derive this function.
* @global $HTTP_POST_VARS
$post = &$this->_importGlobalVariable ('post');
if (isset ($post['username']) && $post['username'] != '') {
if (isset ($post['password']) && $post['password'] != '') {
if (!$this->checkAuth ()) {
* When the user has already entered a username,
* we have to validate it.
if (!empty ($this->username) && $login_ok) {
* If the login failed or the user entered no username,
* output the login screen again.
if (!empty ($this->username) && !$login_ok) {
$this->drawLogin ($this->storage->activeUser );
* Set the maximum expire time
* @param integer time in seconds
* @param bool add time to current expire time or not
* Set the maximum idle time
* @param integer time in seconds
* @param bool add time to current maximum idle time or not
function setIdle($time, $add = false )
* Set name of the session to a customized value.
* If you are using multiple instances of PEAR::Auth
* on the same domain, you can change the name of
* session per application via this function.
* @param string New name for the session
* Should the login form be displayed if neccessary?
* @param bool show login form or not
* Register a callback function to be called on user login.
* The function will receive two parameters, the username and a reference to the auth object.
* @param string callback function name
* @see setLogoutCallback()
* Register a callback function to be called on failed user login.
* The function will receive a single parameter, the username and a reference to the auth object.
* @param string callback function name
* Register a callback function to be called on user logout.
* The function will receive three parameters, the username and a reference to the auth object.
* @param string callback function name
* @see setLoginCallback()
* Register additional information that is to be stored
* @param string Name of the data field
* @param mixed Value of the data field
* @param boolean Should existing data be overwritten? (default
$session = &Auth::_importGlobalVariable ('session');
if (!empty ($session[$this->_sessionName]['data'][$name]) && $overwrite == false ) {
$session[$this->_sessionName]['data'][$name] = $value;
* Get additional information that is stored in the session.
* If no value for the first parameter is passed, the method will
* return all data that is currently stored.
* @param string Name of the data field
* @return mixed Value of the data field.
$session = &Auth::_importGlobalVariable ('session');
if(!isset ($session[$this->_sessionName]['data'])){
if(isset ($session[$this->_sessionName]['data'])) {
return $session[$this->_sessionName]['data'];
if (isset ($session[$this->_sessionName]['data'][$name])) {
return $session[$this->_sessionName]['data'][$name];
* Register variable in a session telling that the user
* has logged in successfully
$session = &Auth::_importGlobalVariable ('session');
if (!isset ($session[$this->_sessionName]) && !isset ($_SESSION)) {
session_register ($this->_sessionName);
if (!isset ($session[$this->_sessionName]) || !is_array($session[$this->_sessionName])) {
$session[$this->_sessionName] = array ();
if(!isset ($session[$this->_sessionName]['data'])){
$session[$this->_sessionName]['data'] = array ();
$session[$this->_sessionName]['registered'] = true;
$session[$this->_sessionName]['username'] = $username;
$session[$this->_sessionName]['timestamp'] = time();
$session[$this->_sessionName]['idle'] = time();
* Checks if there is a session with valid auth information.
* @return boolean Whether or not the user is authenticated.
$session = &$this->_importGlobalVariable ('session');
if (isset ($session[$this->_sessionName])) {
// Check if authentication session is expired
isset ($session[$this->_sessionName]['timestamp']) &&
($session[$this->_sessionName]['timestamp'] + $this->expire) < time()) {
// Check if maximum idle time is reached
isset ($session[$this->_sessionName]['idle']) &&
($session[$this->_sessionName]['idle'] + $this->idle) < time()) {
if (isset ($session[$this->_sessionName]['registered']) &&
isset ($session[$this->_sessionName]['username']) &&
$session[$this->_sessionName]['registered'] == true &&
$session[$this->_sessionName]['username'] != '') {
* Has the user been authenticated?
* @return bool True if the user is logged in, otherwise false.
$session = &$this->_importGlobalVariable ('session');
(isset ($session[$this->_sessionName]['registered']) &&
$session[$this->_sessionName]['registered'] === true ))
* Normally you will not use this output in your application,
* because you can pass a different function name to the
* constructor. For more information on this, please
* consult the documentation.
* @param string Username if already entered
function drawLogin ($username = '')
$server = &$this->_importGlobalVariable ('server');
echo '<i>Your session expired. Please login again!</i>'. "\n";
echo '<i>You have been idle for too long. Please login again!</i>'. "\n";
echo '<i>Wrong login data!</i>'. "\n";
PEAR ::raiseError ('You are using the built-in login screen of PEAR::Auth.<br />See the <a href="http://pear.php.net/manual/">manual</a> for details on how to create your own login function.', null );
echo '<form method="post" action="' . $server['PHP_SELF'] . '">'. "\n";
echo '<table border="0" cellpadding="2" cellspacing="0" summary="login form">'. "\n";
echo ' <td colspan="2" bgcolor="#eeeeee"><b>Login:</b></td>'. "\n";
echo ' <td>Username:</td>'. "\n";
echo ' <td><input type="text" name="username" value="' . $username . '" /></td>'. "\n";
echo ' <td>Password:</td>'. "\n";
echo ' <td><input type="password" name="password" /></td>'. "\n";
echo ' <td colspan="2" bgcolor="#eeeeee"><input type="submit" /></td>'. "\n";
* This function clears any auth tokens in the currently
* active session and executes the logout callback function,
$session = &$this->_importGlobalVariable ('session');
$session[$this->_sessionName] = array ();
unset ($session[$this->_sessionName]);
session_unregister ($this->_sessionName);
$session = &$this->_importGlobalVariable ('session');
$session[$this->_sessionName]['idle'] = time();
$session = &$this->_importGlobalVariable ('session');
if (!isset ($session[$this->_sessionName]['username'])) {
return $session[$this->_sessionName]['username'];
// {{{ sessionValidThru()
* Returns the time up to the session is valid
$session = &$this->_importGlobalVariable ('session');
if (!isset ($session[$this->_sessionName]['idle'])) {
return ($session[$this->_sessionName]['idle'] + $this->idle);
* List all users that are currently available in the storage
return $this->storage->listUsers ();
* Add user to the storage container
* @param mixed Additional parameters
* @return mixed True on success, PEAR error object on error
* and AUTH_METHOD_NOT_SUPPORTED otherwise.
function addUser($username, $password, $additional = '')
return $this->storage->addUser ($username, $password, $additional);
* Remove user from the storage container
* @return mixed True on success, PEAR error object on error
* and AUTH_METHOD_NOT_SUPPORTED otherwise.
return $this->storage->removeUser ($username);
// {{{ _importGlobalVariable()
* Import variables from special namespaces.
* @param string Type of variable (server, session, post)
function &_importGlobalVariable ($variable)
$var = &$GLOBALS['HTTP_SERVER_VARS'];
$var = &$GLOBALS['HTTP_SESSION_VARS'];
$var = &$GLOBALS['HTTP_POST_VARS'];
$var = &$GLOBALS['HTTP_COOKIE_VARS'];
$var = &$GLOBALS['HTTP_GET_VARS'];
Documentation generated on Mon, 11 Mar 2019 14:36:36 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|