Merge branch '3.3.x'

This commit is contained in:
Marc Alexander 2023-11-04 08:28:08 +01:00
commit d65141aa75
No known key found for this signature in database
GPG key ID: 50E0D2423696F995

View file

@ -450,6 +450,12 @@ class session
// Is user banned? Are they excluded? Won't return on ban, exists within method // Is user banned? Are they excluded? Won't return on ban, exists within method
$this->check_ban_for_current_session($config); $this->check_ban_for_current_session($config);
// Update user last visit time accordingly, but in a minute or so
if ((int) $this->data['session_time'] - (int) $this->data['user_lastvisit'] > 60)
{
$this->update_user_lastvisit();
}
return true; return true;
} }
} }
@ -694,14 +700,11 @@ class session
{ {
$this->session_id = $this->data['session_id']; $this->session_id = $this->data['session_id'];
// Only update session DB a minute or so after last update or if page changes // Only sync user last visit time in a minute or so after last session data update or if the page changes
if ($this->time_now - $this->data['session_time'] > 60 || ($this->update_session_page && $this->data['session_page'] != $this->page['page'])) if ((int) $this->data['session_time'] - (int) $this->data['user_lastvisit'] > 60 || ($this->update_session_page && $this->data['session_page'] != $this->page['page']))
{ {
// Update the last visit time // Update the last visit time
$sql = 'UPDATE ' . USERS_TABLE . ' $this->update_user_lastvisit();
SET user_lastvisit = ' . (int) $this->data['session_time'] . '
WHERE user_id = ' . (int) $this->data['user_id'];
$db->sql_query($sql);
} }
$SID = '?sid='; $SID = '?sid=';
@ -837,12 +840,6 @@ class session
{ {
$this->data['session_time'] = $this->data['session_last_visit'] = $this->time_now; $this->data['session_time'] = $this->data['session_last_visit'] = $this->time_now;
// Update the last visit time
$sql = 'UPDATE ' . USERS_TABLE . '
SET user_lastvisit = ' . (int) $this->data['session_time'] . '
WHERE user_id = ' . (int) $this->data['user_id'];
$db->sql_query($sql);
if ($phpbb_container->getParameter('session.force_sid')) if ($phpbb_container->getParameter('session.force_sid'))
{ {
$SID = '?sid='; $SID = '?sid=';
@ -850,6 +847,9 @@ class session
} }
} }
// Update the last visit time
$this->update_user_lastvisit();
$session_data = $sql_ary; $session_data = $sql_ary;
/** /**
* Event to send new session data to extension * Event to send new session data to extension
@ -976,8 +976,8 @@ class session
} }
/** /**
* Get expired sessions for registered users, only most recent for each user * Get most recent session for each registered user to sync user last visit with it
* Inner SELECT gets most recent expired sessions for unique session_user_id * Inner SELECT gets most recent sessions for each unique session_user_id
* Outer SELECT gets data for them * Outer SELECT gets data for them
*/ */
$sql_select = 'SELECT s1.session_page, s1.session_user_id, s1.session_time AS recent_time $sql_select = 'SELECT s1.session_page, s1.session_user_id, s1.session_time AS recent_time
@ -985,8 +985,7 @@ class session
INNER JOIN ( INNER JOIN (
SELECT session_user_id, MAX(session_time) AS recent_time SELECT session_user_id, MAX(session_time) AS recent_time
FROM ' . SESSIONS_TABLE . ' FROM ' . SESSIONS_TABLE . '
WHERE session_time < ' . ($this->time_now - (int) $config['session_length']) . ' WHERE session_user_id <> ' . ANONYMOUS . '
AND session_user_id <> ' . ANONYMOUS . '
GROUP BY session_user_id GROUP BY session_user_id
) AS s2 ) AS s2
ON s1.session_user_id = s2.session_user_id ON s1.session_user_id = s2.session_user_id
@ -1811,4 +1810,20 @@ class session
{ {
return isset($this->data['user_id']) ? (int) $this->data['user_id'] : ANONYMOUS; return isset($this->data['user_id']) ? (int) $this->data['user_id'] : ANONYMOUS;
} }
/**
* Update user last visit time
*/
public function update_user_lastvisit()
{
global $db;
if (isset($this->data['session_time'], $this->data['user_id']))
{
$sql = 'UPDATE ' . USERS_TABLE . '
SET user_lastvisit = ' . (int) $this->data['session_time'] . '
WHERE user_id = ' . (int) $this->data['user_id'];
$db->sql_query($sql);
}
}
} }