195 lines
4.3 KiB
PHP
195 lines
4.3 KiB
PHP
<?php
|
|
/**
|
|
* app/api/connections.php
|
|
*
|
|
* API für Netzwerkverbindungen (Port ↔ Port)
|
|
* - Laden der Topologie
|
|
* - Anlegen / Bearbeiten / Löschen von Verbindungen
|
|
* - Unterstützt freie Verbindungstypen (Kupfer, LWL, BNC, Token Ring, etc.)
|
|
*/
|
|
|
|
require_once __DIR__ . '/../bootstrap.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
// TODO: Single-User-Auth prüfen
|
|
// if (!$_SESSION['user']) { http_response_code(403); exit; }
|
|
|
|
$action = $_GET['action'] ?? 'load';
|
|
|
|
/* =========================
|
|
* Router
|
|
* ========================= */
|
|
|
|
switch ($action) {
|
|
|
|
case 'load':
|
|
loadConnections($sql);
|
|
break;
|
|
|
|
case 'save':
|
|
saveConnection($sql);
|
|
break;
|
|
|
|
case 'delete':
|
|
deleteConnection($sql);
|
|
break;
|
|
|
|
default:
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Unbekannte Aktion']);
|
|
break;
|
|
}
|
|
|
|
/* =========================
|
|
* Aktionen
|
|
* ========================= */
|
|
|
|
/**
|
|
* Lädt alle Geräte + Ports + Verbindungen für eine Netzwerkansicht
|
|
*/
|
|
function loadConnections($sql)
|
|
{
|
|
$contextId = $_GET['context_id'] ?? null;
|
|
|
|
if (!$contextId) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'context_id fehlt']);
|
|
return;
|
|
}
|
|
|
|
// TODO: Kontext definieren (Standort, Rack, Floor, gesamtes Netz)
|
|
|
|
/* ---------- Geräte ---------- */
|
|
$devices = $sql->get(
|
|
"SELECT id, name, device_type_id, pos_x, pos_y
|
|
FROM devices
|
|
WHERE context_id = ?",
|
|
"i",
|
|
[$contextId]
|
|
);
|
|
|
|
/* ---------- Ports ---------- */
|
|
$ports = $sql->get(
|
|
"SELECT p.id, p.device_id, p.name, p.port_type_id
|
|
FROM ports p
|
|
JOIN devices d ON d.id = p.device_id
|
|
WHERE d.context_id = ?",
|
|
"i",
|
|
[$contextId]
|
|
);
|
|
|
|
/* ---------- Verbindungen ---------- */
|
|
$connections = $sql->get(
|
|
"SELECT
|
|
c.id,
|
|
c.connection_type_id,
|
|
c.port_a_id,
|
|
c.port_b_id,
|
|
c.vlan,
|
|
c.mode,
|
|
c.comment
|
|
FROM connections c",
|
|
"",
|
|
[]
|
|
);
|
|
|
|
echo json_encode([
|
|
'devices' => $devices,
|
|
'ports' => $ports,
|
|
'connections' => $connections
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Speichert eine Verbindung (neu oder Update)
|
|
*/
|
|
function saveConnection($sql)
|
|
{
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!$data) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Ungültige JSON-Daten']);
|
|
return;
|
|
}
|
|
|
|
// TODO: Validierung
|
|
// - port_a_id vorhanden
|
|
// - port_b_id vorhanden
|
|
// - Verbindungstyp erlaubt
|
|
|
|
if (!empty($data['id'])) {
|
|
// UPDATE
|
|
$rows = $sql->set(
|
|
"UPDATE connections
|
|
SET connection_type_id = ?, port_a_id = ?, port_b_id = ?, vlan = ?, mode = ?, comment = ?
|
|
WHERE id = ?",
|
|
"iiiissi",
|
|
[
|
|
$data['connection_type_id'],
|
|
$data['port_a_id'],
|
|
$data['port_b_id'],
|
|
$data['vlan'],
|
|
$data['mode'],
|
|
$data['comment'],
|
|
$data['id']
|
|
]
|
|
);
|
|
|
|
echo json_encode([
|
|
'status' => 'updated',
|
|
'rows' => $rows
|
|
]);
|
|
} else {
|
|
// INSERT
|
|
$id = $sql->set(
|
|
"INSERT INTO connections
|
|
(connection_type_id, port_a_id, port_b_id, vlan, mode, comment)
|
|
VALUES (?, ?, ?, ?, ?, ?)",
|
|
"iiiiss",
|
|
[
|
|
$data['connection_type_id'],
|
|
$data['port_a_id'],
|
|
$data['port_b_id'],
|
|
$data['vlan'],
|
|
$data['mode'],
|
|
$data['comment']
|
|
],
|
|
true
|
|
);
|
|
|
|
echo json_encode([
|
|
'status' => 'created',
|
|
'id' => $id
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Löscht eine Verbindung
|
|
*/
|
|
function deleteConnection($sql)
|
|
{
|
|
$id = $_GET['id'] ?? null;
|
|
|
|
if (!$id) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'ID fehlt']);
|
|
return;
|
|
}
|
|
|
|
// TODO: Prüfen, ob Verbindung existiert
|
|
|
|
$rows = $sql->set(
|
|
"DELETE FROM connections WHERE id = ?",
|
|
"i",
|
|
[$id]
|
|
);
|
|
|
|
echo json_encode([
|
|
'status' => 'deleted',
|
|
'rows' => $rows
|
|
]);
|
|
}
|