v1.0.0.0
StocketBase
- <?php
-
- declare(strict_types=1);
-
- final class ActivityLog
- {
- public static function isEnabled(): bool
- {
- try {
- $statement = Database::site()->prepare('SELECT setting_value FROM site_settings WHERE setting_key = :key LIMIT 1');
- $statement->execute(['key' => 'activity_log_enabled']);
-
- return $statement->fetchColumn() === '1';
- } catch (PDOException $e) {
- return false;
- }
- }
-
- public static function record(string $action, ?string $entityType = null, ?int $entityId = null, ?string $details = null, bool $asSystem = false): void
- {
- if (!self::isEnabled()) {
- return;
- }
-
- $user = !$asSystem && class_exists('Auth', false) ? Auth::user() : null;
-
- try {
- $statement = Database::site()->prepare(
- 'INSERT INTO activity_log (user_id, user_name, action, entity_type, entity_id, details, ip_address)
- VALUES (:user_id, :user_name, :action, :entity_type, :entity_id, :details, :ip)'
- );
- $statement->execute([
- 'user_id' => $user['id'] ?? null,
- 'user_name' => mb_substr((string) ($user['display_name'] ?? 'System'), 0, 120),
- 'action' => mb_substr($action, 0, 60),
- 'entity_type' => $entityType,
- 'entity_id' => $entityId,
- 'details' => $details !== null ? mb_substr($details, 0, 500) : null,
- 'ip' => mb_substr((string) ($_SERVER['REMOTE_ADDR'] ?? ''), 0, 45),
- ]);
- } catch (PDOException $e) {
- error_log('StocketBase: could not write activity log: ' . $e->getMessage());
- }
- }
- }
-