<?php
//Let's see if we've got a database set up in settings.ini
try{
	ApplicationSettings::GetSetting("database", "database");
} catch(Exception $e){
	return;
}

ApplicationSettings::RegisterDefaultSetting("error_handling", "enabled", FALSE);
if (ApplicationSettings::GetSetting("error_handling", "enabled")!=true)
	return;

$GLOBALS['php_errors']=array();

set_error_handler(function($errNo, $errStr, $errFile, $errLine, $errContext){
	LogError($errNo,$errStr,$errFile,$errLine,print_r($errContext,true));
	//return false;
});

set_exception_handler(function(Exception $ex){
	LogError($ex->getCode(), $ex->getMessage(), $ex->getFile(), $ex->getLine(), $ex->getTraceAsString());
});

function LogError($code, $message, $file, $line, $extraData){
	$obj=new DBObject("error_log", "error_id", 0);
	$obj->Code=$code;
	$obj->Message=$message;
	$obj->File=$file;
	$obj->Line=$line;
	$obj->ExtraData=$extraData;
	$obj->Save();
	$GLOBALS['php_errors'][]=$obj;
}