Fixes to the logging system

main
Yasen Pramatarov 2024-09-16 19:08:03 +03:00
parent 1c710bef35
commit 33dfbcdeea
7 changed files with 65 additions and 23 deletions

View File

@ -30,16 +30,26 @@ class Log {
} }
// read logs // read logs
public function readLog($user_id, $scope='user') { public function readLog($user_id, $scope, $offset=0, $items_per_page='') {
$sql = 'SELECT * FROM logs';
if ($scope === 'user') { if ($scope === 'user') {
$sql .= ' WHERE user_id = :user_id'; $sql = 'SELECT * FROM logs WHERE user_id = :user_id ORDER BY time DESC';
if ($items_per_page) {
$items_per_page = (int)$items_per_page;
$sql .= ' LIMIT ' . $offset . ',' . $items_per_page;
}
$query = $this->db->prepare($sql); $query = $this->db->prepare($sql);
$query->execute([ $query->execute([
':user_id' => $user_id, ':user_id' => $user_id,
]); ]);
} }
if ($scope === 'system') { if ($scope === 'system') {
$sql = 'SELECT * FROM logs ORDER BY time DESC';
if ($items_per_page) {
$items_per_page = (int)$items_per_page;
$sql .= ' LIMIT ' . $offset . ',' . $items_per_page;
}
$query = $this->db->prepare($sql); $query = $this->db->prepare($sql);
$query->execute(); $query->execute();
} }

View File

@ -8,8 +8,8 @@ try {
// connect to database // connect to database
$dbWeb = connectDB($config); $dbWeb = connectDB($config);
require '../app/classes/user.php'; // require '../app/classes/user.php';
$userObject = new User($dbWeb); // $userObject = new User($dbWeb);
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) { if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
$username = $_POST['username']; $username = $_POST['username'];
@ -44,7 +44,7 @@ try {
// redirect to index // redirect to index
$_SESSION['notice'] = "Login successful"; $_SESSION['notice'] = "Login successful";
$user_id = $userObject->getUserId($username)[0]['id']; $user_id = $userObject->getUserId($username)[0]['id'];
$logObject->insertLog($user_id, "User \"$username\" logged in.", 'user'); $logObject->insertLog($user_id, "Login: User \"$username\" logged in.", 'user');
header('Location: index.php'); header('Location: index.php');
exit(); exit();
@ -52,7 +52,7 @@ try {
} else { } else {
$_SESSION['error'] = "Login failed."; $_SESSION['error'] = "Login failed.";
$user_id = $userObject->getUserId($username)[0]['id']; $user_id = $userObject->getUserId($username)[0]['id'];
$logObject->insertLog($user_id, "Failed login attempt for user \"$username\".", 'user'); $logObject->insertLog($user_id, "Login: Failed login attempt for user \"$username\".", 'user');
header('Location: index.php'); header('Location: index.php');
exit(); exit();
} }

View File

@ -7,22 +7,45 @@
// specify time range // specify time range
include '../app/helpers/time_range.php'; include '../app/helpers/time_range.php';
// pagination variables
$items_per_page = 15;
$browse_page = $_REQUEST['p'] ?? 1;
$browse_page = (int)$browse_page;
$offset = ($browse_page -1) * $items_per_page;
// user or system
$scope = 'user';
// prepare the result // prepare the result
$search = $logObject->readLog($user_id, 'user'); $search = $logObject->readLog($user_id, $scope, $offset, $items_per_page);
$search_all = $logObject->readLog($user_id, $scope);
if (!empty($search)) { if (!empty($search)) {
// we get total items and number of pages
$item_count = count($search_all);
$page_count = ceil($item_count / $items_per_page);
$logs = array(); $logs = array();
$logs['records'] = array(); $logs['records'] = array();
foreach ($search as $item) { foreach ($search as $item) {
extract($item);
// when we show only user's logs, omit user_id column
if ($scope === 'user') {
$log_record = array( $log_record = array(
// assign title to the field in the array record // assign title to the field in the array record
'user ID' => $user_id, 'time' => $item['time'],
'time' => $time, 'log message' => $item['message']
'log message' => $message
); );
} else {
$log_record = array(
// assign title to the field in the array record
'userID' => $item['user_id'],
'time' => $item['time'],
'log message' => $item['message']
);
}
// populate the result array // populate the result array
array_push($logs['records'], $log_record); array_push($logs['records'], $log_record);
} }
@ -35,7 +58,7 @@ $widget['name'] = 'Logs';
$username = $userObject->getUserDetails($user_id)[0]['username']; $username = $userObject->getUserDetails($user_id)[0]['username'];
$widget['title'] = "Log events for user \"$username\""; $widget['title'] = "Log events for user \"$username\"";
$widget['filter'] = true; $widget['filter'] = true;
if (!empty($conferences['records'])) { if (!empty($logs['records'])) {
$widget['full'] = true; $widget['full'] = true;
$widget['table_headers'] = array_keys($logs['records'][0]); $widget['table_headers'] = array_keys($logs['records'][0]);
$widget['table_records'] = $logs['records']; $widget['table_records'] = $logs['records'];

View File

@ -3,7 +3,7 @@
// registration is allowed, go on // registration is allowed, go on
if ($config['registration_enabled'] === true) { if ($config['registration_enabled'] === true) {
require '../app/classes/user.php'; // require '../app/classes/user.php';
unset($error); unset($error);
try { try {
@ -11,7 +11,7 @@ if ($config['registration_enabled'] === true) {
// connect to database // connect to database
$dbWeb = connectDB($config); $dbWeb = connectDB($config);
$userObject = new User($dbWeb); // $userObject = new User($dbWeb);
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) { if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
$username = $_POST['username']; $username = $_POST['username'];

View File

@ -24,7 +24,7 @@
<thead class="thead-dark"> <thead class="thead-dark">
<tr> <tr>
<?php foreach ($widget['table_headers'] as $header) { ?> <?php foreach ($widget['table_headers'] as $header) { ?>
<th scope="col"><?= htmlspecialchars($header) ?></th> <th scope="col" class="th-<?= htmlspecialchars($header) ?>"><?= htmlspecialchars($header) ?></th>
<?php } ?> <?php } ?>
</tr> </tr>
</thead> </thead>

View File

@ -123,15 +123,23 @@ $firstPlatform = $platformsAll[0]['id'];
$platform_id = $_REQUEST['platform'] ?? $firstPlatform; $platform_id = $_REQUEST['platform'] ?? $firstPlatform;
$platformDetails = $platformObject->getPlatformDetails($platform_id); $platformDetails = $platformObject->getPlatformDetails($platform_id);
// init user functions
require '../app/classes/user.php';
include '../app/helpers/profile.php';
$userObject = new User($dbWeb);
// logout is a special case, as we can't use session vars for notices // logout is a special case, as we can't use session vars for notices
if ($page == 'logout') { if ($page == 'logout') {
$notice = "You were logged out.<br />You can log in again.";
$user_id = $userObject->getUserId($currentUser)[0]['id'];
$logObject->insertLog($user_id, "Logout: User \"$currentUser\" logged out.", 'user');
// clean up session // clean up session
session_unset(); session_unset();
session_destroy(); session_destroy();
setcookie('username', "", time() - 100, $config['folder'], $config['domain'], isset($_SERVER['HTTPS']), true); setcookie('username', "", time() - 100, $config['folder'], $config['domain'], isset($_SERVER['HTTPS']), true);
$notice = "You were logged out.<br />You can log in again.";
include '../app/templates/page-header.php'; include '../app/templates/page-header.php';
include '../app/templates/page-menu.php'; include '../app/templates/page-menu.php';
include '../app/templates/block-message.php'; include '../app/templates/block-message.php';
@ -141,9 +149,6 @@ if ($page == 'logout') {
// if user is logged in, we need user details and rights // if user is logged in, we need user details and rights
if (isset($currentUser)) { if (isset($currentUser)) {
require '../app/classes/user.php';
include '../app/helpers/profile.php';
$userObject = new User($dbWeb);
$user_id = $userObject->getUserId($currentUser)[0]['id']; $user_id = $userObject->getUserId($currentUser)[0]['id'];
$userDetails = $userObject->getUserDetails($user_id); $userDetails = $userObject->getUserDetails($user_id);
$userRights = $userObject->getUserRights($user_id); $userRights = $userObject->getUserRights($user_id);

View File

@ -240,3 +240,7 @@
margin-left: 5px; margin-left: 5px;
margin-right: 5px; margin-right: 5px;
} }
.th-time {
width: 200px;
}