diff --git a/tests/php/test_storage_delete_in_use.php b/tests/php/test_storage_delete_in_use.php new file mode 100644 index 0000000000..347606a37d --- /dev/null +++ b/tests/php/test_storage_delete_in_use.php @@ -0,0 +1,62 @@ + Storage delete must refuse a storage area that events or monitors +// still reference, instead of leaving their StorageId dangling. +// +// Run as: php tests/php/test_storage_delete_in_use.php + +namespace ZM { + $GLOBALS['warnings'] = array(); + function Warning($s) { $GLOBALS['warnings'][] = $s; } + function AuditAction($a, $o, $id, $m) { $GLOBALS['audits'][] = $a.':'.$id; } + + class Storage { + private $row; + public function __construct($row) { $this->row = $row; } + public static function find_one($params) { + $rows = $GLOBALS['storage']; + return isset($rows[$params['Id']]) ? new Storage($rows[$params['Id']]) : null; + } + public function Id() { return $this->row['Id']; } + public function Name() { return $this->row['Name']; } + public function EventCount() { return $this->row['Events']; } + public function MonitorCount() { return $this->row['Monitors']; } + } +} + +namespace { + function canEdit($area) { return true; } + function validCardinal($v) { return ctype_digit((string)$v) ? (int)$v : 0; } + function validHtmlStr($s) { return htmlspecialchars($s); } + function dbQuery($sql, $params = array()) { $GLOBALS['queries'][] = array($sql, $params); return true; } + + $failures = 0; + function check($name, $got, $want) { + global $failures; + if ($got === $want) { + echo "ok - $name\n"; + } else { + $failures++; + echo "FAIL - $name (got ".var_export($got, true).", want ".var_export($want, true).")\n"; + } + } + + $GLOBALS['storage'] = array( + 2 => array('Id' => 2, 'Name' => 'Free', 'Events' => 0, 'Monitors' => 0), + 3 => array('Id' => 3, 'Name' => 'WithEvents', 'Events' => 5, 'Monitors' => 0), + 4 => array('Id' => 4, 'Name' => 'WithMonitor', 'Events' => 0, 'Monitors' => 1), + ); + $GLOBALS['queries'] = array(); + $GLOBALS['audits'] = array(); + $error_message = ''; + $action = 'delete'; + $_REQUEST = array('object' => 'storage', 'markIds' => array('2', '3', '4', '99')); + include __DIR__.'/../../web/includes/actions/options.php'; + + $deleted = array_map(function($q) { return $q[1][0]; }, $GLOBALS['queries']); + check('only the unused storage is deleted', $deleted, array(2)); + check('only the deleted storage is audited', $GLOBALS['audits'], array('delete:2')); + check('refusal names the storage with events', strpos($error_message, 'WithEvents') !== false, true); + check('refusal names the storage with monitors', strpos($error_message, 'WithMonitor') !== false, true); + + exit($failures ? 1 : 0); +} diff --git a/tests/php/test_storage_unicode_name.php b/tests/php/test_storage_unicode_name.php new file mode 100644 index 0000000000..a8ec7c07f3 --- /dev/null +++ b/tests/php/test_storage_unicode_name.php @@ -0,0 +1,46 @@ +changes(array('Name' => "\u{0425}\u{0440}\u{0430}\u{043D}\u{0438}\u{043B}\u{0438}\u{0449}\u{0435}", 'Path' => '/mnt/josé/cam/')); +check('cyrillic name kept', $changes['Name'], "\u{0425}\u{0440}\u{0430}\u{043D}\u{0438}\u{043B}\u{0438}\u{0449}\u{0435}"); +check('accented path kept, trailing slash removed', $changes['Path'], '/mnt/josé/cam'); + +$changes = $storage->changes(array('Name' => 'Cam #1', 'Path' => '/mnt/a+b')); +check('ascii punctuation still stripped from name', $changes['Name'], 'Cam 1'); +check('ascii punctuation still stripped from path', $changes['Path'], '/mnt/ab'); + +array_map('unlink', glob($stubdir.'/*')); +rmdir($stubdir); +exit($failures ? 1 : 0); diff --git a/web/includes/Storage.php b/web/includes/Storage.php index f59b473e2e..fc92a5fd6a 100644 --- a/web/includes/Storage.php +++ b/web/includes/Storage.php @@ -20,8 +20,8 @@ class Storage extends ZM_Object { protected $defaults = array( 'Id' => null, - 'Path' => array('type'=>'text','filter_regexp'=>array('/[^\w\-\.\(\)\:\/ ]/','/\/$/'), 'default'=>''), - 'Name' => array('type'=>'text','filter_regexp'=>'/[^\w\-\.\(\)\:\/ ]/', 'default'=>'Storage'), + 'Path' => array('type'=>'text','filter_regexp'=>array('/[^\w\-\.\(\)\:\/ ]/u','/\/$/'), 'default'=>''), + 'Name' => array('type'=>'text','filter_regexp'=>'/[^\w\-\.\(\)\:\/ ]/u', 'default'=>'Storage'), 'Type' => 'local', 'Url' => '', 'DiskSpace' => null, @@ -81,6 +81,13 @@ class Storage extends ZM_Object { return $this->{'EventCount'}; } + public function MonitorCount() { + if ( !property_exists($this, 'MonitorCount') ) { + $this->{'MonitorCount'} = dbFetchOne('SELECT COUNT(*) AS MonitorCount FROM Monitors WHERE StorageId=?', 'MonitorCount', array($this->Id())); + } + return $this->{'MonitorCount'}; + } + public function disk_used_blocks() { $df = shell_exec('df '.escapeshellarg($this->Path())); $space = -1; diff --git a/web/includes/actions/options.php b/web/includes/actions/options.php index 2368ec0a71..ac387b566f 100644 --- a/web/includes/actions/options.php +++ b/web/includes/actions/options.php @@ -47,8 +47,14 @@ if ( $action == 'delete' ) { } else if ( $_REQUEST['object'] == 'storage' ) { if ( !empty($_REQUEST['markIds']) ) { foreach ( $_REQUEST['markIds'] as $Id ) { - dbQuery('DELETE FROM Storage WHERE Id=?', array($Id)); - ZM\AuditAction('delete', 'storage', $Id, ''); + $Storage = ZM\Storage::find_one(array('Id'=>validCardinal($Id))); + if ( !$Storage ) continue; + if ( $Storage->EventCount() or $Storage->MonitorCount() ) { + $error_message .= 'Storage area '.validHtmlStr($Storage->Name()).' is in use by events or monitors and cannot be deleted.
'; + continue; + } + dbQuery('DELETE FROM Storage WHERE Id=?', array($Storage->Id())); + ZM\AuditAction('delete', 'storage', $Storage->Id(), ''); } } $refreshParent = true; diff --git a/web/skins/classic/views/_options_storage.php b/web/skins/classic/views/_options_storage.php index 1ee8e361a6..59aadc9e8f 100644 --- a/web/skins/classic/views/_options_storage.php +++ b/web/skins/classic/views/_options_storage.php @@ -52,8 +52,8 @@ querystring(), $Storage->EventCount().' using '.human_filesize($Storage->event_disk_space() ? $Storage->event_disk_space() : 0) ); ?> EventCount() or !$canEdit) ? ' disabled="disabled"' : ''; - echo $Storage->EventCount() ? ' title="Can\'t delete as long as there are events stored here."' : ''; + echo ($Storage->EventCount() or $Storage->MonitorCount() or !$canEdit) ? ' disabled="disabled"' : ''; + echo $Storage->EventCount() ? ' title="Can\'t delete as long as there are events stored here."' : ($Storage->MonitorCount() ? ' title="Can\'t delete as long as monitors use this storage."' : ''); ?> />