2024-09-16 14:09:37 +00:00
|
|
|
<?php
|
|
|
|
|
2024-11-22 13:33:19 +00:00
|
|
|
/**
|
2024-11-29 16:38:49 +00:00
|
|
|
* class Log
|
2024-11-25 14:09:47 +00:00
|
|
|
*
|
2024-11-22 13:33:19 +00:00
|
|
|
* Handles logging events into a database and reading log entries.
|
|
|
|
*/
|
2024-09-16 14:09:37 +00:00
|
|
|
class Log {
|
2024-11-22 13:33:19 +00:00
|
|
|
/**
|
2024-11-25 14:09:47 +00:00
|
|
|
* @var PDO|null $db The database connection instance.
|
2024-11-22 13:33:19 +00:00
|
|
|
*/
|
2024-09-16 14:09:37 +00:00
|
|
|
private $db;
|
|
|
|
|
2024-11-22 13:33:19 +00:00
|
|
|
/**
|
2024-11-29 16:47:18 +00:00
|
|
|
* Logs constructor.
|
|
|
|
* Initializes the database connection.
|
2024-11-22 13:33:19 +00:00
|
|
|
*
|
2024-11-29 16:47:18 +00:00
|
|
|
* @param object $database The database object to initialize the connection.
|
2024-11-22 13:33:19 +00:00
|
|
|
*/
|
2024-09-16 14:09:37 +00:00
|
|
|
public function __construct($database) {
|
|
|
|
$this->db = $database->getConnection();
|
|
|
|
}
|
|
|
|
|
2024-11-22 13:33:19 +00:00
|
|
|
/**
|
|
|
|
* Insert a log event into the database.
|
|
|
|
*
|
|
|
|
* @param int $user_id The ID of the user associated with the log event.
|
|
|
|
* @param string $message The log message to insert.
|
|
|
|
* @param string $scope The scope of the log event (e.g., 'user', 'system'). Default is 'user'.
|
|
|
|
*
|
|
|
|
* @return bool|string True on success, or an error message on failure.
|
|
|
|
*/
|
2024-09-16 14:09:37 +00:00
|
|
|
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([
|
2025-01-18 11:17:32 +00:00
|
|
|
':user_id' => $user_id,
|
|
|
|
':scope' => $scope,
|
|
|
|
':message' => $message,
|
2024-09-16 14:09:37 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-22 13:33:19 +00:00
|
|
|
/**
|
|
|
|
* Retrieve log entries from the database.
|
|
|
|
*
|
|
|
|
* @param int $user_id The ID of the user whose logs are being retrieved.
|
|
|
|
* @param string $scope The scope of the logs ('user' or 'system').
|
|
|
|
* @param int $offset The offset for pagination. Default is 0.
|
|
|
|
* @param int $items_per_page The number of log entries to retrieve per page. Default is no limit.
|
2025-01-18 11:17:32 +00:00
|
|
|
* @param array $filters Optional array of filters (from_time, until_time, message, id)
|
2024-11-22 13:33:19 +00:00
|
|
|
*
|
|
|
|
* @return array An array of log entries.
|
|
|
|
*/
|
2025-01-18 11:17:32 +00:00
|
|
|
public function readLog($user_id, $scope, $offset=0, $items_per_page='', $filters=[]) {
|
|
|
|
$params = [];
|
|
|
|
$where_clauses = [];
|
|
|
|
|
|
|
|
// Base query with user join
|
|
|
|
$base_sql = 'SELECT l.*, u.username
|
|
|
|
FROM logs l
|
|
|
|
LEFT JOIN users u ON l.user_id = u.id';
|
|
|
|
|
|
|
|
// Add scope condition
|
2024-09-16 14:09:37 +00:00
|
|
|
if ($scope === 'user') {
|
2025-01-18 11:17:32 +00:00
|
|
|
$where_clauses[] = 'l.user_id = :user_id';
|
|
|
|
$params[':user_id'] = $user_id;
|
|
|
|
}
|
2024-09-16 16:08:03 +00:00
|
|
|
|
2025-01-18 11:17:32 +00:00
|
|
|
// Add time range filters if specified
|
|
|
|
if (!empty($filters['from_time'])) {
|
|
|
|
$where_clauses[] = 'l.time >= :from_time';
|
|
|
|
$params[':from_time'] = $filters['from_time'] . ' 00:00:00';
|
|
|
|
}
|
|
|
|
if (!empty($filters['until_time'])) {
|
|
|
|
$where_clauses[] = 'l.time <= :until_time';
|
|
|
|
$params[':until_time'] = $filters['until_time'] . ' 23:59:59';
|
2024-09-16 14:09:37 +00:00
|
|
|
}
|
2024-09-16 16:08:03 +00:00
|
|
|
|
2025-01-18 11:17:32 +00:00
|
|
|
// Add message search if specified
|
|
|
|
if (!empty($filters['message'])) {
|
|
|
|
$where_clauses[] = 'l.message LIKE :message';
|
|
|
|
$params[':message'] = '%' . $filters['message'] . '%';
|
2024-09-16 14:09:37 +00:00
|
|
|
}
|
|
|
|
|
2025-01-18 11:17:32 +00:00
|
|
|
// Add user ID search if specified
|
|
|
|
if (!empty($filters['id'])) {
|
|
|
|
$where_clauses[] = 'l.user_id = :search_user_id';
|
|
|
|
$params[':search_user_id'] = $filters['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Combine WHERE clauses
|
|
|
|
$sql = $base_sql;
|
|
|
|
if (!empty($where_clauses)) {
|
|
|
|
$sql .= ' WHERE ' . implode(' AND ', $where_clauses);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add ordering
|
|
|
|
$sql .= ' ORDER BY l.time DESC';
|
|
|
|
|
|
|
|
// Add pagination
|
|
|
|
if ($items_per_page) {
|
|
|
|
$items_per_page = (int)$items_per_page;
|
|
|
|
$sql .= ' LIMIT ' . $offset . ',' . $items_per_page;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->db->prepare($sql);
|
|
|
|
$query->execute($params);
|
|
|
|
|
2024-09-16 14:09:37 +00:00
|
|
|
return $query->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|