0600, 'timeFormat' => '%X %x') , constant(_M2F_LOG_LEVEL_));
if (!$file_log)
{
m2f_display_error("[" . __FILE__ . " : " . __LINE__ . "] Could not create log in the file at " . _M2F_LOG_FILE_);
die;
}
}
// DATABASE:
if (!(DB::isError($m2f_db)) && ((_M2F_LOG_METHOD_ == 'DATABASE') || (_M2F_LOG_METHOD_ == 'BOTH')))
{
$sql_log = &Log::singleton('sql', _M2F_LOG_TABLE_, '[M2F]', array('db' => $m2f_db, 'sequence' => _M2F_LOG_TABLE_), constant(_M2F_LOG_LEVEL_));
if (!$sql_log)
{
m2f_display_error("[" . __FILE__ . " : " . __LINE__ . "] Could not create database log");
die;
}
}
// Add them together
$m2f_log = &Log::singleton('composite');
if (@$file_log) $m2f_log->addChild($file_log);
if (@$sql_log) $m2f_log->addChild($sql_log);
// If there's been a database error, we can still write the error to the FILE log if it exists
if (DB::isError($m2f_db))
{
$m2f_error_msg = 'Could not connect to the database:
'.$m2f_db->getMessage() . '';
m2f_log(PEAR_LOG_CRIT , $m2f_error_msg , __LINE__ , __FILE__ );
die;
}
// Report on Log creation
m2f_log(PEAR_LOG_INFO , "Log object instantiated" , __LINE__ , __FILE__ );
// Report on database connection
m2f_log(PEAR_LOG_INFO , "Connected to database" , __LINE__ , __FILE__ );
// Set default DB fetch mode to associative
$m2f_db->setFetchMode(DB_FETCHMODE_ASSOC);
// Load configuration parameters
m2f_log(PEAR_LOG_INFO , "Attempting to load config parameters" , __LINE__ , __FILE__ );
m2f_get_config_parameters();
// **********************************************************************************************
// *************************** GLOBAL FUNCTIONS START HERE **************************************
// **********************************************************************************************
// Global Logging function
function m2f_log ($log_level , $msg , $line , $file, $debug_var = '')
{
global $m2f_log;
$file = basename($file);
$log_msg = "[$file, line $line] $msg";
if ($debug_var != '')
{
$log_msg .= " = " . m2f_debug_var($debug_var);
}
$m2f_log->log($log_msg , $log_level);
if ($log_level == PEAR_LOG_CRIT) m2f_display_error($log_msg);
}
// Global var debugging function
function m2f_debug_var($var)
{
ob_start();
print_r($var);
$ret_str = ob_get_contents();
ob_end_clean();
return $ret_str;
}
// Check return value from DB call
function check_db_result($db_result, $sql, $line, $file)
{
if (DB::isError($db_result))
{
$m2f_error_msg = "DB error. sql: '$sql' error: " . $db_result->getMessage();
m2f_log(PEAR_LOG_CRIT , $m2f_error_msg , $line, $file);
die;
}
}
// For important errors which don't need to kill the script
function m2f_display_error($error)
{
echo '[Mail2Forum] ' . $error . '
';
}
// Get config paramters
function m2f_get_config_parameters()
{
global $m2f_config , $m2f_db , $m2f_root_path , $type_db,
$m2f_desired_db_version, $m2f_version;
// See if M2F tables have been created, and read additional configuration directives from the database
$sql = "SELECT config_name, config_value FROM " . _M2F_CONFIG_TABLE_;
$db_result =& $m2f_db->getAssoc($sql, false, array(), DB_FETCHMODE_DEFAULT, true);
if (DB::isError($db_result))
{
m2f_log(PEAR_LOG_WARNING , "M2F config table not found (first run?), attempting to build M2F tables" , __LINE__ , __FILE__ );
// Try to build the DB schema
include_once($m2f_root_path."db/m2f_schema.php");
m2f_create_tables($type_db);
m2f_log(PEAR_LOG_NOTICE , "M2F tables created" , __LINE__ , __FILE__ );
// Try again
$sql = "SELECT config_name, config_value FROM " . _M2F_CONFIG_TABLE_;
$db_result =& $m2f_db->getAssoc($sql, false, array(), DB_FETCHMODE_DEFAULT, true);
check_db_result($db_result, $sql, __LINE__, __FILE__);
}
// Check the M2F version and see if the tables need updating
$cur_db_version = isset($db_result['_M2F_DB_VERSION_']) ? $db_result['_M2F_DB_VERSION_'][0] : 0;
if ($cur_db_version < $m2f_desired_db_version)
{
m2f_log(PEAR_LOG_WARNING , "Updating M2F tables to DB version $m2f_desired_db_version" , __LINE__ , __FILE__);
$current_config = $db_result;
include_once($m2f_root_path."db/m2f_schema.php");
m2f_update_distributionlists();
m2f_update_tables($current_config, $type_db);
m2f_add_hardcoded_patterns();
// Get updated configuration
$sql = "SELECT config_name, config_value FROM " . _M2F_CONFIG_TABLE_;
$db_result =& $m2f_db->getAssoc($sql, false, array(), DB_FETCHMODE_DEFAULT, true);
check_db_result($db_result, $sql, __LINE__, __FILE__);
}
// Populate $m2f_config[] array
$m2f_config = array();
$m2f_config['_M2F_VERSION_'] = "$m2f_version.{$db_result['_M2F_DB_VERSION_'][0]}";
m2f_log(PEAR_LOG_INFO , "Loading config values" , __LINE__ , __FILE__);
foreach ($db_result as $key => $value)
{
if (count($value) > 1)
{
$m2f_config[$key] = $value;
}
else
{
$m2f_config[$key] = $value[0];
}
}
// Try to set the uni-valued config directives as constants
foreach ($m2f_config as $config_name => $config_value)
{
if (!defined($config_name) && !is_array($config_value))
{
define($config_name, $config_value);
}
}
m2f_log(PEAR_LOG_DEBUG , "Config values: " , __LINE__ , __FILE__ , $m2f_config);
}
?>