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

Documentation generated on Mon, 11 Mar 2019 14:36:37 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.