Files
zoneminder/web/skins/classic/views/options.php
T

460 lines
21 KiB
PHP

<?php
//
// ZoneMinder web options view file, $Date$, $Revision$
// Copyright (C) 2001-2008 Philip Coombes
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
if (!canView('System')) {
$view = 'error';
return;
}
$canEdit = canEdit('System');
$tab = isset($_REQUEST['tab']) ? validHtmlStr($_REQUEST['tab']) : 'system';
$focusWindow = true;
// Set by the save action's redirect, which can't carry the flag any other way.
if (!empty($_REQUEST['restart'])) $restartWarning = true;
xhtmlHeaders(__FILE__, translate('Options'));
getBodyTopHTML();
echo getNavBarHTML();
?>
<div class="container-fluid" id="content">
<div class="row flex-nowrap h-100">
<?php
if (!(defined('ZM_WEB_LEFT_MENU_OPTIONS_SUBMENU') and ZM_WEB_LEFT_MENU_OPTIONS_SUBMENU)) {
?>
<nav id="sidebar">
<ul class="nav nav-pills flex-column">
<?php
foreach ($zmMenu::$submenuOptionsItems as $name=>$value) {
echo '<li class="nav-item form-control-sm mb-2 '.$name.'"><a class="nav-link'.($tab == $name ? ' active' : '').'" href="?view='.$view.'&amp;tab='.$name.'">'.$value.'</a></li>'.PHP_EOL;
}
?>
</ul>
</nav>
<?php } ?>
<div id="optionsContainer">
<?php
if ($tab == 'display') {
?>
<form name="optionsForm" method="get" action="?">
<input type="hidden" name="view" value="<?php echo $view ?>"/>
<input type="hidden" name="tab" value="<?php echo $tab ?>"/>
<!-- #optionsContainer is a flex row, so this has to live inside the
form rather than beside it, or the two share the width. -->
<div class="form-text text-muted mb-3"><?php echo translate('ThisSessionHint') ?></div>
<div class="col button-block">
<div id="contentButtons">
<button value="Save" type="submit"><?php echo translate('Save') ?></button>
</div>
</div>
<div class="form-group row">
<label for="skin" class="col-sm-3 col-form-label"><?php echo translate('Skin')?></label>
<div class="col-sm-6">
<select id="skin" name="skin" class="form-control chosen">
<?php
# Have to do this stuff up here before including header.php because of the cookie setting
$skin_options = array_map('basename', glob('skins/*', GLOB_ONLYDIR));
foreach ($skin_options as $dir) {
echo '<option value="'.$dir.'" '.($skin==$dir ? 'SELECTED="SELECTED"' : '').'>'.$dir.'</option>';
}
?>
</select>
<span class="form-text"><?php echo translate('SkinDescription'); ?></span>
</div>
</div>
<div class="form-group row">
<label for="css" class="col-sm-3 col-form-label">CSS</label>
<div class="col-sm-6">
<select id="css" name="css" class="form-control chosen">
<?php
foreach (array_map('basename', glob('skins/'.$skin.'/css/*', GLOB_ONLYDIR)) as $dir) {
echo '<option value="'.$dir.'" '.($css==$dir ? 'SELECTED="SELECTED"' : '').'>'.$dir.'</option>';
}
?>
</select>
<span class="form-text"><?php echo translate('CSSDescription'); ?></span>
</div>
</div>
<div class="form-group row">
<label for="navbar_type" class="col-sm-3 col-form-label"><?php echo translate('Navbar Type') ?></label>
<div class="col-sm-6">
<label for="navbar_type_normal">
<input type="radio" name="navbar_type" value="normal" id="navbar_type_normal" <?php echo $navbar_type == 'normal' ? 'checked="checked"' : '' ?>/>
<?php echo translate('Normal') ?>
</label>
<label for="navbar_type_collapsed">
<input type="radio" name="navbar_type" value="collapsed" id="navbar_type_collapsed" <?php echo $navbar_type == 'collapsed' ? 'checked="checked"' : '' ?>/>
<?php echo translate('Collapsed') ?>
</label>
<label for="navbar_type_left">
<input type="radio" name="navbar_type" value="left" id="navbar_type_left" <?php echo $navbar_type == 'left' ? 'checked="checked"' : '' ?>/>
<?php echo translate('Left') ?>
</label>
</div>
</div>
</form>
<?php
} else if ($tab == 'control') {
if (canView('Control')) {
include('_options_controlcaps.php');
} else {
$redirect = '?view=error';
// Have to do this
header('Location: '.$redirect);
}
} else if ($tab == 'privacy') {
if (canView('System')) {
$redirect = '?view=privacy';
} else {
$redirect = '?view=error';
}
// Have to do this
header('Location: '.$redirect);
} else if ($tab == 'groups') {
if (canView('Groups')) {
$redirect = '?view=groups';
} else {
$redirect = '?view=error';
}
// Have to do this
header('Location: '.$redirect);
} else if ($tab == 'cron') {
if (canView('System')) {
$redirect = '?view=cron';
} else {
$redirect = '?view=error';
}
// Have to do this
header('Location: '.$redirect);
} else if ($tab == 'servers') {
include('_options_servers.php');
} else if ($tab == 'storage') {
include('_options_storage.php');
} else if ($tab == 'dnsmasq' and file_exists('skins/classic/views/_options_dnsmasq.php')) {
include('_options_dnsmasq.php');
} else if ($tab == 'users') {
include('_options_users.php');
} else if ($tab == 'roles') {
include('_options_roles.php');
} else if ($tab == 'API') {
include('_options_api.php');
} else if ($tab == 'menu') {
include('_options_menu.php');
} else if ($tab == 'encoderTemplates') {
include('_options_encoderTemplates.php');
} else if ($tab == 'ai_datasets') {
include('_options_ai_datasets.php');
} else if ($tab == 'ai_models') {
include('_options_ai_models.php');
} else if ($tab == 'ai_classes') {
include('_options_ai_classes.php');
} // $tab == API/menu/encoderTemplates/ai_*
else {
$config = array();
$configCats = array();
$result = $dbConn->query('SELECT * FROM `Config` ORDER BY `Id` ASC');
if (!$result) {
echo mysql_error();
} else {
while ($row = dbFetchNext($result)) {
$config[$row['Name']] = $row;
if ( !($configCat = &$configCats[$row['Category']]) ) {
$configCats[$row['Category']] = array();
}
$configCats[$row['Category']][$row['Name']] = &$config[$row['Name']];
}
}
if ($tab == 'web') {
if (defined('ZM_FORCE_SKIN_DEFAULT'))
$configCats[$tab]['ZM_SKIN_DEFAULT']['Hint'] = ZM_FORCE_SKIN_DEFAULT;
else
$configCats[$tab]['ZM_SKIN_DEFAULT']['Hint'] = join('|', array_map('basename', glob('skins/*',GLOB_ONLYDIR)));
if (defined('ZM_FORCE_CSS_DEFAULT'))
$configCats[$tab]['ZM_CSS_DEFAULT']['Hint'] = ZM_FORCE_CSS_DEFAULT;
else
$configCats[$tab]['ZM_CSS_DEFAULT']['Hint'] = join('|', array_map ( 'basename', glob('skins/'.ZM_SKIN_DEFAULT.'/css/*',GLOB_ONLYDIR) ));
$configCats[$tab]['ZM_BANDWIDTH_DEFAULT']['Hint'] = $bandwidth_options;
// create new multidim array for languages (code1|translation)
$configCats[$tab]['ZM_LANG_DEFAULT']['Hint'] = getLanguages();
$configCats[$tab]['ZM_LOCALE_DEFAULT']['Hint'] = array(''=> translate('System Default'));
$locales = ResourceBundle::getLocales('');
if ($locales) {
foreach ( $locales as $locale) {
$configCats[$tab]['ZM_LOCALE_DEFAULT']['Hint'][$locale] = $locale;
}
}
$sound_files = array();
foreach (glob(ZM_DIR_SOUNDS.'/*') ?: array() as $sound_file) {
if (is_file($sound_file)) $sound_files[basename($sound_file)] = basename($sound_file);
}
$sound_value = $configCats[$tab]['ZM_WEB_ALARM_SOUND']['Value'];
if ($sound_files) {
// Keep a configured file that is no longer in the directory selectable
if ($sound_value != '' && !isset($sound_files[$sound_value])) $sound_files[$sound_value] = $sound_value;
$configCats[$tab]['ZM_WEB_ALARM_SOUND']['Hint'] = $sound_files;
}
$configCats[$tab]['ZM_WEB_HOMEVIEW']['Hint'] = [
'console'=>translate('Console'),
'events'=>'Events',
'map' => 'Map',
'montage'=>'Montage',
'montagereview'=>'Montage Review',
'watch' => 'Watch',
];
} else if ($tab == 'system') {
function timezone_list() {
static $timezones = null;
if ($timezones === null) {
$timezones = [];
$offsets = [];
$now = new DateTime('now', new DateTimeZone('UTC'));
foreach (DateTimeZone::listIdentifiers() as $timezone) {
$now->setTimezone(new DateTimeZone($timezone));
$offsets[] = $offset = $now->getOffset();
$timezones[$timezone] = '(' . format_GMT_offset($offset) . ') ' . format_timezone_name($timezone);
}
array_multisort($offsets, $timezones);
}
return $timezones;
}
function format_GMT_offset($offset) {
$hours = intval($offset / 3600);
$minutes = abs(intval($offset % 3600 / 60));
return 'GMT' . ($offset ? sprintf('%+03d:%02d', $hours, $minutes) : '');
}
function format_timezone_name($name) {
$name = str_replace('/', ', ', $name);
$name = str_replace('_', ' ', $name);
$name = str_replace('St ', 'St. ', $name);
return $name;
}
$configCats[$tab]['ZM_TIMEZONE']['Hint'] = array(''=> translate('TZUnset')) + timezone_list();
} # end if tab == system
?>
<form name="optionsForm" method="post" action="?">
<input type="hidden" name="view" value="<?php echo $view ?>"/>
<input type="hidden" name="tab" value="<?php echo $tab ?>"/>
<input type="hidden" name="action" value="options"/>
<div id="options">
<div class="col button-block">
<div id="contentButtons">
<button type="submit" <?php echo $canEdit?'':' disabled="disabled"' ?>><?php echo translate('Save') ?></button>
</div>
</div>
<div class="wrapper-scroll-table">
<?php
if (!isset($configCats[$tab])) {
echo 'There are no config entries for category '.$tab.'.<br/>';
} else {
// Options set in zm.conf or conf.d belong to the system owner and win over the database
// everywhere, so they are shown with the value in effect and can't be changed here.
// Their Config row is left as is.
$fileConfig = loadConfigFiles();
foreach ($configCats[$tab] as $name=>$value) {
$shortName = preg_replace( '/^ZM_/', '', $name );
$optionPromptText = !empty($OLANG[$shortName])?$OLANG[$shortName]['Prompt']:$value['Prompt'];
$fileSet = isset($fileConfig[$name]);
$optionCanEdit = $canEdit && !$value['System'] && !$fileSet;
$secret = !empty($value['Private']) || $value['Type'] == 'password';
// A Private value (ZM_AUTH_HASH_SECRET and other credentials) is only for
// those who may change it: System=View can read this page, and the auth
// secret alone is enough to sign an admin token. refs GHSA-7g3f-93q8-fhwj
$valueHidden = !empty($value['Private']) && !$canEdit;
if ($valueHidden) $value['Value'] = '';
// $value is this row's copy, so $config keeps the Config rows. A secret set in
// a file never reaches the browser: a placeholder stands in for it, so the masked input
// still shows as set.
if ($fileSet)
$value['Value'] = $secret ? '<set in config file>' : configFileValue($fileConfig[$name]['Value'], $value['Type']);
if ($optionCanEdit && !empty($value['Requires'])) {
// Requires can be compound: "ZM_A=1;ZM_B=hashed" (all must be true)
$requiresMet = true;
foreach (explode(';', $value['Requires']) as $req) {
$req = trim($req);
if (!preg_match('/^(ZM_\w+)=(.+)$/', $req, $reqMatch)) {
// Malformed requirement: treat as unmet
$requiresMet = false;
break;
}
$reqName = $reqMatch[1];
$expectedValue = $reqMatch[2];
// Against the value in effect: a file's value wins over the Config row.
$reqValue = isset($config[$reqName]) ? (isset($fileConfig[$reqName])
? configFileValue($fileConfig[$reqName]['Value'], $config[$reqName]['Type'])
: $config[$reqName]['Value']) : null;
if ($reqValue === null || $reqValue != $expectedValue) {
// Missing or mismatched requirement: treat as unmet
$requiresMet = false;
break;
}
}
if (!$requiresMet) {
$optionCanEdit = false;
}
}
?>
<div class="form-group form-row <?php echo $name ?>"<?php if ($canEdit && !$value['System'] && !$fileSet && !empty($value['Requires'])) echo ' data-requires="'.htmlspecialchars($value['Requires'], ENT_QUOTES).'"' ?>>
<label for="<?php echo $name ?>" class="col-md-4 control-label text-md-right"><?php echo $shortName ?></label>
<div class="col-md">
<?php
if ($value['Type'] == 'boolean') {
echo '<input type="checkbox" id="'.$name.'" name="newConfig['.$name.']" value="1"'.
( $value['Value'] ? ' checked="checked"' : '').
( $optionCanEdit ? '' : ' disabled="disabled"').' />'.PHP_EOL;
} else if (is_array($value['Hint'])) {
$attributes = ['id'=>$name, 'class'=>'form-control-sm chosen'];
if (!$optionCanEdit) $attributes['disabled']='disabled';
echo htmlSelect("newConfig[$name]", $value['Hint'], $value['Value'], $attributes);
} else if (preg_match('/\|/', $value['Hint'])) {
$options = explode('|', $value['Hint']);
if (count($options) > 3) {
$html_options = array();
foreach ($options as $option) {
if (preg_match('/^([^=]+)=(.+)$/', $option, $matches)) {
$html_options[$matches[2]] = $matches[1];
} else {
$html_options[$option] = $option;
}
}
$attributes = ['id'=>$name, 'class'=>'form-control-sm chosen'];
if (!$optionCanEdit) $attributes['disabled']='disabled';
echo htmlSelect("newConfig[$name]", $html_options, $value['Value'], $attributes);
} else {
foreach ($options as $option) {
if (preg_match('/^([^=]+)=(.+)$/', $option)) {
$optionLabel = $matches[1];
$optionValue = $matches[2];
} else {
$optionLabel = $optionValue = $option;
}
?>
<label class="font-weight-bold form-control-sm">
<input type="radio" id="<?php echo $name.'_'.preg_replace('/[^a-zA-Z0-9]/', '', $optionValue) ?>" name="newConfig[<?php echo $name ?>]" value="<?php echo $optionValue ?>"<?php if ( $value['Value'] == $optionValue ) { ?> checked="checked"<?php } ?><?php echo $optionCanEdit?'':' disabled="disabled"' ?>/>
<?php echo htmlspecialchars($optionLabel) ?>
</label>
<?php
} # end foreach option
} # end if count options > 3
} else if ( $value['Type'] == 'text' ) {
echo '<textarea class="form-control-sm" id="'.$name.'" name="newConfig['.$name.']" rows="5" cols="40"'.($optionCanEdit?'':' disabled="disabled"').'>'.validHtmlStr($value['Value']).'</textarea>'.PHP_EOL;
} else if ( $value['Type'] == 'integer' ) {
echo '<input type="number" class="form-control-sm" id="'.$name.'" name="newConfig['.$name.']" value="'.validHtmlStr($value['Value']).'" '.($optionCanEdit?'':' disabled="disabled"' ).' step="1"/>'.PHP_EOL;
} else if ( $value['Type'] == 'hexadecimal' ) {
echo '<input type="text" class="form-control-sm" id="'.$name.'" name="newConfig['.$name.']" value="'.validHtmlStr($value['Value']).'" '.($optionCanEdit?'':' disabled="disabled"' ).'/>'.PHP_EOL;
} else if ( $value['Type'] == 'decimal' ) {
echo '<input type="text" class="form-control-sm" id="'.$name.'" name="newConfig['.$name.']" value="'.validHtmlStr($value['Value']).'" '.($optionCanEdit?'':' disabled="disabled"' ).'/>'.PHP_EOL;
} else if ($secret) {
// Password options and Private ones such as ZM_AUTH_HASH_SECRET are masked, with a
// toggle to show the value; none for a file's placeholder, or for an empty value the
// user can't edit. new-password stops the browser filling in a saved login password,
// which a save would then store as this option.
echo '<input type="password" class="form-control-sm" id="'.$name.'" name="newConfig['.$name.']" value="'.validHtmlStr($value['Value']).'" autocomplete="new-password" '.($optionCanEdit?'':' disabled="disabled"' ).'/>'.PHP_EOL;
if (!$fileSet && ($optionCanEdit || $value['Value'] !== ''))
echo '<span class="material-icons md-18" data-on-click-this="toggle_password_visibility" data-password-input="'.$name.'">visibility</span>';
} else {
echo '<input type="text" class="form-control-sm" id="'.$name.'" name="newConfig['.$name.']" value="'.validHtmlStr($value['Value']).'" '.($optionCanEdit?'':' disabled="disabled"' ).'/>'.PHP_EOL;
}
if ($fileSet) {
$fileText = $secret ? 'OptionSetInConfigFileHidden' : 'OptionSetInConfigFile';
echo '<p class="form-text text-warning font-weight-bold optionfileset">'.validHtmlStr(sprintf(translate($fileText), $fileConfig[$name]['File'])).'</p>'.PHP_EOL;
} else if ($valueHidden) {
// One note: for a file-set secret, OptionSetInConfigFileHidden already says it isn't shown.
echo '<p class="form-text">'.translate('PrivateValueHidden').'</p>'.PHP_EOL;
}
?>
<span class="form-text"><?php echo validHtmlStr($optionPromptText); echo makeHelpLink($name) ?></span>
</div><!-- End .col-md -->
</div><!-- End .form-group -->
<?php
} # end foreach config entry in the category
} # end if category exists
?>
</div>
</div><!--#options-->
</form>
<script nonce="<?php echo $cspNonce ?>">
// Dynamic enable/disable of config fields based on data-requires attribute.
// Supports compound requires: "ZM_A=1;ZM_B=hashed" (all conditions must be met).
document.addEventListener('DOMContentLoaded', function() {
const $opts = $j('#options');
if (!$opts.length) return;
const $depRows = $opts.find('[data-requires]');
function getVal(name) {
const $el = $opts.find('[name="newConfig[' + name + ']"]');
if (!$el.length) return '';
if ($el.is(':checkbox')) return $el.is(':checked') ? '1' : '0';
if ($el.is(':radio')) return $el.filter(':checked').val() || '';
return $el.val();
}
function evalRequires(req) {
for (const chunk of req.split(';')) {
const part = chunk.trim();
if (!part) continue;
const m = part.match(/^(ZM_\w+)=(.+)$/);
if (!m) return false;
if (getVal(m[1]) !== m[2]) return false;
}
return true;
}
function refresh() {
$depRows.each(function() {
const $row = $j(this);
const met = evalRequires($row.attr('data-requires'));
$row.find('input, select, textarea').each(function() {
const $input = $j(this);
$input.prop('disabled', !met);
if ($input.is('select.chosen')) {
$input.trigger('chosen:updated');
}
});
});
}
$opts.on('change', refresh);
// Apply initial state so dependents match current values on first render.
refresh();
});
</script>
<?php
}
?>
</div><!-- end #optionsContainer -->
</div> <!-- end row -->
</div>
<?php xhtmlFooter() ?>