Auth
[ class tree: Auth ] [ index: Auth ] [ all elements ]

Source for file Auth.php

Documentation is available at Auth.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Martin Jansen <mj@php.net>                                  |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Auth.php,v 1.81 2004/06/09 15:03:53 yavo Exp $
  20. //
  21.  
  22. require_once 'PEAR.php';
  23.  
  24. define('AUTH_IDLED',       -1);
  25. define('AUTH_EXPIRED',     -2);
  26. define('AUTH_WRONG_LOGIN'-3);
  27. define('AUTH_SECURITY_BREACH'-4);
  28.  
  29. /**
  30.  * PEAR::Auth
  31.  *
  32.  * The PEAR::Auth class provides methods for creating an
  33.  * authentication system using PHP.
  34.  *
  35.  * @author  Martin Jansen <mj@php.net>
  36.  * @package Auth
  37.  * @version $Revision: 1.81 $
  38.  */
  39. class Auth {
  40.  
  41.     /**
  42.      * Auth lifetime in seconds
  43.      *
  44.      * If this variable is set to 0, auth never expires
  45.      *
  46.      * @var  integer 
  47.      * @see  setExpire(), checkAuth()
  48.      */
  49.     var $expire = 0;
  50.  
  51.     /**
  52.      * Has the auth session expired?
  53.      *
  54.      * @var   bool 
  55.      * @see   checkAuth(), drawLogin()
  56.      */
  57.     var $expired = false;
  58.  
  59.     /**
  60.      * Maximum idletime in seconds
  61.      *
  62.      * The difference to $expire is, that the idletime gets
  63.      * refreshed each time checkAuth() is called. If this
  64.      * variable is set to 0, idletime is never checked.
  65.      *
  66.      * @var integer 
  67.      * @see setIdle(), checkAuth()
  68.      */
  69.     var $idle = 0;
  70.  
  71.     /**
  72.      * Is the maximum idletime over?
  73.      *
  74.      * @var boolean 
  75.      * @see checkAuth(), drawLogin();
  76.      */
  77.     var $idled = false;
  78.  
  79.     /**
  80.      * Storage object
  81.      *
  82.      * @var object 
  83.      * @see Auth(), validateLogin()
  84.      */
  85.     var $storage = '';
  86.  
  87.     /**
  88.      * User-defined function that creates the login screen
  89.      *
  90.      * @var string 
  91.      */
  92.     var $loginFunction = '';
  93.  
  94.     /**
  95.      * Should the login form be displayed, and are users allowed to authenticate via this page?
  96.      *
  97.      * @var   bool 
  98.      * @see   setShowlogin()
  99.      */
  100.     var $showLogin = true;
  101.  
  102.     /**
  103.      * Current authentication status
  104.      *
  105.      * @var string 
  106.      */
  107.     var $status = '';
  108.  
  109.     /**
  110.      * Username
  111.      *
  112.      * @var string 
  113.      */
  114.     var $username = '';
  115.  
  116.     /**
  117.      * Password
  118.      *
  119.      * @var string 
  120.      */
  121.     var $password = '';
  122.  
  123.     /**
  124.      * Login callback function name
  125.      *
  126.      * @var string 
  127.      * @see setLoginCallback()
  128.      */
  129.     var $loginCallback = '';
  130.  
  131.     /**
  132.      * Failed Login callback function name
  133.      *
  134.      * @var string 
  135.      * @see setLoginFailedCallback()
  136.      */
  137.     var $loginFailedCallback = '';
  138.  
  139.     /**
  140.      * Logout callback function name
  141.      *
  142.      * @var string 
  143.      * @see setLogoutCallback()
  144.      */
  145.     var $logoutCallback = '';
  146.  
  147.     /**
  148.      * Auth session-array name
  149.      *
  150.      * @var string 
  151.      */
  152.     var $_sessionName '_authsession';
  153.     
  154.     /**
  155.      * Package Version
  156.      *
  157.      * @var string 
  158.      */
  159.     var $version = "@version@";
  160.  
  161.     /**
  162.      * Flag to use advanced security
  163.      * When set extra checks will be made to see if the
  164.      * user's IP or useragent have changed across requests.
  165.      * Turned off by default to preserve BC.
  166.      *
  167.      * @var boolean 
  168.      */     
  169.     var $advancedsecurity = false;
  170.     
  171.     /**
  172.      * Username key in POST array
  173.      *
  174.      * @var string 
  175.      */
  176.     var $_postUsername 'username';
  177.     
  178.     /**
  179.      * Password key in POST array
  180.      *
  181.      * @var string 
  182.      */
  183.     var $_postPassword 'password';
  184.     
  185.     /**
  186.      * A hash to hold various superglobals as reference
  187.      * @var array 
  188.      */
  189.     var $authdata;
  190.  
  191.     // {{{ Constructor
  192.  
  193.     /**
  194.      * Constructor
  195.      *
  196.      * Set up the storage driver.
  197.      *
  198.      * @param string    Type of the storage driver
  199.      * @param mixed     Additional options for the storage driver
  200.      *                   (example: if you are using DB as the storage
  201.      *                    driver, you have to pass the dsn string here)
  202.      *
  203.      * @param string    Name of the function that creates the login form
  204.      * @param boolean   Should the login form be displayed if neccessary?
  205.      * @return void 
  206.      */
  207.     function Auth($storageDriver$options ''$loginFunction ''$showLogin = true)
  208.     {
  209.         if(is_array($options)){
  210.             if (!empty($options['sessionName'])) {
  211.                 $this->_sessionName $options['sessionName'];
  212.                 unset($options['sessionName']);
  213.             }
  214.             
  215.             if (!empty($options['postUsername'])) {
  216.                 $this->_postUsername $options['postUsername'];
  217.                 unset($options['postUsername']);
  218.             }
  219.             
  220.             if (!empty($options['postPassword'])) {
  221.                 $this->_postPassword $options['postPassword'];
  222.                 unset($options['postPassword']);
  223.             }
  224.         }
  225.         
  226.         if ($loginFunction != '' && is_callable($loginFunction)) {
  227.             $this->loginFunction = $loginFunction;
  228.         }
  229.  
  230.         if (is_bool($showLogin)) {
  231.             $this->showLogin = $showLogin;
  232.         }
  233.  
  234.         if (is_object($storageDriver)) {
  235.             $this->storage =$storageDriver;
  236.         else {
  237.             $this->storage = $this->_factory($storageDriver$options);
  238.         }
  239.         // Pass a reference to auth to the container, ugly but works
  240.         // this is used by the DB container to use method setAuthData not staticaly.
  241.         $this->storage->_auth_obj =$this;
  242.         
  243.     }
  244.  
  245.     // }}}
  246.     // {{{ _factory()
  247.  
  248.     /**
  249.      * Return a storage driver based on $driver and $options
  250.      *
  251.      * @access private
  252.      * @static
  253.      * @param  string $driver  Type of storage class to return
  254.      * @param  string $options Optional parameters for the storage class
  255.      * @return object Object   Storage object
  256.      */
  257.     function _factory($driver$options '')
  258.     {
  259.         $storage_path 'Auth/Container/' $driver '.php';
  260.         $storage_class 'Auth_Container_' $driver;
  261.  
  262.         require_once $storage_path;
  263.  
  264.         return new $storage_class($options);
  265.     }
  266.  
  267.     // }}}
  268.     // {{{ assignData()
  269.  
  270.     /**
  271.      * Assign data from login form to internal values
  272.      *
  273.      * This function takes the values for username and password
  274.      * from $HTTP_POST_VARS/$_POST and assigns them to internal variables.
  275.      * If you wish to use another source apart from $HTTP_POST_VARS/$_POST,
  276.      * you have to derive this function.
  277.      *
  278.      * @access private
  279.      * @global $HTTP_POST_VARS, $_POST 
  280.      * @see    Auth
  281.      * @return void 
  282.      */
  283.     function assignData()
  284.     {
  285.         $post &$this->_importGlobalVariable('post');
  286.  
  287.         if (isset($post[$this->_postUsername]&& $post[$this->_postUsername!= ''{
  288.             $this->username = (get_magic_quotes_gpc(== 1 ? stripslashes($post[$this->_postUsername]$post[$this->_postUsername]);
  289.         }
  290.  
  291.         if (isset($post[$this->_postPassword]&& $post[$this->_postPassword!= ''{
  292.             $this->password = (get_magic_quotes_gpc(== 1 ? stripslashes($post[$this->_postPassword]$post[$this->_postPassword);
  293.         }
  294.  
  295.     }
  296.  
  297.     // }}}
  298.     // {{{ start()
  299.  
  300.     /**
  301.      * Start new auth session
  302.      *
  303.      * @access public
  304.      * @return void 
  305.      */
  306.     function start()
  307.     {
  308.         $this->assignData();
  309.  
  310.         @session_start();
  311.  
  312.         if (!$this->checkAuth(&& $this->showLogin{
  313.             $this->login();
  314.         }
  315.     }
  316.  
  317.     // }}}
  318.     // {{{ login()
  319.  
  320.     /**
  321.      * Login function
  322.      *
  323.      * @access private
  324.      * @return void 
  325.      */
  326.     function login()
  327.     {
  328.         $login_ok = false;
  329.  
  330.         /**
  331.          * When the user has already entered a username,
  332.          * we have to validate it.
  333.          */
  334.         if (!empty($this->username)) {
  335.             if (true === $this->storage->fetchData($this->username$this->password)) {
  336.                 $login_ok = true;
  337.             }
  338.         }
  339.  
  340.         if (!empty($this->username&& $login_ok{
  341.             $this->setAuth($this->username);
  342.             if (is_callable($this->loginCallback)) {
  343.                 call_user_func($this->loginCallback,$this->username@$this);
  344.                 #call_user_func_array($this->loginFailedCallback, array($this->username, &$this));
  345.             }
  346.         }
  347.  
  348.         /**
  349.          * If the login failed or the user entered no username,
  350.          * output the login screen again.
  351.          */
  352.         if (!empty($this->username&& !$login_ok{
  353.             $this->status = AUTH_WRONG_LOGIN;
  354.             if (is_callable($this->loginFailedCallback)) {
  355.                 call_user_func($this->loginFailedCallback,$this->username@$this);
  356.                 #call_user_func_array($this->loginFailedCallback, array($this->username, &$this));
  357.             }
  358.         }
  359.  
  360.         if ((empty($this->username|| !$login_ok&& $this->showLogin{
  361.             $this->drawLogin($this->storage->activeUser);
  362.             return;
  363.         }
  364.     }
  365.  
  366.     // }}}
  367.     // {{{ setExpire()
  368.  
  369.     /**
  370.      * Set the maximum expire time
  371.      *
  372.      * @access public
  373.      * @param  integer time in seconds
  374.      * @param  bool    add time to current expire time or not
  375.      * @return void 
  376.      */
  377.     function setExpire($time$add = false)
  378.     {
  379.         if ($add{
  380.             $this->expire += $time;
  381.         else {
  382.             $this->expire = $time;
  383.         }
  384.     }
  385.  
  386.     // }}}
  387.     // {{{ setIdle()
  388.  
  389.     /**
  390.      * Set the maximum idle time
  391.      *
  392.      * @access public
  393.      * @param  integer time in seconds
  394.      * @param  bool    add time to current maximum idle time or not
  395.      * @return void 
  396.      */
  397.     function setIdle($time$add = false)
  398.     {
  399.         if ($add{
  400.             $this->idle += $time;
  401.         else {
  402.             $this->idle = $time;
  403.         }
  404.     }
  405.  
  406.     // }}}
  407.     // {{{ setSessionname()
  408.  
  409.     /**
  410.      * Set name of the session to a customized value.
  411.      *
  412.      * If you are using multiple instances of PEAR::Auth
  413.      * on the same domain, you can change the name of
  414.      * session per application via this function.
  415.      *
  416.      * @access public
  417.      * @param  string New name for the session
  418.      * @return void 
  419.      */
  420.     function setSessionname($name 'PHPSESSID')
  421.     {
  422.         @session_name($name);
  423.     }
  424.  
  425.     // }}}
  426.     // {{{ setShowLogin()
  427.  
  428.     /**
  429.      * Should the login form be displayed if neccessary?
  430.      *
  431.      * @access public
  432.      * @param  bool    show login form or not
  433.      * @return void 
  434.      */
  435.     function setShowLogin($showLogin = true)
  436.     {
  437.         $this->showLogin = $showLogin;
  438.     }
  439.  
  440.     /**
  441.      * Register a callback function to be called on user login.
  442.      * The function will receive two parameters, the username and a reference to the auth object.
  443.      *
  444.      * @access public
  445.      * @param  string  callback function name
  446.      * @return void 
  447.      * @see    setLogoutCallback()
  448.      */
  449.     function setLoginCallback($loginCallback)
  450.     {
  451.         $this->loginCallback = $loginCallback;
  452.     }
  453.  
  454.     /**
  455.      * Register a callback function to be called on failed user login.
  456.      * The function will receive a single parameter, the username and a reference to the auth object.
  457.      *
  458.      * @access public
  459.      * @param  string  callback function name
  460.      * @return void 
  461.      */
  462.     function setFailedLoginCallback($loginFailedCallback)
  463.     {
  464.         $this->loginFailedCallback = $loginFailedCallback;
  465.     }
  466.  
  467.     /**
  468.      * Register a callback function to be called on user logout.
  469.      * The function will receive three parameters, the username and a reference to the auth object.
  470.      *
  471.      * @access public
  472.      * @param  string  callback function name
  473.      * @return void 
  474.      * @see    setLoginCallback()
  475.      */
  476.     function setLogoutCallback($logoutCallback)
  477.     {
  478.         $this->logoutCallback = $logoutCallback;
  479.     }
  480.  
  481.     // }}}
  482.     // {{{ setAuthData()
  483.  
  484.     /**
  485.      * Register additional information that is to be stored
  486.      * in the session.
  487.      *
  488.      * @access public
  489.      * @param  string  Name of the data field
  490.      * @param  mixed   Value of the data field
  491.      * @param  boolean Should existing data be overwritten? (default
  492.      *                  is true)
  493.      * @return void 
  494.      */
  495.     function setAuthData($name$value$overwrite = true)
  496.     {
  497.         $session &Auth::_importGlobalVariable('session');
  498.  
  499.         if (!empty($session[$this->_sessionName]['data'][$name]&& $overwrite == false{
  500.             return;
  501.         }
  502.         $session[$this->_sessionName]['data'][$name$value;
  503.     }
  504.  
  505.     // }}}
  506.     // {{{ getAuthData()
  507.  
  508.     /**
  509.      * Get additional information that is stored in the session.
  510.      *
  511.      * If no value for the first parameter is passed, the method will
  512.      * return all data that is currently stored.
  513.      *
  514.      * @access public
  515.      * @param  string Name of the data field
  516.      * @return mixed  Value of the data field.
  517.      */
  518.     function getAuthData($name = null)
  519.     {
  520.         $session &Auth::_importGlobalVariable('session');
  521.         if (!isset($session[$this->_sessionName]['data'])) {
  522.             return null;
  523.         }
  524.  
  525.         if (is_null($name)) {
  526.             if (isset($session[$this->_sessionName]['data'])) {
  527.                 return $session[$this->_sessionName]['data'];
  528.             else {
  529.                 return null;
  530.             }
  531.         }
  532.         if (isset($session[$this->_sessionName]['data'][$name])) {
  533.             return $session[$this->_sessionName]['data'][$name];
  534.         else {
  535.             return null;
  536.         }
  537.     }
  538.  
  539.     // }}}
  540.     // {{{ setAuth()
  541.  
  542.     /**
  543.      * Register variable in a session telling that the user
  544.      * has logged in successfully
  545.      *
  546.      * @access public
  547.      * @param  string Username
  548.      * @return void 
  549.      */
  550.     function setAuth($username)
  551.     {
  552.         @session_start();
  553.         $session &Auth::_importGlobalVariable('session');
  554.         $server &$this->_importGlobalVariable('server');
  555.  
  556.         if (!isset($session[$this->_sessionName]&& !isset($_SESSION)) {
  557.             session_register($this->_sessionName);
  558.         }
  559.  
  560.         if (!isset($session[$this->_sessionName]|| !is_array($session[$this->_sessionName])) {
  561.             $session[$this->_sessionName= array();
  562.         }
  563.  
  564.         if (!isset($session[$this->_sessionName]['data'])) {
  565.             $session[$this->_sessionName]['data']       = array();
  566.         }
  567.  
  568.         $session[$this->_sessionName]['sessionip'= isset($_SERVER['REMOTE_ADDR']$_SERVER['REMOTE_ADDR''';
  569.         $session[$this->_sessionName]['sessionuseragent'= isset($_SERVER['HTTP_USER_AGENT']$_SERVER['HTTP_USER_AGENT''';
  570.  
  571.         $session[$this->_sessionName]['registered'= true;
  572.         $session[$this->_sessionName]['username']   $username;
  573.         $session[$this->_sessionName]['timestamp']  time();
  574.         $session[$this->_sessionName]['idle']       time();
  575.     }
  576.     
  577.     function setAdvancedSecurity($flag=true{
  578.         $this->advancedsecurity = $flag;
  579.     }
  580.  
  581.     // }}}
  582.     // {{{ checkAuth()
  583.  
  584.     /**
  585.      * Checks if there is a session with valid auth information.
  586.      *
  587.      * @access private
  588.      * @return boolean  Whether or not the user is authenticated.
  589.      */
  590.     function checkAuth()
  591.     {
  592.         $session &$this->_importGlobalVariable('session');
  593.         
  594.         if ($this->advancedsecurity{
  595.             // Check for ip change
  596.             if (isset($session[$this->_sessionName]['sessionip']&& isset($_SERVER['REMOTE_ADDR']&& $session[$this->_sessionName]['sessionip'!= $_SERVER['REMOTE_ADDR']{
  597.                 // Check if the IP of the user has changed, if so we assume a man in the middle attack and log him out
  598.                 $this->expired = true;
  599.                 $this->status = AUTH_SECURITY_BREACH;
  600.                 $this->logout();
  601.                 return false;
  602.             }
  603.             
  604.             // Check for useragent change
  605.             if (isset($session[$this->_sessionName]['sessionuseragent']&& isset($_SERVER['HTTP_USER_AGENT']&& $session[$this->_sessionName]['sessionuseragent'!= $_SERVER['HTTP_USER_AGENT']{
  606.                 // Check if the User-Agent of the user has changed, if so we assume a man in the middle attack and log him out
  607.  
  608.                 $this->expired = true;
  609.                 $this->status = AUTH_SECURITY_BREACH;
  610.                 return false;
  611.                 $this->logout();
  612.             }
  613.             
  614.         }
  615.  
  616.         if (isset($session[$this->_sessionName])) {
  617.             // Check if authentication session is expired
  618.             if ($this->expire > 0 &&
  619.                 isset($session[$this->_sessionName]['timestamp']&&
  620.                 ($session[$this->_sessionName]['timestamp'$this->expiretime()) {
  621.                 $this->expired = true;
  622.                 $this->status = AUTH_EXPIRED;
  623.                 $this->logout();
  624.                 return false;
  625.             }
  626.  
  627.             // Check if maximum idle time is reached
  628.             if ($this->idle > 0 &&
  629.                 isset($session[$this->_sessionName]['idle']&&
  630.                 ($session[$this->_sessionName]['idle'$this->idletime()) {
  631.  
  632.  
  633.                 $this->idled = true;
  634.                 $this->status = AUTH_IDLED;
  635.                 $this->logout();
  636.                 return false;
  637.             }
  638.  
  639.             if (isset($session[$this->_sessionName]['registered']&&
  640.                 isset($session[$this->_sessionName]['username']&&
  641.                 $session[$this->_sessionName]['registered'== true &&
  642.                 $session[$this->_sessionName]['username'!= ''{
  643.  
  644.                 Auth::updateIdle();
  645.  
  646.                 return true;
  647.             }
  648.         }
  649.  
  650.         return false;
  651.     }
  652.  
  653.     // }}}
  654.     // {{{ getAuth()
  655.  
  656.     /**
  657.      * Has the user been authenticated?
  658.      *
  659.      * @access public
  660.      * @return bool  True if the user is logged in, otherwise false.
  661.      */
  662.     function getAuth()
  663.     {
  664.         $session &$this->_importGlobalVariable('session');
  665.  
  666.         if (!empty($session&&
  667.             (isset($session[$this->_sessionName]['registered']&&
  668.              $session[$this->_sessionName]['registered'=== true))
  669.         {
  670.             return true;
  671.         else {
  672.             return false;
  673.         }
  674.     }
  675.  
  676.     // }}}
  677.     // {{{ drawLogin()
  678.  
  679.     /**
  680.      * Draw the login form
  681.      *
  682.      * Normally you will not use this output in your application,
  683.      * because you can pass a different function name to the
  684.      * constructor. For more information on this, please
  685.      * consult the documentation.
  686.      *
  687.      * @access private
  688.      * @param  string  Username if already entered
  689.      * @return void 
  690.      */
  691.     function drawLogin($username '')
  692.     {
  693.         if (is_callable($this->loginFunction)) {
  694.             call_user_func($this->loginFunction$username$this->status@$this);
  695.             #call_user_func_array($this->loginFailedCallback, array($this->username, &$this));
  696.         else {
  697.             $server &$this->_importGlobalVariable('server');
  698.  
  699.             echo '<center>'."\n";
  700.  
  701.             if (!empty($this->status&& $this->status == AUTH_EXPIRED{
  702.                 echo '<i>Your session has expired. Please login again!</i>'."\n";
  703.             else if (!empty($this->status&& $this->status == AUTH_IDLED{
  704.                 echo '<i>You have been idle for too long. Please login again!</i>'."\n";
  705.             else if (!empty ($this->status&& $this->status == AUTH_WRONG_LOGIN{
  706.                 echo '<i>Wrong login data!</i>'."\n";
  707.             else if (!empty ($this->status&& $this->status == AUTH_SECURITY_BREACH{
  708.                 echo '<i>Security problem detected. </i>'."\n";
  709.             }
  710.             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);
  711.  
  712.             echo '<form method="post" action="' $server['PHP_SELF''">'."\n";
  713.             echo '<table border="0" cellpadding="2" cellspacing="0" summary="login form">'."\n";
  714.             echo '<tr>'."\n";
  715.             echo '    <td colspan="2" bgcolor="#eeeeee"><b>Login:</b></td>'."\n";
  716.             echo '</tr>'."\n";
  717.             echo '<tr>'."\n";
  718.             echo '    <td>Username:</td>'."\n";
  719.             echo '    <td><input type="text" name="username" value="' $username '" /></td>'."\n";
  720.             echo '</tr>'."\n";
  721.             echo '<tr>'."\n";
  722.             echo '    <td>Password:</td>'."\n";
  723.             echo '    <td><input type="password" name="password" /></td>'."\n";
  724.             echo '</tr>'."\n";
  725.             echo '<tr>'."\n";
  726.             echo '    <td colspan="2" bgcolor="#eeeeee"><input type="submit" /></td>'."\n";
  727.             echo '</tr>'."\n";
  728.             echo '</table>'."\n";
  729.             echo '</form>'."\n";
  730.             echo '</center>'."\n\n";
  731.         }
  732.     }
  733.  
  734.     // }}}
  735.     // {{{ logout()
  736.  
  737.     /**
  738.      * Logout function
  739.      *
  740.      * This function clears any auth tokens in the currently
  741.      * active session and executes the logout callback function,
  742.      * if any
  743.      *
  744.      * @access public
  745.      * @return void 
  746.      */
  747.     function logout()
  748.     {
  749.         $session &$this->_importGlobalVariable('session');
  750.  
  751.         if (is_callable($this->logoutCallback)) {
  752.             call_user_func($this->logoutCallback$session[$this->_sessionName]['username']@$this);
  753.             #call_user_func_array($this->loginFailedCallback, array($this->username, &$this));
  754.         }
  755.  
  756.         $this->username = '';
  757.         $this->password = '';
  758.  
  759.         $session[$this->_sessionName= array();
  760.         if (isset($_SESSION)) {
  761.             unset($session[$this->_sessionName]);
  762.         else {
  763.             session_unregister($this->_sessionName);
  764.         }
  765.     }
  766.  
  767.     // }}}
  768.     // {{{ updateIdle()
  769.  
  770.     /**
  771.      * Update the idletime
  772.      *
  773.      * @access private
  774.      * @return void 
  775.      */
  776.     function updateIdle()
  777.     {
  778.         $session &$this->_importGlobalVariable('session');
  779.         $session[$this->_sessionName]['idle'time();
  780.     }
  781.  
  782.     // }}}
  783.     // {{{ getUsername()
  784.  
  785.     /**
  786.      * Get the username
  787.      *
  788.      * @access public
  789.      * @return string 
  790.      */
  791.     function getUsername()
  792.     {
  793.         $session &$this->_importGlobalVariable('session');
  794.         if (!isset($session[$this->_sessionName]['username'])) {
  795.             return '';
  796.         }
  797.         return $session[$this->_sessionName]['username'];
  798.     }
  799.  
  800.     // }}}
  801.     // {{{ getStatus()
  802.  
  803.     /**
  804.      * Get the current status
  805.      *
  806.      * @access public
  807.      * @return string 
  808.      */
  809.     function getStatus()
  810.     {
  811.         return $this->status;
  812.     }
  813.  
  814.     // }}}
  815.     // {{{ sessionValidThru()
  816.  
  817.     /**
  818.      * Returns the time up to the session is valid
  819.      *
  820.      * @access public
  821.      * @return integer 
  822.      */
  823.     function sessionValidThru()
  824.     {
  825.         $session &$this->_importGlobalVariable('session');
  826.         if (!isset($session[$this->_sessionName]['idle'])) {
  827.             return 0;
  828.         }
  829.         return ($session[$this->_sessionName]['idle'$this->idle);
  830.     }
  831.  
  832.     // }}}
  833.     // {{{ listUsers()
  834.  
  835.     /**
  836.      * List all users that are currently available in the storage
  837.      * container
  838.      *
  839.      * @access public
  840.      * @return array 
  841.      */
  842.     function listUsers()
  843.     {
  844.         return $this->storage->listUsers();
  845.     }
  846.  
  847.     // }}}
  848.     // {{{ addUser()
  849.  
  850.     /**
  851.      * Add user to the storage container
  852.      *
  853.      * @access public
  854.      * @param  string Username
  855.      * @param  string Password
  856.      * @param  mixed  Additional parameters
  857.      * @return mixed  True on success, PEAR error object on error
  858.      *                 and AUTH_METHOD_NOT_SUPPORTED otherwise.
  859.      */
  860.     function addUser($username$password$additional '')
  861.     {
  862.         return $this->storage->addUser($username$password$additional);
  863.     }
  864.  
  865.     // }}}
  866.     // {{{ removeUser()
  867.  
  868.     /**
  869.      * Remove user from the storage container
  870.      *
  871.      * @access public
  872.      * @param  string Username
  873.      * @return mixed  True on success, PEAR error object on error
  874.      *                 and AUTH_METHOD_NOT_SUPPORTED otherwise.
  875.      */
  876.     function removeUser($username)
  877.     {
  878.         return $this->storage->removeUser($username);
  879.     }
  880.  
  881.     // }}}
  882.     // {{{ changePassword()
  883.  
  884.     /**
  885.      * Change password for user in the storage container
  886.      *
  887.      * @access public
  888.      * @param string Username
  889.      * @param string The new password
  890.      * @return mixed True on success, PEAR error object on error
  891.      *                and AUTH_METHOD_NOT_SUPPORTED otherwise.
  892.      */
  893.     function changePassword($username$password)
  894.     {
  895.         return $this->storage->changePassword($username$password);
  896.     }
  897.  
  898.     // }}}
  899.     // {{{ _importGlobalVariable()
  900.  
  901.     /**
  902.      * Import variables from special namespaces.
  903.      *
  904.      * @access private
  905.      * @param string Type of variable (server, session, post)
  906.      * @return array 
  907.      */
  908.     function &_importGlobalVariable($variable)
  909.     {
  910.         $var = null;
  911.  
  912.         switch (strtolower($variable)) {
  913.             case 'session' :
  914.                 isset($_SESSION$var &$_SESSION $var &$GLOBALS['HTTP_SESSION_VARS';
  915.                 break;
  916.  
  917.             case 'server' :
  918.                 isset($_SERVER$var &$_SERVER $var &$GLOBALS['HTTP_SERVER_VARS'];
  919.                 break;
  920.  
  921.             case 'post' :
  922.                 isset($_POST$var &$_POST $var &$GLOBALS['HTTP_POST_VARS'];
  923.                 break;
  924.  
  925.             case 'cookie' :
  926.                 isset($_COOKIE$var &$_COOKIE $var &$GLOBALS['HTTP_COOKIE_VARS'];
  927.                 break;
  928.  
  929.             case 'get' :
  930.                 isset($_GET$var &$_GET $var &$GLOBALS['HTTP_GET_VARS'];
  931.                 break;
  932.  
  933.             default:
  934.                 break;
  935.  
  936.         }
  937.  
  938.         return $var;
  939.     }
  940.  
  941.     // }}}
  942. }
  943. ?>

Documentation generated on Mon, 11 Mar 2019 10:17:23 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.