initialise(); // // This section is options normally derived from other options or configuration // define('ZMU_PATH', ZM_PATH_BIN.'/zmu'); // Local path to the ZoneMinder Utility $locale = ZM_LOCALE_DEFAULT; $locales = ResourceBundle::getLocales(''); if ($locale) { if (!array_search($locale, $locales)) { ZM\Warning("Locale $locale does not seem to be valid."); $locale = locale_get_default(); } } else { #ZM\Debug('Using system locale'); $locale = locale_get_default(); } try { if (ZM_TIMEZONE) { $dateFormatter = new IntlDateFormatter($locale, IntlDateFormatter::SHORT, IntlDateFormatter::NONE, ZM_TIMEZONE); $dateTimeFormatter = new IntlDateFormatter($locale, IntlDateFormatter::SHORT, IntlDateFormatter::LONG, ZM_TIMEZONE); $timeFormatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, IntlDateFormatter::LONG, ZM_TIMEZONE); } else { $dateFormatter = new IntlDateFormatter($locale, IntlDateFormatter::SHORT, IntlDateFormatter::NONE); $dateTimeFormatter = new IntlDateFormatter($locale, IntlDateFormatter::SHORT, IntlDateFormatter::LONG); $timeFormatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, IntlDateFormatter::LONG); } } catch(Exception $e) { error_log($e->getMessage()); $dateFormatter = new IntlDateFormatter(null, IntlDateFormatter::SHORT, IntlDateFormatter::NONE); $dateTimeFormatter = new IntlDateFormatter(null, IntlDateFormatter::SHORT, IntlDateFormatter::LONG); $timeFormatter = new IntlDateFormatter(null, IntlDateFormatter::NONE, IntlDateFormatter::LONG); } try { # PHP 8.1.26 made these throw an exception if locale is invalid so have to try if (ZM_DATE_FORMAT_PATTERN) { $dateFormatter->setPattern(ZM_DATE_FORMAT_PATTERN); } if (ZM_DATETIME_FORMAT_PATTERN) { $dateTimeFormatter->setPattern(ZM_DATETIME_FORMAT_PATTERN); } if (ZM_TIME_FORMAT_PATTERN) { $timeFormatter->setPattern(ZM_TIME_FORMAT_PATTERN); } } catch(\Error $e) { error_log($e->getMessage()); } $GLOBALS['defaultUser'] = array( 'Id' => 0, 'Username' => 'admin', 'Password' => '', 'Language' => '', 'Enabled' => 1, 'Stream' => 'View', 'Events' => 'Edit', 'Control' => 'Edit', 'Monitors' => 'Create', 'Groups' => 'Edit', 'Devices' => 'Edit', 'Snapshots' => 'Edit', 'System' => 'Edit', 'MaxBandwidth' => '', 'HomeView' => '', 'MonitorIds' => false ); function loadConfig( $defineConsts=true ) { $configFile = ZM_CONFIG; $localConfigFile = basename($configFile); if ( file_exists($localConfigFile) && filesize($localConfigFile) > 0 ) { if ( php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR']) ) print("Warning, overriding installed $localConfigFile file with local copy\n"); else error_log("Warning, overriding installed $localConfigFile file with local copy"); $configFile = $localConfigFile; } # Process name, value pairs from the main config file first $initial_config = process_configfile($configFile); if (!$initial_config) $initial_config = []; # Search for user created config files. If one or more are found then # update our config value array with those values if ( is_dir(ZM_CONFIG_SUBDIR) ) { if ( is_readable(ZM_CONFIG_SUBDIR) ) { foreach ( glob(ZM_CONFIG_SUBDIR.'/*.conf') as $filename ) { $newconfig = process_configfile($filename); if ($newconfig) $initial_config = array_merge($initial_config, $newconfig); } } else { error_log('WARNING: ZoneMinder configuration subfolder found but is not readable. Check folder permissions on '.$configSubFolder); } } else { error_log('WARNING: ZoneMinder configuration subfolder found but is not a directory. Check '.$configSubFolder); } # Need initial config defines in order to connect to db # Now that our array our finalized, define each key => value # pair in the array as a constant if ($defineConsts) { foreach ( $initial_config as $key => $config ) { define($key, $config['Value']); } } # Must have DB defines in place before including database.php require_once('database.php'); require_once('logger.php'); $config = []; global $dbConn; $result = $dbConn->query('SELECT Name,Value,Type,Private FROM Config'); if ( !$result ) echo mysql_error(); while( $row = dbFetchNext($result) ) { $name = $row['Name']; $config[$name] = $row; if (isset($initial_config[$name])) { $config[$name]['Value'] = $initial_config[$name]['Value']; unset($initial_config[$name]); } if ( $defineConsts ) { # Values in conf.d files override db so check if already defined and update value if (!defined($name)) { define($name, $row['Value']); } else { $config[$name]['Value'] = constant($name); } } } return array_merge($config, $initial_config); # Merge any remaining config not in db } # end function loadConfig require_once('Server.php'); global $Servers; # Ensure that it is global, api doesn't specifically make it so $Servers = ZM\Server::find([], ['order'=>'lower(Name)']); $thisServer = new ZM\Server(); // For Human-readability, use ZM_SERVER_HOST or ZM_SERVER_NAME in zm.conf, and convert it here to a ZM_SERVER_ID if (!defined('ZM_SERVER_ID')) { if (defined('ZM_SERVER_NAME') and ZM_SERVER_NAME) { foreach ($Servers as $s) { if ($s->Name() == ZM_SERVER_NAME) { $thisServer = $s; define('ZM_SERVER_ID', $Server->Id()); } } if (!$thisServer) { error_log('Invalid Multi-Server configration detected. ZM_SERVER_NAME set to ' . ZM_SERVER_NAME . ' in zm.conf, but no corresponding entry found in Servers table.'); } } else if (defined('ZM_SERVER_HOST') and ZM_SERVER_HOST) { foreach ($Servers as $s) { if ($s->Name() == ZM_SERVER_HOST) { $thisServer = $s; define('ZM_SERVER_ID', $s->Id()); } } if (!$thisServer) { error_log('Invalid Multi-Server configration detected. ZM_SERVER_HOST set to ' . ZM_SERVER_HOST . ' in zm.conf, but no corresponding entry found in Servers table.'); } } } else { $thisServer = ZM\Server::find_one(['Id'=>ZM_SERVER_ID]); // Should be cached if(!$thisServer) { error_log('No server found for ZM_SERVER_ID '+ZM_SERVER_ID); } } if ( defined('ZM_TIMEZONE') and ZM_TIMEZONE ) ini_set('date.timezone', ZM_TIMEZONE); function process_configfile($configFile) { $config = array(); if (is_readable($configFile)) { $cfg = fopen($configFile, 'r') or error_log('Could not open config file: '.$configFile); while ( !feof($cfg) ) { $str = fgets($cfg, 256); if ( preg_match('/^\s*(#.*)?$/', $str) ) { continue; } else if ( preg_match('/^\s*([^=\s]+)\s*=\s*[\'"]*(.*?)[\'"]*\s*$/', $str, $matches) ) { $config[$matches[1]] = ['Name'=>$matches[1], 'Value'=>$matches[2]]; } else { error_log("Malformed line in config $configFile\n$str"); } } fclose($cfg); } else { error_log('WARNING: ZoneMinder configuration file found but is not readable. Check file permissions on '.$configFile); } return $config; } ?>