<?php /* Created on: 24.06.2006 */
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

$time_start = microtime(true);
if (!session_id()) @session_start();

include_once dirname(__FILE__).'/../functions/functions.php';

//убираем все слеши от магических кавычек
clean_magic_quotes();

include_once dirname(__FILE__)."/config.php";

spl_autoload_register('autoLoader');

include_once LAB_PATH_KERNEL.'/classes/LabVars.php';
LabVars::$allow_debug=DEMO_MODE?false:(isset($_SESSION['user_info']['group_id']) && $_SESSION['user_info']['group_id']==1);

if(!file_exists(LAB_PATH_DATA.'/install.lock'))
{
	//если движок не проинсталлирован, то вызываем инсталлятор
	include LAB_PATH_KERNEL.'/install/index.php';
	exit();
}

include_once LAB_PATH_KERNEL.'/classes/LabConfig.php';
include_once LAB_PATH_KERNEL.'/classes/Tpl.php';
include_once LAB_PATH_KERNEL.'/classes/User.php';
include_once LAB_PATH_KERNEL.'/classes/LabCommon.php';
include_once LAB_PATH_KERNEL.'/classes/LabCMS.php';
//инициализация конфига
LabConfig::init();
//Парсинг URL
include_once LAB_PATH_KERNEL.'/classes/Url/'.LabConfig::$cfg['url_type'].'.php';
call_user_func(array('Url_'.LabConfig::$cfg['url_type'], 'parse'));
filterNullInSuperglobals();
//проверка доступных обновлений
LabCommon::check_for_updates();

//отправляем заголовки подавляющие кэширование
Header("Expires: Thu, 19 Feb 1998 13:24:18 GMT");
Header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
Header("Cache-Control: no-cache, must-revalidate");
Header("Cache-Control: post-check=0,pre-check=0");
Header("Cache-Control: max-age=0");
Header("Pragma: no-cache");

User::i()->setData();

if (LabCMS::i()->isAjax()) LabVars::$template='blank.tpl';

//закидываем в шаблонизатор настройки сайта, но без параметров БД и секретного кода
$config=LabConfig::$cfg;
unset($config['mysql'], $config['secure']);
Tpl::i()->assign('core_config', $config);
unset($config);

Tpl::i()->assign('_user', User::i());

LabCMS::i()->assignCoreVars();
foreach (LabCMS::i()->modules as LabVars::$current_module)
{
	$mod_template="";
	LabCMS::i()->setModuleTemplate('');
	LabConfig::$mcfg=$config=LabCMS::i()->getModuleConfig();
	Tpl::i()->assign('config', $config);
	Tpl::i()->caching = false;
	if (!LabVars::$break)
	{
		if (LabCMS::i()->disableForDemo())
		{
			if (LabCMS::i()->isAjax())
			{
				echo real_json_encode(array('error'=>LabCMS::i()->errors[0]));
				exit;
			}
			continue;
		}
		include LAB_PATH_MODULES.'/'.LabCMS::i()->getModuleName()."/index.php";

		if ($mod_template) LabCMS::i()->assignModule($mod_template);
		elseif(LabCMS::i()->getModuleTemplate())  LabCMS::i()->assignModule(LabCMS::i()->getModuleTemplate());
	}
}
LabVars::$current_module=null;

Tpl::i()->assign('css', (count(LabVars::$css)?LabVars::$css:LabCMS::i()->getSkinCSS()));
Tpl::i()->assign('js', (count(LabVars::$js)?LabVars::$js:LabCMS::i()->getSkinJS()));

Tpl::i()->assign(array(	'site_name'   => LabConfig::get('site_name'),
								'_description'=> LabConfig::get('description'),
								'_keywords'   => LabConfig::get('keywords'),
								'skin'        => LabConfig::get('skin'),
								'catcher'     => LabConfig::get('catcher'),
								'_title'      => LabConfig::get('title')
						));


$time_end = microtime(true);
$s=array('time'=>0, 'count'=>0);
if (Db::$connected) $s = Db::i()->_statistics;
$sql = $s["time"];
$sqlCount = $s["count"];
$total = ($time_end - $time_start);
$php = $total-$sql;
Tpl::i()->assign("stat", "Время генерации: ".sprintf("%01.3f сек.", $total).", PHP: ".sprintf("%01.3f сек.", $php).", SQL: ".sprintf("%01.3f сек.", $sql)." при $sqlCount запросах");
//print_r($_lc);

if (LabCMS::i()->isAjax()) Debug_HackerConsole_Main::disable();
LabCMS::i()->display();

//сохранение кэша
LabCMS::i()->putToCache();
