From 663c37c7a7a5dfae49f900a40fcbeaf8656d63d3 Mon Sep 17 00:00:00 2001 From: Jukka Lampikoski Date: Thu, 12 Mar 2026 00:59:03 +0200 Subject: [PATCH] Korjaa laitetilan poisto: siivoa viittaukset ennen poistoa MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - dbDeleteLaitetila nollaa devices.laitetila_id, devices.site_id ja ipam.site_id ennen laitetilan poistoa - API: parempi virhekäsittely (\Throwable), logi, tyhjä ID tarkistus - Tiedostojen poisto: @-suppression ja GLOB_BRACE hidden-tiedostoille Co-Authored-By: Claude Opus 4.6 --- api.php | 16 ++++++++++++---- db.php | 4 ++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/api.php b/api.php index dc48f61..abf6867 100644 --- a/api.php +++ b/api.php @@ -4553,6 +4553,11 @@ switch ($action) { try { $input = json_decode(file_get_contents('php://input'), true); $tilaId = $input['id'] ?? ''; + if (empty($tilaId)) { + http_response_code(400); + echo json_encode(['error' => 'Laitetilan ID puuttuu']); + break; + } $tila = dbLoadLaitetila($tilaId); if (!$tila || $tila['company_id'] !== $companyId) { http_response_code(404); @@ -4562,13 +4567,16 @@ switch ($action) { // Poista tiedostot levyltä $tilaDir = DATA_DIR . '/companies/' . $companyId . '/laitetilat/' . $tilaId; if (is_dir($tilaDir)) { - $files = glob($tilaDir . '/*'); - foreach ($files as $f) { if (is_file($f)) unlink($f); } - rmdir($tilaDir); + $items = glob($tilaDir . '/{,.}*', GLOB_BRACE); + foreach ($items as $item) { + if (is_file($item)) @unlink($item); + } + @rmdir($tilaDir); } dbDeleteLaitetila($tilaId); + dbAddLog($companyId, currentUser(), 'laitetila_delete', $tilaId, $tila['nimi'] ?? '', 'Poisti laitetilan'); echo json_encode(['success' => true]); - } catch (Exception $e) { + } catch (\Throwable $e) { http_response_code(500); echo json_encode(['error' => 'Poisto epäonnistui: ' . $e->getMessage()]); } diff --git a/db.php b/db.php index c5217a5..b876881 100644 --- a/db.php +++ b/db.php @@ -1984,6 +1984,10 @@ function dbSaveLaitetila(string $companyId, array $tila): string { function dbDeleteLaitetila(string $laitetilaId): ?array { $tila = _dbFetchOne("SELECT id, company_id FROM laitetilat WHERE id = ?", [$laitetilaId]); if ($tila) { + // Nollaa viittaukset laitteissa ja IPAM:ssa + _dbExecute("UPDATE devices SET laitetila_id = NULL WHERE laitetila_id = ?", [$laitetilaId]); + _dbExecute("UPDATE devices SET site_id = NULL WHERE site_id = ?", [$laitetilaId]); + _dbExecute("UPDATE ipam SET site_id = NULL WHERE site_id = ?", [$laitetilaId]); _dbExecute("DELETE FROM laitetilat WHERE id = ?", [$laitetilaId]); // CASCADE poistaa tiedostot } return $tila;