From 4359270a34195cd8d35c56d52068b87a1a01acf2 Mon Sep 17 00:00:00 2001 From: Jash Date: Sat, 14 Nov 2026 18:55:12 +0530 Subject: [PATCH] fix: re-check Archived after reloading the event row before deleting it refs #5836 --- .../test_event_delete_archived_after_load.php | 84 +++++++++++++++++++ web/includes/Event.php | 6 ++ 2 files changed, 90 insertions(+) create mode 100644 tests/php/test_event_delete_archived_after_load.php diff --git a/tests/php/test_event_delete_archived_after_load.php b/tests/php/test_event_delete_archived_after_load.php new file mode 100644 index 0000000000..a1f7a643dd --- /dev/null +++ b/tests/php/test_event_delete_archived_after_load.php @@ -0,0 +1,84 @@ + 77, 'MonitorId' => 1, 'StorageId' => 3, 'DiskSpace' => 100, + 'Scheme' => 'Medium', 'Archived' => 0, 'StartDateTime' => '2024-05-06 07:08:09'); + +$db_row = array_merge($loaded, array('Archived' => 1)); +$event = new Event($loaded); +$event->delete(); +check('archived since it was loaded: nothing is deleted', $calls, array('rollback')); + +$calls = array(); +$db_row = $loaded; +$event = new Event($loaded); +$event->delete(); +check('still unarchived: files are deleted', in_array('deletePath /var/cache/zoneminder/events/1/2024-05-06/77', $calls), true); + +echo "\n$passes passed, $failures failed\n"; +array_map('unlink', glob($stubdir.'/*')); +@rmdir($stubdir); +exit($failures ? 1 : 0); diff --git a/web/includes/Event.php b/web/includes/Event.php index 85d63576d1..543ef79481 100644 --- a/web/includes/Event.php +++ b/web/includes/Event.php @@ -212,6 +212,12 @@ class Event extends ZM_Object { $dbConn->beginTransaction(); try { $this->lock(); + # lock() reloaded the row, so this sees an archive made after the event was loaded + if ( $this->{'Archived'} ) { + Error('Cannot delete an Archived event.'); + $dbConn->rollback(); + return; + } # dbQuery() reports a failed statement by returning null, not by throwing foreach (array('Stats', 'Event_Data', 'Frames') as $table) { if (null === dbQuery('DELETE FROM '.$table.' WHERE EventId = ?', array($this->{'Id'}))) {