mirror of
https://github.com/processwire/processwire.git
synced 2025-08-11 09:14:58 +02:00
Minor improvements in SessionHandlerDB and ProcessSessionDB
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
*
|
||||
* 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
|
||||
*
|
||||
*/
|
||||
@@ -16,7 +16,7 @@ class ProcessSessionDB extends Process {
|
||||
return array(
|
||||
'title' => __('Sessions', __FILE__), // getModuleInfo title
|
||||
'summary' => __('Enables you to browse active database sessions.', __FILE__), // getModuleInfo summary
|
||||
'version' => 4,
|
||||
'version' => 5,
|
||||
'permanent' => false,
|
||||
'icon' => 'dashboard',
|
||||
'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
|
||||
// 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 */
|
||||
$sessionHandlerDB = $this->modules->get('SessionHandlerDB');
|
||||
$sessionHandlerDB->gc($this->wire('config')->sessionExpireSeconds);
|
||||
$sessionHandlerDB->gc($config->sessionExpireSeconds);
|
||||
|
||||
$useIP = $sessionHandlerDB->useIP;
|
||||
$useUA = $sessionHandlerDB->useUA;
|
||||
|
||||
$mins = (int) $this->input->post('mins');
|
||||
if(!$mins) $mins = (int) $this->session->get('ProcessSessionDB_mins');
|
||||
$mins = (int) $input->post('mins');
|
||||
if(!$mins) $mins = (int) $session->get('ProcessSessionDB_mins');
|
||||
if(!$mins) $mins = 5;
|
||||
$this->session->set('ProcessSessionDB_mins', $mins);
|
||||
$session->set('ProcessSessionDB_mins', $mins);
|
||||
|
||||
/** @var InputfieldForm $form */
|
||||
$form = $this->wire('modules')->get('InputfieldForm');
|
||||
$form = $modules->get('InputfieldForm');
|
||||
|
||||
/** @var InputfieldInteger $field */
|
||||
$field = $this->wire('modules')->get('InputfieldInteger');
|
||||
$field = $modules->get('InputfieldInteger');
|
||||
$field->attr('name', 'mins');
|
||||
$field->attr('value', $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();
|
||||
$userNames = array();
|
||||
$table = SessionHandlerDB::dbTableName;
|
||||
$database = $this->wire('database');
|
||||
$limit = 500;
|
||||
$seconds = $mins * 60;
|
||||
|
||||
@@ -86,12 +93,13 @@ class ProcessSessionDB extends Process {
|
||||
$numRows = $sessionHandlerDB->getNumSessions($mins * 60);
|
||||
}
|
||||
|
||||
$table = $this->wire('modules')->get('MarkupAdminDataTable');
|
||||
/** @var MarkupAdminDataTable $table */
|
||||
$table = $modules->get('MarkupAdminDataTable');
|
||||
$header = array(
|
||||
$this->_('Time'),
|
||||
$this->_('User'),
|
||||
$this->_('Page'),
|
||||
);
|
||||
);
|
||||
if($useIP) $header[] = $this->_('IP Addr');
|
||||
if($useUA) $header[] = $this->_('User Agent');
|
||||
$table->headerRow($header);
|
||||
@@ -110,7 +118,7 @@ class ProcessSessionDB extends Process {
|
||||
$userName = $userNames[$user_id];
|
||||
|
||||
} else {
|
||||
$user = $this->wire('users')->get($user_id);
|
||||
$user = $users->get($user_id);
|
||||
$userName = $user && $user->id ? $user->name : '.';
|
||||
if($userName !== '.') $userNames[$user_id] = $userName;
|
||||
}
|
||||
@@ -119,7 +127,7 @@ class ProcessSessionDB extends Process {
|
||||
$pagePath = $pagePaths[$pages_id];
|
||||
|
||||
} else {
|
||||
$page = $this->wire('pages')->get($pages_id);
|
||||
$page = $pages->get($pages_id);
|
||||
$pagePath = $page->id ? $page->path : '.';
|
||||
if($pagePath !== '.') $pagePaths[$pages_id] = $pagePath;
|
||||
}
|
||||
@@ -149,15 +157,15 @@ class ProcessSessionDB extends Process {
|
||||
"</h2>" .
|
||||
$tableOut;
|
||||
|
||||
if($this->wire('config')->ajax) return $out;
|
||||
if($config->ajax) return $out;
|
||||
|
||||
/** @var InputfieldMarkup $markup */
|
||||
$markup = $this->wire('modules')->get('InputfieldMarkup');
|
||||
$markup = $modules->get('InputfieldMarkup');
|
||||
$markup->value = "<div id='SessionList'>$out</div>";
|
||||
$form->add($markup);
|
||||
|
||||
/** @var InputfieldSubmit $submit */
|
||||
$submit = $this->wire('modules')->get('InputfieldSubmit');
|
||||
$submit = $modules->get('InputfieldSubmit');
|
||||
$submit->attr('value', $this->_('Refresh'));
|
||||
$submit->icon = 'refresh';
|
||||
$submit->attr('id+name', 'submit_session');
|
||||
|
@@ -5,7 +5,7 @@
|
||||
*
|
||||
* @see /wire/core/SessionHandler.php
|
||||
*
|
||||
* ProcessWire 3.x, Copyright 2020 by Ryan Cramer
|
||||
* ProcessWire 3.x, Copyright 2021 by Ryan Cramer
|
||||
* https://processwire.com
|
||||
*
|
||||
* @property int|bool $useIP Track IP address?
|
||||
@@ -21,7 +21,7 @@ class SessionHandlerDB extends WireSessionHandler implements Module, Configurabl
|
||||
public static function getModuleInfo() {
|
||||
return array(
|
||||
'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.",
|
||||
'installs' => array('ProcessSessionDB')
|
||||
);
|
||||
@@ -55,7 +55,7 @@ class SessionHandlerDB extends WireSessionHandler implements Module, Configurabl
|
||||
}
|
||||
|
||||
public function wired() {
|
||||
$this->database = $this->wire('database');
|
||||
$this->database = $this->wire()->database;
|
||||
parent::wired();
|
||||
}
|
||||
|
||||
@@ -121,12 +121,19 @@ class SessionHandlerDB extends WireSessionHandler implements Module, Configurabl
|
||||
public function write($id, $data) {
|
||||
$table = self::dbTableName;
|
||||
$database = $this->database;
|
||||
$user = $this->wire('user');
|
||||
$page = $this->wire('page');
|
||||
$user = $this->wire()->user;
|
||||
$page = $this->wire()->page;
|
||||
$user_id = $user && $user->id ? (int) $user->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) : '';
|
||||
$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(
|
||||
':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()";
|
||||
$query = $database->prepare($sql);
|
||||
|
||||
foreach($binds as $key => $value) {
|
||||
$type = is_int($value) ? \PDO::PARAM_INT : \PDO::PARAM_STR;
|
||||
$query->bindValue($key, $value, $type);
|
||||
try {
|
||||
$query = $database->prepare($sql);
|
||||
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;
|
||||
$query->closeCursor();
|
||||
|
||||
$query = $database->prepare("DO RELEASE_LOCK(:id)");
|
||||
$query->bindValue(':id', $id, \PDO::PARAM_STR);
|
||||
$database->execute($query, false);
|
||||
$query->closeCursor();
|
||||
try {
|
||||
$query = $database->prepare("DO RELEASE_LOCK(:id)");
|
||||
$query->bindValue(':id', $id, \PDO::PARAM_STR);
|
||||
$database->execute($query, false);
|
||||
$query->closeCursor();
|
||||
} catch(\Exception $e) {
|
||||
$this->trackException($e);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
@@ -221,7 +236,7 @@ class SessionHandlerDB extends WireSessionHandler implements Module, Configurabl
|
||||
public function ___install() {
|
||||
|
||||
$table = self::dbTableName;
|
||||
$charset = $this->wire('config')->dbCharset;
|
||||
$charset = $this->wire()->config->dbCharset;
|
||||
|
||||
$sql = "CREATE TABLE `$table` (" .
|
||||
"id CHAR(32) NOT NULL, " .
|
||||
@@ -358,11 +373,10 @@ class SessionHandlerDB extends WireSessionHandler implements Module, Configurabl
|
||||
*
|
||||
*/
|
||||
public function getNumSessions($seconds = 300) {
|
||||
$sql = "SELECT count(*) FROM " . self::dbTableName . " WHERE ts > :ts";
|
||||
$query = $this->database->prepare($sql);
|
||||
$query->bindValue(':ts', date('Y-m-d H:i:s', (time() - $seconds)));
|
||||
$query->execute();
|
||||
list($numSessions) = $query->fetch(\PDO::FETCH_NUM);
|
||||
$seconds = (int) $seconds;
|
||||
$sql = "SELECT COUNT(*) FROM " . self::dbTableName . " WHERE ts > DATE_SUB(NOW(), INTERVAL $seconds SECOND)";
|
||||
$query = $this->database->query($sql);
|
||||
$numSessions = (int) $query->fetchColumn();
|
||||
return $numSessions;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user