<?php
/*
$Id: config.php.in 579 2014-05-17 21:23:44Z bhockney $
(C) 2003-2014 by Bob Hockney <zeus@ix.netcom.com>

config file parser

This code is distributed under the terms of GNU GPL
*/

$conffile = "/usr/local/etc/webfwlog/webfwlog.conf";
if (!($fp = fopen("$conffile", "r"))) die("Cannot open config file $conffile");

While (!feof($fp)) {
	$buffer = fgets($fp, 1024);
	$cptr = strpos($buffer, "#");
	if ($cptr !== false and strlen($buffer) and $buffer = substr($buffer, 0, $cptr))
	if (!strlen($buffer)) continue;
	$params[] = explode("=", "$buffer", 2);
}
fclose($fp);

// Failsafe defaults

$config["db"] = "none";
$config["pgsql_wfwl_schema"] = "public";
$config["wfwl_syslog"] = "/usr/local/bin/wfwl_syslog";
$config["syslog_dir"] = "/var/log";
$config["syslog_filespec"] = "messages";
$config["verbosity_level"] = 2;
$config["default_data_source"] = "db";
$config["title"] = "Firewall Log Report";
$config["refresh"] = 0;
$config["dformat"] = "m/d/Y h:i:sA";
$config["timezone"] = "";
$config["report_order"] = "last_accessed";
$config["show_select_data_source"] = 0;
$config["allow_raw_sql"] = 0;
$config["allow_be_verbose"] = 0;
$config["debug"] = 0;

foreach ($params as $param) {
	switch (trim("{$param[0]}")) {
		case "db":
		case "mysql_server":
		case "mysql_user":
		case "mysql_pass":
		case "mysql_db":
		case "mysql_table":
		case "mysql_ulog_db":
		case "mysql_ulog_table":
		case "mysql_data_db":
		case "mysql_data_table":
		case "mysql_wfwl_db":
		case "pgsql_server":
		case "pgsql_user":
		case "pgsql_pass":
		case "pgsql_db":
		case "pgsql_table":
		case "pgsql_ulog_schema":
		case "pgsql_ulog_table":
		case "pgsql_data_schema":
		case "pgsql_data_table":
		case "pgsql_wfwl_schema":
		case "wfwl_syslog":
		case "syslog_dir":
		case "syslog_filespec":
		case "log_formats";
		case "verbosity_level":
		case "default_data_source":
		case "data_source":
		case "title":
		case "refresh":
		case "dformat":
		case "timezone":
		case "report_order":
		case "show_select_data_source":
		case "allow_show_sql":
		case "allow_additional_where":
		case "allow_raw_sql":
		case "allow_be_verbose":
		case "php_max_memory":
		case "php_timeout":
		case "php_update_timeout":
		case "session_save_path":
		case "debug":
			$config[trim("{$param[0]}")] = trim(trim("{$param[1]}"), "\"'");
			break;
		default:
			if (!substr(trim("{$param[0]}"), 0, 7) == "format.")
				break;
			$config[trim("{$param[0]}")] = trim(trim("{$param[1]}"), "\"'");
			break;
	}

}
unset ($params);

