<?php
@session_start();
@ob_start();
@ob_implicit_flush(0);
@error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);

define('MOZG', true);
define('ROOT_DIR', dirname (__FILE__));
define('ENGINE_DIR', ROOT_DIR.'/system');

header('Content-type: text/html; charset=utf-8');
	
//AJAX
$ajax = $_POST['ajax'];

$logged = false;
$user_info = false;

include ENGINE_DIR.'/initializer.php';

//Опредиления браузера
if(stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.0')) $BadBrowser = 'ie6';
elseif(stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE 7.0')) $BadBrowser = 'ie7';
elseif(stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE 8.0')) $BadBrowser = 'ie8';
elseif(stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE 7.0')) $BadBrowser = 'ie9';
elseif(stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE 8.0')) $BadBrowser = 'ie10';
elseif(stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE 7.0')) $BadBrowser = 'ie11';
if($BadBrowser == 'ie6' OR $BadBrowser == 'ie7' OR $BadBrowser == 'ie8' OR $BadBrowser == 'ie9' OR $BadBrowser == 'ie10' OR $BadBrowser == 'ie11')
	header("Location: ".ENGINE_DIR."/badbrowser.php");

//Новые сообщения
$new_messages = $user_info['messages_num'];
if($new_messages)
	$new_messages = "+{$new_messages}";
else
	$new_messages = '';
	
//Новые друзья
$new_demands = $user_info['friends_demands'];
if($new_demands){
	$new_demands = "+{$new_demands}";
	$new_demands_link = '/requests';
} else
	$new_demands = '';

//Если включен AJAX то загружаем стр.
if($ajax == 'yes'){

	//Если есть POST Запрос и значение AJAX, а $ajax не равняется "yes" то не пропускаем
	if($_SERVER['REQUEST_METHOD'] == 'POST' AND $ajax != 'yes')
		die('AJAX Error №515');

	$result_ajax = "
<script type=\"text/javascript\">
document.title = '{$metatags['title']}';
document.getElementById('new_messages').innerHTML = '{$new_messages}';
document.getElementById('new_demands').innerHTML = '{$new_demands}';
document.getElementById('requests_link').setAttribute('href', '/friends{$new_demands_link}');
</script>
{$tpl->result['info']}{$tpl->result['content']}
";
	echo str_replace('{theme}', '/templates/'.$config['temp'], $result_ajax);

	$tpl->global_clear();
	$db->close();
		
	die();
	
} 

//Если обращение к модулю регистрации или главной и юзер не авторизован то показываем регистрацию
if($go == 'signup_finish' OR $go == 'main' AND !$logged)
	include ENGINE_DIR.'/modules/home.php';

$tpl->load_template('main.tpl');

//Если юзер залогинен
if($logged){
	$tpl->set_block("'\\[not-logged\\](.*?)\\[/not-logged\\]'si","");
	$tpl->set('[logged]','');
	$tpl->set('[/logged]','');
	$tpl->set('{my-page-link}', '/u'.$user_info['user_id']);
	$tpl->set('{my-id}', $user_info['user_id']);
	$tpl->set('{top-name}', $user_info['name_lastname']);
	$tpl->set('{top-email}', $user_info['email']);
	
	if($user_info['avatar']){
		$tpl->set('{top-ava}', "/uploads/users/{$user_info['user_id']}/50_{$user_info['avatar']}");
		$tpl->set('{top-ava_100}', "/uploads/users/{$user_info['user_id']}/100_{$user_info['avatar']}");
	} else {
		$tpl->set('{top-ava}', "{theme}/style/img/no_ava_50.png");
		$tpl->set('{top-ava_100}', "{theme}/style/img/no_ava_100.png");
	}
	
	//Заявки в друзья
	if($new_demands){
		$tpl->set('{new_demands}', $new_demands);
		$tpl->set('{requests-link}', $new_demands_link);
	} else {
		$tpl->set('{new_demands}', '');
		$tpl->set('{requests-link}', '');
	}
	
	//Сообщения
	if($new_messages)
		$tpl->set('{new_messages}', $new_messages);
	else 
		$tpl->set('{new_messages}', '');
		
} else {
	$tpl->set_block("'\\[logged\\](.*?)\\[/logged\\]'si","");
	$tpl->set('[not-logged]','');
	$tpl->set('[/not-logged]','');
	$tpl->set('{my-page-link}', '');
}

$tpl->set('{lang}', $rMyLang);
$tpl->set('{info}', $tpl->result['info']);
$tpl->set('{content}', $tpl->result['content']);

if(!$metatags['title'])
	$metatags['title'] = $config['home'];

//BUILD JS
if($logged)
	$tpl->set('{header}', '<title>'.$metatags['title'].'</title>
<meta name="generator" content="MKM Engine" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<noscript><meta http-equiv="refresh" content="0; URL=/badbrowser.php"></noscript>
<link media="screen" href="{theme}/style/style.css" type="text/css" rel="stylesheet" />  
<script type="text/javascript" src="{theme}/js/jquery.lib.js"></script>
<script type="text/javascript" src="{theme}/js/'.$checkLang.'/lang.js">
</script><script type="text/javascript" src="{theme}/js/main.js"></script>
<script type="text/javascript" src="{theme}/js/profile.js"></script>
<link rel="shortcut icon" href="{theme}/images/fav.png" />');
else
	$tpl->set('{header}', '<title>'.$metatags['title'].'</title>
<meta name="generator" content="MKM Engine" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<noscript><meta http-equiv="refresh" content="0; URL=/badbrowser.php"></noscript>
<link media="screen" href="{theme}/style/style.css" type="text/css" rel="stylesheet" />  
<script type="text/javascript" src="{theme}/js/jquery.lib.js"></script>
<script type="text/javascript" src="{theme}/js/'.$checkLang.'/lang.js"></script>
<script type="text/javascript" src="{theme}/js/main.js"></script>
<script type="text/javascript" src="{theme}/js/signup.js"></script>
<link rel="shortcut icon" href="{theme}/images/fav.png" />');

$tpl->compile('main');

echo str_replace('{theme}', '/templates/'.$config['temp'], $tpl->result['main']);

$tpl->global_clear();
$db->close();

?>