diff --git a/app/classes/log.php b/app/classes/log.php new file mode 100644 index 0000000..63a5ce0 --- /dev/null +++ b/app/classes/log.php @@ -0,0 +1,52 @@ +db = $database->getConnection(); + } + + // insert log event + public function insertLog($user_id, $message, $scope='user') { + try { + $sql = 'INSERT INTO logs + (user_id, scope, message) + VALUES + (:user_id, :scope, :message)'; + + $query = $this->db->prepare($sql); + $query->execute([ + ':user_id' => $user_id, + ':scope' => $scope, + ':message' => $message, + ]); + + return true; + + } catch (Exception $e) { + return $e->getMessage(); + } + } + + // read logs + public function readLog($user_id, $scope='user') { + $sql = 'SELECT * FROM logs'; + if ($scope === 'user') { + $sql .= ' WHERE user_id = :user_id'; + $query = $this->db->prepare($sql); + $query->execute([ + ':user_id' => $user_id, + ]); + } + if ($scope === 'system') { + $query = $this->db->prepare($sql); + $query->execute(); + } + + return $query->fetchAll(PDO::FETCH_ASSOC); + } + +} + +?> diff --git a/app/pages/login.php b/app/pages/login.php index 2694cbb..9c64b89 100644 --- a/app/pages/login.php +++ b/app/pages/login.php @@ -43,12 +43,16 @@ try { // redirect to index $_SESSION['notice'] = "Login successful"; + $user_id = $userObject->getUserId($username)[0]['id']; + $logObject->insertLog($user_id, "User \"$username\" logged in.", 'user'); header('Location: index.php'); exit(); // login failed } else { $_SESSION['error'] = "Login failed."; + $user_id = $userObject->getUserId($username)[0]['id']; + $logObject->insertLog($user_id, "Failed login attempt for user \"$username\".", 'user'); header('Location: index.php'); exit(); } diff --git a/app/pages/logs.php b/app/pages/logs.php new file mode 100644 index 0000000..a853292 --- /dev/null +++ b/app/pages/logs.php @@ -0,0 +1,48 @@ +readLog($user_id, 'user'); + +if (!empty($search)) { + $logs = array(); + $logs['records'] = array(); + + foreach ($search as $item) { + extract($item); + + $log_record = array( + // assign title to the field in the array record + 'user ID' => $user_id, + 'time' => $time, + 'log message' => $message + ); + // populate the result array + array_push($logs['records'], $log_record); + } +} + +// prepare the widget +$widget['full'] = false; +$widget['collapsible'] = false; +$widget['name'] = 'Logs'; +$username = $userObject->getUserDetails($user_id)[0]['username']; +$widget['title'] = "Log events for user \"$username\""; +$widget['filter'] = true; +if (!empty($conferences['records'])) { + $widget['full'] = true; + $widget['table_headers'] = array_keys($logs['records'][0]); + $widget['table_records'] = $logs['records']; +} +$widget['pagination'] = true; + +// display the widget +include '../app/templates/logs-list.php'; + +?> diff --git a/app/templates/logs-filter.php b/app/templates/logs-filter.php new file mode 100644 index 0000000..6ccf7c9 --- /dev/null +++ b/app/templates/logs-filter.php @@ -0,0 +1,26 @@ + + +
time period: = $from_time ?> - = $until_time ?>
+ += htmlspecialchars($header) ?> | + +|
---|---|
= htmlspecialchars($column ?? '') ?> | + += htmlspecialchars($column ?? '') ?> | + +
No matching records found.
+ +