Ignore:
Timestamp:
Feb 20, 2014 3:03:59 AM (10 years ago)
Author:
anonymous
Message:

Completed integrating /branches/eli_branch into /trunk. Changes include:

  • Removed closing ?> from end of files
  • Upgrade old-style contructor methods to use construct() instead.
  • Class properties and methods defined as public, private, static or protected
  • Ensure code runs under E_ALL with only mysql_* deprecated warnings
  • Search for the '@' symbol anywhere it might be used to supress runtime errors, then replace with proper error recovery.
  • Run the php cli -l option to check files for syntax errors.
  • Bring tests up-to-date with latest version and methods of PHPUnit
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/lib/Auth_File.inc.php

    r396 r468  
    44 * For details visit the project site: <http://trac.strangecode.com/codebase/>
    55 * Copyright 2001-2012 Strangecode, LLC
    6  * 
     6 *
    77 * This file is part of The Strangecode Codebase.
    88 *
     
    1111 * Free Software Foundation, either version 3 of the License, or (at your option)
    1212 * any later version.
    13  * 
     13 *
    1414 * The Strangecode Codebase is distributed in the hope that it will be useful, but
    1515 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1616 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    1717 * details.
    18  * 
     18 *
    1919 * You should have received a copy of the GNU General Public License along with
    2020 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
    2121 */
    2222
    23 /**
     23/*
    2424 * Auth_File.inc.php
    2525 *
     
    3030 * @version 1.2
    3131 */
    32  
     32
    3333// Usage example:
    3434// $auth = new Auth_File();
     
    4040// ));
    4141
    42 // Available encryption types for class Auth_SQL.
    43 define('AUTH_ENCRYPT_MD5', 'md5');
    44 define('AUTH_ENCRYPT_CRYPT', 'crypt');
    45 define('AUTH_ENCRYPT_SHA1', 'sha1');
    46 define('AUTH_ENCRYPT_PLAINTEXT', 'plaintext');
    47 
    4842class Auth_File {
    49    
     43
     44    // Available encryption types for class Auth_File.
     45    const ENCRYPT_MD5 = 'md5';
     46    const ENCRYPT_CRYPT = 'crypt';
     47    const ENCRYPT_SHA1 = 'sha1';
     48    const ENCRYPT_PLAINTEXT = 'plaintext';
     49
    5050    // Namespace of this auth object.
    51     var $_ns;
    52    
     51    private $_ns;
     52
    5353    // Parameters to be specified by setParam().
    54     var $_params = array();
    55     var $_default_params = array(
    56        
     54    private $_params = array();
     55    private $_default_params = array(
     56
    5757        // Full path to htpasswd file.
    5858        'htpasswd_file' => null,
    5959
    60         // The type of encryption to use for passwords stored in the db_table. Use one of the AUTH_ENCRYPT_* types specified above.
    61         'encryption_type' => AUTH_ENCRYPT_CRYPT,
     60        // The type of encryption to use for passwords stored in the db_table. Use one of the self::ENCRYPT_* types specified above.
     61        'encryption_type' => self::ENCRYPT_CRYPT,
    6262
    6363        // The URL to the login script.
     
    7777
    7878    // Associative array of usernames to hashed passwords.
    79     var $_users = array();
    80 
    81     /**
    82      * Constructs a new htpasswd authentication object.
    83      *
    84      * @access public
    85      *
    86      * @param optional array $params  A hash containing parameters.
    87      */
    88     function Auth_File($namespace='')
     79    private $_users = array();
     80
     81    /*
     82    * Constructs a new htpasswd authentication object.
     83    *
     84    * @access public
     85    *
     86    * @param optional array $params  A hash containing parameters.
     87    */
     88    public function __construct($namespace='')
    8989    {
    9090        $this->_ns = $namespace;
     
    9494    }
    9595
    96     /**
    97      * Set the params of an auth object.
    98      *
    99      * @param  array $params   Array of parameter keys and value to set.
    100      * @return bool true on success, false on failure
    101      */
    102     function setParam($params)
     96    /*
     97    * Set the params of an auth object.
     98    *
     99    * @param  array $params   Array of parameter keys and value to set.
     100    * @return bool true on success, false on failure
     101    */
     102    public function setParam($params)
    103103    {
    104104        if (isset($params) && is_array($params)) {
     
    108108    }
    109109
    110     /**
    111      * Return the value of a parameter, if it exists.
    112      *
    113      * @access public
    114      * @param string $param        Which parameter to return.
    115      * @return mixed               Configured parameter value.
    116      */
    117     function getParam($param)
    118     {
    119         $app =& App::getInstance();
    120    
     110    /*
     111    * Return the value of a parameter, if it exists.
     112    *
     113    * @access public
     114    * @param string $param        Which parameter to return.
     115    * @return mixed               Configured parameter value.
     116    */
     117    public function getParam($param)
     118    {
     119        $app = &App::getInstance();
     120
    121121        if (isset($this->_params[$param])) {
    122122            return $this->_params[$param];
     
    127127    }
    128128
    129     /**
    130      * Clear any authentication tokens in the current session. A.K.A. logout.
    131      *
    132      * @access public
    133      */
    134     function clear()
     129    /*
     130    * Clear any authentication tokens in the current session. A.K.A. logout.
     131    *
     132    * @access public
     133    */
     134    public function clear()
    135135    {
    136136        $_SESSION['_auth_file'][$this->_ns] = array('authenticated' => false);
    137137    }
    138138
    139 
    140     /**
    141      * Sets a variable into a registered auth session.
    142      *
    143      * @access public
    144      * @param mixed $key      Which value to set.
    145      * @param mixed $val      Value to set variable to.
    146      */
    147     function set($key, $val)
     139    /*
     140    * Sets a variable into a registered auth session.
     141    *
     142    * @access public
     143    * @param mixed $key      Which value to set.
     144    * @param mixed $val      Value to set variable to.
     145    */
     146    public function set($key, $val)
    148147    {
    149148        if (!isset($_SESSION['_auth_file'][$this->_ns]['user_data'])) {
     
    153152    }
    154153
    155     /**
    156      * Returns a specified value from a registered auth session.
    157      *
    158      * @access public
    159      * @param mixed $key      Which value to return.
    160      * @param mixed $default  Value to return if key not found in user_data.
    161      * @return mixed          Value stored in session.
    162      */
    163     function get($key, $default='')
     154    /*
     155    * Returns a specified value from a registered auth session.
     156    *
     157    * @access public
     158    * @param mixed $key      Which value to return.
     159    * @param mixed $default  Value to return if key not found in user_data.
     160    * @return mixed          Value stored in session.
     161    */
     162    public function get($key, $default='')
    164163    {
    165164        if (isset($_SESSION['_auth_file'][$this->_ns][$key])) {
     
    171170        }
    172171    }
    173     /**
    174      * Find out if a set of login credentials are valid. Only supports
    175      * htpasswd files with DES passwords right now.
    176      *
    177      * @access public
    178      *
    179      * @param string $username      The username to check.
    180      * @param array $password      The password to compare to username.
    181      *
    182      * @return boolean  Whether or not the credentials are valid.
    183      */
    184     function authenticate($username, $password)
    185     {
    186         $app =& App::getInstance();
    187    
     172
     173    /*
     174    * Find out if a set of login credentials are valid. Only supports
     175    * htpasswd files with DES passwords right now.
     176    *
     177    * @access public
     178    *
     179    * @param string $username      The username to check.
     180    * @param array $password      The password to compare to username.
     181    *
     182    * @return boolean  Whether or not the credentials are valid.
     183    */
     184    public function authenticate($username, $password)
     185    {
     186        $app = &App::getInstance();
     187
    188188        if ('' == trim($password)) {
    189189            $app->logMsg(_("No password provided for authentication."), LOG_INFO, __FILE__, __LINE__);
    190190            return false;
    191191        }
    192        
     192
    193193        // Load users file.
    194194        $this->_loadHTPasswdFile();
     
    203203            return false;
    204204        }
    205        
     205
    206206        // Authentication successful!
    207207        return true;
    208208    }
    209209
    210     /**
    211      * If user passes authentication create authenticated session.
    212      *
    213      * @access public
    214      *
    215      * @param string $username     The username to check.
    216      * @param array $password     The password to compare to username.
    217      *
    218      * @return boolean  Whether or not the credentials are valid.
    219      */
    220     function login($username, $password)
     210    /*
     211    * If user passes authentication create authenticated session.
     212    *
     213    * @access public
     214    *
     215    * @param string $username     The username to check.
     216    * @param array $password     The password to compare to username.
     217    *
     218    * @return boolean  Whether or not the credentials are valid.
     219    */
     220    public function login($username, $password)
    221221    {
    222222        $username = mb_strtolower(trim($username));
     
    228228            return false;
    229229        }
    230        
     230
    231231        $_SESSION['_auth_file'][$this->_ns] = array(
    232232            'authenticated' => true,
     
    241241    }
    242242
    243     /**
    244      * Test if user has a currently logged-in session.
    245      *  - authentication flag set to true
    246      *  - username not empty
    247      *  - total logged-in time is not greater than login_timeout
    248      *  - idle time is not greater than idle_timeout
    249      *  - remote address is the same as the login remote address.
    250      *
    251      * @access public
    252      */
    253     function isLoggedIn()
    254     {
    255         $app =& App::getInstance();
    256    
     243    /*
     244    * Test if user has a currently logged-in session.
     245    *  - authentication flag set to true
     246    *  - username not empty
     247    *  - total logged-in time is not greater than login_timeout
     248    *  - idle time is not greater than idle_timeout
     249    *  - remote address is the same as the login remote address.
     250    *
     251    * @access public
     252    */
     253    public function isLoggedIn()
     254    {
     255        $app = &App::getInstance();
     256
    257257        // Some users will access from networks with a changing IP number (i.e. behind a proxy server). These users must be allowed entry by adding their IP to the list of trusted_networks.
    258258        if ($trusted_net = ipInRange(getRemoteAddr(), $this->_params['trusted_networks'])) {
     
    268268        // Test login with information stored in session. Skip IP matching for users from trusted networks.
    269269        if (isset($_SESSION['_auth_file'][$this->_ns])
    270             && true === $_SESSION['_auth_file'][$this->_ns]['authenticated']
    271             && !empty($_SESSION['_auth_file'][$this->_ns]['username'])
    272             && strtotime($_SESSION['_auth_file'][$this->_ns]['login_datetime']) > time() - $this->_params['login_timeout']
    273             && strtotime($_SESSION['_auth_file'][$this->_ns]['last_access_datetime']) > time() - $this->_params['idle_timeout']
    274             && ($_SESSION['_auth_file'][$this->_ns]['remote_ip'] == getRemoteAddr() || $user_in_trusted_network)
     270        && true === $_SESSION['_auth_file'][$this->_ns]['authenticated']
     271        && !empty($_SESSION['_auth_file'][$this->_ns]['username'])
     272        && strtotime($_SESSION['_auth_file'][$this->_ns]['login_datetime']) > time() - $this->_params['login_timeout']
     273        && strtotime($_SESSION['_auth_file'][$this->_ns]['last_access_datetime']) > time() - $this->_params['idle_timeout']
     274        && ($_SESSION['_auth_file'][$this->_ns]['remote_ip'] == getRemoteAddr() || $user_in_trusted_network)
    275275        ) {
    276276            // User is authenticated!
     
    303303    }
    304304
    305     /**
    306      * Redirect user to login page if they are not logged in.
    307      *
    308      * @param string $message The text description of a message to raise.
    309      * @param int    $type    The type of message: MSG_NOTICE,
    310      *                        MSG_SUCCESS, MSG_WARNING, or MSG_ERR.
    311      * @param string $file    __FILE__.
    312      * @param string $line    __LINE__.
    313      * @access public
    314      */
    315     function requireLogin($message='', $type=MSG_NOTICE, $file=null, $line=null)
    316     {
    317         $app =& App::getInstance();
    318    
     305    /*
     306    * Redirect user to login page if they are not logged in.
     307    *
     308    * @param string $message The text description of a message to raise.
     309    * @param int    $type    The type of message: MSG_NOTICE,
     310    *                        MSG_SUCCESS, MSG_WARNING, or MSG_ERR.
     311    * @param string $file    __FILE__.
     312    * @param string $line    __LINE__.
     313    * @access public
     314    */
     315    public function requireLogin($message='', $type=MSG_NOTICE, $file=null, $line=null)
     316    {
     317        $app = &App::getInstance();
     318
    319319        if (!$this->isLoggedIn()) {
    320320            // Display message for requiring login. (RaiseMsg will ignore empty strings.)
     
    326326        }
    327327    }
    328    
    329     /**
    330      * Wrapper function for compatibility with lib/Lock.inc.php.
    331      *
    332      * @param  string  $username    Username to return.
    333      * @return string               Username, or false if none found.
    334      */
    335     function getUsername($username)
    336     {
     328
     329    /*
     330    * Wrapper function for compatibility with lib/Lock.inc.php.
     331    *
     332    * @param  string  $username    Username to return.
     333    * @return string               Username, or false if none found.
     334    */
     335    public function getUsername($username) {
    337336        if ('' != $username) {
    338337            return $username;
     
    351350    * @since    18 Apr 2006 18:17:48
    352351    */
    353     function _loadHTPasswdFile()
    354     {
    355         $app =& App::getInstance();
    356    
     352    private function _loadHTPasswdFile()
     353    {
     354        $app = &App::getInstance();
     355
    357356        static $users = null;
    358        
     357
    359358        if (!file_exists($this->_params['htpasswd_file'])) {
    360359            $app->logMsg(sprintf('htpasswd file missing or not specified: %s', $this->_params['htpasswd_file']), LOG_ERR, __FILE__, __LINE__);
    361360            return false;
    362361        }
    363        
     362
    364363        if (!isset($users)) {
    365364            if (false === ($users = file($this->_params['htpasswd_file']))) {
     
    379378    }
    380379
    381     /**
    382      * Hash a given password according to the configured encryption
    383      * type.
    384      *
    385      * @param string $password              The password to encrypt.
    386      * @param string $encrypted_password    The currently encrypted password to use as salt, if needed.
    387      *
    388      * @return string  The hashed password.
    389      */
    390     function _encrypt($password, $encrypted_password=null)
     380    /*
     381    * Hash a given password according to the configured encryption
     382    * type.
     383    *
     384    * @param string $password              The password to encrypt.
     385    * @param string $encrypted_password    The currently encrypted password to use as salt, if needed.
     386    *
     387    * @return string  The hashed password.
     388    */
     389    private function _encrypt($password, $encrypted_password=null)
    391390    {
    392391        switch ($this->_params['encryption_type']) {
    393         case AUTH_ENCRYPT_PLAINTEXT :
     392        case self::ENCRYPT_PLAINTEXT :
    394393            return $password;
    395394            break;
    396395
    397         case AUTH_ENCRYPT_SHA1 :
     396        case self::ENCRYPT_SHA1 :
    398397            return sha1($password);
    399398            break;
    400399
    401         case AUTH_ENCRYPT_MD5 :
     400        case self::ENCRYPT_MD5 :
    402401            return md5($password);
    403402            break;
    404403
    405         case AUTH_ENCRYPT_CRYPT :
     404        case self::ENCRYPT_CRYPT :
    406405        default :
    407406            return crypt($password, $encrypted_password);
     
    411410
    412411} // end class
    413 ?>
Note: See TracChangeset for help on using the changeset viewer.