Changeset 671


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.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/js/Utilities.js

    r654 r671  
    165165Uppercase the first letter of string.
    166166---------------------------------------------------------------------
    167 > 'hello world'.trim()
     167> 'hello world'.ucfirst()
    168168Hello world
    169169---------------------------------------------------------------------
     
    178178}
    179179
     180
     181/*
     182Returns a human readable amount of time for the given amount of seconds.
     183Months are calculated using the real number of days in a year: 365.2422 / 12.
     184@param    int seconds Seconds of time.
     185@param    string max_unit Key value from the units array.
     186@return   string Value of units elapsed.
     187---------------------------------------------------------------------
     188> Strangecode.humanTime(3600)
     1891 hour
     190---------------------------------------------------------------------
     191* @access   public
     192* @version  1.0
     193* @since    06 Mar 2019
     194*/
     195Strangecode.humanTime = function(seconds, max_unit) {
     196    // Units: array of seconds in the unit, singular and plural unit names.
     197    var units = {
     198        'second': [1, 'second', 'seconds'],
     199        'minute': [60, 'minute', 'minutes'],
     200        'hour': [3600, 'hour', 'hours'],
     201        'day': [86400, 'day', 'days'],
     202        'week': [604800, 'week', 'weeks'],
     203        'month': [2629743.84, 'month', 'months'],
     204        'year': [31556926.08, 'year', 'years'],
     205        'decade': [315569260.8, 'decade', 'decades'],
     206        'century': [3155692608, 'century', 'centuries'],
     207    };
     208
     209    // Max unit to calculate.
     210    max_unit = typeof max_unit === 'string' && units[max_unit] ? max_unit : 'year';
     211
     212    var final_time = seconds;
     213    var final_unit = 'second';
     214    for (var k in units) {
     215        if (seconds >= units[k][0]) {
     216            final_time = seconds / units[k][0];
     217            final_unit = k;
     218        }
     219        if (max_unit == final_unit) {
     220            break;
     221        }
     222    }
     223    final_time = Number(final_time).toFixed(0);
     224    return '{1} {2}'.format(final_time, (1 == final_time ? units[final_unit][1] : units[final_unit][2]));
     225}
     226
  • trunk/lib/App.inc.php

    r670 r671  
    479479         */
    480480
    481         $safe_http_host = preg_replace('/[^a-z\d.-]/', '', getenv('HTTP_HOST'));
     481        $safe_http_host = preg_replace('/[^a-z\d.:-]/', '', getenv('HTTP_HOST'));
    482482        if ('' != $safe_http_host && '' == $this->getParam('site_hostname')) {
    483483            $this->setParam(array('site_hostname' => $safe_http_host));
  • 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.
  • trunk/lib/Utilities.inc.php

    r670 r671  
    14051405function absoluteMe()
    14061406{
    1407     $safe_http_host = preg_replace('/[^a-z\d.-]/', '', getenv('HTTP_HOST'));
     1407    $safe_http_host = preg_replace('/[^a-z\d.:-]/', '', getenv('HTTP_HOST'));
    14081408    return sprintf('%s://%s%s', (getenv('HTTPS') ? 'https' : 'http'), $safe_http_host, getenv('REQUEST_URI'));
    14091409}
Note: See TracChangeset for help on using the changeset viewer.