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

	$allow_list = explode( ',', $user_group[$member_id['user_group']]['allow_cats'] );
	$perm = 1;
	$i = 0;
	$news_found = false;
    $allow_full_cache = false;

    $cprefix = "full_".$newsid;
    $row = $cache->open( $cprefix, $sql_news );

    if( $row ) {
	    $row = unserialize($row);
	    $full_cache = true;
    } else {
	    $row = $db->super_query( $sql_news );
	    $full_cache = false;
    }

	$options = news_permission( $row['access'] );
    if( $options[$member_id['user_group']] and $options[$member_id['user_group']] != 3 ) $perm = 1;
    if( $options[$member_id['user_group']] == 3 ) $perm = 0;

    if( $options[$member_id['user_group']] == 1 ) $user_group[$member_id['user_group']]['allow_addc'] = 0;
    if( $options[$member_id['user_group']] == 2 ) $user_group[$member_id['user_group']]['allow_addc'] = 1;

    if( ! $row['approve'] and $member_id['name'] != $row['autor'] and $member_id['user_group'] != '1' ) $perm = 0;
    if( ! $row['approve'] ) $allow_comments = false;

	if( ! $row['category'] ) {
		$my_cat = "---";
		$my_cat_link = "---";
	} else {
		$my_cat = array ();
		$my_cat_link = array ();
		$cat_list = explode( ',', $row['category'] );
	if( count( $cat_list ) == 1 ) {
		if( $allow_list[0] != "all" and ! in_array( $cat_list[0], $allow_list ) ) $perm = 0;
		$my_cat[] = $cat_info[$cat_list[0]]['name'];
		$my_cat_link = get_categories( $cat_list[0] );
	} else {
		foreach ( $cat_list as $element ) {
			if( $allow_list[0] != "all" and ! in_array( $element, $allow_list ) ) $perm = 0;
			if( $element ) {
				$my_cat[] = $cat_info[$element]['name'];
				$my_cat_link[] = "<a href=\"" . $config['http_home_url'] . get_url( $element ) . "/\">{$cat_info[$element]['name']}</a>";
			}}$my_cat_link = implode( ', ', $my_cat_link );
			}$my_cat = implode( ', ', $my_cat );
		}

        if ( $row['id'] AND  $perm ){
        if( strtotime($row['date']) >= ($_TIME - 2592000) ) {$allow_full_cache = true;}
        if ($allow_full_cache AND !$full_cache) $cache->save( $cprefix, serialize($row), $sql_news );

        if( $row['votes'] ) include_once SYSTEM_DIR . '/modules/poll.php';
		$row['category'] = intval( $row['category'] );
		$category_id = $row['category'];
		
		$tpl->load_template( 'fullstory.tpl' );

		if (!$_SESSION["news_read_{$row['id']}"]){
			$db->query( "UPDATE " . PREFIX . "_post set news_read=news_read+1 where id='{$row['id']}'" );
			$_SESSION["news_read_{$row['id']}"] = "1";
		}

		$news_found = TRUE;
        $row['date'] = strtotime( $row['date'] );
        $tpl->set('{id}', $row['id']);
        $tpl->set('{date-month}',$r[(int)date("m",$row['date'])-1]);
        $tpl->set('{date-day}',date("d",$row['date']));

        //???????? ???????????? URL
         preg_match( '/'.$row['id'].'-(.*?).html/is' , $_SERVER["REQUEST_URI"] , $parse_alt_name );
         if($row['alt_name'] != trim($parse_alt_name[1]) )$news_found = FALSE;
         if( !$news_found ){
             @header( "HTTP/1.0 404 Not Found" );
             require_once ROOT_DIR.'/404.php';
             die();}
        //!???????? ???????????? URL

		if( (strlen( $row['full_story'] ) < 13) and (strpos( $tpl->copy_template, "{short-story}" ) === false) ) {
			$row['full_story'] = $row['short_story'];
		}

					$full_link = $config['http_home_url'] . $row['id'] . "-" . $row['alt_name'] . ".html";
					$short_link = $config['http_home_url'];
					$row['alt_name'] = $row['id'] . "-" . $row['alt_name'];
					$link_page = $config['http_home_url'] . 'page,' . $news_page . ',';
					$news_name = $row['alt_name'];
		
		$i ++;

		$metatags['title'] = stripslashes( $row['title'] ) .' » '.$config['home_title'];
		$comments_num = $row['comm_num'];

        if ($is_logged and $comments_num > 0){
            $tpl->set('[comm_sort]', "");
            $tpl->set('{comm_sort}', comm_sort());
            $tpl->set('[/comm_sort]', "");
        }else{$tpl->set_block("'\\[comm_sort\\](.*?)\\[/comm_sort\\]'si", "");}

		$news_find = array ('{comments-num}' => $comments_num, '{views}' => $row['news_read'], '{category}' => $my_cat, '{link-category}' => $my_cat_link, '{news-id}' => $row['id'] );
		
		if( date( Ymd, $row['date'] ) == date( Ymd, $_TIME ) ) {$tpl->set( '{date}', $lang['time_heute'] . langdate( ", H:i", $row['date'] ) );
		} elseif( date( Ymd, $row['date'] ) == date( Ymd, ($_TIME - 86400) ) ) {
			$tpl->set( '{date}', $lang['time_gestern'] . langdate( ", H:i", $row['date'] ) );
		} else {$tpl->set( '{date}', langdate( $config['timestamp_active'], $row['date'] ) );}
		$tpl->copy_template = preg_replace ( "#\{date=(.+?)\}#ie", "langdate('\\1', '{$row['date']}')", $tpl->copy_template );
		
		if( $row['tags'] ) {
			$tpl->set( '[tags]', "" );
			$tpl->set( '[/tags]', "" );
			$tags = array ();
			$row['tags'] = explode( ",", $row['tags'] );
			foreach ( $row['tags'] as $value ) {
				$value = trim( $value );
				$tags[] = "<a href=\"" . $config['http_home_url'] . "tags/" . urlencode( $value ) . "/\">" . $value . "</a>";
			}$tpl->set( '{tags}', implode( ", ", $tags ) );
		} else {
			$tpl->set_block( "'\\[tags\\](.*?)\\[/tags\\]'si", "" );
			$tpl->set( '{tags}', "" );
		}
		
		$tpl->set( '', $news_find );

        if( $row['allow_rate'] ) {$tpl->set( '{rating}', ShortRating( $row['id'], $row['rating'], $user_group[$member_id['user_group']]['allow_rating'] ) );
        }else $tpl->set( '{rating}', "0" );
		
        $tpl->set( '[day-news]', "<a href=\"".$config['http_home_url'] . date( 'Y/m/d/', $row['date'])."\" >" );
		$tpl->set( '[/day-news]', "</a>" );

        $go_page = $config['http_home_url'] . "user/" . urlencode( $row['autor'] ) . "/";

        $tpl->set( '[profile]', "<a href=\"" . $go_page . "\">" );
        $tpl->set( '[/profile]', "</a>" );
		
/*        if ($user_group[$user_color[$row['autor']]['user_group']]['colour']){
   			$group_span = $user_group[$user_color[$row['autor']]['user_group']]['colour'];
            $user =  "<font color={$group_span}>".$row['autor']."</font>";
   		}else{*/
            $user = $row['autor'];
   		//}

        $tpl->set( '{author}', "<a href=\"" . $go_page . "\">" .$user. "</a>" );
		
		$_SESSION['referrer'] = $_SERVER['REQUEST_URI'];
		
        if( $row['remote_addr'] != "" ) {
            $full_link = $config['http_home_url'] . "out.php?url=" . rawurlencode( base64_encode( $row['remote_addr'] ) );
            $tpl->set( '[full-link]', "<a href=\"" . $full_link . "\" target=\"_blank\">" );
            $tpl->set( '[/full-link]', "</a>" );
        } else {
            $full_link = $config['http_home_url'] . $row['id'] . "-" . $row['alt_name'] . ".html";
            $tpl->set( '[full-link]', "<a href=\"" . $full_link . "\">" );
            $tpl->set( '[/full-link]', "</a>" );
        }
		
		$tpl->set( '{full-link}', $full_link );
		
		if( $row['allow_comm'] ) {
			$tpl->set( '[com-link]', "<a href=\"" . $full_link . "#comment\">" );
			$tpl->set( '[/com-link]', "</a>" );
		} else
			$tpl->set_block( "'\\[com-link\\](.*?)\\[/com-link\\]'si", "" );
		
		if( ! $row['approve'] and ($member_id['name'] == $row['autor'] and ! $user_group[$member_id['user_group']]['allow_all_edit']) ) {
			$tpl->set( '[edit]', "<a href=\"" . $config['http_home_url'] . "index.php?do=addnews&amp;id=" . $row['id'] . "\" >" );
			$tpl->set( '[/edit]', "</a>" );
		} elseif( $is_logged and (($member_id['name'] == $row['autor'] and $user_group[$member_id['user_group']]['allow_edit']) or $user_group[$member_id['user_group']]['allow_all_edit']) ) {
			$tpl->set( '[edit]', "<a href=\"/control.php?mod=editnews&action=editnews&id=$row[id]\" target=\"_blank\">" );
			$tpl->set( '[/edit]', "</a>" );
		} else
			$tpl->set_block( "'\\[edit\\](.*?)\\[/edit\\]'si", "" );
		
//??????? ?? ????
			if( $config['allow_cache'] != "yes" ) {
				$config['allow_cache'] = "yes";
				$revert_cache = true;
			} else
				$revert_cache = false;

			if( $revert_cache ) $config['allow_cache'] = "no";
		
//!??????? ?? ????
		
		if( $is_logged ) {
			$fav_arr = explode( ',', $member_id['favorites'] );
			if( ! in_array( $row['id'], $fav_arr ) ) $tpl->set( '{favorites}', "<a id=\"fav-id-" . $row['id'] . "\" href=\"$PHP_SELF?do=favorites&amp;doaction=add&amp;id=" . $row['id'] . "\"><img src=\"" . $config['http_home_url'] . "templates/{$config['skin']}/images/add_fav.png\" onclick=\"doFavorites('" . $row['id'] . "', 'plus'); return false;\" title=\"" . $lang['news_addfav'] . "\" style=\"vertical-align: middle;border: none;\" alt=\"\" /></a>" );
			else $tpl->set( '{favorites}', "<a id=\"fav-id-" . $row['id'] . "\" href=\"$PHP_SELF?do=favorites&amp;doaction=del&amp;id=" . $row['id'] . "\"><img src=\"" . $config['http_home_url'] . "templates/{$config['skin']}/images/del_fav.png\" onclick=\"doFavorites('" . $row['id'] . "', 'minus'); return false;\" title=\"" . $lang['news_minfav'] . "\" style=\"vertical-align: middle;border: none;\" alt=\"\" /></a>" );
		} else $tpl->set( '{favorites}', "" );

        if( $is_logged ){
            $subscribe = $db->super_query("SELECT COUNT(*) as count FROM " . PREFIX . "_subscribe WHERE news_id=$row[id] AND user_id=$member_id[user_id]");
            if($subscribe['count'] == 0 ) $tpl->set( '{sibscribe}', "<a href=\"#\" id=\"subscribe-id-" . $row['id'] . "\" onclick=\"comm_Subscribe('" . $row['id'] . "'); return false;\">Подписаться</a>" );
            else $tpl->set( '{sibscribe}', "<a href=\"#\" id=\"subscribe-id-" . $row['id'] . "\" onclick=\"comm_Subscribe('" . $row['id'] . "'); return false;\">Отписаться</a>" );
        }
		
		if( $row['votes'] ) $tpl->set( '{poll}', $tpl->result['poll'] );
		else $tpl->set( '{poll}', '' );

		$tpl->set( '{title}', stripslashes( $row['title'] ));


		if ($smartphone_detected) {
			if (!$config['allow_smart_format']) {
					$row['short_story'] = strip_tags( $row['short_story'], '<p><br><a>' );
					$row['full_story'] = strip_tags( $row['full_story'], '<p><br><a>' );
			} else {
				if ( !$config['allow_smart_images'] ) {
					$row['short_story'] = preg_replace( "#<!--TBegin-->(.+?)<!--TEnd-->#is", "", $row['short_story'] );
					$row['short_story'] = preg_replace( "#<img(.+?)>#is", "", $row['short_story'] );
					$row['full_story'] = preg_replace( "#<!--TBegin-->(.+?)<!--TEnd-->#is", "", $row['full_story'] );
					$row['full_story'] = preg_replace( "#<img(.+?)>#is", "", $row['full_story'] );
				}
				if ( !$config['allow_smart_video'] ) {
					$row['short_story'] = preg_replace( "#<!--dle_video_begin(.+?)<!--dle_video_end-->#is", "", $row['short_story'] );
					$row['short_story'] = preg_replace( "#<!--dle_audio_begin(.+?)<!--dle_audio_end-->#is", "", $row['short_story'] );
					$row['full_story'] = preg_replace( "#<!--dle_video_begin(.+?)<!--dle_video_end-->#is", "", $row['full_story'] );
					$row['full_story'] = preg_replace( "#<!--dle_audio_begin(.+?)<!--dle_audio_end-->#is", "", $row['full_story'] );
		}}}

		$tpl->set( '{short-story}', stripslashes( $row['short_story'] ) );
		$tpl->set( '{full-story}', stripslashes( "<div id='news-id-" . $row['id'] . "'>" . $row['full_story'] . "</div>" ) );

        include_once SYSTEM_DIR . '/modules/newsimg.php';
		
		if( $row['keywords'] == '' and $row['descr'] == '' ) create_keywords( $row['short_story'] . $row['full_story'] );
		else {$metatags['keywords'] = $row['keywords']; $metatags['description'] = $row['descr'];}
		if ($row['metatitle']) $metatags['header_title'] = $row['metatitle'];

		$tpl->compile( 'content' );

		if( $user_group[$member_id['user_group']]['allow_hide'] ) $tpl->result['content'] = preg_replace( "'\[hide\](.*?)\[/hide\]'si", "\\1", $tpl->result['content']);
		else $tpl->result['content'] = preg_replace ( "'\[hide\](.*?)\[/hide\]'si", "<div class=\"quote\">" . $lang['news_regus'] . "</div>", $tpl->result['content'] );
        $tpl->result['content'] = preg_replace ( "'\[group=(.*?)\](.*?)\[/group\]'sie",'group_hide($1,"\\2")', $tpl->result['content']);

		$news_id = $row['id'];
		$allow_comments = $row['allow_comm'];

		$allow_add = true;
		
		if( isset( $view_template ) ) $allow_comments = false;
	}
	
	$tpl->clear();
    unset( $row );
	
	if( $config['files_allow'] == "yes" ){
        if( strpos( $tpl->result['content'], "[file=" ) !== false ) {$tpl->result['content'] = show_file( $tpl->result['content'], $news_id );}
	    if( strpos( $tpl->result['content'], "[torrent=" ) !== false ) {$tpl->result['content'] = show_torrent( $tpl->result['content'], $news_id );}
    }

