2.php

main 137 lines · 4.5 KB Raw
Alex Alex Commit Initial commit 01/10/2026 09:20
1<?php
2/**
3 * Step 2 - database.
4 *
5 * This is where the installer refuses if the OT server's own schema is not
6 * there. ZnoteX reads `accounts` and `players`; it has never created them and
7 * will not pretend to. Importing TFS/Canary's schema.sql is the server owner's
8 * job, and doing it after ZnoteX would overwrite what we are about to write.
9 */
10
11if (!defined('ZNOTE_INSTALL')) { http_response_code(403); die('Direct access denied.'); }
12
13$tested = false;
14$tables = null;
15$connect = null;
16
17if ($_SERVER['REQUEST_METHOD'] === 'POST') {
18
19 install_state(array(
20 'sqlHost' => trim((string)($_POST['sqlHost'] ?? '127.0.0.1')),
21 'sqlUser' => trim((string)($_POST['sqlUser'] ?? '')),
22 'sqlPassword' => (string)($_POST['sqlPassword'] ?? ''),
23 'sqlDatabase' => trim((string)($_POST['sqlDatabase'] ?? '')),
24 ));
25
26 $tested = true;
27 $link = install_connect($connect);
28
29 if ($link === null) {
30 install_error('Could not connect: ' . ih((string)$connect));
31 } else {
32 $tables = install_server_tables($link);
33 $link->close();
34
35 if (!$tables['ok']) {
36 install_error(
37 'Connected, but this database has no <code>accounts</code> / <code>players</code> tables. '
38 . 'Import your server\'s own <code>schema.sql</code> first, then come back.'
39 );
40 } elseif (!empty($_POST['continue'])) {
41 install_max_step(3);
42 header('Location: ' . install_url(3));
43 exit;
44 }
45 }
46}
47
48// Pre-fill from config.php so a normal install is mostly clicking Next.
49$saved = install_saved_config();
50$value = static function (string $key, string $fallback = '') use ($saved) {
51 $fromState = install_get($key, null);
52 if ($fromState !== null && $fromState !== '') {
53 return (string)$fromState;
54 }
55 return (string)($saved[$key] ?? $fallback);
56};
57?>
58<h1>Database</h1>
59<p class="lead">
60 The same database your Open Tibia server uses. ZnoteX adds its own
61 <code>znote_*</code> tables beside the server's.
62</p>
63
64<form method="post">
65 <div class="row">
66 <div class="field">
67 <label class="lbl" for="sqlHost">Host</label>
68 <input type="text" id="sqlHost" name="sqlHost" value="<?= ih($value('sqlHost', '127.0.0.1')) ?>" required>
69 </div>
70 <div class="field">
71 <label class="lbl" for="sqlDatabase">Database</label>
72 <input type="text" id="sqlDatabase" name="sqlDatabase" value="<?= ih($value('sqlDatabase')) ?>" required>
73 </div>
74 </div>
75
76 <div class="row">
77 <div class="field">
78 <label class="lbl" for="sqlUser">User</label>
79 <input type="text" id="sqlUser" name="sqlUser" value="<?= ih($value('sqlUser')) ?>" required>
80 <p class="hint">Do not use <code>root</code> on a public server.</p>
81 </div>
82 <div class="field">
83 <label class="lbl" for="sqlPassword">Password</label>
84 <input type="password" id="sqlPassword" name="sqlPassword" value="<?= ih($value('sqlPassword')) ?>">
85 </div>
86 </div>
87
88 <?php if ($tables !== null): ?>
89 <h2>Server schema</h2>
90
91 <?php if ($tables['ok']): ?>
92 <p class="good">Connected, and your server's tables are there.</p>
93 <?php else: ?>
94 <div class="warn">
95 <strong>The server schema is missing.</strong><br>
96 ZnoteX reads the tables your OT server creates &mdash; it does not create them, and
97 importing them afterwards would wipe what ZnoteX writes. Import your server's
98 <code>schema.sql</code> into <code><?= ih(install_get('sqlDatabase')) ?></code>, then
99 press Test again.
100 </div>
101 <?php endif; ?>
102
103 <table>
104 <tr><th>Table</th><th>Status</th><th></th></tr>
105 <?php foreach ($tables['required'] as $table => $found): ?>
106 <tr>
107 <td><code><?= ih($table) ?></code></td>
108 <td class="<?= $found ? 'ok' : 'no' ?>"><?= $found ? '&#10003; present' : '&#10007; missing' ?></td>
109 <td>required</td>
110 </tr>
111 <?php endforeach; ?>
112 <?php foreach ($tables['optional'] as $table => $found): ?>
113 <tr>
114 <td><code><?= ih($table) ?></code></td>
115 <td><?= $found ? '&#10003; present' : '&mdash;' ?></td>
116 <td>optional &mdash; some pages need it</td>
117 </tr>
118 <?php endforeach; ?>
119 </table>
120
121 <?php if (!empty($tables['znote_installed'])): ?>
122 <p class="warn">
123 A <code>znote</code> table already exists. The next step will not touch tables that
124 are already there, so nothing you have will be lost.
125 </p>
126 <?php endif; ?>
127 <?php endif; ?>
128
129 <div class="actions">
130 <button class="btn ghost" type="submit" name="test" value="1">Test connection</button>
131 <?php if ($tables !== null && $tables['ok']): ?>
132 <button class="btn" type="submit" name="continue" value="1">Continue</button>
133 <?php endif; ?>
134 <a class="btn ghost" href="<?= install_url(1) ?>">Back</a>
135 </div>
136</form>
137
Top