+6
-22
@@ -364,17 +364,15 @@ function handleComments($method, $id, $db) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleNodes($method, $id, $db) {
|
function handleNodes($method, $id, $db) {
|
||||||
// Ensure node_notes table exists
|
|
||||||
try { $db->exec("CREATE TABLE IF NOT EXISTS node_notes (node_id INT, notes TEXT DEFAULT '')"); } catch (Exception $e) {}
|
|
||||||
switch ($method) {
|
switch ($method) {
|
||||||
case 'GET':
|
case 'GET':
|
||||||
echo json_encode($db->query("SELECT n.*, nn.notes FROM network_nodes n LEFT JOIN node_notes nn ON n.id = nn.node_id ORDER BY n.group_name, n.label")->fetchAll(PDO::FETCH_ASSOC));
|
echo json_encode($db->query("SELECT * FROM network_nodes ORDER BY group_name, label")->fetchAll(PDO::FETCH_ASSOC));
|
||||||
break;
|
break;
|
||||||
case 'POST':
|
case 'POST':
|
||||||
$data = json_decode(file_get_contents('php://input'), true);
|
$data = json_decode(file_get_contents('php://input'), true);
|
||||||
$stmt = $db->prepare("
|
$stmt = $db->prepare("
|
||||||
INSERT INTO network_nodes (label, ip_address, node_type, status, group_name, pos_x, pos_y)
|
INSERT INTO network_nodes (label, ip_address, node_type, status, group_name, notes, pos_x, pos_y)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
");
|
");
|
||||||
$stmt->execute([
|
$stmt->execute([
|
||||||
$data['label'],
|
$data['label'],
|
||||||
@@ -382,24 +380,18 @@ function handleNodes($method, $id, $db) {
|
|||||||
$data['node_type'] ?? 'host',
|
$data['node_type'] ?? 'host',
|
||||||
$data['status'] ?? 'unknown',
|
$data['status'] ?? 'unknown',
|
||||||
$data['group_name'] ?? 'default',
|
$data['group_name'] ?? 'default',
|
||||||
|
$data['notes'] ?? '',
|
||||||
$data['pos_x'] ?? 0,
|
$data['pos_x'] ?? 0,
|
||||||
$data['pos_y'] ?? 0
|
$data['pos_y'] ?? 0
|
||||||
]);
|
]);
|
||||||
$nodeId = $db->lastInsertId();
|
echo json_encode(['id' => $db->lastInsertId()]);
|
||||||
if (!empty($data['notes'])) {
|
|
||||||
try {
|
|
||||||
$db->prepare("INSERT INTO node_notes (node_id, notes) VALUES (?, ?) ON DUPLICATE KEY UPDATE notes = VALUES(notes)")
|
|
||||||
->execute([$nodeId, $data['notes']]);
|
|
||||||
} catch (Exception $e) {}
|
|
||||||
}
|
|
||||||
echo json_encode(['id' => $nodeId]);
|
|
||||||
break;
|
break;
|
||||||
case 'PUT':
|
case 'PUT':
|
||||||
if ($id) {
|
if ($id) {
|
||||||
$data = json_decode(file_get_contents('php://input'), true);
|
$data = json_decode(file_get_contents('php://input'), true);
|
||||||
$fields = [];
|
$fields = [];
|
||||||
$params = [];
|
$params = [];
|
||||||
foreach (['label','ip_address','node_type','status','group_name','pos_x','pos_y'] as $f) {
|
foreach (['label','ip_address','node_type','status','group_name','notes','pos_x','pos_y'] as $f) {
|
||||||
if (isset($data[$f])) {
|
if (isset($data[$f])) {
|
||||||
$fields[] = "$f = ?";
|
$fields[] = "$f = ?";
|
||||||
$params[] = $data[$f];
|
$params[] = $data[$f];
|
||||||
@@ -410,19 +402,11 @@ function handleNodes($method, $id, $db) {
|
|||||||
$stmt = $db->prepare("UPDATE network_nodes SET " . implode(', ', $fields) . " WHERE id = ?");
|
$stmt = $db->prepare("UPDATE network_nodes SET " . implode(', ', $fields) . " WHERE id = ?");
|
||||||
$stmt->execute($params);
|
$stmt->execute($params);
|
||||||
}
|
}
|
||||||
// Update notes via separate table
|
|
||||||
if (isset($data['notes'])) {
|
|
||||||
try {
|
|
||||||
$db->prepare("INSERT INTO node_notes (node_id, notes) VALUES (?, ?) ON DUPLICATE KEY UPDATE notes = VALUES(notes)")
|
|
||||||
->execute([$id, $data['notes']]);
|
|
||||||
} catch (Exception $e) {}
|
|
||||||
}
|
|
||||||
echo json_encode(['updated' => true]);
|
echo json_encode(['updated' => true]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'DELETE':
|
case 'DELETE':
|
||||||
if ($id) {
|
if ($id) {
|
||||||
$db->prepare("DELETE FROM node_notes WHERE node_id = ?")->execute([$id]);
|
|
||||||
$db->prepare("DELETE FROM network_nodes WHERE id = ?")->execute([$id]);
|
$db->prepare("DELETE FROM network_nodes WHERE id = ?")->execute([$id]);
|
||||||
echo json_encode(['deleted' => true]);
|
echo json_encode(['deleted' => true]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,10 +39,8 @@ function migrate($db) {
|
|||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
)");
|
)");
|
||||||
try {
|
try {
|
||||||
$rootDb = new PDO("mysql:host=" . (getenv('DB_HOST') ?: 'mysql') . ";dbname=" . (getenv('DB_NAME') ?: 'neptune') . ";charset=utf8mb4", 'root', getenv('MYSQL_ROOT_PASSWORD') ?: 'neptune_root_pass');
|
$rootDb = new PDO("mysql:host=" . (getenv('DB_HOST') ?: 'mysql') . ";charset=utf8mb4", 'root', getenv('MYSQL_ROOT_PASSWORD') ?: 'neptune_root_pass');
|
||||||
$rootDb->exec("CREATE TABLE IF NOT EXISTS node_notes (id INT AUTO_INCREMENT PRIMARY KEY, node_id INT NOT NULL UNIQUE, notes TEXT DEFAULT '')");
|
$rootDb->exec("ALTER TABLE neptune.network_nodes ADD COLUMN IF NOT EXISTS notes TEXT DEFAULT '' AFTER group_name");
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
// Fallback: try with neptune user
|
|
||||||
$db->exec("CREATE TABLE IF NOT EXISTS node_notes (id INT AUTO_INCREMENT PRIMARY KEY, node_id INT NOT NULL UNIQUE, notes TEXT DEFAULT '')");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -33,6 +33,7 @@ CREATE TABLE IF NOT EXISTS network_nodes (
|
|||||||
node_type ENUM('host','server','router','firewall','switch','cloud','endpoint','other') DEFAULT 'host',
|
node_type ENUM('host','server','router','firewall','switch','cloud','endpoint','other') DEFAULT 'host',
|
||||||
status ENUM('online','offline','unknown','compromised','monitoring') DEFAULT 'unknown',
|
status ENUM('online','offline','unknown','compromised','monitoring') DEFAULT 'unknown',
|
||||||
group_name VARCHAR(100) DEFAULT 'default',
|
group_name VARCHAR(100) DEFAULT 'default',
|
||||||
|
notes TEXT DEFAULT '',
|
||||||
pos_x FLOAT DEFAULT 0,
|
pos_x FLOAT DEFAULT 0,
|
||||||
pos_y FLOAT DEFAULT 0,
|
pos_y FLOAT DEFAULT 0,
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
|||||||
Reference in New Issue
Block a user