<?php
if (! defined ( 'DATALIFEENGINE' )) {die ( "Hacking attempt!" );}

@include (SYSTEM_DIR . '/data/config.php');
@include (SYSTEM_DIR . '/data/repa.php');
require_once SYSTEM_DIR . '/classes/mysql.php';
require_once SYSTEM_DIR . '/data/dbconfig.php';
require_once SYSTEM_DIR . '/modules/functions.php';
require_once SYSTEM_DIR . '/modules/gzip.php';
include_once SYSTEM_DIR . '/cron.php';

$Timer = new microTimer ( );
$Timer->start ();
check_xss ();

if (isset ( $_REQUEST['year'] )) $year = intval ( $_GET['year'] ); else $year = '';
if (isset ( $_REQUEST['month'] )) $month = @$db->safesql ( strip_tags ( str_replace ( '/', '', $_GET['month'] ) ) ); else $month = '';
if (isset ( $_REQUEST['day'] )) $day = @$db->safesql ( strip_tags ( str_replace ( '/', '', $_GET['day'] ) ) ); else $day = '';
if (isset ( $_REQUEST['user'] )) $user = @$db->safesql ( strip_tags ( str_replace ( '/', '', urldecode ( $_GET['user'] ) ) ) ); else $user = '';
if (isset ( $_REQUEST['news_name'] )) $news_name = @$db->safesql ( strip_tags ( str_replace ( '/', '', $_GET['news_name'] ) ) ); else $news_name = '';
if (isset ( $_REQUEST['newsid'] )) $newsid = intval ( $_GET['newsid'] ); else $newsid = 0;
if (isset ( $_REQUEST['cstart'] )) $cstart = intval ( $_GET['cstart'] ); else $cstart = 0;
if (isset ( $_REQUEST['news_page'] )) $news_page = intval ( $_GET['news_page'] ); else $news_page = 0;

if (isset ( $_REQUEST['category'] )) {
	if (substr ( $_GET['category'], - 1, 1 ) == '/') $_GET['category'] = substr ( $_GET['category'], 0, - 1 );
	$category = explode ( '/', $_GET['category'] );
	$category = end ( $category );
	$category = $db->safesql ( strip_tags ( $category ) );
} else $category = '';

$PHP_SELF = $config['http_home_url'] . "index.php";
$pm_alert = "";
$ajax = "";
$_DOCUMENT_DATE = false;
$user_query = "";

$metatags = array (
				'title' => $config['home_title'], 
				'description' => $config['description'], 
				'keywords' => $config['keywords'],
				'header_title' => "" );

$user_color = $cache->get ( "user_color" );
if (! $user_color){
    $user_color = array ();
    $db->query ( "SELECT user_id, name, user_group FROM " . USERPREFIX . "_users WHERE user_group <> '4' " );
    while ( $row = $db->get_row () ){
        $user_color[$row['name']] = array ();
        foreach ( $row as $key => $value ) {
        $user_color[$row['name']][$key] = $value;
    }}
    $cache->set ( "user_color", $user_color );
    $db->free ();
}

$user_group = $cache->get ( "usergroup" );
if (! $user_group) {
	$user_group = array ();
	$db->query ( "SELECT * FROM " . USERPREFIX . "_usergroups ORDER BY id ASC" );
	while ( $row = $db->get_row () ) {
		$user_group[$row['id']] = array ();
		foreach ( $row as $key => $value ) {$user_group[$row['id']][$key] = stripslashes($value);}
	}
    $cache->set ( "usergroup", $user_group );
	$db->free ();
}

$cat_info = $cache->get ( "category" );
if (! is_array ( $cat_info )) {
	$cat_info = array ();
	$db->query ( "SELECT * FROM " . PREFIX . "_category ORDER BY posi ASC" );
	while ( $row = $db->get_row () ) {
		$cat_info[$row['id']] = array ();
		foreach ( $row as $key => $value ) {$cat_info[$row['id']][$key] = stripslashes ( $value );}
	}
    $cache->set ( "category", $cat_info );
	$db->free ();
}

$banned_info = $cache->get ( "banned" );
if (! is_array ( $banned_info )) {
	$banned_info = array ();
	$db->query ( "SELECT * FROM " . USERPREFIX . "_banned" );
	while ( $row = $db->get_row () ) {
		if ($row['users_id']) {$banned_info['users_id'][$row['users_id']] = array ('users_id' => $row['users_id'],  'descr' => stripslashes ( $row['descr'] ),  'date' => $row['date'] );
		} else {if (count ( explode ( ".", $row['ip'] ) ) == 4) $banned_info['ip'][$row['ip']] = array ('ip' => $row['ip'], 'descr' => stripslashes ( $row['descr'] ), 'date' => $row['date']);
		  elseif (strpos ( $row['ip'], "@" ) !== false) $banned_info['email'][$row['ip']] = array ('email' => $row['ip'],  'descr' => stripslashes ( $row['descr'] ), 'date' => $row['date'] );
		  else $banned_info['name'][$row['ip']] = array ('name' => $row['ip'], 'descr' => stripslashes ( $row['descr'] ), 'date' => $row['date'] );
	}}
    $cache->set ( "banned", $banned_info );
	$db->free ();
}

if ($category != '') $category_id = get_ID ( $cat_info, $category ); 
else $category_id = false;

include_once ROOT_DIR . '/language/' . $config['langs'] . '/website.lng';
$config['charset'] = ($lang['charset'] != '') ? $lang['charset'] : $config['charset'];

