adminlog.php

main 53 lines · 1.7 KB Raw
Alex Alex Commit Initial commit 01/10/2026 09:20
1<?php
2/**
3 * Admin action log.
4 *
5 * Records mutating actions taken from the admin panel - bans, skill edits,
6 * points, settings changes, plugin lifecycle, etc - into `znote_admin_log`.
7 * Admin modules call acp_log() right after a successful write; the table is
8 * migrated separately (SQL/migrations/2.0.0_admin_log.sql), so acp_log()
9 * silently no-ops on databases that have not run it yet, matching the
10 * fallback pattern used by theme_menu_items() and friends.
11 */
12
13function acp_log_table_exists(): bool {
14 return znote_table_exists('znote_admin_log');
15}
16
17/**
18 * @param string $action Dotted code, e.g. "player.ban" - see acp_log_action_label().
19 * @param string $target What the action was applied to, e.g. a character name.
20 * @param array $details Small set of extra facts (old/new values, reason...),
21 * stored as JSON and shown expanded in the log viewer.
22 */
23function acp_log(string $action, string $target = '', array $details = array()): bool {
24 if (!acp_log_table_exists()) {
25 return false;
26 }
27
28 $adminId = isset($GLOBALS['session_user_id']) ? (int)$GLOBALS['session_user_id'] : 0;
29 $adminName = isset($GLOBALS['user_data']['name']) ? (string)$GLOBALS['user_data']['name'] : '';
30
31 $detailsJson = '';
32 if ($details) {
33 $encoded = json_encode($details, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
34 if ($encoded !== false) {
35 $detailsJson = $encoded;
36 }
37 }
38
39 return db()->execute(
40 "INSERT INTO `znote_admin_log` (`admin_id`, `admin_name`, `action`, `target`, `details`, `ip`, `created`)
41 VALUES (?, ?, ?, ?, ?, ?, ?);",
42 [
43 $adminId,
44 $adminName,
45 substr($action, 0, 64),
46 substr($target, 0, 191),
47 $detailsJson,
48 getIP(),
49 time(),
50 ]
51 );
52}
53
Top