if (!isset($config["mysql_data_db"])) {
	if (isset($config["mysql_ulog_db"])) {
		$config["mysql_data_db"] = $config["mysql_ulog_db"];
	} elseif (isset($config["mysql_db"])) {
		$config["mysql_data_db"] = $config["mysql_db"];
	} else 
		$config["mysql_data_db"] = "";
}
if (!isset($config["mysql_data_table"])) {
	if(isset($config["mysql_table"])) {
		$config["mysql_data_table"] = $config["mysql_table"];
	} elseif (isset($config["mysql_table"])) {
		$config["mysql_data_table"] = $config["mysql_table"];
	} else
		$config["mysql_data_table"] = "ulog";
}
if (!isset($config["mysql_wfwl_db"]) and isset($config["mysql_db"])) {
	$config["mysql_wfwl_db"] = $config["mysql_db"];
} elseif (!isset($config["mysql_wfwl_db"])) {
	$config["mysql_wfwl_db"] = "";
}
if (!isset($config["pgsql_data_table"])) {
	if (isset($config["pgsql_ulog_table"])) {
		$config["pgsql_data_table"] = $config["pgsql_ulog_table"];
	} elseif (isset($config["pgsql_table"])) {
		$config["pgsql_data_table"] = $config["pgsql_table"];
	} else
		$config["pgsql_data_table"] = "ulog";
}
if (!isset($config["pgsql_data_schema"]) and isset($config["pgsql_ulog_schema"])) {
	$config["pgsql_data_schema"] = $config["pgsql_ulog_schema"];
} elseif (!isset($config["pgsql_data_schema"])) {
	$config["pgsql_data_schema"] = "public";
}

$config["have_cast"] = 0;

switch ("{$config["db"]}") {
	case "mysql":
		$database = $config["mysql_wfwl_db"];
		$data_d = "{$config["mysql_data_db"]}";
		foreach (preg_split("/ +/","{$config["mysql_data_table"]}") as $v)
			$data_t[] = "$data_d.$v";
		$reports = "{$config["mysql_wfwl_db"]}.reports";
		$hostnames = "{$config["mysql_wfwl_db"]}.hostnames";
		$services = "{$config["mysql_wfwl_db"]}.services";
		$update = "{$config["mysql_wfwl_db"]}.tmp_update";
		$tmp_output = "{$config["mysql_wfwl_db"]}.tmp_output";
		$allrows = "{$config["mysql_wfwl_db"]}.allrows";
		$output = "{$config["mysql_wfwl_db"]}.output";
		$q = "`";
		$regexp = "REGEXP";
		$inet_aton="inet_aton";
		$inet_ntoa="inet_ntoa";
		$inet_pton="wfwl_inet_pton";
		$inet_ntop="wfwl_inet_ntop";
		$inet_contains="wfwl_inet_contains";
		$true = "1";
		$config['cast_int'] = "UNSIGNED";
		$config['tcast'] = "";
		break;
	case "pgsql":
		$database = $config["pgsql_db"];
		$data_d = "{$config["pgsql_data_schema"]}";
		$allrows = "allrows";
		$output = "output";
		$tmp_output = "tmp_output";
		$q = "\"";
		$regexp = "~";
		$true = "TRUE";
		$config["have_cast"] = 1;
		$config['cast_int'] = "INTEGER";
		$config['cast_char'] = "char(32)";
		$config['tcast'] = "::text";
		$inet_pton="";
		$inet_ntop="";
		$inet_contains="";
		break;
	default:
		$database = "none";
		$data_d = "";
		$data_t = "";
		$reports = "";
		$hostnames = "";
		$services = "";
		$tmp_output = "";
		$output = "";
		$inet_aton = "";
		$inet_pton="";
		$inet_ntop="";
		$inet_contains="";
		$true = "1";
		break;
}

 // show_sql is deprecated
if (isset($config["allow_show_sql"])) {
	$config["allow_be_verbose"] = "{$config["allow_show_sql"]}";
	unset ($config["allow_show_sql"]);
}

 // allow_additional_where is deprecated
if (isset($config["allow_additional_where"])) {
	$config["allow_raw_sql"] = "{$config["allow_additional_where"]}";
	unset ($config["allow_additional_where"]);
}

if (isset($config["data_source"])) {
	$config["default_data_source"] = "{$config["data_source"]}";
	unset ($config["data_source"]);
}

if (isset($config["php_max_memory"])) {
	ini_set("memory_limit", "{$config["php_max_memory"]}");
}

if (isset($config["php_timeout"])) {
	set_time_limit("{$config["php_timeout"]}");
}

$webfwlog_version = "1.00";

// vim:set ts=4:

?>
