<?PHP
/*
=====================================================
 DataLife Engine - by SoftNews Media Group 
-----------------------------------------------------
 http://www.pc-soft.ru/
-----------------------------------------------------
 Copyright (c) 2004,2006 SoftNews Media Group
=====================================================
 Данный код защищен авторскими правами
=====================================================
 Файл: engine.php
-----------------------------------------------------
 Назначение: подключение основных компонентов
=====================================================
*/
if(!defined('DATALIFEENGINE'))
{
  die("Hacking attempt!");
}

if (isset ($_REQUEST['do'])) $do = $_REQUEST['do']; else $do = "";
if (isset ($_REQUEST['subaction'])) $subaction = $_REQUEST['subaction']; else $subaction = "";
if (isset ($_REQUEST['doaction'])) $doaction = $_REQUEST['doaction']; else $doaction = "";
$CN_HALT = FALSE;

switch ($do) {

case "search" :
	include ENGINE_DIR.'/modules/search.php';
	break;

case "comments" :
	include ENGINE_DIR.'/modules/comments.php';
	break;

case "stats" :
	include ENGINE_DIR.'/modules/stats.php';
	break;

case "addnews" :
	include ENGINE_DIR.'/modules/addnews.php';
	break;

case "register" :
	include ENGINE_DIR.'/modules/register.php';
	break;

case "lostpassword" :
	include ENGINE_DIR.'/modules/lostpassword.php';
	break;

case "static" :
	include ENGINE_DIR.'/modules/static.php';
	break;

case "favorites" :
	if ($is_logged) {
	$config['allow_cache'] = false;
	include ENGINE_DIR.'/modules/favorites.php';
	} else msgbox ($lang['all_err_1'], $lang['fav_error']);
	break;

case "feedback" :
	include ENGINE_DIR.'/modules/feedback.php';
	break;

case "lastcomments" :
	include ENGINE_DIR.'/modules/lastcomments.php';
	break;

case "pm" :
	include ENGINE_DIR.'/modules/pm.php';
	break;

default:

if (!isset($_REQUEST['cstart'])) $cstart = 0; else $cstart = intval($_REQUEST['cstart']);
if ($cstart < 0) $cstart = 0;

$is_main = 0;
$active = false;
$thisdate = date ("Y-m-d H:i:s", (time()+ $config['date_adjust']*60));
$config['news_number'] = intval($config['news_number']);
$attachments = array();

if ($config['news_sort'] == "")  $config['news_sort'] = "date";
if ($config['news_msort'] == "") $config['news_msort'] = "DESC";

$allow_list = explode (',', $user_group[$member_id['user_group']]['allow_cats']);
if ($allow_list[0] != "all") $stop_list = "category regexp '[[:<:]](".implode ('|', $allow_list).")[[:>:]]' AND "; else $stop_list = "";

if ($user_group[$member_id['user_group']]['allow_short']) $stop_list = "";

$sql_select = "SELECT id, autor, date, short_story, full_story, xfields, title, category, alt_name, comm_num, allow_comm, allow_rate, rating, vote_num, news_read FROM " . PREFIX . "_post where {$stop_list}approve = '1' AND allow_main = '1' AND date < '$thisdate' order by fixed desc, ".$config['news_sort']." ".$config['news_msort']." LIMIT ".$cstart.",".$config['news_number'];
$sql_count = "SELECT COUNT(*) as count FROM " . PREFIX . "_post where {$stop_list}approve = '1' AND allow_main = '1' AND date < '$thisdate'";
$sql_news = "";


// ################ Вывод отдельной категории #################
if ($do == "cat" AND $category != '' AND $subaction == '') {
if ($cstart){
$cstart = $cstart - 1;
$cstart = $cstart * $config['news_number'];}

$url_page = $config['http_home_url']."category/".$category;

if (!$category_id) $category_id = 'not detected';

if ($allow_list[0] != "all") {
if (!$user_group[$member_id['user_group']]['allow_short'] AND !in_array ($category_id, $allow_list)) $category_id = 'not detected';
}

$sql_select = "SELECT id, autor, date, short_story, full_story, xfields, title, category, alt_name, comm_num, allow_comm, allow_rate, rating, vote_num, news_read FROM " . PREFIX . "_post where category regexp '[[:<:]]($category_id)[[:>:]]' AND approve = '1' AND date < '$thisdate' order by fixed desc, ".$config['news_sort']." ".$config['news_msort']." LIMIT ".$cstart.",".$config['news_number'];
$sql_count = "SELECT COUNT(*) as count FROM " . PREFIX . "_post where category regexp '[[:<:]]($category_id)[[:>:]]' AND approve = '1' AND date < '$thisdate'";

}
elseif ($subaction == 'userinfo')
{
// ################ Вывод для отдельного юзера #################
if ($cstart){
$cstart = $cstart - 1;
$cstart = $cstart * $config['news_number'];}
$url_page = $config['http_home_url']."user/".urlencode ($user);
$sql_select = "SELECT id, autor, date, short_story, full_story, xfields, title, category, alt_name, comm_num, allow_comm, allow_rate, rating, vote_num, news_read FROM " . PREFIX . "_post where {$stop_list}autor = '$user' AND approve = '1' AND date < '$thisdate' order by ".$config['news_sort']." ".$config['news_msort']." LIMIT ".$cstart.",".$config['news_number'];
$sql_count = "SELECT COUNT(*) as count FROM " . PREFIX . "_post where {$stop_list}autor = '$user' AND approve = '1' AND date < '$thisdate'";
$config['allow_cache'] = false;
}
elseif ($subaction == 'newposts')
{
// ################ Вывод непрочитанных новостей #################
if ($cstart){
$cstart = $cstart - 1;
$cstart = $cstart * $config['news_number'];}
$url_page = $config['http_home_url']."newposts";

$thistime = date ("Y-m-d H:i:s", (time()+ $config['date_adjust']*60));

if (isset($_SESSION['member_lasttime']))
$lasttime = date ("Y-m-d H:i:s", $_SESSION['member_lasttime']);
else
$lasttime = $thistime;

$sql_select = "SELECT id, autor, date, short_story, full_story, xfields, title, category, alt_name, comm_num, allow_comm, allow_rate, rating, vote_num, news_read FROM " . PREFIX . "_post where {$stop_list}approve = '1' AND date between '$lasttime' and '$thistime' order by ".$config['news_sort']." ".$config['news_msort']." LIMIT ".$cstart.",".$config['news_number'];
$sql_count = "SELECT COUNT(*) as count FROM " . PREFIX . "_post where {$stop_list}approve = '1' AND date between '$lasttime' and '$thistime'";
$config['allow_cache'] = false;
}
else {
// ################ Новости на главной #################
if ($year == '' AND $month == '' AND $day == '') {
if ($cstart){
$cstart = $cstart - 1;
$cstart = $cstart * $config['news_number'];}
$url_page = substr($config['http_home_url'], 0, strlen ($config['http_home_url'])-1);

$sql_select = "SELECT id, autor, date, short_story, full_story, xfields, title, category, alt_name, comm_num, allow_comm, allow_rate, rating, vote_num, news_read FROM " . PREFIX . "_post WHERE {$stop_list}approve = '1' AND allow_main = '1' AND date < '$thisdate' order by fixed desc, ".$config['news_sort']." ".$config['news_msort']." LIMIT ".$cstart.",".$config['news_number'];
$sql_count = "SELECT COUNT(*) as count FROM " . PREFIX . "_post WHERE {$stop_list}approve = '1' AND allow_main = '1' AND date < '$thisdate'";
}
// ################ Новости за год #################
if ($year != '' AND $month == '' AND $day == '') {
if ($cstart){
$cstart = $cstart - 1;
$cstart = $cstart * $config['news_number'];}
$url_page = $config['http_home_url'].$year;
$sql_select = "SELECT id, autor, date, short_story, full_story, xfields, title, category, alt_name, comm_num, allow_comm, allow_rate, rating, vote_num, news_read FROM " . PREFIX . "_post where {$stop_list}year(date) = '$year' AND approve = '1' AND date < '$thisdate' order by ".$config['news_sort']." ".$config['news_msort']." LIMIT ".$cstart.",".$config['news_number'];
$sql_count = "SELECT COUNT(*) as count FROM " . PREFIX . "_post where {$stop_list}year(date) = '$year' AND approve = '1' AND date < '$thisdate'";
}
// ################ Новости за месяц #################
if ($year != '' AND $month != '' AND $day == '') {
if ($cstart){
$cstart = $cstart - 1;
$cstart = $cstart * $config['news_number'];}
$url_page = $config['http_home_url'].$year."/".$month;
$sql_select = "SELECT id, autor, date, short_story, full_story, xfields, title, category, alt_name, comm_num, allow_comm, allow_rate, rating, vote_num, news_read FROM " . PREFIX . "_post where {$stop_list}month(date) = '$month' AND year(date) = '$year' AND approve = '1' AND date < '$thisdate' order by ".$config['news_sort']." ".$config['news_msort']." LIMIT ".$cstart.",".$config['news_number'];
$sql_count = "SELECT COUNT(*) as count FROM " . PREFIX . "_post where {$stop_list}DATE_FORMAT(date,'%Y-%m') = '{$year}-{$month}' AND approve = '1' AND date < '$thisdate'";
}
// ################ Новости за день #################
if ($year != '' AND $month != '' AND $day != '' AND $subaction == '') {
if ($cstart){
$cstart = $cstart - 1;
$cstart = $cstart * $config['news_number'];}
$url_page = $config['http_home_url'].$year."/".$month."/".$day;
$sql_select = "SELECT id, autor, date, short_story, full_story, xfields, title, category, alt_name, comm_num, allow_comm, allow_rate, rating, vote_num, news_read FROM " . PREFIX . "_post where {$stop_list}DATE_FORMAT(date,'%Y-%m-%d') = '{$year}-{$month}-{$day}' AND approve = '1' AND date < '$thisdate' order by ".$config['news_sort']." ".$config['news_msort']." LIMIT ".$cstart.",".$config['news_number'];
$sql_count = "SELECT COUNT(*) as count FROM " . PREFIX . "_post where {$stop_list}DATE_FORMAT(date,'%Y-%m-%d') = '{$year}-{$month}-{$day}' AND approve = '1' AND date < '$thisdate'";

$config['allow_cache'] = false;
}
// ################ Новость целиком #################
if ($subaction != '' OR $newsid !='') {
if ($config['allow_alt_url'] == "yes")
$sql_news = "SELECT id, autor, date, short_story, full_story, xfields, title, category, descr, keywords, alt_name, comm_num, allow_comm, allow_rate, rating, vote_num, news_read FROM " . PREFIX . "_post where DATE_FORMAT(date,'%Y-%m-%d') = '{$year}-{$month}-{$day}' AND alt_name ='$news_name' AND approve = '1'";
else
$sql_news = "SELECT id, autor, date, short_story, full_story, xfields, title, category, descr, keywords, alt_name, comm_num, allow_comm, allow_rate, rating, vote_num, news_read FROM " . PREFIX . "_post where  id = '$newsid' AND approve = '1'";
if ($subaction == '') $subaction = "showfull";
}
}



//####################################################################################################################
//             Добавление рейтинга
//####################################################################################################################
if ($doaction == "rate" and $_POST['news_id']){

$news_id = mysql_escape_string($_POST['news_id']);
$go_rate = mysql_escape_string($_POST['go_rate']);

if ($_SERVER['REMOTE_ADDR']) $ip = $_SERVER['REMOTE_ADDR'];
else $ip = "not detected";

$row = $db->super_query("SELECT news_id FROM " . PREFIX . "_logs where news_id ='$news_id' AND (ip ='$ip' OR member = '$member_id[name]')");

if (!$row['news_id']){
$db->query("UPDATE " . PREFIX . "_post set rating=rating+'$go_rate', vote_num=vote_num+1 where id ='$news_id'");

if ($is_logged) $user_name = $member_id['name'];
else $user_name = "noname";

$db->query("INSERT INTO " . PREFIX . "_logs (news_id, ip, member) values ('$news_id', '$ip', '$user_name')");

clear_cache();
	}
}

if ($login == "submit" and !$is_logged) msgbox ($lang['login_err'], $lang['login_err_1']);

$allow_add_comment         = FALSE;
$allow_full_story          = FALSE;
$allow_active_news         = FALSE;
$allow_comments            = FALSE;
$allow_userinfo            = FALSE;
$active 				   = FALSE;

if(($subaction == "showfull" or $subaction == "addcomment") and ((!isset($category) or $category == "")) ){

//####################################################################################################################
//             Добавление комментария в базу данных
//####################################################################################################################
    if($subaction == "addcomment")
		{ 
			$allow_add_comment    = TRUE; 
			$allow_comments = TRUE;
			$ajax_adds = false;

			include_once (ENGINE_DIR.'/modules/addcomments.php');
		}
//####################################################################################################################
//         Просмотр полной новости
//####################################################################################################################
    if($subaction == "showfull") { 
			$allow_full_story = TRUE; 
			$allow_comments = TRUE;

			include (ENGINE_DIR.'/modules/show.full.php');
		}

}
else{
			$allow_active_news = TRUE; 

//####################################################################################################################
//         Просмотр профиля пользователя
//####################################################################################################################
	if ($subaction == 'userinfo'){ 
			$allow_userinfo = TRUE; 

			include_once (ENGINE_DIR.'/modules/profile.php');
		}

//####################################################################################################################
//         Просмотр кратких новостей
//####################################################################################################################

	if ($is_logged AND ($user_group[$member_id['user_group']]['allow_edit'] AND !$user_group[$member_id['user_group']]['allow_all_edit'])) $config['allow_cache'] = false;

	if ($cstart) $cache_id = ($cstart / $config['news_number']) + 1; else $cache_id = "1";
	
	if ($cache_id < 8 AND $login != "submit") $active = dle_cache("news", $config['skin'].$cache_id.$month.$year.$category, true); else $active = false;

		if ($active) $tpl->result['content'] .= $active;
		else {

		include(ENGINE_DIR.'/modules/show.short.php');

		if ($config['files_allow'] == "yes")
			if ( stristr( $tpl->result['content'], "[attachment=" ) )
			{
				$tpl->result['content'] = show_attach($tpl->result['content'], $attachments);
			}

		if ($news_found AND $cache_id < 8) create_cache("news", $tpl->result['content'], $config['skin'].$cache_id.$month.$year.$category, true);

		}

	}

}

