<?php

// Подключаем необходимые классы
require('./_cms/autoexec.php');

require_once(CONFIG_PATH_PLUGIN_DATA_EXT.'/seo_redirect.php');
data_seo_redirect();

// Объявляем необходимые массивы и переменные
$GLOBALS['passed'] = $a_page_module_result = $a_replace = array();
$GLOBALS['path_module'] = FALSE;
$GLOBALS['template_site_dir'] = CONFIG_PATH_TPL.c_page::$a_page['tpl_path'].CONFIG_URI_PLUGIN_TPL_EXT;
$GLOBALS['page_h1'] = c_page::$a_page['name'];

//print '<pre>'; print_r(c_page::$a_page);
// Подключаем файлы подготовки данных и обработки шаблонов
if( isset($o_outerface->a_outerface) && is_array($o_outerface->a_outerface) )
{
	//print '<pre>'; print_r($o_outerface->a_outerface);
	$a_outerface_mask_integration = $o_outerface->a_outerface_mask_integration;

	foreach( $o_outerface->a_outerface AS $a_outerface_info )
	{
		// Подключаем необходимые плагины
		if( is_file(CONFIG_PATH_PLUGIN_DATA_EXT.'/'.$a_outerface_info['plugin_data_dir'].'/'.$a_outerface_info['plugin_data']) )
			require_once(CONFIG_PATH_PLUGIN_DATA_EXT.'/'.$a_outerface_info['plugin_data_dir'].'/'.$a_outerface_info['plugin_data']);
		elseif( is_file(CONFIG_PATH_PLUGIN_DATA_EXT.'/'.$a_outerface_info['plugin_data']) )
			require_once(CONFIG_PATH_PLUGIN_DATA_EXT.'/'.$a_outerface_info['plugin_data']);
		if( is_file(CONFIG_PATH_PLUGIN_EXT.'/'.$a_outerface_info['plugin_template_dir'].'/'.$a_outerface_info['plugin_template']) )
			require_once(CONFIG_PATH_PLUGIN_EXT.'/'.$a_outerface_info['plugin_template_dir'].'/'.$a_outerface_info['plugin_template']);
		elseif( is_file(CONFIG_PATH_PLUGIN_EXT.'/'.$a_outerface_info['plugin_template']) )
			require_once(CONFIG_PATH_PLUGIN_EXT.'/'.$a_outerface_info['plugin_template']);
	}
}

// Маски, отсортированные по run_priority
if( isset($o_outerface->a_outerface_mask) && is_array($o_outerface->a_outerface_mask) && is_array($o_outerface->a_outerface_to_page) )
{
	// Бежим по маскам, отсортированным по run_priority
	foreach( $o_outerface->a_outerface_mask AS $a_outerface_mask_info )
	{
		// Бежим по привякам оутерфейсов к страницам
		foreach( $o_outerface->a_outerface_to_page AS $a_outerface_to_page_info )
		{
			if( $a_outerface_to_page_info['outerface_id'] == $a_outerface_mask_info['id_group'] )
			{
				// Информация об оутерфейсе
				$a_outerface_info = $o_outerface->a_outerface[$a_outerface_to_page_info['outerface_id']];

				// Отрезаем расширения файла для вызова функции
				$a_outerface_to_page_info['plugin_data'] = substr($a_outerface_info['plugin_data'], 0, -4);
				$a_outerface_to_page_info['plugin_template'] = substr($a_outerface_info['plugin_template'], 0, -4);

				// Получить список параметров конфигурации
				if( isset($o_outerface->a_outerface_config[$a_outerface_to_page_info['page_module_id']][$o_outerface->a_outerface[$a_outerface_to_page_info['outerface_id']]['id_row']]) )
					$a_plugin_param = $o_outerface->a_outerface_config[$a_outerface_to_page_info['page_module_id']][$o_outerface->a_outerface[$a_outerface_to_page_info['outerface_id']]['id_row']];
				else
					$a_plugin_param = '';

				// Получить список таблиц
				if( isset($o_outerface->a_outerface_table[$a_outerface_to_page_info['module_id']]) )
					$a_plugin_table = $o_outerface->a_outerface_table[$a_outerface_to_page_info['module_id']];
				else
					$a_plugin_table = '';

				// Запуск плагина подготовки данных
				if( !empty($a_outerface_to_page_info['plugin_data']) )
				{
					$a_outerface_to_page_info['plugin_data'] = 'data_'.$a_outerface_to_page_info['plugin_data'];
					$a_outerface_to_page_info['plugin_data']();
				}
                //print $a_outerface_to_page_info['plugin_data'].' $GLOBALS[path_module] '.$GLOBALS['path_module'].'<br>';
				// Запуск плагина обработки шаблона
				if( !empty($a_outerface_to_page_info['plugin_template']) )
				{
					$a_outerface_to_page_info['plugin_template'] = 'tpl_'.$a_outerface_to_page_info['plugin_template'];

					// Сортировка вывода оутерфесов по позициям
					if(!isset($a_page_module_result[$a_outerface_to_page_info['block_name']][$a_outerface_to_page_info['outerface_position']]))
						$a_page_module_result[$a_outerface_to_page_info['block_name']][$a_outerface_to_page_info['outerface_position']] = $a_outerface_to_page_info['plugin_template']();
					else
						$a_page_module_result[$a_outerface_to_page_info['block_name']][] = $a_outerface_to_page_info['plugin_template']();
				}
				unset($a_plugin_param);
				unset($a_plugin_table);
			}
		}
	}
}

