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

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