source: trunk/lib/Auth_SQL.inc.php @ 757

Last change on this file since 757 was 757, checked in by anonymous, 2 years ago

Fix depreciated notices

File size: 53.6 KB
Line 
1<?php
2/**
3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
5 * Copyright 2001-2012 Strangecode, LLC
6 *
7 * This file is part of The Strangecode Codebase.
8 *
9 * The Strangecode Codebase is free software: you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * The Strangecode Codebase is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24* The Auth_SQL class provides a SQL implementation for authentication.
25*
26* @author  Quinn Comendant <quinn@strangecode.com>
27* @version 2.1
28*/
29
30require_once dirname(__FILE__) . '/Email.inc.php';
31
32class Auth_SQL
33{
34    // Available hash types for class Auth_SQL.
35    const ENCRYPT_PLAINTEXT = 1;
36    const ENCRYPT_CRYPT = 2;
37    const ENCRYPT_SHA1 = 3;
38    const ENCRYPT_SHA1_HARDENED = 4;
39    const ENCRYPT_MD5 = 5;
40    const ENCRYPT_MD5_HARDENED = 6;
41    const ENCRYPT_PASSWORD_BCRYPT = 7;
42    const ENCRYPT_PASSWORD_DEFAULT = 8;
43
44    // Namespace of this auth object.
45    protected $_ns;
46
47    // Static var for test.
48    protected $_authentication_tested;
49
50    // Parameters to be configured by setParam.
51    protected $_params = array();
52    protected $_default_params = array(
53
54        // Automatically create table and verify columns. Better set to false after site launch.
55        // This value is overwritten by the $app->getParam('db_create_tables') setting if it is available.
56        'create_table' => true,
57
58        // The database table containing users to authenticate.
59        'db_table' => 'user_tbl',
60
61        // The name of the primary key for the db_table.
62        'db_primary_key' => 'user_id',
63
64        // The name of the username key for the db_table.
65        'db_username_column' => 'username',
66
67        // If using the db_login_table feature, specify the db_login_table. The primary key must match the primary key for the db_table.
68        'db_login_table' => 'user_login_tbl',
69
70        // The type of hash to use for passwords stored in the db_table. Use one of the Auth_SQL::ENCRYPT_* types specified above.
71        // Hardened password hashes rely on the same key/salt being used to compare hashes.
72        // Be aware that when using one of the hardened types the App signing_key or $more_salt below cannot change!
73        'hash_type' => self::ENCRYPT_MD5,
74        'encryption_type' => null, // Backwards misnomer compatibility.
75
76        // Automatically update stored user hashes when the user next authenticates if the hash type changes (requires user_tbl with populated userpass_hashtype column).
77        'hash_type_autoupdate' => true,
78
79        // The URL to the login script.
80        'login_url' => '/',
81
82        // The maximum amount of time a user is allowed to be logged in. They will be forced to login again if they expire.
83        // In seconds. 21600 seconds = 6 hours.
84        'login_timeout' => 21600,
85
86        // 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.
87        // In seconds. 3600 seconds = 1 hour.
88        'idle_timeout' => 3600,
89
90        // The period of time to compare login abuse attempts. If a threshold of logins is reached in this amount of time the account is blocked.
91        // Days and hours, like this: 'DD:HH'
92        'login_abuse_timeframe' => '04:00',
93
94        // The number of warnings a user will receive (and their password reset each time) before their account is completely blocked.
95        'login_abuse_warnings' => 3,
96
97        // The maximum number of IP addresses a user can login with over the timeout period before their account is blocked.
98        'login_abuse_max_ips' => 5,
99
100        // The IP address subnet size threshold. Uses a CIDR notation network mask (see CIDR cheat-sheet at bottom).
101        // Any integer between 0 and 32 is permitted. Setting this to '24' permits any address in a
102        // class C network (255.255.255.0) to be considered the same. Setting to '32' compares each IP absolutely.
103        // Setting to '0' ignores all IPs, thus disabling login_abuse checking.
104        'login_abuse_ip_bitmask' => 32,
105
106        // Specify usernames to exclude from the account abuse detection system. This is specified as a hardcoded array provided at
107        // class instantiation time, or can be saved in the db_table under the login_abuse_exempt field.
108        'login_abuse_exempt_usernames' => array(),
109
110        // Specify usernames to exclude from remote_ip matching. Users behind proxy servers should be appended to this array so their shifting remote IP will not log them out.
111        'match_remote_ip_exempt_usernames' => array(),
112
113        // Match the user's current remote IP against the one they logged in with.
114        'match_remote_ip' => true,
115
116        // An array of IP blocks that are bypass the remote_ip comparison check. Useful for dynamic IPs or those behind proxy servers.
117        'trusted_networks' => array(),
118
119        // Allow user accounts to be blocked? Requires the user table to have the columns 'blocked' and 'blocked_reason'
120        'blocking' => false,
121
122        // Use a db_login_table to detect excessive logins. This requires blocking to be enabled.
123        'abuse_detection' => false,
124
125        // Allow users to save login form passwords in their browser? Setting to 'true' may pose a potential security risk.
126        'login_form_allow_autocomplete' => false,
127    );
128
129    /**
130     * Constructs a new authentication object.
131     *
132     * @access public
133     * @param optional array $params  A hash containing parameters.
134     */
135    public function __construct($namespace='')
136    {
137        $app =& App::getInstance();
138
139        $this->_ns = $namespace;
140
141        // Initialize default parameters.
142        $this->setParam($this->_default_params);
143
144        // Get create tables config from global context.
145        if (!is_null($app->getParam('db_create_tables'))) {
146            $this->setParam(array('create_table' => $app->getParam('db_create_tables')));
147        }
148
149        if (!isset($_SESSION['_auth_sql'][$this->_ns])) {
150            $app->logMsg(sprintf('No _auth_sql session found; initializing', null), LOG_DEBUG, __FILE__, __LINE__);
151            $this->clear();
152        }
153    }
154
155    /**
156     * Setup the database tables for this class.
157     *
158     * @access  public
159     * @author  Quinn Comendant <quinn@strangecode.com>
160     * @since   26 Aug 2005 17:09:36
161     */
162    public function initDB($recreate_db=false)
163    {
164        $app =& App::getInstance();
165        $db =& DB::getInstance();
166
167        static $_db_tested = false;
168
169        if ($recreate_db || !$_db_tested && $this->getParam('create_table')) {
170
171            // User table.
172            if ($recreate_db) {
173                $db->query("DROP TABLE IF EXISTS " . $this->getParam('db_table'));
174                $app->logMsg(sprintf('Dropping and recreating table %s.', $this->getParam('db_table')), LOG_INFO, __FILE__, __LINE__);
175            }
176
177            // The minimal columns for a table compatible with the Auth_SQL class.
178            $db->query(sprintf(
179                "CREATE TABLE IF NOT EXISTS %1\$s (
180                    %2\$s MEDIUMINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
181                    %3\$s varchar(255) NOT NULL default '',
182                    userpass VARCHAR(255) NOT NULL DEFAULT '',
183                    userpass_hashtype TINYINT UNSIGNED NOT NULL DEFAULT '0',
184                    first_name VARCHAR(50) NOT NULL DEFAULT '',
185                    last_name VARCHAR(50) NOT NULL DEFAULT '',
186                    email VARCHAR(255) NOT NULL DEFAULT '',
187                    login_abuse_exempt ENUM('true') DEFAULT NULL,
188                    blocked ENUM('true') DEFAULT NULL,
189                    blocked_reason VARCHAR(255) NOT NULL DEFAULT '',
190                    abuse_warning_level TINYINT NOT NULL DEFAULT '0',
191                    seconds_online INT NOT NULL DEFAULT '0',
192                    last_login_datetime DATETIME NOT NULL DEFAULT '%4\$s 00:00:00',
193                    last_access_datetime DATETIME NOT NULL DEFAULT '%4\$s 00:00:00',
194                    last_login_ip VARCHAR(45) NOT NULL DEFAULT '0.0.0.0',
195                    added_by_user_id SMALLINT DEFAULT NULL,
196                    modified_by_user_id SMALLINT DEFAULT NULL,
197                    added_datetime DATETIME NOT NULL DEFAULT '%4\$s 00:00:00',
198                    modified_datetime DATETIME NOT NULL DEFAULT '%4\$s 00:00:00',
199                    KEY %5\$s (%5\$s),
200                    KEY userpass (userpass),
201                    KEY email (email),
202                    KEY last_login_datetime (last_login_datetime),
203                    KEY last_access_datetime (last_access_datetime)
204                )",
205                $db->escapeString($this->getParam('db_table')),
206                $this->getParam('db_primary_key'),
207                $this->getParam('db_username_column'),
208                $db->getParam('zero_date'),
209                $this->getParam('db_username_column')
210            ));
211
212            if (!$db->columnExists($this->getParam('db_table'), array(
213                $this->getParam('db_primary_key'),
214                $this->getParam('db_username_column'),
215                'userpass',
216                'first_name',
217                'last_name',
218                'email',
219                'login_abuse_exempt',
220                'blocked',
221                'blocked_reason',
222                'abuse_warning_level',
223                'seconds_online',
224                'last_login_datetime',
225                'last_access_datetime',
226                'last_login_ip',
227                'added_by_user_id',
228                'modified_by_user_id',
229                'added_datetime',
230                'modified_datetime',
231            ), false, false)) {
232                $app->logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_table')), LOG_ALERT, __FILE__, __LINE__);
233                trigger_error(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_table')), E_USER_ERROR);
234            }
235
236            // Login table is used for abuse_detection features.
237            if ($this->getParam('abuse_detection')) {
238                if ($recreate_db) {
239                    $db->query("DROP TABLE IF EXISTS " . $this->getParam('db_login_table'));
240                    $app->logMsg(sprintf('Dropping and recreating table %s.', $this->getParam('db_login_table')), LOG_INFO, __FILE__, __LINE__);
241                }
242                $db->query(sprintf(
243                    "CREATE TABLE IF NOT EXISTS %1\$s (
244                        %2\$s MEDIUMINT UNSIGNED NOT NULL DEFAULT '0',
245                        login_datetime DATETIME NOT NULL DEFAULT '%3\$s 00:00:00',
246                        remote_ip_binary CHAR(32) NOT NULL DEFAULT '',
247                        KEY %4\$s (%4\$s),
248                        KEY login_datetime (login_datetime),
249                        KEY remote_ip_binary (remote_ip_binary)
250                    )",
251                    $this->getParam('db_login_table'),
252                    $this->getParam('db_primary_key'),
253                    $db->getParam('zero_date'),
254                    $this->getParam('db_primary_key')
255                ));
256
257                if (!$db->columnExists($this->getParam('db_login_table'), array(
258                    $this->getParam('db_primary_key'),
259                    'login_datetime',
260                    'remote_ip_binary',
261                ), false, false)) {
262                    $app->logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_login_table')), LOG_ALERT, __FILE__, __LINE__);
263                    trigger_error(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_login_table')), E_USER_ERROR);
264                }
265            }
266        }
267        $_db_tested = true;
268    }
269
270    /**
271     * Set the params of an auth object.
272     *
273     * @param  array $params   Array of parameter keys and value to set.
274     * @return bool true on success, false on failure
275     */
276    public function setParam($params)
277    {
278        $app =& App::getInstance();
279
280        if (isset($params['match_remote_ip_exempt_usernames'])) {
281            $params['match_remote_ip_exempt_usernames'] = array_map('strtolower', $params['match_remote_ip_exempt_usernames']);
282        }
283        if (isset($params['login_abuse_exempt_usernames'])) {
284            $params['login_abuse_exempt_usernames'] = array_map('strtolower', $params['login_abuse_exempt_usernames']);
285        }
286        if (isset($params['encryption_type'])) {
287            // Backwards misnomer compatibility.
288            $params['hash_type'] = $params['encryption_type'];
289        }
290        if (isset($params['hash_type']) && version_compare(PHP_VERSION, '5.5.0', '<') && in_array($params['hash_type'], array(self::ENCRYPT_PASSWORD_BCRYPT, self::ENCRYPT_PASSWORD_DEFAULT))) {
291            // These hash types require the password_* userland lib in PHP < 5.5.0
292            $pw_compat_lib = 'vendor/ircmaxell/password-compat/lib/password.php';
293            if (false !== stream_resolve_include_path($pw_compat_lib)) {
294                include_once $pw_compat_lib;
295            } else {
296                $app->logMsg(sprintf('Hash type %s requires password-compat lib in PHP < 5.5.0; falling back to ENCRYPT_SHA1_HARDENED', $params['hash_type']), LOG_ERR, __FILE__, __LINE__);
297                $params['hash_type'] = self::ENCRYPT_SHA1_HARDENED;
298            }
299        }
300        if (isset($params['hash_type']) && !in_array($params['hash_type'], array(self::ENCRYPT_PLAINTEXT, self::ENCRYPT_CRYPT, self::ENCRYPT_SHA1, self::ENCRYPT_SHA1_HARDENED, self::ENCRYPT_MD5, self::ENCRYPT_MD5_HARDENED, self::ENCRYPT_PASSWORD_BCRYPT, self::ENCRYPT_PASSWORD_DEFAULT))) {
301            $app->logMsg(sprintf('Invalid hash type %s; falling back to ENCRYPT_SHA1_HARDENED', $params['hash_type']), LOG_ERR, __FILE__, __LINE__);
302            $params['hash_type'] = self::ENCRYPT_SHA1_HARDENED;
303        }
304        if (isset($params) && is_array($params)) {
305            // Merge new parameters with old overriding only those passed.
306            $this->_params = array_merge($this->_params, $params);
307        }
308    }
309
310    /**
311     * Return the value of a parameter, if it exists.
312     *
313     * @access public
314     * @param string $param        Which parameter to return.
315     * @return mixed               Configured parameter value.
316     */
317    public function getParam($param)
318    {
319        $app =& App::getInstance();
320
321        if (array_key_exists($param, $this->_params)) {
322            return $this->_params[$param];
323        } else {
324            $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
325            return null;
326        }
327    }
328
329    /**
330     * Clear any authentication tokens in the current session. A.K.A. logout.
331     *
332     * @access public
333     */
334    public function clear()
335    {
336        $app =& App::getInstance();
337        $db =& DB::getInstance();
338
339        if ($this->get('user_id', false)) {
340
341            $this->initDB();
342
343            // FIX ME: Should we check if the session is active?
344            $db->query(sprintf(
345                "UPDATE %s SET
346                    seconds_online = seconds_online + ABS(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(last_access_datetime)),
347                    last_login_datetime = '%s 00:00:00'
348                    WHERE %s = '%s'
349                ",
350                $this->_params['db_table'],
351                $db->getParam('zero_date'),
352                $this->_params['db_primary_key'],
353                $this->get('user_id')
354            ));
355        }
356        $_SESSION['_auth_sql'][$this->_ns] = array(
357            'authenticated'         => false,
358            'user_id'               => null,
359            'username'              => null,
360            'login_datetime'        => null,
361            'last_access_datetime'  => null,
362            'remote_ip'             => getRemoteAddr(),
363            'login_abuse_exempt'    => null,
364            'match_remote_ip_exempt'=> null,
365            'user_data'             => null,
366        );
367
368        $app->logMsg(sprintf('Cleared %s auth', $this->_ns), LOG_DEBUG, __FILE__, __LINE__);
369    }
370
371    /**
372     * Sets a variable into a registered auth session.
373     *
374     * @access public
375     * @param mixed $key      Which value to set.
376     * @param mixed $val      Value to set variable to.
377     */
378    public function set($key, $val)
379    {
380        if (!isset($_SESSION['_auth_sql'][$this->_ns]['user_data'])) {
381            $_SESSION['_auth_sql'][$this->_ns]['user_data'] = array();
382        }
383
384        if (isset($_SESSION['_auth_sql'][$this->_ns][$key])) {
385            $_SESSION['_auth_sql'][$this->_ns][$key] = $val;
386        } else {
387            $_SESSION['_auth_sql'][$this->_ns]['user_data'][$key] = $val;
388        }
389    }
390
391    /**
392     * Returns a specified value from a registered auth session.
393     *
394     * @access public
395     * @param mixed $key      Which value to return.
396     * @param mixed $default  Value to return if key not found in user_data.
397     * @return mixed          Value stored in session.
398     */
399    public function get($key, $default='')
400    {
401        if (isset($_SESSION['_auth_sql'][$this->_ns][$key])) {
402            return $_SESSION['_auth_sql'][$this->_ns][$key];
403        } else if (isset($_SESSION['_auth_sql'][$this->_ns]['user_data'][$key])) {
404            return $_SESSION['_auth_sql'][$this->_ns]['user_data'][$key];
405        } else {
406            return $default;
407        }
408    }
409
410    /**
411     * Retrieve and verify the given username and password against a matching user record in the database.
412     *
413     * @access private
414     * @param string $username      The username to check.
415     * @param string $password      The password to compare to username.
416     * @return mixed  False if credentials not found in DB, or returns DB row matching credentials.
417     */
418    public function authenticate($username, $password)
419    {
420        $app =& App::getInstance();
421        $db =& DB::getInstance();
422
423        $this->initDB();
424
425        // Get user data for specified username.
426        $qid = $db->query("
427            SELECT *, " . $this->_params['db_primary_key'] . " AS user_id
428            FROM " . $this->_params['db_table'] . "
429            WHERE " . $this->_params['db_username_column'] . " = '" . $db->escapeString($username) . "'
430        ");
431        if (!$user_data = mysql_fetch_assoc($qid)) {
432            $app->logMsg(sprintf('Username %s not found for authentication', $username), LOG_NOTICE, __FILE__, __LINE__);
433            return false;
434        }
435
436        // TODO: log all auth attempts to db_login_table, not just successful ones. Then, rate-limit login attempts.
437
438        // Check given password against hashed DB password.
439        $old_hash_type = isset($user_data['userpass_hashtype']) && !empty($user_data['userpass_hashtype']) ? $user_data['userpass_hashtype'] : $this->getParam('hash_type');
440        if ($this->verifyPassword($password, $user_data['userpass'], $old_hash_type)) {
441            $app->logMsg(sprintf('Authentication successful for %s (user_id=%s)', $username, $user_data['user_id']), LOG_INFO, __FILE__, __LINE__);
442            unset($user_data['userpass']); // Avoid revealing the encrypted password in the $user_data.
443            if ($this->getParam('hash_type_autoupdate') && $old_hash_type != $this->getParam('hash_type')) {
444                // Let's update user's password hash to new type (just run setPassword with this authenticated password
).
445                $this->setPassword($user_data['user_id'], $password);
446                $app->logMsg(sprintf('User %s password hash type updated from %s to %s', $username, $old_hash_type, $this->getParam('hash_type')), LOG_INFO, __FILE__, __LINE__);
447            }
448            return $user_data;
449        }
450
451        $app->logMsg(sprintf('Authentication failed for %s (user_id=%s)', $username, $user_data['user_id']), LOG_NOTICE, __FILE__, __LINE__);
452        return false;
453    }
454
455    /**
456     * Check username and password, and create new session if authenticated.
457     *
458     * @access private
459     * @param string $username     The username to check.
460     * @param string $password     The password to compare for username.
461     * @return boolean  Whether or not the credentials are valid.
462     */
463    public function login($username, $password)
464    {
465        $app =& App::getInstance();
466        $db =& DB::getInstance();
467
468        if ($user_data = $this->authenticate($username, $password)) {
469            // The credentials match. Now setup the session.
470            return $this->createSession($user_data);
471        }
472        // No login: failed authentication!
473        return false;
474    }
475
476    /**
477     * Create new login session for given user.
478     *
479     * @access private
480     * @param string $user_data User data that is normally returned from this->authenticate(). If provided manually:
481     *                          Required array values:
482     *                              'user_id' => '1'
483     *                              'username' => 'name'
484     *                          Optional array values:
485     *                              'match_remote_ip_exempt' => true
486     *                              'login_abuse_exempt' => true
487     *                              'abuse_warning_level' => true
488     *                              'blocked' => true
489     *                              'blocked_reason' => ''
490     *                              '
' => '
' (any other values that should be retrievable via this->get())
491     * @return boolean          Whether or not the session was created. It will return true unless abuse detection is enabled and triggered.
492     */
493    public function createSession($user_data)
494    {
495        $app =& App::getInstance();
496        $db =& DB::getInstance();
497
498        $this->initDB();
499
500        $this->clear();
501
502        // Convert 'priv' to 'user_type' nomenclature to support older implementations.
503        if (isset($user_data['priv'])) {
504            $user_data['user_type'] = $user_data['priv'];
505        }
506
507        // Register authenticated session.
508        $_SESSION['_auth_sql'][$this->_ns] = array(
509            'authenticated'         => true,
510            'user_id'               => $user_data['user_id'],
511            'username'              => $user_data['username'],
512            'login_datetime'        => date('Y-m-d H:i:s'),
513            'last_access_datetime'  => date('Y-m-d H:i:s'),
514            'remote_ip'             => getRemoteAddr(),
515            'login_abuse_exempt'    => isset($user_data['login_abuse_exempt']) ? !empty($user_data['login_abuse_exempt']) : in_array(strtolower($user_data['username']), $this->_params['login_abuse_exempt_usernames']),
516            'match_remote_ip_exempt'=> isset($user_data['match_remote_ip_exempt']) ? !empty($user_data['match_remote_ip_exempt']) : in_array(strtolower($user_data['username']), $this->_params['match_remote_ip_exempt_usernames']),
517            'user_data'             => $user_data
518        );
519
520        /**
521         * Check if the account is blocked, respond in context to reason. Cancel the login if blocked.
522         */
523        if ($this->getParam('blocking')) {
524            if (isset($user_data['blocked']) && !empty($user_data['blocked'])) {
525                switch ($this->get('blocked_reason')) {
526                case 'account abuse' :
527                    $app->raiseMsg(sprintf(_("This account has been blocked due to possible account abuse. Please contact an administrator to reactivate."), null), MSG_WARNING, __FILE__, __LINE__);
528                    break;
529                default :
530                    $app->raiseMsg(sprintf(_("This account is currently not active. %s"), $this->get('blocked_reason')), MSG_WARNING, __FILE__, __LINE__);
531                    break;
532                }
533
534                // No login: user is blocked!
535                $app->logMsg(sprintf('User_id %s (%s) login failed due to blocked account: %s', $this->get('user_id'), $this->get('username'), $this->get('blocked_reason')), LOG_NOTICE, __FILE__, __LINE__);
536                $this->clear();
537                return false;
538            }
539        }
540
541        /**
542         * Check the db_login_table for too many logins under this account.
543         * (1) Count the number of unique IP addresses that logged in under this user within the login_abuse_timeframe
544         * (2) If this number exceeds the login_abuse_max_ips, assume multiple people are logging in under the same account.
545        **/
546        // TODO: make this ipv6 compatible. At the moment, ipv6 addresses are converted into zero for remote_ip_binary.
547        // http://www.highonphp.com/5-tips-for-working-with-ipv6-in-php
548        // https://stackoverflow.com/questions/444966/working-with-ipv6-addresses-in-php
549        if ($this->getParam('abuse_detection') && !$this->get('login_abuse_exempt')) {
550            $qid = $db->query("
551                SELECT COUNT(DISTINCT LEFT(remote_ip_binary, " . $this->_params['login_abuse_ip_bitmask'] . "))
552                FROM " . $this->_params['db_login_table'] . "
553                WHERE " . $this->_params['db_primary_key'] . " = '" . $this->get('user_id') . "'
554                AND DATE_ADD(login_datetime, INTERVAL '" . $this->_params['login_abuse_timeframe'] . "' DAY_HOUR) > NOW()
555            ");
556            list($distinct_ips) = mysql_fetch_row($qid);
557            if ($distinct_ips > $this->_params['login_abuse_max_ips']) {
558                if ($this->get('abuse_warning_level') < $this->_params['login_abuse_warnings']) {
559                    // Warn the user with a password reset.
560                    $this->resetPassword(null, _("This is a security precaution. We have detected this account has been accessed from multiple computers simultaneously. It is against policy to share credentials with others. If further account abuse is detected this account will be blocked."));
561                    $app->raiseMsg(_("Your password has been reset as a security precaution. Please check your email for more information."), MSG_NOTICE, __FILE__, __LINE__);
562                    $app->logMsg(sprintf('Account abuse detected for user_id %s (%s) from IP %s', $this->get('user_id'), $this->get('username'), $this->get('remote_ip')), LOG_WARNING, __FILE__, __LINE__);
563                } else {
564                    // Block the account with the reason of account abuse.
565                    $this->blockAccount(null, 'account abuse');
566                    $app->raiseMsg(_("Your account has been blocked as a security precaution. Please contact us for more information."), MSG_NOTICE, __FILE__, __LINE__);
567                    $app->logMsg(sprintf('Account blocked for user_id %s (%s) from IP %s', $this->get('user_id'), $this->get('username'), $this->get('remote_ip')), LOG_ALERT, __FILE__, __LINE__);
568                }
569                // Increment user's warning level.
570                $db->query("UPDATE " . $this->_params['db_table'] . " SET abuse_warning_level = abuse_warning_level + 1 WHERE " . $this->_params['db_primary_key'] . " = '" . $this->get('user_id') . "'");
571                // Reset the login counter for this user.
572                $db->query("DELETE FROM " . $this->_params['db_login_table'] . " WHERE " . $this->_params['db_primary_key'] . " = '" . $this->get('user_id') . "'");
573                // No login: reset password because of account abuse!
574                $this->clear();
575                return false;
576            }
577
578            // Update the login counter table with this login access. Convert IP to binary.
579            // TODO: this query could benefit from INSERT DELAYED.
580            $db->query("
581                INSERT INTO " . $this->_params['db_login_table'] . " (
582                    " . $this->_params['db_primary_key'] . ",
583                    login_datetime,
584                    remote_ip_binary
585                ) VALUES (
586                    '" . $this->get('user_id') . "',
587                    '" . $this->get('login_datetime') . "',
588                    '" . sprintf('%032b', ip2long($this->get('remote_ip'))) . "'
589                )
590            ");
591        }
592
593        // Update user table with this login.
594        $db->query("
595            UPDATE " . $this->_params['db_table'] . " SET
596                last_login_datetime = '" . $this->get('login_datetime') . "',
597                last_access_datetime = '" . $this->get('login_datetime') . "',
598                last_login_ip = '" . $this->get('remote_ip') . "'
599            WHERE " . $this->_params['db_primary_key'] . " = '" . $this->get('user_id') . "'
600        ");
601
602        // Session created! We're logged-in!
603        return true;
604    }
605
606    /**
607     * Test if user has a currently logged-in session.
608     *  - authentication flag set to true
609     *  - username not empty
610     *  - total logged-in time is not greater than login_timeout
611     *  - idle time is not greater than idle_timeout
612     *  - remote address is the same as the login remote address
613     *
614     * TODO: implement persisten sessions as per https://paragonie.com/blog/2015/04/secure-authentication-php-with-long-term-persistence
615     *
616     * @access public
617     */
618    public function isLoggedIn($user_id=null)
619    {
620        $app =& App::getInstance();
621        $db =& DB::getInstance();
622
623        $this->initDB();
624
625        if (isset($user_id)) {
626            // Check the login status of a specific user.
627            $qid = $db->query("
628                SELECT
629                    TIMESTAMPDIFF(SECOND, last_login_datetime, NOW()) AS seconds_since_last_login,
630                    TIMESTAMPDIFF(SECOND, last_access_datetime, NOW()) AS seconds_since_last_access
631                FROM " . $this->_params['db_table'] . "
632                WHERE " . $this->_params['db_primary_key'] . " = '" . $db->escapeString($user_id) . "'
633                AND last_login_datetime > DATE_SUB(NOW(), INTERVAL '" . $db->escapeString($this->_params['login_timeout']) . "' SECOND)
634                AND last_access_datetime > DATE_SUB(NOW(), INTERVAL '" . $db->escapeString($this->_params['idle_timeout']) . "' SECOND)
635            ");
636            $result = mysql_fetch_assoc($qid);
637            if (mysql_num_rows($qid) > 0 && isset($result['seconds_since_last_login']) && isset($result['seconds_since_last_access'])) {
638                $seconds_until_login_timeout = max(0, $this->_params['login_timeout'] - $result['seconds_since_last_login']);
639                $seconds_until_idle_timeout = max(0, $this->_params['idle_timeout'] - $result['seconds_since_last_access']);
640                $session_expiry_seconds = min($seconds_until_login_timeout, $seconds_until_idle_timeout);
641                $app->logMsg(sprintf('Returning true login status for user_id %s (session expires in %s seconds)', $user_id, $session_expiry_seconds), LOG_DEBUG, __FILE__, __LINE__);
642                return $session_expiry_seconds;
643            } else {
644                $app->logMsg(sprintf('Returning false login status for user_id %s', $user_id), LOG_DEBUG, __FILE__, __LINE__);
645                return false;
646            }
647        }
648
649        // User login test need only be run once per script execution. We cache the result in the session.
650        if ($this->_authentication_tested && isset($_SESSION['_auth_sql'][$this->_ns]['authenticated'])) {
651            $app->logMsg(sprintf('Returning cached authentication status: %s', ($_SESSION['_auth_sql'][$this->_ns]['authenticated'] ? 'true' : 'false')), LOG_DEBUG, __FILE__, __LINE__);
652            return $_SESSION['_auth_sql'][$this->_ns]['authenticated'];
653        }
654
655        // Testing login should occur once. This is the first time. Set flag.
656        $this->_authentication_tested = true;
657
658        // Some users will access from networks with a changing IP number (i.e. behind a proxy server).
659        // These users must be allowed entry by adding their IP to the list of trusted_networks, or their usernames to the list of match_remote_ip_exempt_usernames.
660        if ($trusted_net = ipInRange(getRemoteAddr(), $this->_params['trusted_networks'])) {
661            $user_in_trusted_network = true;
662            $app->logMsg(sprintf('User_id %s accessing from trusted network %s',
663                ($this->get('user_id') ? $this->get('user_id') . ' (' .  $this->get('username') . ')' : 'unknown'),
664                $trusted_net
665            ), LOG_DEBUG, __FILE__, __LINE__);
666        } else {
667            $user_in_trusted_network = false;
668        }
669
670        // Do we match the user's remote IP at all? Yes, if set in config and not disabled for specific user.
671        if ($this->getParam('match_remote_ip') && !$this->get('match_remote_ip_exempt')) {
672            $remote_ip_is_matched = (isset($_SESSION['_auth_sql'][$this->_ns]['remote_ip']) && $_SESSION['_auth_sql'][$this->_ns]['remote_ip'] == getRemoteAddr()) || $user_in_trusted_network;
673        } else {
674            $app->logMsg(sprintf('User_id %s exempt from remote_ip match (comparing %s == %s)',
675                ($this->get('user_id') ? $this->get('user_id') . ' (' .  $this->get('username') . ')' : 'unknown'),
676                $_SESSION['_auth_sql'][$this->_ns]['remote_ip'],
677                getRemoteAddr()
678            ), LOG_DEBUG, __FILE__, __LINE__);
679            $remote_ip_is_matched = true;
680        }
681
682        // Test login with information stored in session. Skip IP matching for users from trusted networks.
683        if (isset($_SESSION['_auth_sql'][$this->_ns]['authenticated'])
684            && true === $_SESSION['_auth_sql'][$this->_ns]['authenticated']
685            && isset($_SESSION['_auth_sql'][$this->_ns]['username'])
686            && !empty($_SESSION['_auth_sql'][$this->_ns]['username'])
687            && isset($_SESSION['_auth_sql'][$this->_ns]['login_datetime'])
688            && strtotime($_SESSION['_auth_sql'][$this->_ns]['login_datetime']) > (time() - $this->_params['login_timeout'])
689            && isset($_SESSION['_auth_sql'][$this->_ns]['last_access_datetime'])
690            && strtotime($_SESSION['_auth_sql'][$this->_ns]['last_access_datetime']) > (time() - $this->_params['idle_timeout'])
691            && $remote_ip_is_matched
692        ) {
693            // User is authenticated!
694
695            // Update the last_access_datetime to now.
696            $this->set('last_access_datetime', date('Y-m-d H:i:s'));
697
698            // Update the DB with the last_access_datetime and increment the seconds_online.
699            $db->query("
700                UPDATE " . $this->_params['db_table'] . " SET
701                seconds_online = seconds_online + ABS(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(last_access_datetime)) + 1,
702                last_access_datetime = '" . $this->get('last_access_datetime') . "'
703                WHERE " . $this->_params['db_primary_key'] . " = '" . $this->get('user_id') . "'
704            ");
705            if (mysql_affected_rows($db->getDBH()) > 0) {
706                // User record still exists in DB. Do this to ensure user was not delete from DB between accesses. Notice "+ 1" in SQL above to ensure record is modified.
707                $app->logMsg(sprintf('Session authenticated for user_id %s (%s).', $this->get('user_id'), $this->get('username')), LOG_DEBUG, __FILE__, __LINE__);
708                // TODO: This auth check doesn't match parity when calling isLoggedIn($user_id) with a user_id, because the latter checks last_login_datetime in DB, and the former only checks SESSION. These two can be out-of-sync after loading DB via sdbdown.
709                return true;
710            } else {
711                $app->logMsg(sprintf('Session update failed; record not found for user_id %s (%s).', $this->get('user_id'), $this->get('username')), LOG_NOTICE, __FILE__, __LINE__);
712            }
713        } else if (isset($_SESSION['_auth_sql'][$this->_ns]['authenticated']) && true === $_SESSION['_auth_sql'][$this->_ns]['authenticated']) {
714            // User is authenticated, but login has expired.
715
716            // Log the reason for login expiration.
717            $expire_reasons = array();
718            $user_notified = false;
719            if (!isset($_SESSION['_auth_sql'][$this->_ns]['username']) || empty($_SESSION['_auth_sql'][$this->_ns]['username'])) {
720                $expire_reasons[] = 'username not found';
721            }
722            if (!isset($_SESSION['_auth_sql'][$this->_ns]['login_datetime']) || strtotime($_SESSION['_auth_sql'][$this->_ns]['login_datetime']) <= (time() - $this->_params['login_timeout'])) {
723                $expire_reasons[] = sprintf('login_timeout expired (%s older than %s seconds ago)', $_SESSION['_auth_sql'][$this->_ns]['login_datetime'], $this->_params['login_timeout']);
724            }
725            if (!isset($_SESSION['_auth_sql'][$this->_ns]['last_access_datetime']) || strtotime($_SESSION['_auth_sql'][$this->_ns]['last_access_datetime']) <= (time() - $this->_params['idle_timeout'])) {
726                $expire_reasons[] = sprintf('idle_timeout expired (%s older than %s seconds ago)', $_SESSION['_auth_sql'][$this->_ns]['last_access_datetime'], $this->_params['idle_timeout']);
727                if (strtotime($_SESSION['_auth_sql'][$this->_ns]['last_access_datetime']) > (time() - 43200)) {
728                    // Only raise message if last session is less than 12 hours old.
729                    // Notify user why they were logged out if they haven't yet been given a reason.
730                    $user_notified || $app->raiseMsg(sprintf(_("For your security, we logged you out after being idle for %s. Please log in again."), humanTime($this->_params['idle_timeout'], 'hour', '%01.0f')), MSG_NOTICE, __FILE__, __LINE__);
731                    $user_notified = true;
732                }
733            }
734            if (!isset($_SESSION['_auth_sql'][$this->_ns]['remote_ip']) || $_SESSION['_auth_sql'][$this->_ns]['remote_ip'] != getRemoteAddr()) {
735                if ($this->getParam('match_remote_ip') && !$this->get('match_remote_ip_exempt') && !$user_in_trusted_network) {
736                    // There are three cases when a remote IP match will be the cause of a session termination:
737                    //   1. match_remote_ip config is enabled
738                    //   2. user is not match_remote_ip_exempt (set in the user_data, or in the match_remote_ip_exempt_usernames list)
739                    //   3. the user is connecting from a trusted network (their IP is listed in the trusted_networks)
740                    $expire_reasons[] = sprintf('remote_ip not matched (%s != %s)', $_SESSION['_auth_sql'][$this->_ns]['remote_ip'], getRemoteAddr());
741                    // Notify user why they were logged out if they haven't yet been given a reason.
742                    $user_notified || $app->raiseMsg(sprintf(_("For your security, we logged you out because your IP address changed. Please log in again."), null), MSG_NOTICE, __FILE__, __LINE__);
743                    $user_notified = true;
744                } else {
745                    $expire_reasons[] = sprintf('remote_ip not matched but user was exempt from this check (%s != %s)', $_SESSION['_auth_sql'][$this->_ns]['remote_ip'], getRemoteAddr());
746                }
747            }
748            $app->logMsg(sprintf('User_id %s (%s) session expired: %s', $this->get('user_id'), $this->get('username'), join(', ', $expire_reasons)), LOG_INFO, __FILE__, __LINE__);
749        } else {
750            $app->logMsg('Session is not authenticated', LOG_DEBUG, __FILE__, __LINE__);
751        }
752
753        // User is not authenticated.
754        $this->clear();
755        return false;
756    }
757
758    /**
759     * Redirect user to login page if they are not logged in.
760     *
761     * @param string $message The text description of a message to raise.
762     * @param int    $type    The type of message: MSG_NOTICE,
763     *                        MSG_SUCCESS, MSG_WARNING, or MSG_ERR.
764     * @param string $file    __FILE__.
765     * @param string $line    __LINE__.
766     * @access public
767     */
768    public function requireLogin($message='', $type=MSG_NOTICE, $file=null, $line=null)
769    {
770        $app =& App::getInstance();
771
772        if (!$this->isLoggedIn()) {
773            // Display message for requiring login. (RaiseMsg will ignore empty strings.)
774            if ('' != $message) {
775                $app->raiseMsg($message, $type, $file, $line);
776            }
777
778            // Login scripts must have the same 'login' tag for boomerangURL verification/manipulation.
779            $app->setBoomerangURL(getenv('REQUEST_URI'), 'login');
780            $app->dieURL($this->_params['login_url']);
781        }
782    }
783
784    /**
785     * This sets the 'blocked' field for a user in the db_table, and also
786     * adds an optional reason
787     *
788     * @param  string   $reason      The reason for blocking the account.
789     */
790    public function blockAccount($user_id=null, $reason='')
791    {
792        $app =& App::getInstance();
793        $db =& DB::getInstance();
794
795        $this->initDB();
796
797        if ($this->getParam('blocking')) {
798            if (mb_strlen($db->escapeString($reason)) > 255) {
799                // blocked_reason field is varchar(255).
800                $app->logMsg(sprintf('Blocked reason provided is greater than 255 characters: %s', $reason), LOG_WARNING, __FILE__, __LINE__);
801            }
802
803            // Get user_id if specified.
804            $user_id = isset($user_id) ? $user_id : $this->get('user_id');
805            $db->query("
806                UPDATE " . $this->_params['db_table'] . " SET
807                blocked = 'true',
808                blocked_reason = '" . $db->escapeString($reason) . "'
809                WHERE " . $this->_params['db_primary_key'] . " = '" . $db->escapeString($user_id) . "'
810            ");
811        }
812    }
813
814    /**
815     * Tests if the "blocked" flag is set for a user.
816     *
817     * @param  int      $user_id    User id to look for.
818     * @return boolean              True if the user is blocked, false otherwise.
819     */
820    public function isBlocked($user_id=null)
821    {
822        $db =& DB::getInstance();
823
824        $this->initDB();
825
826        if ($this->getParam('blocking')) {
827            // Get user_id if specified.
828            $user_id = isset($user_id) ? $user_id : $this->getVal('user_id');
829            $qid = $db->query("
830                SELECT 1
831                FROM " . $this->_params['db_table'] . "
832                WHERE blocked = 'true'
833                AND " . $this->_params['db_primary_key'] . " = '" . $db->escapeString($user_id) . "'
834            ");
835            return mysql_num_rows($qid) === 1;
836        }
837    }
838
839    /**
840     * Unblocks a user in the db_table, and clears any blocked_reason.
841     */
842    public function unblockAccount($user_id=null)
843    {
844        $db =& DB::getInstance();
845
846        $this->initDB();
847
848        if ($this->getParam('blocking')) {
849            // Get user_id if specified.
850            $user_id = isset($user_id) ? $user_id : $this->get('user_id');
851            $db->query("
852                UPDATE " . $this->_params['db_table'] . " SET
853                blocked = NULL,
854                blocked_reason = ''
855                WHERE " . $this->_params['db_primary_key'] . " = '" . $db->escapeString($user_id) . "'
856            ");
857        }
858    }
859
860    /**
861     * Returns true if username already exists in database.
862     *
863     * @param  string  $username    Username to look for.
864     * @return bool                 True if username exists.
865     */
866    public function usernameExists($username)
867    {
868        $db =& DB::getInstance();
869
870        $this->initDB();
871
872        $qid = $db->query("
873            SELECT 1
874            FROM " . $this->_params['db_table'] . "
875            WHERE " . $this->_params['db_username_column'] . " = '" . $db->escapeString($username) . "'
876        ");
877        return (mysql_num_rows($qid) > 0);
878    }
879
880    /**
881     * Returns a username for a specified user id.
882     *
883     * @param  string  $user_id     User id to look for.
884     * @return string               Username, or false if none found.
885     */
886    public function getUsername($user_id)
887    {
888        $db =& DB::getInstance();
889
890        $this->initDB();
891
892        $qid = $db->query("
893            SELECT " . $this->_params['db_username_column'] . "
894            FROM " . $this->_params['db_table'] . "
895            WHERE " . $this->_params['db_primary_key'] . " = '" . $db->escapeString($user_id) . "'
896        ");
897        if (list($username) = mysql_fetch_row($qid)) {
898            return $username;
899        } else {
900            return false;
901        }
902    }
903
904    /**
905     * Returns a user_id for a specified username.
906     *
907     * @param  string  $username    Username to look for.
908     * @return string               User_id, or false if none found.
909     */
910    public function getUserID($username)
911    {
912        $db =& DB::getInstance();
913
914        $this->initDB();
915
916        $qid = $db->query("
917            SELECT " . $this->_params['db_primary_key'] . "
918            FROM " . $this->_params['db_table'] . "
919            WHERE " . $this->_params['db_username_column'] . " = '" . $db->escapeString($username) . "'
920        ");
921        if (list($user_id) = mysql_fetch_row($qid)) {
922            return $user_id;
923        } else {
924            return false;
925        }
926    }
927
928    /*
929    * Generate a cryptographically secure, random password.
930    *
931    * @access   public
932    * @param    int  $bytes     Length of password (in bytes)
933    * @return   string          Random string of characters.
934    * @author   Quinn Comendant <quinn@strangecode.com>
935    * @version  1.0
936    * @since    15 Nov 2014 20:30:27
937    */
938    public function generatePassword($bytes=10)
939    {
940        $app =& App::getInstance();
941
942        $bytes = is_numeric($bytes) ? $bytes : 10;
943        $string = strtok(base64_encode(openssl_random_pseudo_bytes($bytes, $strong)), '=');
944        if (!$strong) {
945            $app->logMsg(sprintf('Password generated was not "cryptographically strong"; check your openssl.', null), LOG_NOTICE, __FILE__, __LINE__);
946        }
947
948        return $string;
949    }
950
951    /**
952     *
953     */
954    public function encryptPassword($password, $salt=null, $hash_type=null)
955    {
956        $app =& App::getInstance();
957
958        $password = (string)$password;
959
960        // Existing password hashes rely on the same key/salt being used to compare hashs.
961        // Don't change this (or the value applied to signing_key) unless you know existing hashes or signatures will not be affected!
962        $more_salt = 'B36D18E5-3FE4-4D58-8150-F26642852B81';
963
964        $hash_type = isset($hash_type) && !empty($hash_type) ? $hash_type : $this->getParam('hash_type');
965
966        switch ($hash_type) {
967        case self::ENCRYPT_PLAINTEXT :
968            $encrypted_password = $password;
969            break;
970
971        case self::ENCRYPT_CRYPT :
972            // If comparing password with an existing hashed password, provide the hashed password as the salt.
973            $encrypted_password = isset($salt) ? crypt($password, $salt) : crypt($password);
974            break;
975
976        case self::ENCRYPT_SHA1 :
977            $encrypted_password = sha1($password);
978            break;
979
980        case self::ENCRYPT_SHA1_HARDENED :
981            $encrypted_password = sha1($app->getParam('signing_key') . $password . $more_salt);
982            for ($i=0; $i < pow(2, 20); $i++) {
983                $encrypted_password = sha1($password . $encrypted_password);
984            }
985            break;
986
987        case self::ENCRYPT_MD5 :
988            $encrypted_password = md5($password);
989            break;
990
991        case self::ENCRYPT_MD5_HARDENED :
992            $encrypted_password = md5($app->getParam('signing_key') . $password . $more_salt);
993            for ($i=0; $i < pow(2, 20); $i++) {
994                $encrypted_password = md5($password . $encrypted_password);
995            }
996            break;
997
998        case self::ENCRYPT_PASSWORD_BCRYPT :
999            $encrypted_password = password_hash($password, PASSWORD_BCRYPT, array('cost' => 12));
1000            break;
1001
1002        case self::ENCRYPT_PASSWORD_DEFAULT :
1003            $encrypted_password = password_hash($password, PASSWORD_DEFAULT, array('cost' => 12));
1004            break;
1005
1006        default :
1007            $app->logMsg(sprintf('Unknown hash type: %s', $hash_type), LOG_WARNING, __FILE__, __LINE__);
1008            return false;
1009        }
1010
1011        // In case our hashing function returns 'false' or another empty value, bail out.
1012        if ('' == trim((string)$encrypted_password)) {
1013            $app->logMsg(sprintf('Invalid password hash returned ("%s") for hash type %s; check yo crypto!', $encrypted_password, $hash_type), LOG_ALERT, __FILE__, __LINE__);
1014            return false;
1015        }
1016
1017        return $encrypted_password;
1018    }
1019
1020    /*
1021    *
1022    *
1023    * @access   public
1024    * @param
1025    * @return
1026    * @author   Quinn Comendant <quinn@strangecode.com>
1027    * @version  1.0
1028    * @since    15 Nov 2014 21:37:28
1029    */
1030    public function verifyPassword($password, $encrypted_password, $hash_type=null)
1031    {
1032        $app =& App::getInstance();
1033
1034        $hash_type = isset($hash_type) && !empty($hash_type) ? $hash_type : $this->getParam('hash_type');
1035
1036        switch ($hash_type) {
1037        case self::ENCRYPT_CRYPT :
1038            return $this->encryptPassword($password, $encrypted_password, $hash_type) == $encrypted_password;
1039
1040        case self::ENCRYPT_PLAINTEXT :
1041        case self::ENCRYPT_MD5 :
1042        case self::ENCRYPT_MD5_HARDENED :
1043        case self::ENCRYPT_SHA1 :
1044        case self::ENCRYPT_SHA1_HARDENED :
1045            return $this->encryptPassword($password, $encrypted_password, $hash_type) == $encrypted_password;
1046
1047        case self::ENCRYPT_PASSWORD_BCRYPT :
1048        case self::ENCRYPT_PASSWORD_DEFAULT :
1049            return password_verify($password, $encrypted_password);
1050
1051        default :
1052            $app->logMsg(sprintf('Unknown hash type: %s', $hash_type), LOG_WARNING, __FILE__, __LINE__);
1053            return false;
1054        }
1055
1056    }
1057
1058    /**
1059     *
1060     */
1061    public function setPassword($user_id, $password, $hash_type=null)
1062    {
1063        $app =& App::getInstance();
1064        $db =& DB::getInstance();
1065
1066        $this->initDB();
1067
1068        // Get user_id if specified.
1069        $user_id = isset($user_id) ? $user_id : $this->get('user_id');
1070
1071        // New hash type.
1072        $hash_type = isset($hash_type) ? $hash_type : $this->getParam('hash_type');
1073
1074        // Save the hash method used if a table exists for it.
1075        $userpass_hashtype_clause = '';
1076        if ($db->columnExists($this->_params['db_table'], 'userpass_hashtype', false)) {
1077            $userpass_hashtype_clause = ", userpass_hashtype = '" . $db->escapeString($hash_type) . "'";
1078        }
1079
1080        // Issue the password change query.
1081        $db->query("
1082            UPDATE " . $this->_params['db_table'] . " SET
1083                userpass = '" . $db->escapeString($this->encryptPassword($password, null, $hash_type)) . "',
1084                modified_datetime = NOW(),
1085                modified_by_user_id = '" . $db->escapeString($user_id) . "'
1086                $userpass_hashtype_clause
1087            WHERE " . $this->_params['db_primary_key'] . " = '" . $db->escapeString($user_id) . "'
1088        ");
1089
1090        if (mysql_affected_rows($db->getDBH()) != 1) {
1091            $app->logMsg(sprintf('Failed to update password for user_id %s (no affected rows)', $user_id), LOG_WARNING, __FILE__, __LINE__);
1092            return false;
1093        }
1094
1095        $app->logMsg(sprintf('Password change successful for user_id %s', $user_id), LOG_INFO, __FILE__, __LINE__);
1096        return true;
1097    }
1098
1099    /**
1100     * Resets the password for the user with the specified id.
1101     *
1102     * @param  string $user_id   The id of the user to reset.
1103     * @param  string $reason    Additional message to add to the reset email.
1104     * @return string            The user's new password.
1105     */
1106    public function resetPassword($user_id=null, $reason='')
1107    {
1108        $app =& App::getInstance();
1109        $db =& DB::getInstance();
1110
1111        $this->initDB();
1112
1113        // Get user_id if specified.
1114        $user_id = isset($user_id) ? $user_id : $this->get('user_id');
1115
1116        // Reset password of a specific user.
1117        $qid = $db->query("
1118            SELECT * FROM " . $this->_params['db_table'] . "
1119            WHERE " . $this->_params['db_primary_key'] . " = '" . $db->escapeString($user_id) . "'
1120        ");
1121        if (!$user_data = mysql_fetch_assoc($qid)) {
1122            $app->logMsg(sprintf('Reset password failed. User_id %s not found.', $user_id), LOG_NOTICE, __FILE__, __LINE__);
1123            return false;
1124        }
1125
1126        // Get new password.
1127        $password = $this->generatePassword();
1128
1129        // Update password query.
1130        $this->setPassword($user_id, $password);
1131
1132        // Make sure user has an email on record before continuing.
1133        if (!isset($user_data['email']) || '' == trim($user_data['email'])) {
1134            $app->logMsg(sprintf('Password reset but notification failed, no email address for user_id %s (%s).', $user_data[$this->_params['db_primary_key']], $user_data[$this->_params['db_username_column']]), LOG_NOTICE, __FILE__, __LINE__);
1135        } else {
1136            // Send the new password in an email.
1137            $email = new Email(array(
1138                'to' => $user_data['email'],
1139                'from' => sprintf('"%s" <%s>', addcslashes($app->getParam('site_name'), '"'), $app->getParam('site_email')),
1140                'subject' => sprintf('%s password change', $app->getParam('site_name'))
1141            ));
1142            $email->setTemplate('codebase/services/templates/email_reset_password.txt');
1143            $email->replace(array(
1144                'SITE_NAME' => $app->getParam('site_name'),
1145                'SITE_URL' => $app->getParam('site_url'),
1146                'SITE_EMAIL' => $app->getParam('site_email'),
1147                'NAME' => ('' != $user_data['first_name'] . $user_data['last_name'] ? $user_data['first_name'] . ' ' . $user_data['last_name'] : $user_data[$this->_params['db_username_column']]),
1148                'USERNAME' => $user_data[$this->_params['db_username_column']],
1149                'PASSWORD' => $password,
1150                'REASON' => ('' == trim($reason) ? '' : trim($reason) . ' '), // Add a space after the reason if it exists for better formatting.
1151            ));
1152            $email->send();
1153        }
1154
1155        return array(
1156            'username' => $user_data[$this->_params['db_username_column']],
1157            'userpass' => $password
1158        );
1159    }
1160
1161} // end class
Note: See TracBrowser for help on using the repository browser.