Repository navigation
Expand file tree
/
Copy pathmaint.php
More file actions
365 lines (333 loc) · 16.1 KB
/
Copy pathmaint.php
File metadata and controls
365 lines (333 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
declare(strict_types=1);
/**
* maint.php
*
* DimensioneX Web Administration & Maintenance Page.
* Allows checking/initializing MySQL database, managing saved player profiles,
* and inspecting server diagnostics including lock state and chat.
*/
if (session_status() === PHP_SESSION_NONE && !headers_sent()) {
session_start();
}
require_once __DIR__ . '/autoload.php';
use Dimx\PersistenceManager;
$config = file_exists(__DIR__ . '/config.php') ? require __DIR__ . '/config.php' : [];
$dbConfig = $config['db'] ?? [];
$adminPasswd = (string)($config['adminPasswd'] ?? '');
$pm = new PersistenceManager($dbConfig, __DIR__ . '/data');
$action = $_POST['action'] ?? $_GET['action'] ?? '';
$inputPasswd = (string)($_POST['adminPasswd'] ?? $_GET['adminPasswd'] ?? $_SESSION['adminPasswd'] ?? '');
$isAuthenticated = ($adminPasswd !== '' && hash_equals($adminPasswd, $inputPasswd));
$message = '';
$messageType = 'alert';
if (!empty($action) && (($_SERVER['REQUEST_METHOD'] ?? '') === 'POST' || $action === 'snapshot')) {
if (!$isAuthenticated) {
$message = "Access denied: incorrect or missing admin password. Please enter the valid admin password.";
$messageType = 'err';
} else {
$_SESSION['adminPasswd'] = $inputPasswd;
switch ($action) {
case 'snapshot':
$worldId = trim((string)($_SERVER['DIMX_WORLD_ID'] ?? $_POST['world_id'] ?? $_GET['world_id'] ?? ($config['world_id'] ?? 'main')));
$worldFile = $_SERVER['DIMX_WORLD_FILE'] ?? $_ENV['DIMX_WORLD_FILE'] ?? ($config['world_file'] ?? $config['default_world'] ?? null);
if (empty($worldFile) || !file_exists($worldFile)) {
if (file_exists(__DIR__ . '/world3net.dxw')) {
$worldFile = __DIR__ . '/world3net.dxw';
} elseif (file_exists(__DIR__ . '/../docs/dxw-examples/world3net.dxw')) {
$worldFile = __DIR__ . '/../docs/dxw-examples/world3net.dxw';
}
}
$baseUrl = $config['base_url'] ?? '';
$engine = new \Dimx\GameEngine($pm, $worldId, $worldFile, $baseUrl, $config);
$world = $engine->loadOrSeedWorld();
if (!headers_sent()) {
header('Content-Type: text/html; charset=UTF-8');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?= htmlspecialchars($world->name, ENT_QUOTES, 'UTF-8') ?> - Snapshot</title>
<style>
body {
background: #0f172a;
color: #f8fafc;
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
font-size: 13px;
line-height: 1.5;
margin: 0;
padding: 24px;
}
a { color: #38bdf8; text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { color: #38bdf8; font-size: 18px; margin-top: 24px; border-bottom: 1px solid #334155; padding-bottom: 4px; }
ul { list-style-type: square; padding-left: 20px; }
li { margin: 2px 0; }
b { color: #e2e8f0; }
.snapshot-container { max-width: 960px; margin: 0 auto; }
</style>
</head>
<body>
<?= $world->htmlAdminSnapshot() ?>
</body>
</html>
<?php
if (!defined('DIMX_TESTING')) {
exit;
}
return;
case 'init_db':
if ($pm->isConnected()) {
$res = $pm->initTables();
$message = $res ? "Database tables successfully initialized (all 4 tables)." : "Failed to initialize tables.";
} else {
$message = "Cannot initialize tables: MySQL is not connected.";
$messageType = 'err';
}
break;
case 'clear_file_cache':
$files = array_merge(
glob(__DIR__ . '/data/*.json') ?: [],
glob(__DIR__ . '/data/*.bin') ?: [],
glob(__DIR__ . '/data/*.jsonl') ?: []
);
foreach ($files as $f) { @unlink($f); }
$message = "Local file cache cleared (" . count($files) . " files removed).";
break;
case 'reset_world':
$worldId = trim((string)($_SERVER['DIMX_WORLD_ID'] ?? $_POST['world_id'] ?? $_GET['world_id'] ?? ($config['world_id'] ?? 'main')));
$purgePlayers = !empty($_POST['purge_players']);
// Delete debug.log before world reset
$debugLogFiles = array_unique([
__DIR__ . '/debug.log',
\Dimx\GameEngine::getSystemFolder() . '/debug.log',
]);
foreach ($debugLogFiles as $logFile) {
if (file_exists($logFile)) {
@unlink($logFile);
}
}
$pm->resetWorld($worldId, $purgePlayers);
$message = "The game world has been reset and all active player sessions have been invalidated. It will be re-seeded from server configuration on the next player connection.";
break;
case 'clear_chat':
$worldId = trim((string)($_SERVER['DIMX_WORLD_ID'] ?? $_POST['world_id'] ?? $_GET['world_id'] ?? ($config['world_id'] ?? 'main')));
if ($pm->isConnected()) {
try {
$pdo = $pm->getPdo();
$pdo->prepare("DELETE FROM dimx_messages WHERE world_id = :w")->execute([':w' => $worldId]);
$message = "Chat history cleared for the game world.";
} catch (\Throwable $e) {
$message = "Error: " . $e->getMessage();
$messageType = 'err';
}
} else {
$file = __DIR__ . "/data/chat_{$worldId}.jsonl";
if (file_exists($file)) { @unlink($file); }
$message = "Local chat history cleared.";
}
break;
}
}
}
$diag = $pm->checkConnection();
$savedFiles = array_merge(
glob(__DIR__ . '/data/*.json') ?: [],
glob(__DIR__ . '/data/*.bin') ?: [],
glob(__DIR__ . '/data/*.jsonl') ?: []
);
// Row counts for diagnostics
$worlds = $pm->countRows('dimx_worlds');
$players = $pm->countRows('dimx_players');
$settings = $pm->countRows('dimx_settings');
$messages = $pm->countRows('dimx_messages');
// Active MySQL advisory locks (information_schema)
$activeLocks = [];
if ($pm->isConnected()) {
try {
$pdo = $pm->getPdo();
$stmt = $pdo->query("
SELECT SUBSTRING_INDEX(LOCK_NAME, ':', 1) AS lock_name, LOCK_STATUS
FROM performance_schema.metadata_locks
WHERE LOCK_TYPE = 'USER LEVEL LOCK' AND LOCK_STATUS = 'GRANTED'
");
$activeLocks = $stmt ? $stmt->fetchAll() : [];
} catch (\Throwable $e) {
$activeLocks = [];
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DimensioneY Maintenance Console</title>
<style>
body{background:#0f172a;color:#f8fafc;font-family:ui-sans-serif,system-ui,sans-serif;margin:0;padding:24px}
.maint-card{background:#1e293b;border:1px solid #334155;border-radius:8px;padding:20px;margin-bottom:20px;max-width:900px}
.maint-card h3{margin-top:0;color:#38bdf8}
.btn{background:#2563eb;color:#fff;border:none;padding:8px 16px;border-radius:4px;cursor:pointer;font-weight:500}
.btn:hover{background:#1d4ed8}
.btn-danger{background:#dc2626}.btn-danger:hover{background:#b91c1c}
.status-badge{display:inline-block;padding:4px 10px;border-radius:4px;font-weight:600;font-size:13px}
.badge-ok{background:#065f46;color:#34d399}.badge-warn{background:#854d0e;color:#fde047}
.alert{padding:12px;border-radius:6px;margin-bottom:16px;background:#1e40af;color:#dbeafe}
.alert-err{background:#991b1b;color:#fecaca}
table{width:100%;border-collapse:collapse;font-size:13px}
th,td{padding:6px 10px;text-align:left;border-bottom:1px solid #334155}
th{color:#94a3b8;text-transform:uppercase;font-size:11px;letter-spacing:.05em}
input[type=text]{background:#0f172a;border:1px solid #475569;border-radius:4px;color:#f8fafc;padding:5px 8px;font-size:13px}
.stat-grid{display:grid;grid-template-columns:repeat(4,1fr);gap:12px}
.stat-box{background:#0f172a;border-radius:6px;padding:12px;text-align:center}
.stat-num{font-size:28px;font-weight:700;color:#38bdf8}
.stat-label{font-size:11px;color:#94a3b8;text-transform:uppercase;margin-top:4px}
</style>
</head>
<body>
<div style="max-width:900px;margin:0 auto">
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:24px">
<h1 style="margin:0;font-size:22px">⚙ DimensioneY Maintenance Console</h1>
<a href="multiplayer.php" class="btn" style="text-decoration:none;font-size:13px">▶ Return to Game</a>
</div>
<?php if (!empty($message)): ?>
<div class="alert <?= $messageType === 'err' ? 'alert-err' : '' ?>"><?= htmlspecialchars($message) ?></div>
<?php endif; ?>
<!-- Admin Authentication Card -->
<div class="maint-card" style="border-left:4px solid #38bdf8">
<h3>🔑 Administrator Authentication</h3>
<p style="font-size:13px;color:#94a3b8">Enter the <code>adminPasswd</code> configured in <code>config.php</code> to authorize world reset and maintenance operations.</p>
<div style="display:flex;align-items:center;gap:12px;flex-wrap:wrap">
<label for="adminPasswd" style="font-size:13px;font-weight:600">Admin Password:</label>
<input type="password" id="adminPasswd" name="adminPasswd" value="<?= htmlspecialchars($inputPasswd) ?>" placeholder="Enter admin password…" style="padding:6px 10px;background:#0f172a;border:1px solid #475569;border-radius:4px;color:#fff;font-size:14px;width:240px" oninput="updateAllFormPasswords(this.value)" />
<?php if ($isAuthenticated): ?>
<span class="status-badge badge-ok">✔ Authenticated</span>
<?php else: ?>
<span class="status-badge badge-warn">🔒 Authentication Required</span>
<?php endif; ?>
</div>
</div>
<!-- World management -->
<div class="maint-card">
<h3>🌍 World Management</h3>
<p style="font-size:13px;color:#94a3b8">Reset the game world. On the next player connection, the authoritative state will be re-seeded from the server's configured definition file. All active player sessions will be invalidated (players forced to log in fresh) and player positions will be reset.</p>
<form method="POST" onsubmit="return confirmAction(this, 'Reset the game world? All in-game state will be lost and players will be logged out.')">
<input type="hidden" name="action" value="reset_world" />
<input type="hidden" name="adminPasswd" class="form-admin-passwd" value="<?= htmlspecialchars($inputPasswd) ?>" />
<div style="margin-bottom:10px">
<label style="font-size:13px;color:#cbd5e1;cursor:pointer">
<input type="checkbox" name="purge_players" value="1" /> Also purge all player accounts and credentials
</label>
</div>
<button type="submit" class="btn btn-danger">🔄 Reset Game World</button>
</form>
</div>
<!-- System Snapshot -->
<div class="maint-card">
<h3>📸 System Snapshot</h3>
<p style="font-size:13px;color:#94a3b8">Generate a full runtime text snapshot of the game engine and world (rooms, characters, items, links, events, panels, and properties).</p>
<form method="POST" onsubmit="return syncPasswd(this)">
<input type="hidden" name="action" value="snapshot" />
<input type="hidden" name="adminPasswd" class="form-admin-passwd" value="<?= htmlspecialchars($inputPasswd) ?>" />
<button type="submit" class="btn">📸 Generate System Snapshot</button>
</form>
</div>
<!-- Chat management -->
<div class="maint-card">
<h3>💬 Chat Management</h3>
<p style="font-size:13px;color:#94a3b8">Clear the world chat log history.</p>
<form method="POST" onsubmit="return confirmAction(this, 'Clear all chat messages for the world?')">
<input type="hidden" name="action" value="clear_chat" />
<input type="hidden" name="adminPasswd" class="form-admin-passwd" value="<?= htmlspecialchars($inputPasswd) ?>" />
<button type="submit" class="btn btn-danger">Clear Chat History</button>
</form>
</div>
<!-- Local file cache -->
<div class="maint-card">
<h3>📂 Local File Cache</h3>
<p style="font-size:14px;color:#94a3b8">Found <?= count($savedFiles) ?> files in <code>php/data/</code> (used as fallback when MySQL is unavailable).</p>
<?php if (!empty($savedFiles)): ?>
<ul style="font-size:12px;color:#cbd5e1;font-family:monospace;max-height:180px;overflow-y:auto">
<?php foreach ($savedFiles as $file): ?>
<li><?= htmlspecialchars(basename($file)) ?> (<?= number_format(filesize($file)) ?> B)</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<form method="POST" style="margin-top:10px"
onsubmit="return confirmAction(this, 'Erase all local cache files?')">
<input type="hidden" name="action" value="clear_file_cache" />
<input type="hidden" name="adminPasswd" class="form-admin-passwd" value="<?= htmlspecialchars($inputPasswd) ?>" />
<button type="submit" class="btn btn-danger">Clear Local File Cache</button>
</form>
</div>
<!-- Storage diagnostics -->
<div class="maint-card">
<h3>Storage & Database</h3>
<p>
Status:
<?php if ($diag['connected']): ?>
<span class="status-badge badge-ok">✔ <?= $diag['type'] ?></span>
<?php else: ?>
<span class="status-badge badge-warn">⚠ <?= $diag['type'] ?></span>
<?php endif; ?>
</p>
<p style="font-size:14px;color:#94a3b8"><?= htmlspecialchars($diag['message']) ?></p>
<?php if ($diag['connected']): ?>
<!-- Row count stats -->
<div class="stat-grid" style="margin:16px 0">
<div class="stat-box"><div class="stat-num">Active</div><div class="stat-label">Single World</div></div>
<div class="stat-box"><div class="stat-num"><?= $players >= 0 ? $players : '?' ?></div><div class="stat-label">Players</div></div>
<div class="stat-box"><div class="stat-num"><?= $settings >= 0 ? $settings : '?' ?></div><div class="stat-label">Settings</div></div>
<div class="stat-box"><div class="stat-num"><?= $messages >= 0 ? $messages : '?' ?></div><div class="stat-label">Chat Messages</div></div>
</div>
<?php endif; ?>
<form method="POST" style="display:inline" onsubmit="return syncPasswd(this)">
<input type="hidden" name="action" value="init_db" />
<input type="hidden" name="adminPasswd" class="form-admin-passwd" value="<?= htmlspecialchars($inputPasswd) ?>" />
<button type="submit" class="btn">Initialize / Upgrade MySQL Tables</button>
</form>
</div>
<!-- Active world locks -->
<div class="maint-card">
<h3>🔐 Active World Locks</h3>
<?php if (empty($activeLocks)): ?>
<p style="color:#64748b;font-size:14px">No world locks currently held. (Requires performance_schema access.)</p>
<?php else: ?>
<table>
<tr><th>Lock Name</th><th>Status</th></tr>
<?php foreach ($activeLocks as $lk): ?>
<tr>
<td><?= htmlspecialchars($lk['lock_name'] ?? '') ?></td>
<td><?= htmlspecialchars($lk['LOCK_STATUS'] ?? '') ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
</div>
</div>
<script>
function updateAllFormPasswords(val) {
document.querySelectorAll('.form-admin-passwd').forEach(function(el) {
el.value = val;
});
}
function syncPasswd(form) {
var pwdInput = document.getElementById('adminPasswd');
var pwd = pwdInput ? pwdInput.value : '';
var hidden = form.querySelector('.form-admin-passwd');
if (hidden) hidden.value = pwd;
if (!pwd) {
alert('Please enter the admin password first.');
if (pwdInput) pwdInput.focus();
return false;
}
return true;
}
function confirmAction(form, msg) {
if (!syncPasswd(form)) return false;
return confirm(msg);
}
</script>
</body>
</html>