Ignore:
Timestamp:
Jun 3, 2006 7:47:48 PM (18 years ago)
Author:
scdev
Message:

Q - Merged branches/2.0singleton into trunk. Completed updating classes to use singleton methods. Implemented tests. Fixed some bugs. Changed some interfaces.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Auth_File.inc.php

    r103 r136  
    11<?php
    22/**
    3  * The Auth_File:: class provides a htpasswd file implementation for
     3 * Auth_File.inc.php
     4 * code by strangecode :: www.strangecode.com :: this document contains copyrighted information
     5 *
     6 * The Auth_File class provides a htpasswd file implementation for
    47 * authentication.
    58 *
     
    2427
    2528class Auth_File {
    26 
    27     var $_auth = '';
    28     var $_sess = '_auth_';
     29   
     30    // Namespace of this auth object.
     31    var $_ns;
     32   
     33    // Parameters to be specified by setParam().
    2934    var $_params = array();
    3035    var $_default_params = array(
     
    6166     * @param optional array $params  A hash containing parameters.
    6267     */
    63     function Auth_File($auth_name=null)
    64     {
    65         if (isset($auth_name)) {
    66             $this->_auth = $auth_name;
    67             $this->_sess .= $auth_name;
    68         }
     68    function Auth_File($namespace='null')
     69    {
     70        $this->_ns = '_auth_' . $namespace;
    6971
    7072        // Initialize default parameters.
     
    9597    function getParam($param)
    9698    {
     99        $app =& App::getInstance();
     100   
    97101        if (isset($this->_params[$param])) {
    98102            return $this->_params[$param];
    99103        } else {
    100             App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
     104            $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_NOTICE, __FILE__, __LINE__);
    101105            return null;
    102106        }
     
    110114    function clearAuth()
    111115    {
    112         $_SESSION[$this->_sess] = array('authenticated' => false);
     116        $_SESSION[$this->_ns] = array('authenticated' => false);
    113117    }
    114118
     
    123127    function setVal($key, $val)
    124128    {
    125         if (!isset($_SESSION[$this->_sess]['user_data'])) {
    126             $_SESSION[$this->_sess]['user_data'] = array();
    127         }
    128         $_SESSION[$this->_sess]['user_data'][$key] = $val;
     129        if (!isset($_SESSION[$this->_ns]['user_data'])) {
     130            $_SESSION[$this->_ns]['user_data'] = array();
     131        }
     132        $_SESSION[$this->_ns]['user_data'][$key] = $val;
    129133    }
    130134
     
    139143    function getVal($key, $default='')
    140144    {
    141         if (isset($_SESSION[$this->_sess][$key])) {
    142             return $_SESSION[$this->_sess][$key];
    143         } else if (isset($_SESSION[$this->_sess]['user_data'][$key])) {
    144             return $_SESSION[$this->_sess]['user_data'][$key];
     145        if (isset($_SESSION[$this->_ns][$key])) {
     146            return $_SESSION[$this->_ns][$key];
     147        } else if (isset($_SESSION[$this->_ns]['user_data'][$key])) {
     148            return $_SESSION[$this->_ns]['user_data'][$key];
    145149        } else {
    146150            return $default;
     
    160164    function authenticate($username, $password)
    161165    {
     166        $app =& App::getInstance();
     167   
    162168        if ('' == trim($password)) {
    163             App::logMsg(_("No password provided for authentication."), LOG_INFO, __FILE__, __LINE__);
     169            $app->logMsg(_("No password provided for authentication."), LOG_INFO, __FILE__, __LINE__);
    164170            return false;
    165171        }
     
    169175
    170176        if (!isset($this->_users[$username])) {
    171             App::logMsg(_("User ID provided does not exist."), LOG_INFO, __FILE__, __LINE__);
     177            $app->logMsg(_("User ID provided does not exist."), LOG_INFO, __FILE__, __LINE__);
    172178            return false;
    173179        }
    174180
    175181        if ($this->_encrypt($password, $this->_users[$username]) != $this->_users[$username]) {
    176             App::logMsg(sprintf('Authentication failed for user %s', $username), LOG_INFO, __FILE__, __LINE__);
     182            $app->logMsg(sprintf('Authentication failed for user %s', $username), LOG_INFO, __FILE__, __LINE__);
    177183            return false;
    178184        }
     
    203209        }
    204210       
    205         $_SESSION[$this->_sess] = array(
     211        $_SESSION[$this->_ns] = array(
    206212            'authenticated' => true,
    207213            'username' => $username,
     
    227233    function isLoggedIn()
    228234    {
     235        $app =& App::getInstance();
     236   
    229237        // 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.
    230238        if ($trusted_net = ipInRange(getRemoteAddr(), $this->_params['trusted_networks'])) {
    231239            $user_in_trusted_network = true;
    232             App::logMsg(sprintf('User %s accessing from trusted network %s', $_SESSION[$this->_sess]['username'], $trusted_net), LOG_DEBUG, __FILE__, __LINE__);
     240            $app->logMsg(sprintf('User %s accessing from trusted network %s', $_SESSION[$this->_ns]['username'], $trusted_net), LOG_DEBUG, __FILE__, __LINE__);
    233241        } else if (preg_match('/proxy.aol.com$/i', getRemoteAddr(true))) {
    234242            $user_in_trusted_network = true;
    235             App::logMsg(sprintf('User %s accessing from trusted network proxy.aol.com', $_SESSION[$this->_sess]['username']), LOG_DEBUG, __FILE__, __LINE__);
     243            $app->logMsg(sprintf('User %s accessing from trusted network proxy.aol.com', $_SESSION[$this->_ns]['username']), LOG_DEBUG, __FILE__, __LINE__);
    236244        } else {
    237245            $user_in_trusted_network = false;
     
    239247
    240248        // Test login with information stored in session. Skip IP matching for users from trusted networks.
    241         if (isset($_SESSION[$this->_sess])
    242             && true === $_SESSION[$this->_sess]['authenticated']
    243             && !empty($_SESSION[$this->_sess]['username'])
    244             && strtotime($_SESSION[$this->_sess]['login_datetime']) > time() - $this->_params['login_timeout']
    245             && strtotime($_SESSION[$this->_sess]['last_access_datetime']) > time() - $this->_params['idle_timeout']
    246             && ($_SESSION[$this->_sess]['remote_ip'] == getRemoteAddr() || $user_in_trusted_network)
     249        if (isset($_SESSION[$this->_ns])
     250            && true === $_SESSION[$this->_ns]['authenticated']
     251            && !empty($_SESSION[$this->_ns]['username'])
     252            && strtotime($_SESSION[$this->_ns]['login_datetime']) > time() - $this->_params['login_timeout']
     253            && strtotime($_SESSION[$this->_ns]['last_access_datetime']) > time() - $this->_params['idle_timeout']
     254            && ($_SESSION[$this->_ns]['remote_ip'] == getRemoteAddr() || $user_in_trusted_network)
    247255        ) {
    248256            // User is authenticated!
    249             $_SESSION[$this->_sess]['last_access_datetime'] = date('Y-m-d H:i:s');
     257            $_SESSION[$this->_ns]['last_access_datetime'] = date('Y-m-d H:i:s');
    250258            return true;
    251         } else if (isset($_SESSION[$this->_sess]) && true === $_SESSION[$this->_sess]['authenticated']) {
    252             if (strtotime($_SESSION[$this->_sess]['last_access_datetime']) > time() - 43200) {
     259        } else if (isset($_SESSION[$this->_ns]) && true === $_SESSION[$this->_ns]['authenticated']) {
     260            if (strtotime($_SESSION[$this->_ns]['last_access_datetime']) > time() - 43200) {
    253261                // Only raise message if last session is less than 12 hours old.
    254                 App::raiseMsg(_("Your session has closed. You need to log-in again."), MSG_NOTICE, __FILE__, __LINE__);
     262                $app->raiseMsg(_("Your session has closed. You need to log-in again."), MSG_NOTICE, __FILE__, __LINE__);
    255263            }
    256264
    257265            // Log the reason for login expiration.
    258266            $expire_reasons = array();
    259             if (empty($_SESSION[$this->_sess]['username'])) {
     267            if (empty($_SESSION[$this->_ns]['username'])) {
    260268                $expire_reasons[] = 'username not found';
    261269            }
    262             if (strtotime($_SESSION[$this->_sess]['login_datetime']) <= time() - $this->_params['login_timeout']) {
     270            if (strtotime($_SESSION[$this->_ns]['login_datetime']) <= time() - $this->_params['login_timeout']) {
    263271                $expire_reasons[] = 'login_timeout expired';
    264272            }
    265             if (strtotime($_SESSION[$this->_sess]['last_access_datetime']) <= time() - $this->_params['idle_timeout']) {
     273            if (strtotime($_SESSION[$this->_ns]['last_access_datetime']) <= time() - $this->_params['idle_timeout']) {
    266274                $expire_reasons[] = 'idle_timeout expired';
    267275            }
    268             if ($_SESSION[$this->_sess]['remote_ip'] != getRemoteAddr() && !$user_in_trusted_network) {
    269                 $expire_reasons[] = sprintf('remote_ip not matched (%s != %s)', $_SESSION[$this->_sess]['remote_ip'], getRemoteAddr());
    270             }
    271             App::logMsg(sprintf('User %s session expired: %s', $_SESSION[$this->_sess]['username'], join(', ', $expire_reasons)), LOG_INFO, __FILE__, __LINE__);
     276            if ($_SESSION[$this->_ns]['remote_ip'] != getRemoteAddr() && !$user_in_trusted_network) {
     277                $expire_reasons[] = sprintf('remote_ip not matched (%s != %s)', $_SESSION[$this->_ns]['remote_ip'], getRemoteAddr());
     278            }
     279            $app->logMsg(sprintf('User %s session expired: %s', $_SESSION[$this->_ns]['username'], join(', ', $expire_reasons)), LOG_INFO, __FILE__, __LINE__);
    272280        }
    273281
     
    287295    function requireLogin($message='', $type=MSG_NOTICE, $file=null, $line=null)
    288296    {
     297        $app =& App::getInstance();
     298   
    289299        if (!$this->isLoggedIn()) {
    290300            // Display message for requiring login. (RaiseMsg will ignore empty strings.)
    291             App::raiseMsg($message, $type, $file, $line);
     301            $app->raiseMsg($message, $type, $file, $line);
    292302
    293303            // Login scripts must have the same 'login' tag for boomerangURL verification/manipulation.
    294             App::setBoomerangURL(absoluteMe(), 'login');
    295             App::dieURL($this->_params['login_url']);
     304            $app->setBoomerangURL(absoluteMe(), 'login');
     305            $app->dieURL($this->_params['login_url']);
    296306        }
    297307    }
     
    308318    function _loadHTPasswdFile()
    309319    {
     320        $app =& App::getInstance();
     321   
    310322        static $users = null;
    311323       
    312324        if (!file_exists($this->_params['htpasswd_file'])) {
    313             App::logMsg(sprintf('htpasswd file missing or not specified: %s', $this->_params['htpasswd_file']), LOG_ERR, __FILE__, __LINE__);
     325            $app->logMsg(sprintf('htpasswd file missing or not specified: %s', $this->_params['htpasswd_file']), LOG_ERR, __FILE__, __LINE__);
    314326            return false;
    315327        }
     
    317329        if (!isset($users)) {
    318330            if (false === ($users = file($this->_params['htpasswd_file']))) {
    319                 App::logMsg(sprintf(_("Could not read htpasswd file: %s"), $this->_params['htpasswd_file']), LOG_ERR, __FILE__, __LINE__);
     331                $app->logMsg(sprintf(_("Could not read htpasswd file: %s"), $this->_params['htpasswd_file']), LOG_ERR, __FILE__, __LINE__);
    320332                return false;
    321333            }
Note: See TracChangeset for help on using the changeset viewer.