282 lines
6.1 KiB
C++
Executable File
282 lines
6.1 KiB
C++
Executable File
/*
|
|
* This file is a part of CMSLU -- Content Management System like Unix
|
|
* and is not publicly distributed
|
|
*
|
|
* Copyright (c) 2008-2009, Tomasz Sowa
|
|
* All rights reserved.
|
|
*
|
|
*/
|
|
|
|
#include "config.h"
|
|
#include "log.h"
|
|
#include "data.h"
|
|
#include "plugin.h"
|
|
#include "misc.h"
|
|
|
|
|
|
|
|
Config::Config()
|
|
{
|
|
default_str.clear();
|
|
default_int = 0;
|
|
default_bool = false;
|
|
|
|
errors_to_stdout = true;
|
|
}
|
|
|
|
|
|
|
|
//!! czy tu w ogole mozemy uzywac log << ?
|
|
//!! przeciez jeszcze nie zostal przetworzony plik konfiguracyjny
|
|
|
|
|
|
void Config::ShowError()
|
|
{
|
|
|
|
switch( conf_parser.status )
|
|
{
|
|
case ConfParser::ok:
|
|
log << log2 << "Config: syntax ok" << logend;
|
|
break;
|
|
|
|
case ConfParser::cant_open_file:
|
|
if( errors_to_stdout )
|
|
std::cout << "Config: cant open a config file: " << data.config_file << std::endl;
|
|
|
|
log << log1 << "Config: cant open a config file: " << data.config_file << logend;
|
|
break;
|
|
|
|
case ConfParser::syntax_error:
|
|
if( errors_to_stdout )
|
|
std::cout << "Config: syntax error, line: " << conf_parser.line << std::endl;
|
|
|
|
log << log1 << "Config: syntax error, line: " << conf_parser.line << logend;
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Config::ReadConfig(bool errors_to_stdout_)
|
|
{
|
|
errors_to_stdout = errors_to_stdout_;
|
|
|
|
if( data.config_file.empty() )
|
|
{
|
|
log << log2 << "Config: name of the config file is empty" << logend;
|
|
return false;
|
|
}
|
|
|
|
log << log2 << "Config: reading a config file" << logend;
|
|
|
|
ConfParser::Status status = conf_parser.Parse( data.config_file.c_str() );
|
|
|
|
|
|
if( status == ConfParser::ok )
|
|
{
|
|
AssignValues();
|
|
|
|
data.SetAdditionalVariables();
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
ShowError();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void Config::AssignValues()
|
|
{
|
|
data.log_file = Text("log_file");
|
|
data.log_notify_file = Text("log_notify_file");
|
|
data.fcgi_socket = Text("fcgi_socket");
|
|
data.fcgi_socket_chmod = Int("fcgi_socket_chmod", 0770);
|
|
data.fcgi_socket_user = Text("fcgi_socket_user");
|
|
data.fcgi_socket_group = Text("fcgi_socket_group");
|
|
data.log_level = Int("log_level", 1);
|
|
data.log_request = Int("log_request", 1);
|
|
|
|
if( !data.stdout_is_closed )
|
|
data.log_stdout = Bool("log_stdout", false);
|
|
else
|
|
data.log_stdout = false;
|
|
|
|
data.post_file_max = Int("post_file_max", 8388608); // 8 MB
|
|
data.static_auth_dir = Text("static_auth_dir");
|
|
data.static_auth_dir_tmp = Text("static_auth_dir_tmp");
|
|
|
|
data.templates_dir = Text("templates_dir");
|
|
data.templates_dir_default = Text("templates_dir_default");
|
|
data.http_session_id_name = Text("http_session_id_name");
|
|
data.db_database = Text("db_database");
|
|
data.db_user = Text("db_user");
|
|
data.db_pass = Text("db_pass");
|
|
data.item_url_empty = Text("item_url_empty");
|
|
|
|
data.base_server = Text("base_server");
|
|
data.base_url_prefix = Text("base_url_prefix");
|
|
data.base_url_static_prefix = Text("base_url_static_prefix");
|
|
data.base_url_static_ext_prefix = Text("base_url_static_ext_prefix");
|
|
data.base_url_static_auth_prefix = Text("base_url_static_auth_prefix");
|
|
|
|
NoLastSlash(data.base_server);
|
|
NoFirstHttp(data.base_server);
|
|
|
|
data.base_url = data.base_url_prefix + data.base_server;
|
|
data.base_url_static = data.base_url_static_prefix + data.base_server;
|
|
data.base_url_static_ext = data.base_url_static_ext_prefix + data.base_server;
|
|
data.base_url_static_auth = data.base_url_static_auth_prefix + data.base_server;
|
|
|
|
data.priv_no_user = Text("priv_no_user", "-- no user --");
|
|
data.priv_no_group = Text("priv_no_group", "-- no group --");
|
|
|
|
data.session_max_idle = Int("session_max_idle", 10800); // 3h
|
|
data.session_remember_max_idle = Int("session_remember_max_idle", 16070400); // 3 months
|
|
data.session_file = Text("session_file");
|
|
|
|
data.compression = Bool("compression", true);
|
|
|
|
std::string p = Text("plugin");
|
|
data.plugin_file.push_back(p);
|
|
|
|
data.html_filter = Bool("html_filter", true);
|
|
|
|
data.locale_str = Text("locale", "en");
|
|
data.locale_dir = Text("locale_dir");
|
|
data.locale_dir_default = Text("locale_dir_default");
|
|
|
|
Locale::Lang lang = Locale::StrToLang(data.locale_str);
|
|
|
|
if( lang != Locale::lang_unknown )
|
|
data.locale.SetLang(lang);
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::string & Config::Text(const char * name)
|
|
{
|
|
return Text(std::string(name), default_str);
|
|
}
|
|
|
|
|
|
|
|
const std::string & Config::Text(const char * name, const char * def)
|
|
{
|
|
return Text(std::string(name), std::string(def));
|
|
}
|
|
|
|
|
|
const std::string & Config::Text(const std::string & name, const std::string & def)
|
|
{
|
|
ConfParser::Table::iterator i = conf_parser.table.find(name);
|
|
|
|
if( i == conf_parser.table.end() )
|
|
return def;
|
|
|
|
return i->second;
|
|
}
|
|
|
|
|
|
|
|
int Config::Int(const char * name)
|
|
{
|
|
return Int(std::string(name), default_int);
|
|
}
|
|
|
|
|
|
int Config::Int(const char * name, int def)
|
|
{
|
|
return Int(std::string(name), def);
|
|
}
|
|
|
|
|
|
int Config::Int(const std::string & name, int def)
|
|
{
|
|
ConfParser::Table::iterator i = conf_parser.table.find(name);
|
|
|
|
if( i == conf_parser.table.end() || i->second.empty() )
|
|
return def;
|
|
|
|
long res = (i->second[0] == '0')? strtol(i->second.c_str() + 1, 0, 8) : strtol(i->second.c_str(), 0, 10);
|
|
|
|
return res;
|
|
}
|
|
|
|
|
|
bool Config::Bool(const char * name)
|
|
{
|
|
return Bool(std::string(name), default_bool);
|
|
}
|
|
|
|
|
|
bool Config::Bool(const char * name, bool def)
|
|
{
|
|
return Bool(std::string(name), def);
|
|
}
|
|
|
|
|
|
bool Config::Bool(const std::string & name, bool def)
|
|
{
|
|
ConfParser::Table::iterator i = conf_parser.table.find(name);
|
|
|
|
if( i == conf_parser.table.end() || i->second.empty() )
|
|
return def;
|
|
|
|
bool res = false;
|
|
|
|
if( EqualNoCase(i->second.c_str(), "true") ||
|
|
EqualNoCase(i->second.c_str(), "yes") ||
|
|
EqualNoCase(i->second.c_str(), "1")
|
|
)
|
|
res = true;
|
|
|
|
return res;
|
|
}
|
|
|
|
|
|
|
|
|
|
void Config::NoLastSlash(std::string & s)
|
|
{
|
|
if( s.empty() )
|
|
return;
|
|
|
|
if( *(--s.end()) == '/' )
|
|
s.erase(--s.end());
|
|
}
|
|
|
|
|
|
void Config::NoFirstHttp(std::string & s)
|
|
{
|
|
if( s.empty() )
|
|
return;
|
|
|
|
const char http[] = "http://";
|
|
const char https[] = "https://";
|
|
|
|
if( IsSubStringNoCase(http, s.c_str()) )
|
|
{
|
|
s.erase(0, sizeof(http)/sizeof(char));
|
|
}
|
|
else
|
|
if( IsSubStringNoCase(https, s.c_str()) )
|
|
{
|
|
s.erase(0, sizeof(https)/sizeof(char));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|