WebOrbiton
v1.0.0.0

StocketBase

109 lines · 4.2 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. require_once __DIR__ . '/includes/config.php';
  6. require_once __DIR__ . '/includes/database.php';
  7. require_once __DIR__ . '/includes/auth.php';
  8. require_once __DIR__ . '/includes/csrf.php';
  9. require_once __DIR__ . '/includes/activity-log.php';
  10. ​
  11. Auth::boot();
  12. Auth::requireRoleAtLeast(Auth::ROLE_STORE_OWNER);
  13. ​
  14. $currentUser = Auth::user();
  15. $currentRole = Auth::role();
  16. $db = Database::site();
  17. $logEnabled = ActivityLog::isEnabled();
  18. ​
  19. $flashMessage = null;
  20. $flashType = 'success';
  21. ​
  22. if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'clear_activity_log') {
  23. if (!Csrf::verify($_POST['csrf_token'] ?? null)) {
  24. $flashMessage = 'Security check failed, please try again.';
  25. $flashType = 'error';
  26. } else {
  27. $days = max(0, (int) ($_POST['older_than_days'] ?? 0));
  28. if ($days > 0) {
  29. $statement = $db->prepare('DELETE FROM activity_log WHERE created_at < DATE_SUB(NOW(), INTERVAL :days DAY)');
  30. $statement->bindValue('days', $days, PDO::PARAM_INT);
  31. $statement->execute();
  32. } else {
  33. $db->exec('DELETE FROM activity_log');
  34. }
  35. $flashMessage = 'Activity log cleared.';
  36. }
  37. }
  38. ​
  39. $perPage = 50;
  40. $page = max(1, (int) ($_GET['page'] ?? 1));
  41. $total = (int) $db->query('SELECT COUNT(*) FROM activity_log')->fetchColumn();
  42. $totalPages = max(1, (int) ceil($total / $perPage));
  43. $page = min($page, $totalPages);
  44. ​
  45. $statement = $db->prepare('SELECT * FROM activity_log ORDER BY id DESC LIMIT :limit OFFSET :offset');
  46. $statement->bindValue('limit', $perPage, PDO::PARAM_INT);
  47. $statement->bindValue('offset', ($page - 1) * $perPage, PDO::PARAM_INT);
  48. $statement->execute();
  49. $entries = $statement->fetchAll();
  50. ​
  51. $dashActivePage = 'activity';
  52. $dashPageTitle = 'Activity log';
  53. ​
  54. require __DIR__ . '/includes/dash-header.php';
  55. ​
  56. ?>
  57. ​
  58. <?php if ($flashMessage !== null): ?>
  59. <div class="dash-flash dash-flash-<?= htmlspecialchars($flashType) ?>"><?= Icons::icon($flashType === 'error' ? 'x' : 'check', 'icon icon-sm') ?><?= htmlspecialchars($flashMessage) ?></div>
  60. <?php endif; ?>
  61. ​
  62. <h1 class="dash-title"><?= Icons::icon('stats', 'icon icon-lg') ?>Activity log</h1>
  63. ​
  64. <?php if (!$logEnabled): ?>
  65. <div class="dash-flash dash-flash-error"><?= Icons::icon('x', 'icon icon-sm') ?>Activity log is turned off, so new actions are not being recorded. Enable it in Settings → General.</div>
  66. <?php endif; ?>
  67. ​
  68. <table class="dash-table">
  69. <thead><tr><th>When</th><th>User</th><th>Action</th><th>Item</th><th>Details</th><th>IP</th></tr></thead>
  70. <tbody>
  71. <?php foreach ($entries as $entry): ?>
  72. <tr>
  73. <td><?= htmlspecialchars((string) $entry['created_at']) ?></td>
  74. <td><?= htmlspecialchars((string) $entry['user_name']) ?></td>
  75. <td><?= htmlspecialchars((string) $entry['action']) ?></td>
  76. <td><?= htmlspecialchars(trim((string) $entry['entity_type'] . ' ' . ($entry['entity_id'] !== null ? '#' . $entry['entity_id'] : ''))) ?></td>
  77. <td><?= htmlspecialchars((string) $entry['details']) ?></td>
  78. <td><?= htmlspecialchars((string) $entry['ip_address']) ?></td>
  79. </tr>
  80. <?php endforeach; ?>
  81. <?php if (empty($entries)): ?>
  82. <tr><td colspan="6">No activity recorded yet.</td></tr>
  83. <?php endif; ?>
  84. </tbody>
  85. </table>
  86. ​
  87. <?php if ($totalPages > 1): ?>
  88. <p style="margin-top:16px;display:flex;gap:12px;align-items:center;">
  89. <?php if ($page > 1): ?><a href="activity.php?page=<?= $page - 1 ?>" class="dash-btn-small">&larr; Newer</a><?php endif; ?>
  90. <span>Page <?= $page ?> of <?= $totalPages ?></span>
  91. <?php if ($page < $totalPages): ?><a href="activity.php?page=<?= $page + 1 ?>" class="dash-btn-small">Older &rarr;</a><?php endif; ?>
  92. </p>
  93. <?php endif; ?>
  94. ​
  95. <form method="post" style="margin-top:24px;" onsubmit="return confirm('Delete these log entries?');">
  96. <?= Csrf::field() ?>
  97. <input type="hidden" name="action" value="clear_activity_log">
  98. <label>Clear entries</label>
  99. <select name="older_than_days">
  100. <option value="30">Older than 30 days</option>
  101. <option value="90">Older than 90 days</option>
  102. <option value="365">Older than 1 year</option>
  103. <option value="0">All entries</option>
  104. </select>
  105. <button type="submit" class="dash-btn dash-btn-danger" style="margin-top:12px;"><?= Icons::icon('trash', 'icon icon-sm') ?>Clear</button>
  106. </form>
  107. ​
  108. <?php require __DIR__ . '/includes/dash-footer.php'; ?>
  109. ​