Compare commits
2 Commits
1e975f7b18
...
81b4187ae8
Author | SHA1 | Date |
---|---|---|
|
81b4187ae8 | |
|
bbccb54059 |
|
@ -94,11 +94,11 @@ class Component {
|
|||
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!empty($result)) {
|
||||
$logObject->insertLog(0, "Retrieved " . count($result) . " Jitsi component events");
|
||||
$logObject->log('info', "Retrieved " . count($result) . " Jitsi component events", ['user_id' => $userId, 'scope' => 'system']);
|
||||
}
|
||||
return $result;
|
||||
} catch (PDOException $e) {
|
||||
$logObject->insertLog(0, "Failed to retrieve Jitsi component events: " . $e->getMessage());
|
||||
$logObject->log('error', "Failed to retrieve Jitsi component events: " . $e->getMessage(), ['user_id' => $userId, 'scope' => 'system']);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
@ -162,7 +162,7 @@ class Component {
|
|||
$result = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
return (int)$result['total'];
|
||||
} catch (PDOException $e) {
|
||||
$logObject->insertLog(0, "Failed to retrieve component events count: " . $e->getMessage());
|
||||
$logObject->log('error', "Failed to retrieve component events count: " . $e->getMessage(), ['user_id' => $userId, 'scope' => 'system']);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -140,7 +140,7 @@ class Config {
|
|||
}
|
||||
|
||||
if (!empty($allLogs)) {
|
||||
$logObject->insertLog($userId, implode("\n", $allLogs), 'system');
|
||||
$logObject->log('info', implode("\n", $allLogs), ['user_id' => $userId, 'scope' => 'system']);
|
||||
}
|
||||
|
||||
return [
|
||||
|
@ -148,7 +148,7 @@ class Config {
|
|||
'updated' => $updated
|
||||
];
|
||||
} catch (Exception $e) {
|
||||
$logObject->insertLog($userId, "Config update error: " . $e->getMessage(), 'system');
|
||||
$logObject->log('error', "Config update error: " . $e->getMessage(), ['user_id' => $userId, 'scope' => 'system']);
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => $e->getMessage()
|
||||
|
|
|
@ -29,14 +29,12 @@ class Log {
|
|||
}
|
||||
|
||||
/**
|
||||
* Delegate insertLog to underlying logger
|
||||
*
|
||||
* @param mixed $userId
|
||||
* PSR-3 compatible log method
|
||||
* @param string $level
|
||||
* @param string $message
|
||||
* @param string|null $scope
|
||||
* @return mixed True on success or error message
|
||||
* @param array $context
|
||||
*/
|
||||
public function insertLog($userId, string $message, ?string $scope = null) {
|
||||
return $this->logger->insertLog($userId, $message, $scope);
|
||||
public function log(string $level, string $message, array $context = []): void {
|
||||
$this->logger->log($level, $message, $context);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -232,7 +232,7 @@ class RateLimiter {
|
|||
if ($this->isIpBlacklisted($ip)) {
|
||||
$message = "Cannot whitelist {$ip} - IP is currently blacklisted";
|
||||
if ($userId) {
|
||||
$this->logger->info("IP Whitelist: {$message}", ['user_id' => $userId]);
|
||||
$this->logger->log('info', "IP Whitelist: {$message}", ['user_id' => $userId, 'scope' => 'system']);
|
||||
Feedback::flash('ERROR', 'DEFAULT', $message);
|
||||
}
|
||||
return false;
|
||||
|
@ -256,14 +256,14 @@ class RateLimiter {
|
|||
$createdBy,
|
||||
$description
|
||||
);
|
||||
$this->logger->info($logMessage, ['user_id' => $userId ?? null]);
|
||||
$this->logger->log('info', $logMessage, ['user_id' => $userId ?? null, 'scope' => 'system']);
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
||||
} catch (Exception $e) {
|
||||
if ($userId) {
|
||||
$this->logger->error("IP Whitelist: Failed to add {$ip}: " . $e->getMessage(), ['user_id' => $userId]);
|
||||
$this->logger->log('error', "IP Whitelist: Failed to add {$ip}: " . $e->getMessage(), ['user_id' => $userId, 'scope' => 'system']);
|
||||
Feedback::flash('ERROR', 'DEFAULT', "IP Whitelist: Failed to add {$ip}: " . $e->getMessage());
|
||||
}
|
||||
return false;
|
||||
|
@ -291,14 +291,14 @@ class RateLimiter {
|
|||
$removedBy,
|
||||
$ipDetails['created_by']
|
||||
);
|
||||
$this->logger->info($logMessage, ['user_id' => $userId ?? null]);
|
||||
$this->logger->log('info', $logMessage, ['user_id' => $userId ?? null, 'scope' => 'system']);
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
||||
} catch (Exception $e) {
|
||||
if ($userId) {
|
||||
$this->logger->error("IP Whitelist: Failed to remove {$ip}: " . $e->getMessage(), ['user_id' => $userId]);
|
||||
$this->logger->log('error', "IP Whitelist: Failed to remove {$ip}: " . $e->getMessage(), ['user_id' => $userId, 'scope' => 'system']);
|
||||
Feedback::flash('ERROR', 'DEFAULT', "IP Whitelist: Failed to remove {$ip}: " . $e->getMessage());
|
||||
}
|
||||
return false;
|
||||
|
@ -311,7 +311,7 @@ class RateLimiter {
|
|||
if ($this->isIpWhitelisted($ip)) {
|
||||
$message = "Cannot blacklist {$ip} - IP is currently whitelisted";
|
||||
if ($userId) {
|
||||
$this->logger->info("IP Blacklist: {$message}", ['user_id' => $userId]);
|
||||
$this->logger->log('info', "IP Blacklist: {$message}", ['user_id' => $userId, 'scope' => 'system']);
|
||||
Feedback::flash('ERROR', 'DEFAULT', $message);
|
||||
}
|
||||
return false;
|
||||
|
@ -339,13 +339,13 @@ class RateLimiter {
|
|||
$reason,
|
||||
$expiryTime ?? 'never'
|
||||
);
|
||||
$this->logger->info($logMessage, ['user_id' => $userId ?? null]);
|
||||
$this->logger->log('info', $logMessage, ['user_id' => $userId ?? null, 'scope' => 'system']);
|
||||
}
|
||||
|
||||
return $result;
|
||||
} catch (Exception $e) {
|
||||
if ($userId) {
|
||||
$this->logger->error("IP Blacklist: Failed to add {$ip}: " . $e->getMessage(), ['user_id' => $userId]);
|
||||
$this->logger->log('error', "IP Blacklist: Failed to add {$ip}: " . $e->getMessage(), ['user_id' => $userId, 'scope' => 'system']);
|
||||
Feedback::flash('ERROR', 'DEFAULT', "IP Blacklist: Failed to add {$ip}: " . $e->getMessage());
|
||||
}
|
||||
return false;
|
||||
|
@ -373,13 +373,13 @@ class RateLimiter {
|
|||
$ipDetails['created_by'],
|
||||
$ipDetails['reason']
|
||||
);
|
||||
$this->logger->info($logMessage, ['user_id' => $userId ?? null]);
|
||||
$this->logger->log('info', $logMessage, ['user_id' => $userId ?? null, 'scope' => 'system']);
|
||||
}
|
||||
|
||||
return $result;
|
||||
} catch (Exception $e) {
|
||||
if ($userId) {
|
||||
$this->logger->error("IP Blacklist: Failed to remove {$ip}: " . $e->getMessage(), ['user_id' => $userId]);
|
||||
$this->logger->log('error', "IP Blacklist: Failed to remove {$ip}: " . $e->getMessage(), ['user_id' => $userId, 'scope' => 'system']);
|
||||
Feedback::flash('ERROR', 'DEFAULT', "IP Blacklist: Failed to remove {$ip}: " . $e->getMessage());
|
||||
}
|
||||
return false;
|
||||
|
@ -414,7 +414,7 @@ class RateLimiter {
|
|||
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
$this->logger->error("Failed to cleanup expired entries: " . $e->getMessage());
|
||||
$this->logger->log('error', "Failed to cleanup expired entries: " . $e->getMessage(), ['user_id' => $userId ?? null, 'scope' => 'system']);
|
||||
Feedback::flash('ERROR', 'DEFAULT', "Failed to cleanup expired entries: " . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -8,17 +8,7 @@ namespace App\Core;
|
|||
class NullLogger
|
||||
{
|
||||
/**
|
||||
* No-op insertLog.
|
||||
*
|
||||
* @param mixed $userId
|
||||
* @param string $message
|
||||
* @param string|null $type
|
||||
* @return void
|
||||
*/
|
||||
public function insertLog($userId, string $message, ?string $type = null): void {}
|
||||
|
||||
/**
|
||||
* PSR-3 log stub.
|
||||
* PSR-3 compatible log stub.
|
||||
* @param string $level
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
* Returns a logger instance: plugin Log if available, otherwise NullLogger.
|
||||
*
|
||||
* @param mixed $database Database or DatabaseConnector instance.
|
||||
* @return mixed Logger instance with insertLog() method.
|
||||
* @return mixed Logger instance with PSR-3 log() compatible method.
|
||||
*/
|
||||
function getLoggerInstance($database) {
|
||||
if (class_exists('Log')) {
|
||||
|
|
|
@ -40,7 +40,7 @@ function applyCsrfMiddleware() {
|
|||
$_GET['page'] ?? 'unknown',
|
||||
$_SESSION['username'] ?? 'anonymous'
|
||||
);
|
||||
$logObject->insertLog(null, $logMessage, 'system');
|
||||
$logObject->log('error', $logMessage, ['user_id' => null, 'scope' => 'system']);
|
||||
|
||||
// Return error message
|
||||
http_response_code(403);
|
||||
|
|
|
@ -21,36 +21,6 @@ class Log {
|
|||
$this->db = $database->getConnection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a log event into the database.
|
||||
*
|
||||
* @param int $userId 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.
|
||||
*/
|
||||
public function insertLog($userId, $message, $scope = 'user') {
|
||||
try {
|
||||
$sql = 'INSERT INTO log
|
||||
(user_id, scope, message)
|
||||
VALUES
|
||||
(:user_id, :scope, :message)';
|
||||
|
||||
$query = $this->db->prepare($sql);
|
||||
$query->execute([
|
||||
':user_id' => $userId,
|
||||
':scope' => $scope,
|
||||
':message' => $message,
|
||||
]);
|
||||
|
||||
return true;
|
||||
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve log entries from the database.
|
||||
*
|
||||
|
@ -67,8 +37,8 @@ class Log {
|
|||
$where_clauses = [];
|
||||
|
||||
// Base query with user join
|
||||
$base_sql = 'SELECT l.*, u.username
|
||||
FROM log l
|
||||
$base_sql = 'SELECT l.*, u.username
|
||||
FROM log l
|
||||
LEFT JOIN user u ON l.user_id = u.id';
|
||||
|
||||
// Add scope condition
|
||||
|
@ -120,10 +90,29 @@ class Log {
|
|||
return $query->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
// PSR-3 style log method
|
||||
/**
|
||||
* PSR-3 style log method - inserts a log event into the database.
|
||||
*
|
||||
* @param string $level The log level (emergency, alert, critical, error, warning, notice, info, debug).
|
||||
* @param string $message The log message to insert.
|
||||
* @param string $scope The scope of the log event (e.g., 'user', 'system'). Default is 'system'.
|
||||
*/
|
||||
public function log(string $level, string $message, array $context = []): void {
|
||||
$userId = $context['user_id'] ?? null;
|
||||
$scope = $context['scope'] ?? 'system';
|
||||
$this->insertLog($userId, "[$level] " . $message, $scope);
|
||||
try {
|
||||
$sql = 'INSERT INTO log
|
||||
(user_id, scope, message)
|
||||
VALUES
|
||||
(:user_id, :scope, :message)';
|
||||
$query = $this->db->prepare($sql);
|
||||
$query->execute([
|
||||
':user_id' => $userId,
|
||||
':scope' => $scope,
|
||||
':message' => "[$level] " . $message,
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
// swallowing exceptions or here we could log to error log for testing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -204,7 +204,7 @@ if ($page == 'logout') {
|
|||
setcookie('username', "", time() - 100, $config['folder'], $config['domain'], isset($_SERVER['HTTPS']), true);
|
||||
|
||||
// Log successful logout
|
||||
$logObject->insertLog($userId, "Logout: User \"$currentUser\" logged out. IP: $user_IP", 'user');
|
||||
$logObject->log('info', "Logout: User \"$currentUser\" logged out. IP: $user_IP", ['user_id' => $userId, 'scope' => 'user']);
|
||||
|
||||
// Set success message
|
||||
Feedback::flash('LOGIN', 'LOGOUT_SUCCESS');
|
||||
|
|
Loading…
Reference in New Issue