if( ! $news_found and ! $perm ) msgbox( $lang['all_err_1'], "<b>{$user_group[$member_id['user_group']]['group_name']}</b> " . $lang['news_err_28'] );
	elseif( ! $news_found ) {
		@header( "HTTP/1.0 404 Not Found" );
		require_once ROOT_DIR.'/404.php';
        die();
	}

if($news_found) {
	include_once SYSTEM_DIR . '/classes/comments.class.php';
    $comments = new Comments( $db, $comments_num, intval($config['comm_nummers']) );
    $comments->nid = $news_id;

    $comments->query = "SELECT " . PREFIX . "_comments.id, post_id, " . PREFIX . "_comments.user_id, date, autor as gast_name, " . PREFIX . "_comments.email as gast_email, text, ip, is_register, name, rating, " . USERPREFIX . "_users.email, news_num, comm_num, user_group, reg_date, signature, lastdate, foto, fullname, land, icq, repa, repa_mod, repa_off FROM " . PREFIX . "_comments LEFT JOIN " . USERPREFIX . "_users ON " . PREFIX . "_comments.user_id=" . USERPREFIX . "_users.user_id WHERE " . PREFIX . "_comments.post_id = '$news_id' ORDER BY date ".$dle_comm_sort;
    if ( $allow_full_cache ) $allow_full_cache = $news_id;    

	$comments->build_comments('news', $allow_full_cache );
	$comments->build_navigation('navigation.tpl', $link_page . "{page}," . $news_name . ".html#comment", "newsid=" . $newsid);
    $comments->build_comm_form();
    unset ($comments);

}
?>