cvs: pear /Auth Auth.php Container.php package.xml /Auth/Container DB.php

From: Date: Sat, 21 Jul 2001 22:02:48 +0000
Subject: cvs: pear /Auth Auth.php Container.php package.xml /Auth/Container DB.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-305@lists.php.net to get a copy of this message
mj Sat Jul 21 18:02:48 2001 EDT Modified files: /pear/Auth Auth.php Container.php package.xml /pear/Auth/Container DB.php Log: Improved some things in Auth: * Renamed some internal functions to be more compliant to the coding standards * Moved some parts of the code to the storage container. * Fixed a bug with expire time. * Added maximum idle time, which is (unlike expire time) refreshed on every request. * Improved behaviour of Container/DB.php

Index: pear/Auth/Auth.php diff -u pear/Auth/Auth.php:1.1 pear/Auth/Auth.php:1.2 --- pear/Auth/Auth.php:1.1 Wed Jul 11 16:35:31 2001 +++ pear/Auth/Auth.php Sat Jul 21 18:02:48 2001 @@ -16,7 +16,7 @@ // | Authors: Martin Jansen <mj@php.net> | // +----------------------------------------------------------------------+ // -// $Id: Auth.php,v 1.1 2001/07/11 20:35:31 mj Exp $ +// $Id: Auth.php,v 1.2 2001/07/21 22:02:48 mj Exp $ // /** @@ -25,9 +25,27 @@ * The PEAR::Auth class provides methods for creating an * authentication system using PHP. * + * Usage example: + * + * require_once "Auth/Auth.php"; + * + * // We use a MySQL as storage container in this example + * $a = new Auth("DB","mysql://martin:test@localhost/test"); + * + * // Detect, if the user is already logged in. If not, draw the + * // login form. + * $a->start(); + * + * if ($a->getAuth()) { + * echo "Welcome user ".$a->getUsername()."!<br>\n"; + * // output the content of your site, that is only visible + * // for users which have been authenticated successfully. + * } + * + * * @author Martin Jansen <mj@php.net> * @package Auth - * @version 0.1 2001-07-11 + * @version 0.2 2001-07-21 */ class Auth { @@ -38,7 +56,7 @@ * If this variable is set to 0, auth never expires * * @var integer - * @see check_auth() + * @see checkAuth() */ var $expire = 0; @@ -46,30 +64,39 @@ * Has the auth session expired? * * @var bool - * @see check_auth(), draw_login(); + * @see checkAuth(), drawLogin() */ var $expired = false; /** - * Storage object + * Maximum time of idleness in seconds * - * @var object - * @see Auth(), validate_login() + * 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. + * + * @var integer + * @see checkAuth() */ - var $storage = null; + var $idle = 5; /** - * Hash with information about the user - * @var array + * Is the maximum idletime over? + * + * @var bool + * @see checkAuth(), drawLogin(); */ - var $user_data = array(); + var $idled = false; /** - * Use md5 encryption to protect the password? - * @var bool - * @see validate_login() + * Storage object + * + * @var object + * @see Auth(), validateLogin() */ - var $use_md5 = true; + var $storage = ""; + + // {{{ Constructor /** * Constructor @@ -83,19 +110,23 @@ */ function Auth($storage_driver = "DB",$storage_options = "") { - $this->storage = $this->factory($storage_driver,$storage_options); + $this->storage = $this->_factory($storage_driver,$storage_options); + } + // }}} + // {{{ _factory /** * Return a storage driver based on $driver and $options * + * @access private + * @static * @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 = "") - { - + function _factory($driver, $options = "") + { $storage_path = "Auth/Container/".$driver.".php"; $storage_class = "Auth_Container_".$driver; @@ -104,20 +135,8 @@ return new $storage_class($options); } - /** - * Start new auth session - */ - function start() - { - session_start(); - - $this->assign_data(); - - if (!$this->check_auth()) { - $this->login(); - } - } - + // }}} + // {{{ assignData() /** * Assign data from login form to internal values @@ -131,7 +150,7 @@ * @global $HTTP_POST_VARS * @see Auth */ - function assign_data() + function assignData() { global $HTTP_POST_VARS; @@ -144,38 +163,26 @@ } } + // }}} + // {{{ start() + /** - * Validate if login data is ok - * - * This function fetches the login data from the storage - * container and compares it to the values of $this->username - * and $this->password. - * Note: Because of security reasons, we use md5-encryption - * for the passwords by default. If you dont' want to - * use md5, set $this->use_md5 on false. - * - * @see login - * @return bool + * Start new auth session */ - function validate_login() + function start() { - $login_data = $this->storage->fetch_data($this->username); - - $this->user_data = $login_data; - - if ($this->use_md5) { - $compare_password = md5($this->password); - } else { - $compare_password = $this->password; - } + session_start(); - if ($login_data['username'] == $this->username && $login_data['password'] == $compare_password) { - return true; - } else { - return false; + $this->assignData(); + + if (!$this->checkAuth()) { + $this->login(); } } + // }}} + // {{{ login() + /** * Login function */ @@ -187,29 +194,20 @@ * we have to validate it. */ if ($this->username != "") { - $login_ok = $this->validate_login(); + $login_ok = $this->storage->fetchData($this->username,$this->password); } - if (!$login_ok) { - $this->draw_login(); - } else { - $this->set_auth(); + /** + * If the login failed, output the login screen again + */ + if (!$login_ok || $this->username == "") { + $this->drawLogin(); } } - /** - * Logout function - */ - function logout() - { - $this->username = ""; - $this->password = ""; - - $GLOBALS['HTTP_SESSION_VARS']['registered'] = false; - $GLOBALS['HTTP_SESSION_VARS']['username'] = ""; - $GLOBALS['HTTP_SESSION_VARS']['timestamp'] = ""; - } + // }}} + // {{{ setExpire() /** * Set the maximum expire time @@ -218,7 +216,7 @@ * @param integer time in seconds * @param bool add time to current expire time or not */ - function set_expire($time,$add = false) + function setExpire($time,$add = false) { if ($add) { $this->expire += $time; @@ -227,26 +225,44 @@ } } + // }}} + // {{{ checkAuth() + /** * Checks if there is a session with valid auth information. * * @return boolean Whether or not the user is authenticated. */ - function check_auth() + function checkAuth() { if (isset($GLOBALS['HTTP_SESSION_VARS']['auth'])) { + /** Check if authentication session is expired */ if ($this->expire > 0 && ($GLOBALS['HTTP_SESSION_VARS']['auth']['timestamp'] + $this->expire) < time()) { $this->logout(); $this->expired = true; + + Auth::updateIdle(); + + return false; + } + + /** Check if maximum idle time is reached */ + if ($this->idle > 0 && + ($GLOBALS['HTTP_SESSION_VARS']['auth']['idle'] + $this->idle) < time()) { + + $this->logout(); + $this->idled = true; return false; } if ($GLOBALS['HTTP_SESSION_VARS']['auth']['registered'] == true && $GLOBALS['HTTP_SESSION_VARS']['auth']['username'] != "") { + Auth::updateIdle(); + return true; } } @@ -254,23 +270,50 @@ return false; } + // }}} + // {{{ setAuth() + /** - * Register variable in a session telling that the user has logged in successfully + * Register variable in a session telling that the user + * has logged in successfully + * + * @param string Username */ - function set_auth() + function setAuth($username) { if (!isset($GLOBALS['HTTP_SESSION_VARS']['auth'])) { session_register('auth'); } - $GLOBALS['auth'] = &$GLOBALS['HTTP_SESSION_VARS']['auth']; - $GLOBALS['auth'] = array(); + $GLOBALS['auth'] = &$GLOBALS['HTTP_SESSION_VARS']['auth']; + $GLOBALS['auth'] = array(); $GLOBALS['auth']['registered'] = true; - $GLOBALS['auth']['username'] = $this->username; - $GLOBALS['auth']['timestamp'] = time(); + $GLOBALS['auth']['username'] = $username; + $GLOBALS['auth']['timestamp'] = time(); + $GLOBALS['auth']['idle'] = time(); } + // }}} + // {{{ getAuth() + + /** + * Has the user been authenticated? + * + * @return bool True if the user is logged in, otherwise false. + */ + function getAuth() { + + if ($GLOBALS['HTTP_SESSION_VARS']['auth']['registered'] == true) { + return true; + } else { + return false; + } + } + + // }}} + // {{{ drawLogin() + /** * Draw the login form * @@ -282,13 +325,15 @@ * @param string Username if already entered * @param string Password if already entered */ - function draw_login($username = "", $password = "") + function drawLogin($username = "", $password = "") { echo "<center>\n"; if ($this->expired) { echo "<i>Your session expired. Please login again!</i>\n"; + } else if ($this->idled) { + echo "<i>You have been idle for too long. Please login again!</i>\n"; } echo "<form method=\"post\" action=\"".$GLOBALS['PHP_SELF']."\">\n"; @@ -302,45 +347,71 @@ echo "</tr>\n"; echo "<tr>\n"; echo " <td>Password:</td>\n"; - echo " <td><input type=\"password\" name=\"password\" value=\"".$password."\"></td>\n"; + echo " <td><input type=\"password\" name=\"password\"></td>\n"; echo "</tr>\n"; echo "<tr>\n"; echo " <td colspan=\"2\" bgcolor=\"#eeeeee\"><input type=\"submit\"></td>\n"; echo "</tr>\n"; echo "</table>\n"; echo "</form>\n"; - echo "</center>\n\n"; - + echo "</center>\n\n"; + } + + // }}} + // {{{ logout() + + /** + * Logout function + * + * This function clears any auth tokes in the currently + * active session + */ + function logout() + { + $this->username = ""; + $this->password = ""; + + session_unregister('auth'); } + // }}} + // {{{ updateIdle() + + /** + * Update the idletime + */ + function updateIdle() { + $GLOBALS['auth'] = &$GLOBALS['HTTP_SESSION_VARS']['auth']; + $GLOBALS['auth']['idle'] = time(); + } + + // }}} + // {{{ getUsername() + /** * Get the username * * @return string */ - function get_username() + function getUsername() { return $this->user_data['username']; } + // }}} + // {{{ getPassword() + /** * Get the password * * @return string */ - function get_password() + function getPassword() { return $this->user_data['password']; } - + // }}} } -// $a = new Auth("DB","mysql://martin:test@localhost/test"); - -// $a->start(); - -// echo "<br><br>\n"; - -// $a->logout(); ?> Index: pear/Auth/Container.php diff -u pear/Auth/Container.php:1.1 pear/Auth/Container.php:1.2 --- pear/Auth/Container.php:1.1 Wed Jul 11 16:35:31 2001 +++ pear/Auth/Container.php Sat Jul 21 18:02:48 2001 @@ -16,7 +16,7 @@ // | Authors: Martin Jansen <mj@php.net> | // +----------------------------------------------------------------------+ // -// $Id: Container.php,v 1.1 2001/07/11 20:35:31 mj Exp $ +// $Id: Container.php,v 1.2 2001/07/21 22:02:48 mj Exp $ // /** @@ -28,6 +28,8 @@ class Auth_Container { + // {{{ Constructor + /** * Constructor * @@ -39,6 +41,9 @@ { } + // }}} + // {{{ fetchData + /** * Fetch data from storage container * @@ -46,8 +51,10 @@ * * @access public */ - function fetch_data() + function fetchData() { } + + // }}} } ?> Index: pear/Auth/package.xml diff -u pear/Auth/package.xml:1.1 pear/Auth/package.xml:1.2 --- pear/Auth/package.xml:1.1 Wed Jul 11 16:38:16 2001 +++ pear/Auth/package.xml Sat Jul 21 18:02:48 2001 @@ -16,6 +16,24 @@ This is the initial independent release of the Auth package. </Notes> </Release> + + <Release> + <Version>0.2</Version> + <Date>2001-07-22</Date> + <Notes> + Improved a bunch of things: + + * Renamed some internal functions to be more compliant + to the coding standards + * Moved some parts of the code to the storage + container. + * Fixed a bug with expire time. + * Added maximum idle time, which is (unlike expire + time) refreshed on every request. + * Improved behaviour of Container/DB.php + </Notes> + </Release> + <FileList> <Dir>/</Dir> <File>Auth.php</File> Index: pear/Auth/Container/DB.php diff -u pear/Auth/Container/DB.php:1.1 pear/Auth/Container/DB.php:1.2 --- pear/Auth/Container/DB.php:1.1 Wed Jul 11 16:35:31 2001 +++ pear/Auth/Container/DB.php Sat Jul 21 18:02:48 2001 @@ -16,7 +16,7 @@ // | Authors: Martin Jansen <mj@php.net> | // +----------------------------------------------------------------------+ // -// $Id: DB.php,v 1.1 2001/07/11 20:35:31 mj Exp $ +// $Id: DB.php,v 1.2 2001/07/21 22:02:48 mj Exp $ // require_once "Auth/Container.php"; @@ -46,7 +46,9 @@ * @var string * @see fetch_data */ - var $username_col = "username"; + var $usernameCol = "username"; + + // {{{ Constructor /** * Constructor of the container class @@ -63,8 +65,8 @@ $this->db = DB::Connect($dsn); - if (DB::isError($db)) { - return new DB_Error($db->code,PEAR_ERROR_DIE); + if (DB::isError($this->db)) { + return new DB_Error($this->db->code,PEAR_ERROR_DIE); } } @@ -80,10 +82,13 @@ } else { - return new PEAR_Error("The given dsn (".$dsn.") was not valid in file ".__FILE__." at line ".__LINE__,41,PEAR_ERROR_RETURN,null,null); + return new PEAR_Error("The given dsn was not valid in file ".__FILE__." at line ".__LINE__,41,PEAR_ERROR_RETURN,null,null); } } + // }}} + // {{{ fetchData() + /** * Get user information from database * @@ -94,26 +99,38 @@ * table. * * @param string Username - * @return array Hash with database information for $username + * @param string Password + * @return mixed Error object or boolean */ - function fetch_data($username) + function fetchData($username,$password) { - $query = sprintf("SELECT * FROM %s WHERE %s = '%s'", + $query = sprintf("SELECT username FROM %s + WHERE username = %s + AND password = '%s'", $this->table, - $this->username_col, - $username + $this->db->quoteString($username), + md5($password) ); $res = $this->db->query($query); if (DB::isError($res)) { - return new DB_Error($dsn->code,PEAR_ERROR_DIE); + return new DB_Error($res->code,PEAR_ERROR_DIE); } else { $entry = $res->FetchRow(DB_FETCHMODE_ASSOC); - - return $entry; + + if (is_array($entry)) { + Auth::setAuth($entry['username']); + $res->free(); + + return true; + } else { + return false; + } } } + + // }}} } ?>
« previous php.pear.cvs (#305) next »