2003-11-05 21:50:08 +08:00
|
|
|
//
|
|
|
|
// ZoneMinder Configuration Implementation, $Date$, $Revision$
|
2008-07-25 17:33:23 +08:00
|
|
|
// Copyright (C) 2001-2008 Philip Coombes
|
2003-11-05 21:50:08 +08:00
|
|
|
//
|
|
|
|
// 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "zm.h"
|
|
|
|
#include "zm_db.h"
|
2004-12-29 00:46:48 +08:00
|
|
|
|
2011-06-21 17:19:10 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2015-07-16 22:04:56 +08:00
|
|
|
#include "zm_utils.h"
|
|
|
|
|
2004-12-29 00:46:48 +08:00
|
|
|
void zmLoadConfig()
|
|
|
|
{
|
|
|
|
FILE *cfg;
|
2005-01-27 04:51:35 +08:00
|
|
|
char line[512];
|
2005-12-16 18:13:59 +08:00
|
|
|
if ( (cfg = fopen( ZM_CONFIG, "r")) == NULL )
|
2004-12-29 00:46:48 +08:00
|
|
|
{
|
2011-06-21 17:19:10 +08:00
|
|
|
Fatal( "Can't open %s: %s", ZM_CONFIG, strerror(errno) );
|
2004-12-29 00:46:48 +08:00
|
|
|
}
|
2005-01-27 04:51:35 +08:00
|
|
|
while ( fgets( line, sizeof(line), cfg ) != NULL )
|
2004-12-29 00:46:48 +08:00
|
|
|
{
|
2005-01-27 04:51:35 +08:00
|
|
|
char *line_ptr = line;
|
|
|
|
|
|
|
|
// Trim off any cr/lf line endings
|
|
|
|
int chomp_len = strcspn( line_ptr, "\r\n" );
|
|
|
|
line_ptr[chomp_len] = '\0';
|
|
|
|
|
|
|
|
// Remove leading white space
|
|
|
|
int white_len = strspn( line_ptr, " \t" );
|
|
|
|
line_ptr += white_len;
|
|
|
|
|
|
|
|
// Check for comment or empty line
|
|
|
|
if ( *line_ptr == '\0' || *line_ptr == '#' )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Remove trailing white space
|
|
|
|
char *temp_ptr = line_ptr+strlen(line_ptr)-1;
|
|
|
|
while ( *temp_ptr == ' ' || *temp_ptr == '\t' )
|
|
|
|
{
|
2005-12-16 18:13:59 +08:00
|
|
|
*temp_ptr-- = '\0';
|
2005-01-27 04:51:35 +08:00
|
|
|
temp_ptr--;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now look for the '=' in the middle of the line
|
|
|
|
temp_ptr = strchr( line_ptr, '=' );
|
|
|
|
if ( !temp_ptr )
|
2004-12-29 00:46:48 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Warning( "Invalid data in %s: '%s'", ZM_CONFIG, line );
|
2004-12-29 00:46:48 +08:00
|
|
|
continue;
|
|
|
|
}
|
2005-01-27 04:51:35 +08:00
|
|
|
|
|
|
|
// Assign the name and value parts
|
|
|
|
char *name_ptr = line_ptr;
|
|
|
|
char *val_ptr = temp_ptr+1;
|
|
|
|
|
|
|
|
// Trim trailing space from the name part
|
|
|
|
do
|
|
|
|
{
|
|
|
|
*temp_ptr = '\0';
|
|
|
|
temp_ptr--;
|
|
|
|
}
|
|
|
|
while ( *temp_ptr == ' ' || *temp_ptr == '\t' );
|
|
|
|
|
|
|
|
// Remove leading white space from the value part
|
|
|
|
white_len = strspn( val_ptr, " \t" );
|
|
|
|
val_ptr += white_len;
|
|
|
|
|
2008-07-25 01:08:50 +08:00
|
|
|
if ( strcasecmp( name_ptr, "ZM_DB_HOST" ) == 0 )
|
2014-11-15 20:32:43 +08:00
|
|
|
staticConfig.DB_HOST = std::string(val_ptr);
|
2008-07-25 01:08:50 +08:00
|
|
|
else if ( strcasecmp( name_ptr, "ZM_DB_NAME" ) == 0 )
|
2014-11-15 20:32:43 +08:00
|
|
|
staticConfig.DB_NAME = std::string(val_ptr);
|
2008-07-25 01:08:50 +08:00
|
|
|
else if ( strcasecmp( name_ptr, "ZM_DB_USER" ) == 0 )
|
2014-11-15 20:32:43 +08:00
|
|
|
staticConfig.DB_USER = std::string(val_ptr);
|
2008-07-25 01:08:50 +08:00
|
|
|
else if ( strcasecmp( name_ptr, "ZM_DB_PASS" ) == 0 )
|
2014-11-15 20:32:43 +08:00
|
|
|
staticConfig.DB_PASS = std::string(val_ptr);
|
2008-07-25 01:08:50 +08:00
|
|
|
else if ( strcasecmp( name_ptr, "ZM_PATH_WEB" ) == 0 )
|
2014-11-15 20:32:43 +08:00
|
|
|
staticConfig.PATH_WEB = std::string(val_ptr);
|
2015-07-16 04:44:16 +08:00
|
|
|
else if ( strcasecmp( name_ptr, "ZM_SERVER_HOST" ) == 0 )
|
|
|
|
staticConfig.SERVER_NAME = std::string(val_ptr);
|
|
|
|
else if ( strcasecmp( name_ptr, "ZM_SERVER_NAME" ) == 0 )
|
|
|
|
staticConfig.SERVER_NAME = std::string(val_ptr);
|
|
|
|
else if ( strcasecmp( name_ptr, "ZM_SERVER_ID" ) == 0 )
|
|
|
|
staticConfig.SERVER_ID = atoi(val_ptr);
|
2004-12-29 00:46:48 +08:00
|
|
|
else
|
|
|
|
{
|
2005-12-16 18:13:59 +08:00
|
|
|
// We ignore this now as there may be more parameters than the
|
|
|
|
// c/c++ binaries are bothered about
|
2008-07-14 22:43:47 +08:00
|
|
|
// Warning( "Invalid parameter '%s' in %s", name_ptr, ZM_CONFIG );
|
2004-12-29 00:46:48 +08:00
|
|
|
}
|
2015-07-16 04:44:16 +08:00
|
|
|
} // end foreach line of the config
|
|
|
|
fclose( cfg );
|
2005-01-16 01:45:51 +08:00
|
|
|
zmDbConnect();
|
2005-01-16 01:27:03 +08:00
|
|
|
config.Load();
|
2005-05-16 17:27:06 +08:00
|
|
|
config.Assign();
|
2015-07-16 04:44:16 +08:00
|
|
|
|
|
|
|
// Populate the server config entries
|
|
|
|
if ( ! staticConfig.SERVER_ID ) {
|
2015-07-16 22:04:56 +08:00
|
|
|
if ( ! staticConfig.SERVER_NAME.empty() ) {
|
2015-07-16 04:44:16 +08:00
|
|
|
|
2015-07-16 22:04:56 +08:00
|
|
|
std::string sql = stringtf("SELECT Id FROM Servers WHERE Name='%s'", staticConfig.SERVER_NAME.c_str() );
|
2015-07-16 04:44:16 +08:00
|
|
|
if ( mysql_query( &dbconn, sql.c_str() ) )
|
|
|
|
{
|
|
|
|
Error( "Can't run query: %s", mysql_error( &dbconn ) );
|
2015-07-16 22:04:56 +08:00
|
|
|
Fatal("Can't get ServerId for Server %s", staticConfig.SERVER_NAME.c_str() );
|
2015-07-16 04:44:16 +08:00
|
|
|
}
|
|
|
|
MYSQL_RES *result = mysql_store_result( &dbconn );
|
|
|
|
if ( !result )
|
|
|
|
{
|
|
|
|
Error( "Can't use query result: %s", mysql_error( &dbconn ) );
|
2015-07-16 22:04:56 +08:00
|
|
|
Fatal("Can't get ServerId for Server %s", staticConfig.SERVER_NAME.c_str() );
|
2015-07-16 04:44:16 +08:00
|
|
|
}
|
|
|
|
int n_rows = mysql_num_rows( result );
|
|
|
|
if ( n_rows != 1 )
|
|
|
|
{
|
|
|
|
Error( "Bogus number of lines return from Server lookup, %d, returned. Can't reload", n_rows );
|
2015-07-16 22:04:56 +08:00
|
|
|
Fatal("Can't get ServerId for Server %s", staticConfig.SERVER_NAME.c_str() );
|
2015-07-16 04:44:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( MYSQL_ROW dbrow = mysql_fetch_row( result ) )
|
|
|
|
{
|
|
|
|
staticConfig.SERVER_ID = atoi(dbrow[0]);
|
|
|
|
}
|
|
|
|
mysql_free_result( result );
|
|
|
|
|
|
|
|
} // end if has SERVER_NAME
|
2015-07-16 22:04:56 +08:00
|
|
|
} else if ( staticConfig.SERVER_NAME.empty() ) {
|
2015-07-16 04:44:16 +08:00
|
|
|
std::string sql = stringtf("SELECT Name FROM Servers WHERE Id='%d'", staticConfig.SERVER_ID );
|
|
|
|
if ( mysql_query( &dbconn, sql.c_str() ) )
|
|
|
|
{
|
|
|
|
Error( "Can't run query: %s", mysql_error( &dbconn ) );
|
|
|
|
Fatal("Can't get ServerName for Server ID %d", staticConfig.SERVER_ID );
|
|
|
|
}
|
|
|
|
|
|
|
|
MYSQL_RES *result = mysql_store_result( &dbconn );
|
|
|
|
if ( !result )
|
|
|
|
{
|
|
|
|
Error( "Can't use query result: %s", mysql_error( &dbconn ) );
|
|
|
|
Fatal("Can't get ServerName for Server ID %d", staticConfig.SERVER_ID );
|
|
|
|
}
|
|
|
|
int n_rows = mysql_num_rows( result );
|
|
|
|
if ( n_rows != 1 )
|
|
|
|
{
|
|
|
|
Error( "Bogus number of lines return from Server lookup, %d, returned. Can't reload", n_rows );
|
|
|
|
Fatal("Can't get ServerName for Server ID %d", staticConfig.SERVER_ID );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( MYSQL_ROW dbrow = mysql_fetch_row( result ) )
|
|
|
|
{
|
|
|
|
staticConfig.SERVER_NAME = std::string(dbrow[0]);
|
|
|
|
}
|
|
|
|
mysql_free_result( result );
|
|
|
|
|
|
|
|
}
|
2004-12-29 00:46:48 +08:00
|
|
|
}
|
2003-11-05 21:50:08 +08:00
|
|
|
|
2008-07-25 01:12:53 +08:00
|
|
|
StaticConfig staticConfig;
|
|
|
|
|
2003-11-05 21:50:08 +08:00
|
|
|
ConfigItem::ConfigItem( const char *p_name, const char *p_value, const char *const p_type )
|
|
|
|
{
|
|
|
|
name = new char[strlen(p_name)+1];
|
|
|
|
strcpy( name, p_name );
|
|
|
|
value = new char[strlen(p_value)+1];
|
|
|
|
strcpy( value, p_value );
|
|
|
|
type = new char[strlen(p_type)+1];
|
|
|
|
strcpy( type, p_type );
|
|
|
|
|
2008-07-14 22:43:47 +08:00
|
|
|
//Info( "Created new config item %s = %s (%s)\n", name, value, type );
|
2003-11-05 21:50:08 +08:00
|
|
|
|
|
|
|
accessed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConfigItem::~ConfigItem()
|
|
|
|
{
|
|
|
|
delete[] name;
|
|
|
|
delete[] value;
|
|
|
|
delete[] type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigItem::ConvertValue() const
|
|
|
|
{
|
|
|
|
if ( !strcmp( type, "boolean" ) )
|
|
|
|
{
|
|
|
|
cfg_type = CFG_BOOLEAN;
|
2004-01-15 19:36:42 +08:00
|
|
|
cfg_value.boolean_value = (bool)strtol( value, 0, 0 );
|
2003-11-05 21:50:08 +08:00
|
|
|
}
|
|
|
|
else if ( !strcmp( type, "integer" ) )
|
|
|
|
{
|
|
|
|
cfg_type = CFG_INTEGER;
|
|
|
|
cfg_value.integer_value = strtol( value, 0, 10 );
|
|
|
|
}
|
|
|
|
else if ( !strcmp( type, "hexadecimal" ) )
|
|
|
|
{
|
|
|
|
cfg_type = CFG_INTEGER;
|
|
|
|
cfg_value.integer_value = strtol( value, 0, 16 );
|
|
|
|
}
|
|
|
|
else if ( !strcmp( type, "decimal" ) )
|
|
|
|
{
|
|
|
|
cfg_type = CFG_DECIMAL;
|
2004-01-15 05:26:47 +08:00
|
|
|
cfg_value.decimal_value = strtod( value, 0 );
|
2003-11-05 21:50:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cfg_type = CFG_STRING;
|
|
|
|
cfg_value.string_value = value;
|
|
|
|
}
|
|
|
|
accessed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConfigItem::BooleanValue() const
|
|
|
|
{
|
|
|
|
if ( !accessed )
|
|
|
|
ConvertValue();
|
|
|
|
|
|
|
|
if ( cfg_type != CFG_BOOLEAN )
|
2005-12-02 18:15:40 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Attempt to fetch boolean value for %s, actual type is %s. Try running 'zmupdate.pl -f' to reload config.", name, type );
|
2005-12-02 18:15:40 +08:00
|
|
|
exit( -1 );
|
|
|
|
}
|
2003-11-05 21:50:08 +08:00
|
|
|
|
|
|
|
return( cfg_value.boolean_value );
|
|
|
|
}
|
|
|
|
|
|
|
|
int ConfigItem::IntegerValue() const
|
|
|
|
{
|
|
|
|
if ( !accessed )
|
|
|
|
ConvertValue();
|
|
|
|
|
|
|
|
if ( cfg_type != CFG_INTEGER )
|
2005-12-02 18:15:40 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Attempt to fetch integer value for %s, actual type is %s. Try running 'zmupdate.pl -f' to reload config.", name, type );
|
2005-12-02 18:15:40 +08:00
|
|
|
exit( -1 );
|
|
|
|
}
|
2003-11-05 21:50:08 +08:00
|
|
|
|
|
|
|
return( cfg_value.integer_value );
|
|
|
|
}
|
|
|
|
|
|
|
|
double ConfigItem::DecimalValue() const
|
|
|
|
{
|
|
|
|
if ( !accessed )
|
|
|
|
ConvertValue();
|
|
|
|
|
|
|
|
if ( cfg_type != CFG_DECIMAL )
|
2005-12-02 18:15:40 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Attempt to fetch decimal value for %s, actual type is %s. Try running 'zmupdate.pl -f' to reload config.", name, type );
|
2005-12-02 18:15:40 +08:00
|
|
|
exit( -1 );
|
|
|
|
}
|
2003-11-05 21:50:08 +08:00
|
|
|
|
|
|
|
return( cfg_value.decimal_value );
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ConfigItem::StringValue() const
|
|
|
|
{
|
|
|
|
if ( !accessed )
|
|
|
|
ConvertValue();
|
|
|
|
|
|
|
|
if ( cfg_type != CFG_STRING )
|
2005-12-02 18:15:40 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Attempt to fetch string value for %s, actual type is %s. Try running 'zmupdate.pl -f' to reload config.", name, type );
|
2005-12-02 18:15:40 +08:00
|
|
|
exit( -1 );
|
|
|
|
}
|
2003-11-05 21:50:08 +08:00
|
|
|
|
|
|
|
return( cfg_value.string_value );
|
|
|
|
}
|
|
|
|
|
|
|
|
Config::Config()
|
|
|
|
{
|
|
|
|
n_items = 0;
|
|
|
|
items = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Config::~Config()
|
|
|
|
{
|
2005-12-02 18:15:40 +08:00
|
|
|
if ( items )
|
2003-11-05 21:50:08 +08:00
|
|
|
{
|
2005-12-02 18:15:40 +08:00
|
|
|
for ( int i = 0; i < n_items; i++ )
|
|
|
|
{
|
|
|
|
delete items[i];
|
|
|
|
}
|
|
|
|
delete[] items;
|
2003-11-05 21:50:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::Load()
|
|
|
|
{
|
2010-11-11 20:22:35 +08:00
|
|
|
static char sql[ZM_SQL_SML_BUFSIZ];
|
|
|
|
|
|
|
|
strncpy( sql, "select Name, Value, Type from Config order by Id", sizeof(sql) );
|
2003-11-05 21:50:08 +08:00
|
|
|
if ( mysql_query( &dbconn, sql ) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't run query: %s", mysql_error( &dbconn ) );
|
2003-11-05 21:50:08 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
MYSQL_RES *result = mysql_store_result( &dbconn );
|
|
|
|
if ( !result )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't use query result: %s", mysql_error( &dbconn ) );
|
2003-11-05 21:50:08 +08:00
|
|
|
exit( mysql_errno( &dbconn ) );
|
|
|
|
}
|
|
|
|
n_items = mysql_num_rows( result );
|
2005-12-02 18:15:40 +08:00
|
|
|
|
|
|
|
if ( n_items <= ZM_MAX_CFG_ID )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Config mismatch, expected %d items, read %d. Try running 'zmupdate.pl -f' to reload config.", ZM_MAX_CFG_ID+1, n_items );
|
2005-12-02 18:15:40 +08:00
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
|
2003-11-05 21:50:08 +08:00
|
|
|
items = new ConfigItem *[n_items];
|
|
|
|
for( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row( result ); i++ )
|
|
|
|
{
|
|
|
|
items[i] = new ConfigItem( dbrow[0], dbrow[1], dbrow[2] );
|
|
|
|
}
|
2005-11-30 01:51:06 +08:00
|
|
|
mysql_free_result( result );
|
2003-11-05 21:50:08 +08:00
|
|
|
}
|
|
|
|
|
2005-05-16 17:27:06 +08:00
|
|
|
void Config::Assign()
|
|
|
|
{
|
|
|
|
ZM_CFG_ASSIGN_LIST
|
|
|
|
}
|
|
|
|
|
2003-11-05 21:50:08 +08:00
|
|
|
const ConfigItem &Config::Item( int id )
|
|
|
|
{
|
|
|
|
if ( !n_items )
|
|
|
|
{
|
|
|
|
Load();
|
2005-10-20 19:47:03 +08:00
|
|
|
Assign();
|
2003-11-05 21:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( id < 0 || id > ZM_MAX_CFG_ID )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Attempt to access invalid config, id = %d. Try running 'zmupdate.pl -f' to reload config.", id );
|
2003-11-05 21:50:08 +08:00
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
ConfigItem *item = items[id];
|
|
|
|
|
|
|
|
if ( !item )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Can't find config item %d", id );
|
2003-11-05 21:50:08 +08:00
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( *item );
|
|
|
|
}
|
|
|
|
|
|
|
|
Config config;
|