Merge pull request #5825 from AJ0070/storage-fixes

fix: don't delete storage in use and keep non-ASCII storage names and paths
This commit is contained in:
Isaac Connor authored and GitHub committed 2026-11-14 11:52:45 -05:00
commit 7128c19fb2
5 files changed
+127 -6

No files matched your search

+62
View File
@@ -0,0 +1,62 @@
<?php
// Options > 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);
}
+46
View File
@@ -0,0 +1,46 @@
<?php
// A storage Name or Path with non-ASCII letters must not be stripped by the
// input filter, while punctuation outside the allowed set still is.
//
// Run as: php tests/php/test_storage_unicode_name.php
namespace ZM;
$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";
}
}
$stubdir = sys_get_temp_dir().'/zm_storage_unicode_stubs_'.getmypid();
@mkdir($stubdir, 0700, true);
foreach (array('database.php', 'Event.php') as $stub) {
file_put_contents($stubdir.'/'.$stub, "<?php\n");
}
set_include_path($stubdir.PATH_SEPARATOR.__DIR__.'/../../web/includes'.PATH_SEPARATOR.get_include_path());
define('ZM_DIR_EVENTS', '/var/cache/zoneminder/events');
function Warning($s) {}
function Error($s) {}
function Debug($s) {}
require_once 'Storage.php';
$storage = new Storage();
$changes = $storage->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);
+9 -2
View File
@@ -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;
+8 -2
View File
@@ -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.<br/>';
continue;
}
dbQuery('DELETE FROM Storage WHERE Id=?', array($Storage->Id()));
ZM\AuditAction('delete', 'storage', $Storage->Id(), '');
}
}
$refreshParent = true;
+2 -2
View File
@@ -52,8 +52,8 @@
<td class="ColEvents"><?php echo makeLink('?view=events'.$filter->querystring(), $Storage->EventCount().' using '.human_filesize($Storage->event_disk_space() ? $Storage->event_disk_space() : 0) ); ?></td>
<td class="colMark"><input type="checkbox" name="markIds[]" value="<?php echo $Storage->Id() ?>" data-on-click-this="configureDeleteButton"
<?php
echo ($Storage->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."' : '');
?>
/></td>
</tr>