<?php
// ======================================================================== //
//                                                                          //
//   KooBI- Webportalsystem                                                 //
//   SCRiPTMAFiA 2005 - THE DiRTY HANDS ON YOUR SCRiPTS                     //
//                                                                          //
//   ====================================================================   //
//                                                                          //   
//   dream4 | webservices - Am Michelsgrund 7 - 69469 Weinheim - Germany    //    
//   tel. +49(0) 6201 - 980442 / fax. +49(0) 6201 - 64809                   //
//                                                                          //
// ======================================================================== //
if(!checkactive(news)) include(dirname(__FILE__)."/lib/notactive.php" );

$limit = limit();
if (!permission("shownews")) {
	$EOUT = stderror("nopermission", $lang['nopermission_t'], str_replace("__URL__", "index.php", $lang['redirect']), "index.php", 1);
	$error = 1;
	$NOOUT = 1;
}

if(!isset($_REQUEST['newsid'])){
	header("location:index.php");
}

$cookie_id = "news_" . $_REQUEST['newsid'] . "_" . $area . "_readed";
if($_COOKIE["news_" . $_REQUEST['newsid'] . "_" . $area] !=  $cookie_id){
	$sql = new dbc("UPDATE ".PREFIX."_news SET hits=hits+1 WHERE newsid='".escs($_REQUEST['newsid'])."'");
	@setcookie ("news_" . $_REQUEST['newsid'] . "_" . $area, $cookie_id, time()+3600); 
}
		


$sql = new dbc("SELECT * FROM ".PREFIX."_news WHERE (newsid='".escs($_REQUEST['newsid'])."') && (((time_end>=".time().") || (time_end='1') || (time_end='0')) && (((ispublic='1')) && (time_start<=".time().")))");
$row = $sql->fetchrow();

// ===============================================================
// <<-- FEHLERHAFTER AUFRUF  -->>
// ===============================================================
if( ($sql->numrows()<1) || (!is_numeric($_REQUEST['newsid'])) ){
	$EOUT = stderror("error_once", $lang['documentnotexists'], "", "", "");
	$error = 1;
	$MISC = 1;
}

