Ignore:
Timestamp:
Mar 7, 2019 9:07:15 PM (5 years ago)
Author:
anonymous
Message:

Add Auth_SQL->isLoggedIn(CLIENT_ID) return seconds until session expiry. Add humanTime() JS function. Fix site_hostname port separator.

File:
1 edited

Legend:

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

    r634 r671  
    381381            $_SESSION['_auth_sql'][$this->_ns]['user_data'] = array();
    382382        }
    383         $_SESSION['_auth_sql'][$this->_ns]['user_data'][$key] = $val;
     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        }
    384389    }
    385390
     
    617622            // Check the login status of a specific user.
    618623            $qid = $db->query("
    619                 SELECT 1 FROM " . $this->_params['db_table'] . "
     624                SELECT
     625                    TIMESTAMPDIFF(SECOND, last_login_datetime, NOW()) AS seconds_since_last_login,
     626                    TIMESTAMPDIFF(SECOND, last_access_datetime, NOW()) AS seconds_since_last_access
     627                FROM " . $this->_params['db_table'] . "
    620628                WHERE " . $this->_params['db_primary_key'] . " = '" . $db->escapeString($user_id) . "'
    621                 AND last_login_datetime > DATE_SUB(NOW(), INTERVAL '" . $this->_params['login_timeout'] . "' SECOND)
    622                 AND last_access_datetime > DATE_SUB(NOW(), INTERVAL '" . $this->_params['idle_timeout'] . "' SECOND)
     629                AND last_login_datetime > DATE_SUB(NOW(), INTERVAL '" . $db->escapeString($this->_params['login_timeout']) . "' SECOND)
     630                AND last_access_datetime > DATE_SUB(NOW(), INTERVAL '" . $db->escapeString($this->_params['idle_timeout']) . "' SECOND)
    623631            ");
    624             $login_status = (mysql_num_rows($qid) > 0);
    625             $app->logMsg(sprintf('Returning %s login status for user_id %s', ($login_status ? 'true' : 'false'), $user_id), LOG_DEBUG, __FILE__, __LINE__);
    626             return $login_status;
     632            $result = mysql_fetch_assoc($qid);
     633            if (mysql_num_rows($qid) > 0 && isset($result['seconds_since_last_login']) && isset($result['seconds_since_last_access'])) {
     634                $seconds_until_login_timeout = max(0, $this->_params['login_timeout'] - $result['seconds_since_last_login']);
     635                $seconds_until_idle_timeout = max(0, $this->_params['idle_timeout'] - $result['seconds_since_last_access']);
     636                $session_expiry_seconds = min($seconds_until_login_timeout, $seconds_until_idle_timeout);
     637                $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__);
     638                return $session_expiry_seconds;
     639            } else {
     640                $app->logMsg(sprintf('Returning false login status for user_id %s', $user_id), LOG_DEBUG, __FILE__, __LINE__);
     641                return false;
     642            }
    627643        }
    628644
     
    672688        ) {
    673689            // User is authenticated!
    674             $_SESSION['_auth_sql'][$this->_ns]['last_access_datetime'] = date('Y-m-d H:i:s');
     690
     691            // Update the last_access_datetime to now.
     692            $this->set('last_access_datetime', date('Y-m-d H:i:s'));
    675693
    676694            // Update the DB with the last_access_datetime and increment the seconds_online.
Note: See TracChangeset for help on using the changeset viewer.