$o_outerface->__destruct();
unset($o_outerface);

// Вывод ошибки 404 - Страница не найдена
//if( ( isset(c_page::$a_page['path_module']) && c_page::$a_page['path_module'] && !$GLOBALS['path_module']) )
if( !$GLOBALS['path_module'] )
{
	header("HTTP/1.0 404 Not Found");
	require('404.php');
	exit;
}

// Загружаем основной шаблон страницы
$content_template = file_get_contents(CONFIG_PATH_TPL.c_page::$a_page['tpl_path'].'/'.c_page::$a_page['tpl_file']);

// Проставляем title
if( isset($GLOBALS['seo_headers']['title']) )
	$a_page_module_result['head_title'][] = $GLOBALS['seo_headers']['title'];
else
	$a_page_module_result['head_title'][] = c_page::$a_page['name'];

// Вывод заголовков
if( isset($GLOBALS['seo_headers']['header_expires']) && !empty($GLOBALS['seo_headers']['header_expires']) )
	header($GLOBALS['seo_headers']['header_expires']);
if( isset($GLOBALS['seo_headers']['header_cachecontrol']) && !empty($GLOBALS['seo_headers']['header_cachecontrol']) )
	header($GLOBALS['seo_headers']['header_cachecontrol']);
if( isset($GLOBALS['seo_headers']['header_pragma']) && !empty($GLOBALS['seo_headers']['header_pragma']) )
	header($GLOBALS['seo_headers']['header_pragma']);
if( isset($GLOBALS['seo_headers']['header_lastmodified']) && !empty($GLOBALS['seo_headers']['header_lastmodified']) )
	header($GLOBALS['seo_headers']['header_lastmodified']);

// Отсортировать контент на местах по полю position
if( count($a_page_module_result) )
{
	foreach($a_page_module_result AS $template_place => $a_place)
	{
		ksort($a_place);
		foreach($a_place AS $position => $content)
		{
			if( isset($a_replace[$template_place]) )
				$a_replace[$template_place] .= $content;
			else
				$a_replace[$template_place] = $content;
		}
	}
}
unset($a_page_module_result);
unset($a_place);

// Выполнить подстановку контента по местам
if( count($a_replace) )
	foreach($a_replace as $template_place => $content)
		$content_template = str_replace('[['.$template_place.']]', $content, $content_template);
unset($a_replace);

// удаляем  все /> закрывашки, потому как они нахрен не нужны и в спецификации HTML считаются НЕ верными и нужны лишь в XHTML!
$content_template = str_replace('/>', '>', $content_template);

if(strstr(c_page::$a_page['path'], 'reservation') || strstr(c_page::$a_page['path'], 'antalya') || strstr(c_page::$a_page['path'], 'aegean_sea') || strstr(c_page::$a_page['path'], 'dead_sea'))
{
	echo preg_replace("/[\s]+$/m", '', $content_template);
}
else
{
	//Удаляем все пустые строки, не нужные блоки шаблона и т.п.
	echo preg_replace(array("'\[\[.*?\]\]'", "/[\s]+$/m"), '', $content_template);
}
//echo '<p><center>Страница сгенерирована за '.round((microtime(1) - $gen_page_start)*1000, 5).' миллисекунд</center></p>';

?>