Ignore:
Timestamp:
Dec 18, 2005 12:16:03 AM (18 years ago)
Author:
scdev
Message:

detabbed all files ;P

File:
1 edited

Legend:

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

    r41 r42  
    88
    99// Available encryption types for class Auth_SQL.
    10 define('AUTH_ENCRYPT_MD5', 'md5'); 
    11 define('AUTH_ENCRYPT_CRYPT', 'crypt'); 
    12 define('AUTH_ENCRYPT_SHA1', 'sha1'); 
    13 define('AUTH_ENCRYPT_PLAINTEXT', 'plaintext'); 
     10define('AUTH_ENCRYPT_MD5', 'md5');
     11define('AUTH_ENCRYPT_CRYPT', 'crypt');
     12define('AUTH_ENCRYPT_SHA1', 'sha1');
     13define('AUTH_ENCRYPT_PLAINTEXT', 'plaintext');
    1414
    1515class Auth_SQL {
     
    2222    // Default param values.
    2323    var $_default_params = array(
    24    
     24
    2525        // Message displayed by requireLogin().
    2626        'login_required_message' => 'Please login',
    27        
     27
    2828        // Automatically create table and verify columns. Better set to false after site launch.
    2929        'create_table' => true,
    30        
     30
    3131        // The database table containing users to authenticate.
    3232        'db_table' => 'user_tbl',
    33        
     33
    3434        // The name of the primary key for the db_table.
    3535        'db_primary_key' => 'user_id',
    36        
     36
    3737        // The name of the username key for the db_table.
    3838        'db_username_column' => 'username',
    39        
     39
    4040        // If using the db_login_table feature, specify the db_login_table. The primary key must match the primary key for the db_table.
    4141        'db_login_table' => 'user_login_tbl',
    42        
     42
    4343        // The type of encryption to use for passwords stored in the db_table. Use one of the AUTH_ENCRYPT_* types specified above.
    4444        'encryption_type' => AUTH_ENCRYPT_MD5,
     
    5050        // This applies to admins and users. In seconds. 21600 seconds = 6 hours.
    5151        'login_timeout' => 21600,
    52        
     52
    5353        // The maximum amount of time a user is allowed to be idle before their session expires. They will be forced to login again if they expire.
    5454        // This applies to admins and users. In seconds. 3600 seconds = 1 hour.
     
    6565        'login_abuse_max_ips' => 5,
    6666
    67         // The IP address subnet size threshold. Uses a CIDR notation network mask (see CIDR cheatsheet at bottom). 
    68         // Any integar between 0 and 32 is permitted. Setting this to '24' permits any address in a 
     67        // The IP address subnet size threshold. Uses a CIDR notation network mask (see CIDR cheatsheet at bottom).
     68        // Any integar between 0 and 32 is permitted. Setting this to '24' permits any address in a
    6969        // class C network (255.255.255.0) to be considered the same. Setting to '32' compares each IP absolutely.
    7070        // Setting to '0' ignores all IPs, thus disabling login_abuse checking.
    7171        'login_abuse_ip_bitmask' => 32,
    7272
    73         // Specify usernames to exclude from the account abuse detection system. This is specified as a hardcoded array provided at 
     73        // Specify usernames to exclude from the account abuse detection system. This is specified as a hardcoded array provided at
    7474        // class instantiation time, or can be saved in the db_table under the login_abuse_exempt field.
    7575        'login_abuse_exempt_usernames' => array(),
    76        
     76
    7777        // An array of IP blocks that are bypass the remote_addr comparison check. Useful for dynamic IPs or those behind proxy servers.
    7878        'trusted_networks' => array(),
     
    8080        // Allow user accounts to be blocked? Requires the user table to have the columns 'blocked' and 'blocked_reason'
    8181        'blocking' => false,
    82    
     82
    8383        // Use a db_login_table to detect excessive logins. This requires blocking to be enabled.
    8484        'abuse_detection' => false,
     
    106106        }
    107107    }
    108    
     108
    109109    /**
    110110     * Setup the database tables for this class.
     
    117117    {
    118118        static $_db_tested = false;
    119    
     119
    120120        if ($recreate_db || !$_db_tested && $this->getParam('create_table')) {
    121        
     121
    122122            // User table.
    123123            if ($recreate_db) {
     
    154154
    155155            if (!DB::columnExists($this->getParam('db_table'), array(
    156                 $this->getParam('db_primary_key'),
    157                 $this->getParam('db_username_column'),
    158                 'userpass',
    159                 'first_name',
    160                 'last_name',
    161                 'email',
    162                 'user_type',
    163                 'login_abuse_exempt',
    164                 'blocked',
    165                 'blocked_reason',
    166                 'abuse_warning_level',
    167                 'seconds_online',
    168                 'last_login_datetime',
    169                 'last_access_datetime',
    170                 'last_login_ip',
    171                 'added_by_user_id',
    172                 'modified_by_user_id',
    173                 'added_datetime',
    174                 'modified_datetime',
     156                $this->getParam('db_primary_key'),
     157                $this->getParam('db_username_column'),
     158                'userpass',
     159                'first_name',
     160                'last_name',
     161                'email',
     162                'user_type',
     163                'login_abuse_exempt',
     164                'blocked',
     165                'blocked_reason',
     166                'abuse_warning_level',
     167                'seconds_online',
     168                'last_login_datetime',
     169                'last_access_datetime',
     170                'last_login_ip',
     171                'added_by_user_id',
     172                'modified_by_user_id',
     173                'added_datetime',
     174                'modified_datetime',
    175175            ), false, false)) {
    176176                App::logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_table')), LOG_ALERT, __FILE__, __LINE__);
    177177                trigger_error(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_table')), E_USER_ERROR);
    178178            }
    179            
     179
    180180            // Login table is used for abuse_detection features.
    181181            if ($this->getParam('abuse_detection')) {
     
    192192                    KEY remote_ip_binary (remote_ip_binary)
    193193                )");
    194                
     194
    195195                if (!DB::columnExists($this->getParam('db_login_table'), array(
    196196                    $this->getParam('db_primary_key'),
     
    202202                }
    203203            }
    204         }   
     204        }
    205205        $_db_tested = true;
    206206    }
     
    239239        }
    240240    }
    241    
     241
    242242    /**
    243243     * Set the params of an auth object.
     
    279279    {
    280280        $this->initDB();
    281        
     281
    282282        DB::query("
    283             UPDATE " . $this->_params['db_table'] . " SET 
     283            UPDATE " . $this->_params['db_table'] . " SET
    284284            seconds_online = seconds_online + (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(last_access_datetime)),
    285285            last_login_datetime = '0000-00-00 00:00:00'
     
    301301    {
    302302        $this->initDB();
    303        
     303
    304304        // Query DB for user matching credentials.
    305305        $qid = DB::query("
    306             SELECT *, " . $this->_params['db_primary_key'] . " AS user_id 
     306            SELECT *, " . $this->_params['db_primary_key'] . " AS user_id
    307307            FROM " . $this->_params['db_table'] . "
    308308            WHERE " . $this->_params['db_username_column'] . " = '" . addslashes($username) . "'
    309309            AND BINARY userpass = '" . addslashes($this->encryptPassword($password)) . "'
    310310        ");
    311        
     311
    312312        // Return user data if found.
    313313        if ($user_data = mysql_fetch_assoc($qid)) {
     
    331331    {
    332332        $this->initDB();
    333        
     333
    334334        $this->clearAuth();
    335335
     
    351351            'user_data'             => $user_data
    352352        );
    353        
     353
    354354        /**
    355355         * Check if the account is blocked, respond in context to reason. Cancel the login if blocked.
     
    357357        if ($this->getParam('blocking')) {
    358358            if (!empty($user_data['blocked'])) {
    359                
     359
    360360                App::logMsg(sprintf('%s %s (%s) login failed due to blocked account: %s', ucfirst($this->_auth), $this->getVal('user_id'), $this->getVal('username'), $this->getVal('blocked_reason')), LOG_NOTICE, __FILE__, __LINE__);
    361                
     361
    362362                switch ($user_data['blocked_reason']) {
    363363                    case 'account abuse' :
     
    368368                        break;
    369369                }
    370                
     370
    371371                // No login: user is blocked!
    372372                $this->clearAuth();
     
    374374            }
    375375        }
    376        
     376
    377377        /**
    378378         * Check the db_login_table for too many logins under this account.
     
    412412            DB::query("
    413413                INSERT INTO " . $this->_params['db_login_table'] . " (
    414                     " . $this->_params['db_primary_key'] . ", 
    415                     login_datetime, 
     414                    " . $this->_params['db_primary_key'] . ",
     415                    login_datetime,
    416416                    remote_ip_binary
    417417                ) VALUES (
     
    422422            ");
    423423        }
    424        
     424
    425425        // Update user table with this login.
    426426        DB::query("
     
    431431            WHERE " . $this->_params['db_primary_key'] . " = '" . $this->getVal('user_id') . "'
    432432        ");
    433        
     433
    434434        // We're logged-in!
    435435        return true;
     
    449449    {
    450450        $this->initDB();
    451        
     451
    452452        if (isset($user_id)) {
    453453            // Check the login status of a specific user.
     
    465465            return $_SESSION[$this->_sess]['authenticated'];
    466466        }
    467        
     467
    468468        // Tesing login should occur once. This is the first time. Set flag.
    469469        $this->_authentication_tested = true;
     
    472472        if ($trusted_net = ipInRange(getRemoteAddr(), $this->_params['trusted_networks'])) {
    473473            $user_in_trusted_network = true;
    474             App::logMsg(sprintf('%s%s accessing from trusted network %s', 
    475                 ucfirst($this->_auth), 
     474            App::logMsg(sprintf('%s%s accessing from trusted network %s',
     475                ucfirst($this->_auth),
    476476                ($this->getVal('user_id') ? ' ' . $this->getVal('user_id') . ' (' .  $this->getVal('username') . ')' : ''),
    477477                $trusted_net
     
    479479        } else if (preg_match('/proxy.aol.com$/i', getRemoteAddr(true))) {
    480480            $user_in_trusted_network = true;
    481             App::logMsg(sprintf('%s%s accessing from trusted network proxy.aol.com', 
    482                 ucfirst($this->_auth), 
     481            App::logMsg(sprintf('%s%s accessing from trusted network proxy.aol.com',
     482                ucfirst($this->_auth),
    483483                ($this->getVal('user_id') ? ' ' . $this->getVal('user_id') . ' (' .  $this->getVal('username') . ')' : '')
    484484            ), LOG_INFO, __FILE__, __LINE__);
     
    486486            $user_in_trusted_network = false;
    487487        }
    488        
     488
    489489        // Test login with information stored in session. Skip IP matching for users from trusted networks.
    490490        if (isset($_SESSION[$this->_sess])
     
    500500            // Update the DB with the last_access_datetime and increment the seconds_online.
    501501            DB::query("
    502                 UPDATE " . $this->_params['db_table'] . " SET 
     502                UPDATE " . $this->_params['db_table'] . " SET
    503503                seconds_online = seconds_online + (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(last_access_datetime)) + 1,
    504504                last_access_datetime = '" . $this->getVal('last_access_datetime') . "'
     
    517517                App::raiseMsg(sprintf(_("Your %s session has closed. You need to log-in again."), strtolower($this->_auth)), MSG_NOTICE, __FILE__, __LINE__);
    518518            }
    519            
     519
    520520            // Log the reason for login expiration.
    521521            $expire_reasons = array();
     
    565565     * This sets the 'blocked' field for a user in the db_table, and also
    566566     * adds an optional reason
    567      * 
     567     *
    568568     * @param  string   $reason      The reason for blocking the account.
    569569     */
     
    571571    {
    572572        $this->initDB();
    573        
     573
    574574        if ($this->getParam('blocking')) {
    575575            if (strlen(addslashes($reason)) > 255) {
     
    577577                App::logMsg(sprintf('Blocked reason provided is greater than 255 characters: %s', $reason), LOG_WARNING, __FILE__, __LINE__);
    578578            }
    579            
     579
    580580            // Get user_id if specified.
    581581            $user_id = isset($user_id) ? $user_id : $this->getVal('user_id');
     
    590590
    591591    /**
    592      * Unblocks a user in the db_table, and clears any blocked_reason. 
     592     * Unblocks a user in the db_table, and clears any blocked_reason.
    593593     */
    594594    function unblockAccount($user_id=null)
    595595    {
    596596        $this->initDB();
    597        
     597
    598598        if ($this->getParam('blocking')) {
    599599            // Get user_id if specified.
     
    615615     */
    616616    function usernameExists($username)
    617     {   
    618         $this->initDB();
    619        
     617    {
     618        $this->initDB();
     619
    620620        $qid = DB::query("
    621             SELECT 1 
     621            SELECT 1
    622622            FROM " . $this->_params['db_table'] . "
    623623            WHERE " . $this->_params['db_username_column'] . " = '" . addslashes($username) . "'
     
    633633     */
    634634    function getUsername($user_id)
    635     {   
    636         $this->initDB();
    637        
     635    {
     636        $this->initDB();
     637
    638638        $qid = DB::query("
    639639            SELECT " . $this->_params['db_username_column'] . "
     
    679679        return $str;
    680680    }
    681    
     681
    682682    /**
    683683     *
     
    689689            return $password;
    690690            break;
    691            
     691
    692692        case AUTH_ENCRYPT_CRYPT :
    693693            return crypt($password, crypt($password));
    694694            break;
    695            
     695
    696696        case AUTH_ENCRYPT_SHA1 :
    697697            return sha1($password);
    698698            break;
    699            
     699
    700700        case AUTH_ENCRYPT_MD5 :
    701701        default :
     
    706706
    707707    /**
    708      * 
     708     *
    709709     */
    710710    function setPassword($user_id=null, $password)
    711     {   
    712         $this->initDB();
    713              
     711    {
     712        $this->initDB();
     713
    714714        // Get user_id if specified.
    715715        $user_id = isset($user_id) ? $user_id : $this->getVal('user_id');
    716        
     716
    717717        // Issue the password change query.
    718718        DB::query("
    719             UPDATE " . $this->_params['db_table'] . " 
     719            UPDATE " . $this->_params['db_table'] . "
    720720            SET userpass = '" . addslashes($this->encryptPassword($password)) . "'
    721721            WHERE " . $this->_params['db_primary_key'] . " = '" . addslashes($user_id) . "'
     
    733733    {
    734734        $this->initDB();
    735        
     735
    736736        // Get user_id if specified.
    737737        $user_id = isset($user_id) ? $user_id : $this->getVal('user_id');
    738        
     738
    739739        // Reset password of a specific user.
    740740        $qid = DB::query("
     
    746746            return false;
    747747        }
    748        
     748
    749749        // Make sure user has an email on record.
    750750        if (!isset($user_data['email']) || '' == trim($user_data['email'])) {
     
    754754        // Get new password.
    755755        $password = $this->generatePassword();
    756        
     756
    757757        // Update password query.
    758758        $this->setPassword($user_id, $password);
     
    791791        ));
    792792        $email->send();
    793    
     793
    794794        return array(
    795             'username' => $user_data[$this->_params['db_username_column']], 
     795            'username' => $user_data[$this->_params['db_username_column']],
    796796            'userpass' => $password
    797797        );
    798798    }
    799    
     799
    800800    /**
    801801     * If the current user has access to the specified $security_zone, return true.
    802      * If the optional $priv is supplied, test that against the zone. 
     802     * If the optional $priv is supplied, test that against the zone.
    803803     *
    804804     * @param  constant $security_zone   string of comma delimited priviliges for the zone
     
    811811        $zone_members = preg_split('/,\s*/', $security_zone);
    812812        $priv = empty($priv) ? $this->getVal('priv') : $priv;
    813        
    814         // If the current user's privilege level is NOT in that array or if the 
     813
     814        // If the current user's privilege level is NOT in that array or if the
    815815        // user has no privilege, return false. Otherwise the user is clear.
    816816        if (!in_array($priv, $zone_members) || empty($priv)) {
     
    820820        }
    821821    }
    822    
     822
    823823    /**
    824824     * This function tests a list of arguments $security_zone against the priv that the current user has.
    825      * If the user doesn't have one of the supplied privs, die. 
     825     * If the user doesn't have one of the supplied privs, die.
    826826     *
    827827     * @param  constant $security_zone   string of comma delimited priviliges for the zone
     
    831831        return true;
    832832        $zone_members = preg_split('/,\s*/', $security_zone);
    833    
    834         /* If the current user's privilege level is NOT in that array or if the 
     833
     834        /* If the current user's privilege level is NOT in that array or if the
    835835         * user has no privilege, DIE with a message. */
    836836        if (!in_array($this->getVal('priv'), $zone_members) || !$this->getVal('priv')) {
     
    845845// CIDR cheatsheet
    846846//
    847 // Netmask              Netmask (binary)                 CIDR     Notes   
     847// Netmask              Netmask (binary)                 CIDR     Notes
    848848// _____________________________________________________________________________
    849849// 255.255.255.255  11111111.11111111.11111111.11111111  /32  Host (single addr)
     
    856856// 255.255.255.128  11111111.11111111.11111111.10000000  /25  126  useable
    857857// 255.255.255.0    11111111.11111111.11111111.00000000  /24 "Class C" 254 useable
    858 // 
     858//
    859859// 255.255.254.0    11111111.11111111.11111110.00000000  /23    2  Class C's
    860860// 255.255.252.0    11111111.11111111.11111100.00000000  /22    4  Class C's
     
    865865// 255.255.128.0    11111111.11111111.10000000.00000000  /17  128  Class C's
    866866// 255.255.0.0      11111111.11111111.00000000.00000000  /16  "Class B"
    867 //     
     867//
    868868// 255.254.0.0      11111111.11111110.00000000.00000000  /15    2  Class B's
    869869// 255.252.0.0      11111111.11111100.00000000.00000000  /14    4  Class B's
     
    874874// 255.128.0.0      11111111.10000000.00000000.00000000  /9   128  Class B's
    875875// 255.0.0.0        11111111.00000000.00000000.00000000  /8   "Class A"
    876 //   
     876//
    877877// 254.0.0.0        11111110.00000000.00000000.00000000  /7
    878878// 252.0.0.0        11111100.00000000.00000000.00000000  /6
Note: See TracChangeset for help on using the changeset viewer.