$smartphone_detected = false;
if( isset( $_REQUEST['action'] ) and $_REQUEST['action'] == "mobiledisable" ) $_SESSION['mobile_disable'] = 1;
if( isset( $_REQUEST['action'] ) and $_REQUEST['action'] == "mobile" ) { $_SESSION['mobile_enable'] = 1; $_SESSION['mobile_disable'] = 0;}
if( !isset( $_SESSION['mobile_disable'] ) ) $_SESSION['mobile_disable'] = 0;
if( !isset( $_SESSION['mobile_enable'] ) ) $_SESSION['mobile_enable'] = 0;
if ( !$_SESSION['mobile_disable'] ) {
	if ( check_smartphone() ) {
		if ( @is_dir ( ROOT_DIR . '/templates/smartphone' ) ) {
			$config['skin'] = "smartphone";
			$smartphone_detected = true;
			$config['ajax'] = false;
}}}

require_once SYSTEM_DIR . '/classes/templates.class.php';

$tpl = new dle_template ( );
$tpl->dir = ROOT_DIR . '/templates/' . $config['skin'];
define ( 'TEMPLATE_DIR', $tpl->dir );

if (isset ( $_POST['set_new_sort'] )) {
	$allowed_sort = array ('date', 'rating', 'news_read', 'comm_num', 'title' );
	$find_sort = str_replace ( ".", "", totranslit ( $_POST['set_new_sort'] ) );
	$direction_sort = str_replace ( ".", "", totranslit ( $_POST['set_direction_sort'] ) );

	if (in_array ( $_POST['dlenewssortby'], $allowed_sort )) {
		if ($_POST['dledirection'] == "desc" or $_POST['dledirection'] == "asc") {
			$_SESSION[$find_sort] = $_POST['dlenewssortby'];
			$_SESSION[$direction_sort] = $_POST['dledirection'];
			$_SESSION['dle_no_cache'] = "1";
}}}

if ($_POST['dle_comm_sort'] == "DESC" or $_POST['dle_comm_sort'] == "ASC"){
	    set_cookie("dle_comm_sort", $_POST['dle_comm_sort'], 365);
	    $dle_comm_sort = $_POST['dle_comm_sort'];
	    $_SESSION['dle_comm_sort'] = $_POST['dle_comm_sort'];
	    $_SESSION['dle_no_cache'] = "1";
//	}elseif ($_COOKIE['dle_comm_sort'] == "DESC" or $_COOKIE['dle_comm_sort'] == "ASC"){
    }elseif ($_SESSION['dle_comm_sort'] == "DESC" or $_SESSION['dle_comm_sort'] == "ASC"){
	    //$dle_comm_sort = $_COOKIE['dle_comm_sort'];
        $dle_comm_sort = $_SESSION['dle_comm_sort'];
	    $_SESSION['dle_no_cache'] = "1";
	}else{$dle_comm_sort = $config['comm_msort'];}

	include_once SYSTEM_DIR . '/modules/sitelogin.php';

    //Проверяем забанен ли пользователь
    if ( isset( $banned_info['ip'] ) ) $blockip = check_ip ( $banned_info['ip'] );  else $blockip = false;
	if (($is_logged and $member_id['banned'] == "yes") or $blockip) include_once SYSTEM_DIR . '/modules/banned.php';

    if ($is_logged) {
	    set_cookie ( "dle_newpm", $member_id['pm_unread'], 365 );
	    if ($member_id['pm_unread'] > intval ( $_COOKIE['dle_newpm'] ) AND !$smartphone_detected) {
	    include_once SYSTEM_DIR . '/modules/pm_alert.php';
    }}

    $tpl->load_template('login.tpl');
	if ($is_logged) {
			$tpl->set('{profile_name}', $member_id['name']);
			$tpl->set('{profile_link}', $config['http_home_url'] . "user/" . urlencode ( $member_id['name'] ) . "/");
			$tpl->set('{addnews_link}', $config['http_home_url'] . "addnews.html");
			$tpl->set('{newsposts_link}', $config['http_home_url'] . "newposts/");
			$tpl->set('{fav_link}', $config['http_home_url'] . "favorites/");
			$tpl->set('{pm_link}', $PHP_SELF . "?do=pm");
			$tpl->set('{logout_link}', $PHP_SELF . "?action=logout");
			$tpl->set('{admin_link}', $config['http_home_url'] . $config['admin_path'] . "?mod=main");
            if ($member_id['favorites']) {$fav = count(explode("," ,$member_id['favorites']));} else $fav = '0';
            $tpl->set('{fav_count}', $fav);
            $tpl->set( '{new-pm}', $member_id['pm_unread'] );
			$tpl->set( '{all-pm}', $member_id['pm_all'] );
	}
	$tpl->set('{reg_link}', $PHP_SELF . "?do=register");
	$tpl->set('{lost_link}', $PHP_SELF . "?do=lostpassword");
	$tpl->compile('login_panel');
	$tpl->clear();
	
include_once SYSTEM_DIR . '/offline.php';
require_once ROOT_DIR . '/system/engine.php';
require_once SYSTEM_DIR.'/modules/online.php';
include_once SYSTEM_DIR . '/modules/toptables.php';
include_once SYSTEM_DIR . '/modules/chat/block.php';
include_once SYSTEM_DIR . '/modules/comments-last.php';
?>