<?php

	/**
	----------------------------------------------------------------------------------------
	If you are seeing this message in your web browser, it means PHP has not been setup
	or enabled on your web server. Enable PHP from your web hosting control panel or contact
	your web host to enable it for you.
	----------------------------------------------------------------------------------------
	*/

	// a date in the past
	header("Expires: Mon, 23 Jul 1993 05:00:00 GMT");
	// always modified
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
	// HTTP/1.1
	header("Cache-Control: no-store, no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0, max-age=0", false);

	require(dirname(__FILE__).'/../init.php');

	/**
	* my_error_handler
	*
	* The error handler for the backend when debug is on
	*
	* @param int $errno
	* @param string $errstr
	* @param string $errfile
	* @param int $errline
	*
	* @return void
	*/
	function my_error_handler($errno, $errstr, $errfile, $errline)
	{
		$bt = debug_backtrace();
		array_shift($bt);
		$bt = print_r($bt, true);
		$bt = htmlspecialchars($bt);

		switch ($errno) {
			case E_USER_ERROR:
			case E_ERROR:
				echo '<div class="eh error" ><b>ERROR:</b> '.$errstr."<br />\n";
				echo '  Fatal error in line '.$errline.' of file '.$errfile;
				echo ', PHP '.PHP_VERSION.' ('.PHP_OS.')<br /><a href="#" onclick="ToggleDisplay(this.nextSibling.nextSibling);">Backtrace</a> <pre style="display: none;">'.$bt.'</pre>'."</div>\n";
				die();
			break;
			case E_USER_WARNING:
			case E_WARNING:
				echo '<div class="eh warning"> <b>WARNING:</b> '.$errstr.'<br /><a href="#" onclick="ToggleDisplay(this.nextSibling.nextSibling);">Backtrace</a> <pre style="display: none;">'.$bt.'</pre>'."</div>\n";
			break;
			case E_USER_NOTICE:
			case E_NOTICE:
				echo '<div class="eh notice"><b>NOTICE:</b> '.$errstr.'<br /><a href="#" onclick="ToggleDisplay(this.nextSibling.nextSibling);">Backtrace</a> <pre style="display: none;">'.$bt.'</pre>'."</div>\n";
			break;
			case E_STRICT:
			break;
			default:
				echo '<div class="eh unknown">Unkown error type: ['.$errno.'] '.$errstr.'<br /><a href="#" onclick="ToggleDisplay(this.nextSibling.nextSibling);">Backtrace</a> <pre style="display: none;">'.$bt.'</pre>'."</div>\n";
		}
	}

	if (isset($GLOBALS['debug']) && $GLOBALS['debug'] == true) {
		set_error_handler('my_error_handler');
	}

	$Lang = 'en';
	if (isset($GLOBALS['currentLanguage'])) {
		$Lang = $GLOBALS['currentLanguage'];
	}
	define('AKB_WL_LANG_FILE',			dirname(__FILE__).'/../includes/language/'.$Lang.'/whitelabel.ini');
	define('AKB_LANG_FILE',				dirname(__FILE__).'/../includes/language/'.$Lang.'/back_language.ini');
	define('AKB_TPL_LOAD_PATH',			dirname(__FILE__).'/templates/');
	define('AKB_TPL_PHP_PATH',			dirname(__FILE__).'/includes/display/');
	define('AKB_TPL_HTTP_PATH',			$GLOBALS['pathToKB'].'/admin/templates/');
	define('AKB_TPL_IMAGE_PATH',		AKB_TPL_HTTP_PATH.'Images');
	define('AKB_PANEL_LOAD_PATH',		AKB_TPL_LOAD_PATH.'Panels/');
	define('AKB_SNIPPET_LOAD_PATH',	AKB_TPL_LOAD_PATH.'Snippets/');
	define('CLASS_DIR',					dirname(__FILE__).'/includes/classes');

	require(dirname(__FILE__).'/../lib/email.php');
	require_once(dirname(__FILE__).'/../lib/multibyte.php');
	require(dirname(__FILE__).'/includes/helpers/helper.template.php');

	$GLOBALS['SNIPPETS'] = '';
	$GLOBALS['Message'] = '';
	$GLOBALS['HidePanels'] = array();
	if (!class_exists('AKB_HELPER')) {
		require(CLASS_DIR.'/class.helper.php');
	}
	require(CLASS_DIR.'/class.category.php');
	require(CLASS_DIR.'/class.fileindex.php');
	require(CLASS_DIR.'/class.news.php');
	require(CLASS_DIR.'/class.template.php');
	require(CLASS_DIR.'/class.auth.php');
	require(CLASS_DIR.'/class.install.php');
	require(CLASS_DIR.'/class.engine.php');
	require(CLASS_DIR.'/class.admintable.php');
	require(CLASS_DIR.'/class.customfields.php');
	$GLOBALS['e'] = ucwords(strtolower(AKB_HELPER::get1n()));
	$GLOBALS['AKB_CLASS_FILEINDEX']		= new AKB_FILEINDEX();
	$GLOBALS['AKB_CLASS_HELPER']		= new AKB_HELPER();
	$GLOBALS['AKB_CLASS_CATEGORY']		= new AKB_CATEGORY();
	$GLOBALS['AKB_CLASS_NEWS']			= new AKB_NEWS();
	$GLOBALS['AKB_CLASS_CUSTOMFIELDS']	= new AKB_CUSTOMFIELDS();
	$GLOBALS['AKB_CLASS_TEMPLATE']		= new AKB_TEMPLATE();
	$GLOBALS['AKB_CLASS_AUTH']			= new AKB_AUTH();
	$GLOBALS['AKB_CLASS_INSTALL']		= new AKB_INSTALL();
	$GLOBALS['AKB_CLASS_ENGINE']		= new AKB_ENGINE();
	
	if (!pageLoad()) {
		$_GET['sessionloggedout'] = true;
		$GLOBALS['AKB_CLASS_AUTH']->DoLogin();
		die();
	}
	
	$GLOBALS['AKB_CLASS_ENGINE']->HandlePage();
	$end_time = microtime_float();
	if (isset($GLOBALS['debug']) && $GLOBALS['debug'] == true) {
		if (function_exists('memory_get_usage')) {
			$mem_used = memory_get_usage()/1024/1024;
		} else {
			$mem_used = 0;
		}
		printf('<div id="debug">Time taken to generate the page %.4f seconds and performed %d sql queries and used %.4f MB of memory</div>', ($end_time - $start_time), $GLOBALS['AKB_DB']->NumQueries, $mem_used);
	}
?>