if($error!=1){
	$cancomment = $row->cancomment;
	$title = kstripslashes($row->title);
	
	
	$sname = simpletitle($lang['newspage'] . $lang['f_navi_separator'] . getNavigationptitle($row->newscat, 'newscat', '', '','catid','catname') . $lang['f_navi_separator'] . $title);
	$tmpl->assign('navigation', getNavigationPath($row->newscat, 'newscat', 'index', 'categ','catid','catname'));
	$header_nav = getNavigationPath($row->newscat, 'newscat', 'newsarchive', 'categ','catid','catname');
	
	
	
	
	
	// ===============================================================
	// <<-- SEITEN -->>
	// ===============================================================
	if(!isset($_REQUEST['artpage'])){ $_REQUEST['artpage'] = 1; };
	
	$newstext = kstripslashes($row->textmore);
	$seite_anzeigen = explode("[--NEU--]", $newstext);
	$anzahl_seiten = count($seite_anzeigen);
	$text = $seite_anzeigen[$_REQUEST['artpage']-1];
	
	
	
	if(isset($_REQUEST['print'])) {
		$prinlink = "&amp;print=1";
	}
	
	if($anzahl_seiten>1){
		$navi = artpage($anzahl_seiten, $lang['aktpos'], " <a class=\"page_navigation\" href=\"index.php?nav=1&amp;area=$area&amp;p=news&amp;newsid=".$_REQUEST['newsid']."$prinlink&amp;artpage={s}\">{t}</a> ");
	}
	
	// ===============================================================
	// <<-- HIER WIRD VERSUCHT, DEN BESTMÖGLICHSTEN KOMPROMIS ZU FINDEN, 
	//      DER KEINE COOKIES BENÖTIGT. ES WIRD DIE IP, SOWOHL DIE USER-
	//      ID DES BESUCHERS GESPEICHERT.  -->>
	// ===============================================================
	if($row->canvote=="yes"){
		$votes_ip = explode(";", $row->votes_ip);
		$votes_uid = explode(";", $row->votes_uid);
		
		if ( (@in_array($_SERVER['REMOTE_ADDR'], $votes_ip )) || (@in_array(UID, $votes_uid)) ) {
			$canvote = -1;
			$showres = 1;
		} else {
			$canvote = 1;
			$showres = -1;
			
			// ===============================================================
			// <<-- BEWERTUNG ÜBERNEHMEN -->>
			// ===============================================================
			if($_REQUEST['dopoll']==1){
				$sql = new dbc("
				UPDATE ".PREFIX."_news 
				SET 
				votes_data=votes_data+'".escs($_REQUEST['vote'])."', 
				votes=votes+1 
				WHERE 
				newsid='".escs($_REQUEST['newsid'])."'");
				
				// ===============================================================
				// <<-- IP, BZW. UID DER BEWERTUNG ANFÜGEN -->>
				// ===============================================================
				if(UGROUP != 2) { $zusatz = " , votes_uid = CONCAT(votes_uid, ';', '" . UID . "')"; }
				$sql = new dbc("UPDATE " . PREFIX . "_news SET
				votes_ip = CONCAT(votes_ip, ';', '" . $_SERVER['REMOTE_ADDR'] . "') 
				$zusatz 
				WHERE newsid = $_REQUEST[newsid]");
				header("Location:index.php?p=news&newsid=".$_REQUEST['newsid']."&area=".$_REQUEST['area']."");
			}
		}
		$tmpl->assign('canvote', $canvote);
		$tmpl->assign('showres', $showres);
		if($row->votes>0){ $note = round($row->votes_data/$row->votes,1); }	
		
		$artpollerg = $lang['artpollerg'];
		$artpollerg = str_replace("__BESUCHERN__", $row->votes, $artpollerg);
		$artpollerg = str_replace("__NOTE__", $note, $artpollerg);
	}
	// ===============================================================
	// <<-- ENDE BEWERTUNG -->>>
	// ===============================================================
	
	if ($row->image != "") {
		$tmpl->assign("newsbild", $row->image);
		$tmpl->assign("align", $row->image_align);
		$tmpl->assign("newsbild_ja", 1);
	}
	
	
	// ===============================================================
	// <<-- GALERIEN ZU DIESEM ARTIKEL -->>
	// ===============================================================
	
	$sql_linkedgals = new dbc("SELECT galids,newscat FROM " . PREFIX . "_news WHERE newsid = '". $_REQUEST['newsid'] . "'" );
	$rowlinked_gals = $sql_linkedgals->fetchrow();
	
	if($rowlinked_gals->galids){
		$linked_gals = explode(",", $rowlinked_gals->galids);
		$i = 0;
		foreach ($linked_gals as $value) {
			$sql = new dbc("SELECT id,title,ctime FROM " . PREFIX . "_gallery WHERE id=$value" );
			$row_g = $sql->fetchrow();
			if($row_g->ctime > 100){
				$galname[$i]['name'] = $row_g->title;
				$galname[$i]['galid'] = $value;
				$galname[$i]['time'] = mygmt($row_g->ctime);
				
				$sql = new dbc("SELECT id,path FROM ".PREFIX."_gallery_items WHERE  gal_id='".$row_g->id."' order by rand() limit 1");
				$row_e = $sql->fetchrow();
				if(is_file("uploads/galerie/".$row_e->path)){
					$galname[$i]['id'] = $row_e->id ;
				}
				$i++;
			}
		}
	}
	
	// ===============================================================
	// <<-- VERKNUEPFTE NEWS & ARTIKEL
	//      SOLLTEN PRODUKTE VERWENDET WERDEN, WERDEN NEWS DES GLEICHEN
	//      PRODUKTS AUSGELESEN, WENN NICHT, WIRD DAS SCHLAGWORT ALS 
	//      VERKNUEPFUNG GEWÄHLT.  -->>
	// ===============================================================
	if(($row->product!="") && ($row->product!=0) ){
		$dbfirst = "product='$row->product'";
	} else {
		if($row->matchword!="") { 
			$dbfirst = "matchword like '%$row->matchword%'"; 
		} else {
			$dbfirst = "matchword = 'unknown'";
		}
	}
	// ===============================================================
	// <<-- NEWS -->>
	// ===============================================================
	$q = "SELECT title,newsid,time_start FROM ".PREFIX."_news 
	WHERE
	($dbfirst AND newsid!=".$_REQUEST['newsid'].") 
	&& 
	(((time_end>=".time().") || (time_end='1') || (time_end='0')) && (((ispublic='1')) && (time_start<=".time().")))
	ORDER BY time_start DESC";
	
	if( $row->product!="" || $row->matchword!="" ) { 
		$sql = new dbc($q);
		$i = 0;
		while($row_linkednews = $sql->fetchrow()){
			$linked_news[$i]['title'] = $row_linkednews->title;
			$linked_news[$i]['newsid'] = $row_linkednews->newsid;
			$linked_news[$i]['time'] = mygmt($row_linkednews->time_start);
			$i++;
		}
		$tmpl->assign('linkednews', $linked_news);
	}
	// ===============================================================
	// <<-- ARTIKEL -->>
	// ===============================================================
	$sql2 = new dbc("SELECT id, title, time_start, type FROM ".PREFIX."_articles   
	WHERE 
	($dbfirst)
	&&
	(((time_end>=".time().") || (time_end='1') || (time_end='0')) && (((active='1')) && (time_start<=".time().")))
	ORDER BY time_start DESC");
	$i = 0;
	include(BASEDIR . '/inc/functions_top.php');
	while($row_linkedarticles = $sql2->fetchrow()){
		$linked_articles[$i]['title'] = $row_linkedarticles->title;
		$linked_articles[$i]['id'] = $row_linkedarticles->id;
		$linked_articles[$i]['time'] = mygmt($row_linkedarticles->time_start);
		$linked_articles[$i]['type'] = getArticleType($row_linkedarticles->type);
		$linked_articles[$i]['type_num'] = $row_linkedarticles->type;
		$i++;
	}
	
	
	$visitors_vote = explode(",", $lang['vvoting']);
	$news = autowords(stripslashes($row->text));
	$news = high($news);
	$news = inlinescreens($row->inline_screens, $news);

	$newsmore = eregi_replace("\[--NEU--\]", "", $text);
	$newsmore = autowords(stripslashes($newsmore));
	$newsmore = high($newsmore);
	$newsmore = inlinescreens($row->inline_screens, $newsmore);
	
	$tmpl->assign('vvote', $visitors_vote);
	$tmpl->assign('linkedarticles', $linked_articles);
	$tmpl->assign('galname', $galname);
	$tmpl->assign('area', $area);
	$tmpl->assign('navi', $navi);
	$tmpl->assign('lang', $lang);
	$tmpl->assign('row', $row);
	$tmpl->assign('artpollerg', $artpollerg);
	$tmpl->assign('showres', $showres);
	$tmpl->assign('area', $area);
	$tmpl->assign('canvote', $canvote);
	$tmpl->assign('newstitel', $title);
	$tmpl->assign('news', $news);
	$tmpl->assign('newsid', $row->newsid);
	$tmpl->assign('newsmore', $newsmore);
	$tmpl->assign('navi', $navi);
	$tmpl->assign('ntime', mygmt($row->time_start) );
	$tmpl->assign('links', fetch_links('news', $row->newsid, 'newsid'));
	$tmpl->assign('page', $_REQUEST['artpage']);
	$tmpl->assign('printtext', $lang['printtext']);
	$tmpl->assign('printlink', "index.php?p=news&amp;area=".$area."&amp;newsid=".$row->newsid."&amp;print=1");
	$tmpl->assign('userlink', "index.php?area=".$area."&amp;p=user&amp;id=".$row->uid."");
	$tmpl->assign('user', fetchusername($row->uid));
	$tmpl->register_function('permission', 'permission');
	
	
	// ===============================================================
	// <<-- ALLES ZUWEISEN -->>
	// ===============================================================
	if($linked_news) $tmpl->assign("linked_news",1);
	if(($linked_articles) && (checkactive(articles)) ) $tmpl->assign("linked_articles",1);
	if(($galname) && (checkactive(gallery)) ) $tmpl->assign("linked_gallery", 1);
	
	
	
	// ===============================================================
	// <<-- Kommentare Dürfen News kommentiert werden? -->>
	// ===============================================================
	if(COMMENTNEWS == 1){
		if($cancomment){
			
			$sql = new dbc("SELECT spamprotect,spamprotect_time,mailbycomment FROM " . PREFIX . "_settings");
			$row_spam = $sql->fetchrow();
			
			// Kommentar eintragen
			
			if($_REQUEST['post']==1){
				$error = "";
				// ====================================================================================
				// <<-- WURDE DER SPAMSCHUTZ AKTIVIERT? -->>
				// ====================================================================================
				if($row_spam->spamprotect==1){
					$sql = new dbc("SELECT ip,ctime FROM ".PREFIX."_newscomments WHERE ip='".$_SERVER['REMOTE_ADDR']."' AND newsid='".$_REQUEST['newsid']."' ORDER BY id DESC limit 1");
					
					$row = $sql->fetchrow();
					if($row){
						if( ($row->ctime) + (60*$row_spam->spamprotect_time) > time()){
							
							$EOUT = msg("spamprotect","spamprotect_text", str_replace("__URL__", "index.php?p=news&newsid=".escs($_REQUEST['newsid'])."&area=".$_REQUEST['area']."", $lang['redirect']), "index.php?p=news&newsid=".escs($_REQUEST['newsid'])."&area=".$_REQUEST['area']."");
							$NOOUT = 1;
							$error = 1;
						} 
					}
				}
				// ====================================================================================
				// <<-- WENN NICHT IN SPAMSCHUTZ- ZEIT, KOMMENTAR EINTRAGEN -->>
				// ====================================================================================
				if($error != 1){
					// ====================================================================================
					// <<-- WENN MAIL GESENDET WERDEN SOLL -->>
					// ====================================================================================
					if($row_spam->mailbycomment == 1){
						$adminlink = "http://" . $_SERVER['HTTP_HOST'] . str_replace("/index.php","",$_SERVER['PHP_SELF']) . "/admin/news.php?area=$_REQUEST[area]&action=comments&id=$_REQUEST[newsid]";
						$path = dirname(__FILE__);
						include( $path ."/lib/mailbycomment.php" );
					}
					
					
					$text = formtext($_POST['text'],$MAXCOMMLENGTH);
					$sql = new dbc("
					INSERT INTO ".PREFIX."_newscomments(
					id,
					newsid,
					ctime,
					autor,
					title,
					comment,
					ip
					) VALUES (
					'',
					'".escs($_REQUEST['newsid'])."',
					'".time()."',
					'".$UID."',
					'".iform($_POST['title'],"255","")."',
					'".$text."','".$_SERVER['REMOTE_ADDR']."'
					) ");
					$EOUT = msg("thankyou","pollcomments_thanks", str_replace("__URL__", "index.php?p=news&newsid=".escs($_REQUEST['newsid'])."&area=".$_REQUEST['area']."", $lang['redirect']), "index.php?p=news&newsid=".escs($_REQUEST['newsid'])."&area=".$_REQUEST['area']."");
					$NOOUT = 1;
				}
			}
			
			
			if( !isset($_REQUEST['sort']) && $_REQUEST['sort']!="desc" && $_REQUEST['sort']!="asc" ){
				$sorts = "desc";
				$_REQUEST['sort'] = "desc";
			} else {
				$sorts = $_REQUEST['sort'];
				$_REQUEST['sort'] = $_REQUEST['sort'];
			}
			
			
			
			$sql =& new dbc("SELECT id FROM ".PREFIX."_newscomments WHERE newsid='".escs($_REQUEST['newsid'])."'");
			$num = $sql->numrows();
			
			$seiten = ceil($num / $limit);
			$a = prepage() * $limit - $limit;
			
			$sql =& new dbc("SELECT * FROM ".PREFIX."_newscomments WHERE newsid='".escs($_REQUEST['newsid'])."' ORDER BY id $sorts limit $a,$limit");
			$erg = $sql->numrows();
			
			
			$path = dirname(__FILE__);
			require_once($path ."/lib/pagesobjects.php" );
			require_once($path ."/lib/commentobjects.php" );
			
			// Seiten-Navigation generieren und Kommentare dem Container zuweisen...
			
			
			if($num>$limit){
				$nav = pagenav($seiten, $lang['aktpos'], " <a class=\"page_navigation\" href=\"index.php?p=news&amp;newsid=".escs($_REQUEST['newsid'])."&amp;area=".$area."&amp;pp=".$limit."&amp;sort=".$_REQUEST['sort']."&amp;page={s}\">{t}</a> ");
			}
			
			$tmpl->assign('pages', $nav);
			$tmpl->assign("guest", $lang['guest']);
			$tmpl->assign("comments_array", $comments_array);
			$tmpl->assign("comments", parsetrue("container/".container("comments"), $lang['newscomments'], $tmpl->fetch("comments/comments.tpl")));
			
			// Hat Benutzer die Rechte zu kommenteren?
			if(permission("newscomment")){
				$tmpl->assign('comment', $lang['commentonce']);
				$tmpl->assign("formlink", "index.php?p=news&amp;newsid=".escs($_REQUEST['newsid'])."&amp;post=1&amp;area=".$area."");
				
				if(COMMENTSMILEYS == 1) {
					$tmpl->assign('smilie', 1);
					$tmpl->assign('listemos', $smilies);
				}
				
				if (COMMENTSBBCODE == 1) {
					$tmpl->assign('bbcodes', 1);
				}
				
				if($_REQUEST['print']!=1){
					$tmpl->assign("lang",$lang);
					$tmpl->assign("commentform", parsetrue("container/".container("comments"), $lang['postcomment'], $tmpl->fetch("comments/commentform.tpl")));
				}
			}
			
			if(!permission("newscomment")){
				$tmpl->assign('commentform', $tmpl->fetch('comments/commentform_false.tpl'));
			}
		}
	}
	// Dürfen News kommentiert werden? -> Ende
	
	
	$tmpl->assign("numcomm", $numcomm);
	$tmpl->assign("theme", $THEME);
	$tmpl->assign("content", parsetrue("container/".container("news"), "<a class=\"boxnavi\" href=\"index.php?p=newsarchive&amp;categ=all&amp;area=$area\">$lang[newspage]</a>" . SEPARATOR . $header_nav , $tmpl->fetch("news/news.tpl")));
}

?>