-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrop_table.php
More file actions
91 lines (77 loc) · 3.11 KB
/
Copy pathdrop_table.php
File metadata and controls
91 lines (77 loc) · 3.11 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
<?php
/**
* TironData — DBMS: Data Builder for Massive Samples
*
* @package TironData
* @author Harits Nala B. <developer.haritsnb@gmail.com>
* @license MIT License <https://opensource.org/licenses/MIT>
* @link https://github.com/haritsnb/tirondata
*/
session_start();
header('Content-Type: application/json; charset=utf-8');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['success' => false, 'message' => 'Metode tidak diizinkan']);
exit;
}
if (!isset($_SESSION['db_connection'])) {
http_response_code(401);
echo json_encode(['success' => false, 'message' => 'Sesi koneksi tidak ditemukan']);
exit;
}
$input = json_decode(file_get_contents('php://input'), true);
if (!$input || empty($input['table'])) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Nama tabel wajib diisi']);
exit;
}
$table = trim($input['table']);
/* Validasi: nama tabel hanya boleh berisi karakter aman */
if (!preg_match('/^[a-zA-Z0-9_]+$/', $table)) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Nama tabel tidak valid. Hanya huruf, angka, dan underscore yang diizinkan.']);
exit;
}
/* Daftar tabel yang dilindungi (opsional, sesuaikan kebutuhan) */
$protected = ['mysql', 'information_schema', 'performance_schema', 'sys'];
if (in_array(strtolower($table), $protected)) {
http_response_code(403);
echo json_encode(['success' => false, 'message' => 'Tabel "' . $table . '" dilindungi dan tidak dapat dihapus']);
exit;
}
$c = $_SESSION['db_connection'];
try {
$dsn = "mysql:host={$c['host']};port={$c['port']};dbname={$c['database']};charset=utf8mb4";
$pdo = new PDO($dsn, $c['username'], $c['password'], [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);
/* Pastikan tabel benar-benar ada di database ini sebelum dihapus */
$stmt = $pdo->prepare(
"SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = :db AND TABLE_NAME = :tbl AND TABLE_TYPE = 'BASE TABLE'"
);
$stmt->execute([':db' => $c['database'], ':tbl' => $table]);
$exists = (int) $stmt->fetchColumn();
if ($exists === 0) {
http_response_code(404);
echo json_encode(['success' => false, 'message' => 'Tabel "' . $table . '" tidak ditemukan di database ini']);
exit;
}
/* Eksekusi DROP TABLE */
$safeTable = str_replace('`', '``', $table);
$pdo->exec("DROP TABLE `{$safeTable}`");
echo json_encode([
'success' => true,
'message' => 'Tabel "' . $table . '" berhasil dihapus',
'dropped' => $table,
]);
} catch (PDOException $e) {
$msg = $e->getMessage();
if (strpos($msg, 'foreign key constraint') !== false) {
$msg = 'Gagal menghapus: tabel "' . $table . '" masih direferensikan oleh foreign key tabel lain';
} elseif (strpos($msg, 'LOCK') !== false) {
$msg = 'Gagal menghapus: tabel sedang dikunci oleh proses lain';
}
http_response_code(500);
echo json_encode(['success' => false, 'message' => $msg]);
}