From 2fcaa1e2ff15f43c31d48a8430e3ebbec36a1a60 Mon Sep 17 00:00:00 2001 From: test Date: Fri, 4 Sep 2026 13:59:59 +0200 Subject: [PATCH] fix flag internet mode --- MEMORY.md | 6 ++++++ README.md | 1 + front/asset.form.php | 8 ++++++++ src/AssetTab.php | 29 +++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+) diff --git a/MEMORY.md b/MEMORY.md index fc9478d..de27345 100644 --- a/MEMORY.md +++ b/MEMORY.md @@ -2,6 +2,12 @@ ## Ultima modifica: 02/09/2026 +## 02/09/2026 — Fix: Internet mode switch mostra sempre disabilitato dopo il toggle (cache 30s non invalidata) +- **Sintomo**: nel tab Computer → Azioni, togliendo/attivando "Internet mode" il form fa POST, la pagina ricarica tornando su "Stato client", e riaprendo "Azioni" lo switch risulta ancora nello stato precedente. +- **Analisi (causa radice)**: la chiamata API `saveInternetMode()` funziona correttamente (verificato empiricamente su localhost:55414: `clientsettings_save` con `t_clientid`+`overwrite=true`+`internet_mode_enabled` toggla e rilegge bene `internet_mode_enabled=true/false`). Anche il parsing PHP del campo duplicato `internet_mode` è corretto (hidden 0 + checkbox 1 → PHP tiene l'ULTIMO valore = 1 in enable). **Il vero bug** è la **session cache 30s** `urbackup_data_{serverid}_{itemtype}_{itemsid}` in `AssetTab::loadApiData()` (AssetTab.php:374-377) che NON veniva invalidata dopo alcuna azione mutante: dopo il redirect il tab mostrava i dati cached (stato vecchio) per max 30s. +- **Fix**: nuovo `AssetTab::clearApiCache(CommonDBTM $item)` (AssetTab.php, ricalcola la stessa cache_key e fa `unset($_SESSION[$cache_key])`); chiamato da `front/asset.form.php` prima di ogni azione mutante (backup incr/full file/image, create/delete client, set_internet_mode, set_default_dirs). Così dopo il redirect `loadApiData()` ri-fetches dati freschi e lo switch riflette lo stato reale del server. +- Nessun bump versione (solo fix UI/comportamento, nessuna modifica DB, regola 6). Lint `php -l` OK. **Verifica UI utente OBBLIGATORIA**: attivare/disattivare Internet mode e confermare che lo switch resti nello stato scelto dopo il ricaricamento (anche subito, senza attendere). + ## 02/09/2026 — Pulizia capacity all'uninstall (nessun bump versione) - **`install/uninstall.php`**: nuova `plugin_urbackup_uninstall_cleanup_capacities(Migration, DBmysql)` — rimuove il riferimento `GlpiPlugin\Urbackup\Capacity\UrBackupCapacity` dalla colonna JSON `capacities` di `glpi_assetdefinitions` (query builder `$DB->request()/update()`, idempotente, filtrata sulle righe con `capacities <> '[]'`). Invocata in `plugin_urbackup_uninstall_process()` con `global $DB`. - Motivo: altrimenti resta una reference orfana dopo la disinstallazione. Il core GLPI la ignora in sicurezza (`AssetDefinition::decodeCapacities()`/`AssetDefinition.php:698-701`, "May be a previously enabled capacity from a disabled plugin"), ma ora il DB resta pulito. diff --git a/README.md b/README.md index e645837..a017565 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,7 @@ plugin_urbackup/ - `server_test.ajax.php`: entity-aware `check()` authorization; added `public/js/urbackup.js` (registered via `ADD_JAVASCRIPT`) that sends the `X-Glpi-Csrf-Token` header on plugin AJAX POSTs; `dropdown_host.ajax.php` now requires READ right - Renamed the server form client tabs to be clearer: "Linked clients" → "Managed clients", "Unlinked clients" → "Linkable clients", "Missing clients" → "Clients to manage"; removed the sort/search on the missing clients table to match the other tab layouts; updated it_IT/de_DE/en_GB translations and recompiled locales - **Uninstall cleanup**: the UrBackup capacity reference (`GlpiPlugin\Urbackup\Capacity\UrBackupCapacity`) is now removed from the `capacities` JSON column of `glpi_assetdefinitions` so the database keeps no stale reference to the disinstalled plugin (GLPI core safely ignores such orphaned capacities, but they are now cleaned up) +- **Internet mode fix**: the "Enable Internet mode" switch in the asset tab no longer appears stuck on its previous state after toggling. The 30 s API-data session cache was not invalidated after mutations, so the tab kept showing stale settings right after the redirect; a new `AssetTab::clearApiCache()` now clears it on every mutating action (backups, create/delete client, internet mode, default dirs) ### 0.7.2 - UrBackup on Computer is now configurable: new `enable_computer` setting on the plugin configuration page (previously always enabled, hardcoded) diff --git a/front/asset.form.php b/front/asset.form.php index 7614d5d..ccaabe3 100644 --- a/front/asset.form.php +++ b/front/asset.form.php @@ -109,11 +109,13 @@ if (isset($_POST['execute'])) { switch ($action) { case 'incremental_file_backup': if (Profile::canCurrentUser(UPDATE)) { + AssetTab::clearApiCache($item); AssetTab::startBackup($item, 'file'); } break; case 'full_file_backup': if (Profile::canCurrentUser(UPDATE)) { + AssetTab::clearApiCache($item); $link = ServerAsset::getLinkForAsset($itemtype, $items_id, false); if ($link !== null) { $server = new \GlpiPlugin\Urbackup\Server(); @@ -130,11 +132,13 @@ if (isset($_POST['execute'])) { break; case 'incremental_image_backup': if (Profile::canCurrentUser(UPDATE)) { + AssetTab::clearApiCache($item); AssetTab::startBackup($item, 'image'); } break; case 'full_image_backup': if (Profile::canCurrentUser(UPDATE)) { + AssetTab::clearApiCache($item); $link = ServerAsset::getLinkForAsset($itemtype, $items_id, false); if ($link !== null) { $server = new \GlpiPlugin\Urbackup\Server(); @@ -151,6 +155,7 @@ if (isset($_POST['execute'])) { break; case 'create_client': if (Profile::canCurrentUser(CREATE)) { + AssetTab::clearApiCache($item); $link = ServerAsset::getLinkForAsset($itemtype, $items_id, false); if ($link !== null) { $server = new \GlpiPlugin\Urbackup\Server(); @@ -167,6 +172,7 @@ if (isset($_POST['execute'])) { break; case 'delete_client': if (Profile::canCurrentUser(PURGE)) { + AssetTab::clearApiCache($item); $link = ServerAsset::getLinkForAsset($itemtype, $items_id, false); if ($link !== null) { $server = new \GlpiPlugin\Urbackup\Server(); @@ -184,6 +190,7 @@ if (isset($_POST['execute'])) { case 'set_internet_mode': if (Profile::canCurrentUser(UPDATE)) { $enabled = (int) ($_POST['internet_mode'] ?? 0) === 1; + AssetTab::clearApiCache($item); if (!AssetTab::saveInternetMode($item, $enabled)) { Session::addMessageAfterRedirect( __('Failed to save internet mode', 'urbackup'), @@ -196,6 +203,7 @@ if (isset($_POST['execute'])) { case 'set_default_dirs': if (Profile::canCurrentUser(UPDATE)) { $dirs = (string) ($_POST['default_dirs'] ?? ''); + AssetTab::clearApiCache($item); if (!AssetTab::saveDefaultDirs($item, $dirs)) { Session::addMessageAfterRedirect( __('Failed to save default directories', 'urbackup'), diff --git a/src/AssetTab.php b/src/AssetTab.php index 41da905..395280f 100644 --- a/src/AssetTab.php +++ b/src/AssetTab.php @@ -998,6 +998,35 @@ class AssetTab extends CommonDBTM return false; } + /** + * Clear the API data session cache for the given asset. + * + * `loadApiData()` caches the API results (client status, settings, logs) + * for 30 seconds under a per-asset session key. Any action that mutates + * the client on the UrBackup server must clear this cache, otherwise the + * tab keeps displaying stale data (e.g. the Internet mode switch still + * showing the previous state) right after the redirect. + * + * @param CommonDBTM $item Asset whose cached API data must be cleared + * + * @return void + */ + public static function clearApiCache(CommonDBTM $item): void + { + $link = ServerAsset::getLinkForAsset($item::class, (int) $item->fields['id'], false); + if ($link === null) { + return; + } + + $server_id = (int) ($link['plugin_urbackup_servers_id'] ?? 0); + if ($server_id <= 0) { + return; + } + + $cache_key = 'urbackup_data_' . $server_id . '_' . $item::class . '_' . $item->fields['id']; + unset($_SESSION[$cache_key]); + } + public static function saveInternetMode(CommonDBTM $item, bool $enabled): bool { $link = ServerAsset::getLinkForAsset($item::class, (int) $item->fields['id'], false);