/*
=====================================================
 Вывод заголовка страницы 
=====================================================
*/
$titl_e = '';
$nam_e ='';

if ($do == "cat" AND $category != '' AND $subaction == '') 
{

	$metatags['description'] = ($cat_info[$category_id]['descr'] != '') ? $cat_info[$category_id]['descr'] : $metatags['description'];
	$metatags['keywords'] = ($cat_info[$category_id]['keywords'] != '') ? $cat_info[$category_id]['keywords'] : $metatags['keywords'];
	$nam_e= $cat_info[$category_id]['name'];
}
elseif ($subaction == 'userinfo') $metatags['title'] = $user.' &raquo; '.$metatags['title'].' | '.$lang['title_users'];
elseif ($subaction == 'newposts') $nam_e=$lang['title_new'];
elseif ($do == 'stats') $nam_e=$lang['title_stats']; 
elseif ($do == 'addnews') $nam_e=$lang['title_addnews'];
elseif ($do == 'register') $nam_e=$lang['title_register']; 
elseif ($do == 'favorites') $nam_e=$lang['title_fav']; 
elseif ($do == 'pm') $nam_e=$lang['title_pm']; 
elseif ($do == 'feedback') $nam_e=$lang['title_feed'];
elseif ($do == 'static') $titl_e=$static_descr; 
else {

    if ($year != '' AND $month == '' AND $day == '') $nam_e=$lang['title_date'].' '.$year.' '.$lang['title_year'];
    if ($year != '' AND $month != '' AND $day == '') $nam_e=$lang['title_date'].' '.$r[$month-1].' '.$year.' '.$lang['title_year1']; 
    if ($year != '' AND $month != '' AND $day != '' AND $subaction == '') $nam_e=$lang['title_date'].' '.$day.'.'.$month.'.'.$year;
    if ($subaction != '' OR $newsid !='') $titl_e=$metatags['title'];

     }

if($nam_e) $metatags['title'] .=' | '.$nam_e;
if($titl_e) $metatags['title'] = $titl_e.' &raquo; '.$config['home_title'];

$metatags = <<<HTML
<title>{$metatags['title']}</title>
<meta http-equiv="Content-Type" content="text/html; charset={$config['charset']}" />
<meta name="description" content="{$metatags['description']}" />
<meta name="keywords" content="{$metatags['keywords']}" />
<meta name="generator" content="DataLife Engine v.{$config['version_id']} (http://pc-soft.ru)" />
<meta name="robots" content="all" />
<meta name="revisit-after" content="1 days" />
HTML;
?>