v1.0.0.1
StocketBase
- <?php
-
- declare(strict_types=1);
-
- require_once __DIR__ . '/includes/config.php';
- require_once __DIR__ . '/includes/database.php';
- require_once __DIR__ . '/includes/auth.php';
- require_once __DIR__ . '/includes/csrf.php';
- require_once __DIR__ . '/includes/activity-log.php';
-
- Auth::boot();
- Auth::requireRoleAtLeast(Auth::ROLE_STORE_OWNER);
-
- $currentUser = Auth::user();
- $currentRole = Auth::role();
- $db = Database::site();
- $logEnabled = ActivityLog::isEnabled();
-
- $flashMessage = null;
- $flashType = 'success';
-
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'clear_activity_log') {
- if (!Csrf::verify($_POST['csrf_token'] ?? null)) {
- $flashMessage = 'Your session expired. Please try again.';
- $flashType = 'error';
- } else {
- $days = max(0, (int) ($_POST['older_than_days'] ?? 0));
- if ($days > 0) {
- $statement = $db->prepare('DELETE FROM activity_log WHERE created_at < DATE_SUB(NOW(), INTERVAL :days DAY)');
- $statement->bindValue('days', $days, PDO::PARAM_INT);
- $statement->execute();
- } else {
- $db->exec('DELETE FROM activity_log');
- }
- $flashMessage = 'Done, those entries are gone.';
- }
- }
-
- $perPage = 50;
- $page = max(1, (int) ($_GET['page'] ?? 1));
- $total = (int) $db->query('SELECT COUNT(*) FROM activity_log')->fetchColumn();
- $totalPages = max(1, (int) ceil($total / $perPage));
- $page = min($page, $totalPages);
-
- $statement = $db->prepare('SELECT * FROM activity_log ORDER BY id DESC LIMIT :limit OFFSET :offset');
- $statement->bindValue('limit', $perPage, PDO::PARAM_INT);
- $statement->bindValue('offset', ($page - 1) * $perPage, PDO::PARAM_INT);
- $statement->execute();
- $entries = $statement->fetchAll();
-
- $dashActivePage = 'activity';
- $dashPageTitle = 'Activity log';
-
- require __DIR__ . '/includes/dash-header.php';
-
- ?>
-
- <?php if ($flashMessage !== null): ?>
- <div class="dash-flash dash-flash-<?= htmlspecialchars($flashType) ?>"><?= Icons::icon($flashType === 'error' ? 'x' : 'check', 'icon icon-sm') ?><?= htmlspecialchars($flashMessage) ?></div>
- <?php endif; ?>
-
- <h1 class="dash-title"><?= Icons::icon('stats', 'icon icon-lg') ?>Activity log</h1>
-
- <?php if (!$logEnabled): ?>
- <div class="dash-flash dash-flash-error"><?= Icons::icon('x', 'icon icon-sm') ?>The activity log is off, so nothing new is being recorded. You can turn it on in Settings, General.</div>
- <?php endif; ?>
-
- <table class="dash-table">
- <thead><tr><th>When</th><th>Who</th><th>What they did</th><th>On</th><th>Details</th><th>IP address</th></tr></thead>
- <tbody>
- <?php foreach ($entries as $entry): ?>
- <tr>
- <td><?= htmlspecialchars((string) $entry['created_at']) ?></td>
- <td><?= htmlspecialchars((string) $entry['user_name']) ?></td>
- <td><?= htmlspecialchars((string) $entry['action']) ?></td>
- <td><?= htmlspecialchars(trim((string) $entry['entity_type'] . ' ' . ($entry['entity_id'] !== null ? '#' . $entry['entity_id'] : ''))) ?></td>
- <td><?= htmlspecialchars((string) $entry['details']) ?></td>
- <td><?= htmlspecialchars((string) $entry['ip_address']) ?></td>
- </tr>
- <?php endforeach; ?>
- <?php if (empty($entries)): ?>
- <tr><td colspan="6">Nothing recorded yet.</td></tr>
- <?php endif; ?>
- </tbody>
- </table>
-
- <?php if ($totalPages > 1): ?>
- <p style="margin-top:16px;display:flex;gap:12px;align-items:center;">
- <?php if ($page > 1): ?><a href="activity.php?page=<?= $page - 1 ?>" class="dash-btn-small">← Newer</a><?php endif; ?>
- <span>Page <?= $page ?> of <?= $totalPages ?></span>
- <?php if ($page < $totalPages): ?><a href="activity.php?page=<?= $page + 1 ?>" class="dash-btn-small">Older →</a><?php endif; ?>
- </p>
- <?php endif; ?>
-
- <form method="post" style="margin-top:24px;" onsubmit="return confirm('Delete these entries? You can\'t get them back.');">
- <?= Csrf::field() ?>
- <input type="hidden" name="action" value="clear_activity_log">
- <label>Clean up the log</label>
- <select name="older_than_days">
- <option value="30">Older than 30 days</option>
- <option value="90">Older than 90 days</option>
- <option value="365">Older than 1 year</option>
- <option value="0">Everything</option>
- </select>
- <button type="submit" class="dash-btn dash-btn-danger" style="margin-top:12px;"><?= Icons::icon('trash', 'icon icon-sm') ?>Clear</button>
- </form>
-
- <?php require __DIR__ . '/includes/dash-footer.php'; ?>
-