1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-12 17:54:44 +02:00

Minor improvements in SessionHandlerDB and ProcessSessionDB

This commit is contained in:
Ryan Cramer
2021-08-20 13:45:17 -04:00
parent 616c388f29
commit a00e89d0ed
2 changed files with 61 additions and 39 deletions

View File

@@ -5,7 +5,7 @@
* *
* This module accompanies installation of the SessionHandlerDB module * This module accompanies installation of the SessionHandlerDB module
* *
* ProcessWire 3.x, Copyright 2020 by Ryan Cramer * ProcessWire 3.x, Copyright 2021 by Ryan Cramer
* https://processwire.com * https://processwire.com
* *
*/ */
@@ -16,7 +16,7 @@ class ProcessSessionDB extends Process {
return array( return array(
'title' => __('Sessions', __FILE__), // getModuleInfo title 'title' => __('Sessions', __FILE__), // getModuleInfo title
'summary' => __('Enables you to browse active database sessions.', __FILE__), // getModuleInfo summary 'summary' => __('Enables you to browse active database sessions.', __FILE__), // getModuleInfo summary
'version' => 4, 'version' => 5,
'permanent' => false, 'permanent' => false,
'icon' => 'dashboard', 'icon' => 'dashboard',
'requires' => array('SessionHandlerDB'), 'requires' => array('SessionHandlerDB'),
@@ -40,23 +40,31 @@ class ProcessSessionDB extends Process {
// clean out any stray sessions that may have not yet hit the gc probability // clean out any stray sessions that may have not yet hit the gc probability
// because we don't want them in the list that we display // because we don't want them in the list that we display
$modules = $this->wire()->modules;
$config = $this->wire()->config;
$session = $this->wire()->session;
$database = $this->wire()->database;
$input = $this->wire()->input;
$pages = $this->wire()->pages;
$users = $this->wire()->users;
/** @var SessionHandlerDB $sessionHandlerDB */ /** @var SessionHandlerDB $sessionHandlerDB */
$sessionHandlerDB = $this->modules->get('SessionHandlerDB'); $sessionHandlerDB = $this->modules->get('SessionHandlerDB');
$sessionHandlerDB->gc($this->wire('config')->sessionExpireSeconds); $sessionHandlerDB->gc($config->sessionExpireSeconds);
$useIP = $sessionHandlerDB->useIP; $useIP = $sessionHandlerDB->useIP;
$useUA = $sessionHandlerDB->useUA; $useUA = $sessionHandlerDB->useUA;
$mins = (int) $this->input->post('mins'); $mins = (int) $input->post('mins');
if(!$mins) $mins = (int) $this->session->get('ProcessSessionDB_mins'); if(!$mins) $mins = (int) $session->get('ProcessSessionDB_mins');
if(!$mins) $mins = 5; if(!$mins) $mins = 5;
$this->session->set('ProcessSessionDB_mins', $mins); $session->set('ProcessSessionDB_mins', $mins);
/** @var InputfieldForm $form */ /** @var InputfieldForm $form */
$form = $this->wire('modules')->get('InputfieldForm'); $form = $modules->get('InputfieldForm');
/** @var InputfieldInteger $field */ /** @var InputfieldInteger $field */
$field = $this->wire('modules')->get('InputfieldInteger'); $field = $modules->get('InputfieldInteger');
$field->attr('name', 'mins'); $field->attr('name', 'mins');
$field->attr('value', $mins); $field->attr('value', $mins);
$field->label = sprintf($this->_n('Sessions active in last minute', 'Sessions active in last %d minutes', $mins), $mins); $field->label = sprintf($this->_n('Sessions active in last minute', 'Sessions active in last %d minutes', $mins), $mins);
@@ -67,7 +75,6 @@ class ProcessSessionDB extends Process {
$pagePaths = array(); $pagePaths = array();
$userNames = array(); $userNames = array();
$table = SessionHandlerDB::dbTableName; $table = SessionHandlerDB::dbTableName;
$database = $this->wire('database');
$limit = 500; $limit = 500;
$seconds = $mins * 60; $seconds = $mins * 60;
@@ -86,12 +93,13 @@ class ProcessSessionDB extends Process {
$numRows = $sessionHandlerDB->getNumSessions($mins * 60); $numRows = $sessionHandlerDB->getNumSessions($mins * 60);
} }
$table = $this->wire('modules')->get('MarkupAdminDataTable'); /** @var MarkupAdminDataTable $table */
$table = $modules->get('MarkupAdminDataTable');
$header = array( $header = array(
$this->_('Time'), $this->_('Time'),
$this->_('User'), $this->_('User'),
$this->_('Page'), $this->_('Page'),
); );
if($useIP) $header[] = $this->_('IP Addr'); if($useIP) $header[] = $this->_('IP Addr');
if($useUA) $header[] = $this->_('User Agent'); if($useUA) $header[] = $this->_('User Agent');
$table->headerRow($header); $table->headerRow($header);
@@ -110,7 +118,7 @@ class ProcessSessionDB extends Process {
$userName = $userNames[$user_id]; $userName = $userNames[$user_id];
} else { } else {
$user = $this->wire('users')->get($user_id); $user = $users->get($user_id);
$userName = $user && $user->id ? $user->name : '.'; $userName = $user && $user->id ? $user->name : '.';
if($userName !== '.') $userNames[$user_id] = $userName; if($userName !== '.') $userNames[$user_id] = $userName;
} }
@@ -119,7 +127,7 @@ class ProcessSessionDB extends Process {
$pagePath = $pagePaths[$pages_id]; $pagePath = $pagePaths[$pages_id];
} else { } else {
$page = $this->wire('pages')->get($pages_id); $page = $pages->get($pages_id);
$pagePath = $page->id ? $page->path : '.'; $pagePath = $page->id ? $page->path : '.';
if($pagePath !== '.') $pagePaths[$pages_id] = $pagePath; if($pagePath !== '.') $pagePaths[$pages_id] = $pagePath;
} }
@@ -149,15 +157,15 @@ class ProcessSessionDB extends Process {
"</h2>" . "</h2>" .
$tableOut; $tableOut;
if($this->wire('config')->ajax) return $out; if($config->ajax) return $out;
/** @var InputfieldMarkup $markup */ /** @var InputfieldMarkup $markup */
$markup = $this->wire('modules')->get('InputfieldMarkup'); $markup = $modules->get('InputfieldMarkup');
$markup->value = "<div id='SessionList'>$out</div>"; $markup->value = "<div id='SessionList'>$out</div>";
$form->add($markup); $form->add($markup);
/** @var InputfieldSubmit $submit */ /** @var InputfieldSubmit $submit */
$submit = $this->wire('modules')->get('InputfieldSubmit'); $submit = $modules->get('InputfieldSubmit');
$submit->attr('value', $this->_('Refresh')); $submit->attr('value', $this->_('Refresh'));
$submit->icon = 'refresh'; $submit->icon = 'refresh';
$submit->attr('id+name', 'submit_session'); $submit->attr('id+name', 'submit_session');

View File

@@ -5,7 +5,7 @@
* *
* @see /wire/core/SessionHandler.php * @see /wire/core/SessionHandler.php
* *
* ProcessWire 3.x, Copyright 2020 by Ryan Cramer * ProcessWire 3.x, Copyright 2021 by Ryan Cramer
* https://processwire.com * https://processwire.com
* *
* @property int|bool $useIP Track IP address? * @property int|bool $useIP Track IP address?
@@ -21,7 +21,7 @@ class SessionHandlerDB extends WireSessionHandler implements Module, Configurabl
public static function getModuleInfo() { public static function getModuleInfo() {
return array( return array(
'title' => 'Session Handler Database', 'title' => 'Session Handler Database',
'version' => 5, 'version' => 6,
'summary' => "Installing this module makes ProcessWire store sessions in the database rather than the file system. Note that this module will log you out after install or uninstall.", 'summary' => "Installing this module makes ProcessWire store sessions in the database rather than the file system. Note that this module will log you out after install or uninstall.",
'installs' => array('ProcessSessionDB') 'installs' => array('ProcessSessionDB')
); );
@@ -55,7 +55,7 @@ class SessionHandlerDB extends WireSessionHandler implements Module, Configurabl
} }
public function wired() { public function wired() {
$this->database = $this->wire('database'); $this->database = $this->wire()->database;
parent::wired(); parent::wired();
} }
@@ -121,12 +121,19 @@ class SessionHandlerDB extends WireSessionHandler implements Module, Configurabl
public function write($id, $data) { public function write($id, $data) {
$table = self::dbTableName; $table = self::dbTableName;
$database = $this->database; $database = $this->database;
$user = $this->wire('user'); $user = $this->wire()->user;
$page = $this->wire('page'); $page = $this->wire()->page;
$user_id = $user && $user->id ? (int) $user->id : 0; $user_id = $user && $user->id ? (int) $user->id : 0;
$pages_id = $page && $page->id ? (int) $page->id : 0; $pages_id = $page && $page->id ? (int) $page->id : 0;
$ua = ($this->useUA && isset($_SERVER['HTTP_USER_AGENT'])) ? substr(strip_tags($_SERVER['HTTP_USER_AGENT']), 0, 255) : ''; $ua = ($this->useUA && isset($_SERVER['HTTP_USER_AGENT'])) ? substr(strip_tags($_SERVER['HTTP_USER_AGENT']), 0, 255) : '';
$ip = $this->useIP ? ((int) $this->wire('session')->getIP(true)) : ''; $ip = '';
if($this->useIP) {
$session = $this->wire()->session;
$ip = $session->getIP();
$ip = (strlen($ip) && strpos($ip, ':') === false ? ip2long($ip) : '');
// @todo DB schema for ipv6
}
$binds = array( $binds = array(
':id' => $id, ':id' => $id,
@@ -147,20 +154,28 @@ class SessionHandlerDB extends WireSessionHandler implements Module, Configurabl
} }
$sql = "INSERT INTO $table SET id=:id, $s ON DUPLICATE KEY UPDATE $s, ts=NOW()"; $sql = "INSERT INTO $table SET id=:id, $s ON DUPLICATE KEY UPDATE $s, ts=NOW()";
$query = $database->prepare($sql);
foreach($binds as $key => $value) { try {
$type = is_int($value) ? \PDO::PARAM_INT : \PDO::PARAM_STR; $query = $database->prepare($sql);
$query->bindValue($key, $value, $type); foreach($binds as $key => $value) {
$type = is_int($value) ? \PDO::PARAM_INT : \PDO::PARAM_STR;
$query->bindValue($key, $value, $type);
}
$result = $database->execute($query, false) ? true : false;
$query->closeCursor();
} catch(\Exception $e) {
$result = false;
$this->trackException($e);
} }
$result = $database->execute($query, false) ? true : false; try {
$query->closeCursor(); $query = $database->prepare("DO RELEASE_LOCK(:id)");
$query->bindValue(':id', $id, \PDO::PARAM_STR);
$query = $database->prepare("DO RELEASE_LOCK(:id)"); $database->execute($query, false);
$query->bindValue(':id', $id, \PDO::PARAM_STR); $query->closeCursor();
$database->execute($query, false); } catch(\Exception $e) {
$query->closeCursor(); $this->trackException($e);
}
return $result; return $result;
} }
@@ -221,7 +236,7 @@ class SessionHandlerDB extends WireSessionHandler implements Module, Configurabl
public function ___install() { public function ___install() {
$table = self::dbTableName; $table = self::dbTableName;
$charset = $this->wire('config')->dbCharset; $charset = $this->wire()->config->dbCharset;
$sql = "CREATE TABLE `$table` (" . $sql = "CREATE TABLE `$table` (" .
"id CHAR(32) NOT NULL, " . "id CHAR(32) NOT NULL, " .
@@ -358,11 +373,10 @@ class SessionHandlerDB extends WireSessionHandler implements Module, Configurabl
* *
*/ */
public function getNumSessions($seconds = 300) { public function getNumSessions($seconds = 300) {
$sql = "SELECT count(*) FROM " . self::dbTableName . " WHERE ts > :ts"; $seconds = (int) $seconds;
$query = $this->database->prepare($sql); $sql = "SELECT COUNT(*) FROM " . self::dbTableName . " WHERE ts > DATE_SUB(NOW(), INTERVAL $seconds SECOND)";
$query->bindValue(':ts', date('Y-m-d H:i:s', (time() - $seconds))); $query = $this->database->query($sql);
$query->execute(); $numSessions = (int) $query->fetchColumn();
list($numSessions) = $query->fetch(\PDO::FETCH_NUM);
return $numSessions; return $numSessions;
} }