<?php
ob_start();
require_once("init.php");

$incdir = dirname(__FILE__);
define("INC", $incdir);

$area = ( (!isset($area)) || ($_REQUEST['area']=="") ) ? 1 : $_REQUEST['area'] ;
$actform = ($_REQUEST['action']=="new") ? "action=new&send=1" : "action=edit&send=1&id=$_REQUEST[id]";

$tmpl = new Koobi("templates/".ADMINTHEME."/news/");
$tmpl->assign("theme", ADMINTHEME);
$tmpl->assign("langadmin", $langadmin);
$tmpl->assign("area", $area);
$tmpl->assign("kversion", KVERSION);
$tmpl->assign("cat", $_REQUEST['cat']);
$tmpl->assign("rid", $_REQUEST['id']);
$tmpl->assign("rpage", $_REQUEST['page']);
$tmpl->assign("rmode", $_REQUEST['mode']);
$tmpl->register_function("permission", "permission");
$tmpl->assign('pref', $pref);
$tmpl->assign('uid', UID);

// ===============================================
// <<-- Newskommentare -->>
// ===============================================
if($_REQUEST['action']=="comments"){
	if(!permission("editcomments")) { echo error($langadmin['noperms']); exit(); }
	
	if($_REQUEST['subaction']=="save") {
		foreach ($_POST["title"] as $id => $title) {
			$sql = new dbc("UPDATE " . PREFIX . "_newscomments SET title='".$_POST['title'][$id]."', comment='".$_POST['comment'][$id]."' WHERE id = $id");
		}
		
		foreach ($_POST["del"] as $id => $del) {
			$sql = new dbc("DELETE FROM " . PREFIX . "_newscomments WHERE id = $id");
		}
		
		header("Location:news.php?area=".$_REQUEST['area']."");
	}
	
	if($_REQUEST['subaction']=="del") {
		$sql =& new dbc("DELETE FROM ".PREFIX."_newscomments WHERE id='".$_REQUEST['nid']."'");
	}
	
	$sql =& new dbc("SELECT * FROM ".PREFIX."_newscomments WHERE newsid='".$_REQUEST['id']."' ORDER by id");
	
	
	$i = 0;
	while (	$row = $sql->fetchrow()) {
		$comments[$i]["comment"] = htmlspecialchars($row->comment);
		$comments[$i]["title"] = htmlspecialchars($row->title);
		$comments[$i]["ctime"] = mygmt($row->ctime);
		$comments[$i]["id"] = $row->id;
		$comments[$i]["author"] = fetchusername($row->autor);
		$i++;
	}
	$tmpl->assign("comments", $comments);
	$tmpl->display("newscomments.tpl");
}


// ===============================================
// <<-- Kategorie löschen... -->>
// ===============================================
if($_REQUEST['action']=="catdel"){
	if(!permission("newscatdel")){ echo error($langadmin['noperms']); exit(); }
	
	function catdel($id, &$cat, &$area) {
		$query = new dbc(" SELECT * FROM " . PREFIX . "_newscat  WHERE parent_id = $id or catid = $id and area = $area order by posi ASC");
		
		if (!$query->numrows()) {
			return;
		}
		
		while ($item = $query->fetchrow()) {
			$sql = new dbc("DELETE FROM " . PREFIX . "_newscat WHERE catid = $item->catid");
			$sql = new dbc("DELETE FROM " . PREFIX . "_newscat WHERE parent_id = $item->catid");
			
			$sql2 = new dbc(" SELECT newsid,newscat FROM " . PREFIX . "_news  WHERE newscat = $id");
			while($row = $sql2->fetchrow()){
				$sql3 = new dbc("DELETE FROM " . PREFIX . "_newscomments WHERE newsid = $row->newsid");
			}
			$sql = new dbc("DELETE FROM " . PREFIX . "_news WHERE newscat = $item->catid");
			$sql = new dbc("DELETE FROM " . PREFIX . "_news WHERE newscat = $item->parent_id");
			
			
			catdel($item->catid, $cat, $area);
		}
		return;
	}
	
	catdel($_REQUEST['id'], $cat, $_REQUEST['area']);
	header("Location:news.php?action=newscat&area=".$_REQUEST['area']."");
}


// ===============================================
// <<-- Kategorie bearbeiten -->>
// ===============================================
$target = "../uploads/newscat_icons/";
if($_REQUEST['action']=="editcateg"){
	
	if(!permission("newscat")){ echo error($langadmin['noperms']); exit(); }
	
	if($_REQUEST['editsave']==1){
		
		// ===============================================
		// <<-- Automatische Umbrüche? -->>
		// ===============================================
		if($_REQUEST['autobr']==1) {
			$descr = nl2br(strip_tags($_REQUEST['descr']));
		} else {
			$descr = strip_tags($_REQUEST['descr']);
		}
		
		// ===============================================
		// <<-- Altes Bild löschen? -->>
		// ===============================================
		if($_REQUEST['imagedel']==1) {
			@unlink($target . $_REQUEST['oldimage']);
			$sql_in = " icon='', ";
		}
		
		// ===============================================
		// <<-- Oder neues Bild der Kategorie zuweisen -->>
		// ===============================================
		if($_FILES['file']['size']!=0) {
			if($_REQUEST['oldimage']) {
				@unlink($target . $_REQUEST['oldimage']);
			}
			
			if(	$_FILES['file']['type']=="image/pjpeg" || 
			$_FILES['file']['type']=="image/jpeg" || 
			$_FILES['file']['type']=="image/x-png" || 
			$_FILES['file']['type']=="image/png" || 
			$_FILES['file']['type']=="image/gif")
			{
				
				$fupload_name = ktrim($_FILES['file']['name'],1);
				$fupload_name = rand_tostring($target ,$fupload_name);
				
				move_uploaded_file($_FILES['file']['tmp_name'], $target . $fupload_name);
				@chmod($target . $fupload_name,0777);
				$sql_in = " icon='$fupload_name', ";
			}
		}
		
		$sql = new dbc("UPDATE ".PREFIX."_newscat SET $sql_in catname='".strip_tags($_REQUEST['title'])."', catdesc='".$descr."' WHERE catid='".$_REQUEST['id']."' ");
		header("Location:news.php?action=newscat&area=".$_REQUEST['area']."");
	}
	
	$sql = new dbc("SELECT icon,catid,catname,catdesc FROM ".PREFIX."_newscat WHERE catid='".$_REQUEST['id']."'");
	$row = $sql->fetchrow();
	
	if($row->icon!=""){
		$tmpl->assign("templates/{$theme}/image", 1);
	}
	
	
	$news = array();
	getNews(0, "", $news, $area);
	$tmpl->assign("news", $news);
	$tmpl->assign("catid", $_REQUEST['id']);
	$tmpl->assign("row", $row );
	$tmpl->display("newscat_edit.tpl");
}

// ===============================================
// <<-- Neue Kategorie hinzufügen -->>
// ===============================================
if($_REQUEST['action']=="addcateg"){
	if(!permission("newscat")){ echo error($langadmin['noperms']); exit(); }
	if( ($_REQUEST['save']==1) && ($_REQUEST['title']!="") ){
		
		// ===============================================
		// <<-- Kategorie- Icon -->>
		// ===============================================
		if(	$_FILES['file']['type']=="image/pjpeg" || 
		$_FILES['file']['type']=="image/jpeg" || 
		$_FILES['file']['type']=="image/x-png" || 
		$_FILES['file']['type']=="image/png" || 
		$_FILES['file']['type']=="image/gif")
		{
			
			$fupload_name = ktrim($_FILES['file']['name'],1);
			$fupload_name = rand_tostring($target ,$fupload_name);
			
			move_uploaded_file($_FILES['file']['tmp_name'], $target . $fupload_name);
			@chmod($target . $fupload_name,0777);
			$icon = "$fupload_name";
		}
		
		
		// ===============================================
		// <<-- Automatische Umbrüche? -->>
		// ===============================================
		if($_REQUEST['autobr']==1) {
			$descr = nl2br(strip_tags($_REQUEST['descr']));
		} else {
			$descr = strip_tags($_REQUEST['descr']);
		}
		
		$query = new dbc("INSERT INTO " . PREFIX . "_newscat (catid,parent_id,catname,catdesc,area,icon) VALUES ('','$_REQUEST[thegal]','".strip_tags($_REQUEST['title'])."','$descr','$_REQUEST[area]','$icon') ");
		header("Location:news.php?action=newscat&area=".$_REQUEST['area']."");
	}
	
	$news = array();
	getNews(0, "", $news, $area);
	$tmpl->assign("parent_id", $_REQUEST['parent_id']);
	$tmpl->assign("news", $news);
	$tmpl->assign("catid", $_REQUEST['id']);
	$tmpl->display("newscat_new.tpl");
}


// ===============================================
// <<-- Newskategorie- Übersicht und Aktionen -->>
// ===============================================
if($_REQUEST['action']=="newscat"){
	if(!permission("newscat")){ echo error($langadmin['noperms']); exit(); }
	
	
	
	if($_REQUEST['edit']==1){
		
		foreach ($_POST["posi"] as $id => $posi) {
			if($posi != "") {
				$query = new dbc("UPDATE " . PREFIX . "_newscat SET posi=$posi WHERE catid = $id");
			}
		}
		header("Location:news.php?action=newscat&area=".$_REQUEST['area']."");
	}
	
	$news = array();
	getNews(0, "", $news, $area);
	$tmpl->assign("news", $news);
	$tmpl->assign("donow", $donow);
	$tmpl->assign("area", $area);
	$tmpl->display("newscat.tpl");
}


// =================================================================
// <<-- News suchen -->>
// =================================================================
if($_REQUEST['action']=="searchnews"){
	if(!permission("composenews") && !permission("editnews")){ echo error($langadmin['noperms']); exit(); }
	
	$tmpl->assign("","");
	$tmpl->display("newssearch.tpl");
	
}


if((!permission("composenews")) && (!permission("editnews")) && (!permission("newsview")) ){
	echo error($langadmin['noperms']);
	exit();
}

if(isset($_REQUEST['pp']) && $_REQUEST['pp']!=""){
	$limit = $_REQUEST['pp'];
	} else {
	$limit = 10;
}


if(!isset($page)){
	$page = 1;
}

define("FORMS", "OK");

// Schliessen (Massen)
if($_REQUEST['seloption']=="massclose"){
	$sp = base64_decode($_REQUEST['specials']);
	$_REQUEST['area'] = $_REQUEST['area'];
	$_REQUEST['cat'] = $_REQUEST['cat'];
	reset ($_POST);
	while(list($key,$val) = each($_POST)) {
		if (substr($key,0,5)=="news_") {
			$aktid = str_replace("news_","",$key);
			if (check_action('editnewsall','news','newsid',$aktid)){
				$sql =& new dbc("UPDATE ".PREFIX."_news SET ispublic='0' WHERE newsid='".escs($aktid)."'");
			}
		}
	}
	header("Location:news.php?area=".$_REQUEST['area']."&page=1&cat=".$_REQUEST['cat']."&subaction=".$_REQUEST['subaction'].$sp."");
}

// Öffnen (Massen)
if($_REQUEST['seloption']=="massopen"){
	$_REQUEST['cat'] = $_REQUEST['cat'];
	$sp = base64_decode($_REQUEST['specials']);
	$_REQUEST['area'] = $_REQUEST['area'];
	reset ($_POST);
	while(list($key,$val) = each($_POST)) {
		if (substr($key,0,5)=="news_") {
			$aktid = str_replace("news_","",$key);
			if (check_action('editnewsall','news','newsid',$aktid)){
				$sql =& new dbc("UPDATE ".PREFIX."_news SET ispublic='1' WHERE newsid='".escs($aktid)."'");
			}
		}
	}
	header("Location:news.php?area=".$_REQUEST['area']."&page=1&cat=".$_REQUEST['cat']."&subaction=".$_REQUEST['subaction'].$sp."");
}

// Löschen (Massen)
if($_REQUEST['seloption']=="massdel"){
	if(!permission("editnewsall")){ echo error($langadmin['noperms']); exit(); }
	$_REQUEST['cat'] = $_REQUEST['cat'];
	reset ($_POST);
	while(list($key,$val) = each($_POST)) {
		if (substr($key,0,5)=="news_") {
			$aktid = str_replace("news_","",$key);
			$sql = new dbc("DELETE FROM ".PREFIX."_news WHERE newsid='".escs($aktid)."'");
			$sql = new dbc("DELETE FROM ".PREFIX."_newscomments WHERE newsid='".escs($aktid)."'");
		}
	}
	header("Location:news.php?area=".$_REQUEST['area']."&page=1&cat=".$_REQUEST['cat']."&subaction=".$_REQUEST['subaction'].base64_decode($_REQUEST['specials'])."");
}

// ===================================================
// <<-- Neue News verfassen -->>
// ===================================================
if($_REQUEST['action']=="new"){
	if(!permission("composenews")){ echo error($langadmin['nopermcomposenews']); exit(); }
	
	// News speichern (messagenorm)
	
	if($_REQUEST['send']==1){
		if(!permission("postnewsnow")){
			$status = 0;
			$msg = $langadmin['newsokbutmoderate'];
		} else {
			$msg = $langadmin['newsok'];
			$status = 1;
		}
		
		if($_REQUEST['isnotpublic']==1){
			$status = 0;
		}
		
		// In die Datenbank eintragen...
		$ok = 1;
		if($_REQUEST['sel']==""){
			$emsg .= errorpre("enonewscat");
			$ok = -1;
		}
		
		if($ok == 1){
			$start = mktime($_REQUEST['s_hour'],$_REQUEST['s_minute'],$_REQUEST['s_second'],$_REQUEST['s_month'],$_REQUEST['s_day'],$_REQUEST['s_year']);
			
			if( (!empty($_REQUEST['e_month'])) && (!empty($_REQUEST['e_year'])) && (!empty($_REQUEST['e_day'])) ){
				$end = mktime($_REQUEST['e_hour'],$_REQUEST['e_minute'],$_REQUEST['e_second'],$_REQUEST['e_month'],$_REQUEST['e_day'],$_REQUEST['e_year']);
			} else {
				$end = 1;
			}
			
			if( (!empty($_REQUEST['etop_month'])) && (!empty($_REQUEST['etop_year'])) && (!empty($_REQUEST['etop_day'])) ){
				$end_top = mktime($_REQUEST['etop_hour'],$_REQUEST['etop_minute'],$_REQUEST['etop_second'],$_REQUEST['etop_month'],$_REQUEST['etop_day'],$_REQUEST['etop_year']);
			} else {
				$end_top = 1;
			}
			
			
			if($_REQUEST['autobr1']==1){ $text1 = nl2br(nicehtml($_REQUEST['text1'],"")); } else { $text1 = nicehtml($_REQUEST['text1'],""); }
			if($_REQUEST['autobr2']==1){ $text2 = nl2br(nicehtml($_REQUEST['text2'],"")); } else { $text2 = nicehtml($_REQUEST['text2'],""); }
			
			$insert = "INSERT INTO ".PREFIX."_news (";
			$insert .= "links,manufacturer,product,matchword,shortintro,galids,newsid,area_id,newscat,uid,ntime,time_start,time_end,ispublic,title,text,";
			$insert .= "textmore,image,image_align,";
			$insert .= "cansearch,canvote,cancomment,isontop,allareas,end_top";
			$insert .= ") VALUES (";
			$insert .= "'".str_replace(array("\r\n","\n"), ";", $_REQUEST['links'])."', '".$_REQUEST['manufacturer']."','".$_REQUEST['product']."','".$_REQUEST['matchword']."','".$_REQUEST['shortintro']."','".implode(",", $_REQUEST['galids'])."','','".$area."','".$_REQUEST['sel']."','".$_REQUEST['uid']."','".time()."','".$start."','".$end."','".$status."','".strip_tags($_REQUEST['title'])."','".$text1."',";
			$insert .= "'".$text2."','".$_REQUEST['images']."','".$_REQUEST['image_align']."',";
			$insert .= "'".$_REQUEST['cansearch']."','".$_REQUEST['canvote']."','".$_REQUEST['cancomment']."','".$_REQUEST['isontop']."','".$_REQUEST['allareas']."','".$end_top."'";
			$insert .= ")";
			
			$sql =& new dbc($insert);
			header("Location:news.php?area=".$_REQUEST['area']."");
			
		} else {
			echo error("<ul>".$emsg."</ul>");
			exit();
		}
		// Nachricht ausgeben und weiterleiten...
		echo messagenorm($msg,"news.php?area=".$_REQUEST['area']."");
		exit();
	}
	
	
	
	// =================================================================
	// <<-- Newsformular initialisieren -->>
	// =================================================================
	include("forms/newsinit.php");
}



// =================================================================
// <<-- News in eine andere Kategorie / Sektion verschieben -->>
// =================================================================
if($_REQUEST['seloption']=="moveto"){
	if(!permission("editnews")){ echo error($langadmin['noperms']); exit(); }
	
	$sql =& new dbc("SELECT uid FROM ".PREFIX."_news WHERE newsid='".$_REQUEST['id']."'");
	$row = $sql->fetchrow();
	if($row->uid!=$UID && !permission("editnewsall")){ echo error($langadmin['nopermeditnews']); exit();}
	
	
	if($_REQUEST['send']==1){
		if($_REQUEST['new_cat']!=""){
			$_REQUEST['cat'] = $_REQUEST['cat'];
			$area_id = $_REQUEST['area_id'];
			$newcat = $_REQUEST['new_cat'];
			reset ($_POST);
			while(list($key,$val) = each($_POST)) {
				if (substr($key,0,5)=="news_") {
					$aktid = str_replace("news_","",$key);
					$sql =& new dbc("UPDATE  ".PREFIX."_news SET  newscat='".$new_cat."' WHERE newsid='".$aktid."'");
					
					$sql =& new dbc("SELECT area FROM ".PREFIX."_newscat WHERE catid='".$newcat."'");
					$area = $sql->fetchrow();
					
					$sql =& new dbc("UPDATE  ".PREFIX."_news SET  area_id='".$area->area."' WHERE newsid='".$aktid."'");
				}
			}
		}
		header("Location:news.php?area=".$_REQUEST['area']."&cat=".$_REQUEST['cat']."&page=1");
	}
	
	
	$moveto = array();
	reset ($_POST);
	while(list($key,$val) = each($_POST)) {
		if (substr($key,0,5)=="news_") {
			$aktid = str_replace("news_","",$key);
			
			$sql =& new dbc("SELECT newsid,title FROM ".PREFIX."_news WHERE newsid='".$aktid."'");
			$row = $sql->fetchrow();
			
			
			array_push($moveto, $row);
		}
	}
	
	
	if($_REQUEST['mode']==7){
		$aktid = str_replace("news_","",$_REQUEST['utd']);
		$sql = new dbc("SELECT title FROM ".PREFIX."_news WHERE newsid='".$aktid."'");
		$row = $sql->fetchrow();
		$tmpl->assign("ntitle", $row->title);
		$tmpl->assign("naktid", $aktid);
	}
	
	// =================================================================
	// <<-- Dropdownmenü erzeugen -->>
	// =================================================================
	$sql =& new dbc("SELECT area_id,area_name FROM ".PREFIX."_areas  ORDER BY area_id ASC");
	while($row=$sql->fetchrow()){
		
		$drd .='<optgroup label="'.$row->area_name.'"></optgroup>';
		$sql2 =& new dbc("SELECT * FROM ".PREFIX."_newscat WHERE area='".$row->area_id."' ORDER BY catname ASC");
		while ($row2=$sql2->fetchrow()){ 
			$drd .='<option value="'.$row2->catid.'">&nbsp;- '.$row2->catname.'</option>';
			
		}
	}
	
	$tmpl->assign("drd", $drd);
	$tmpl->assign("moveto", $moveto);
	$tmpl->display("newsmove.tpl");
	
} 



if ($_REQUEST['seloption']!="moveto"){
	
	
	// =================================================================
	// <<-- News bearbeiten -->>
	// =================================================================
	if($_REQUEST['action']=="edit" ){
		if(!permission("editnews")){ echo error($langadmin['noperms']); exit(); }
		
		if($_REQUEST['send']==1){
			
			if(!permission("postnewsnow")){
				$status = 0;
			} else {
				$status = 1;
			}
			
			if($_REQUEST['isnotpublic']==1){
				$status = 0;
			}
			
			$ok = 1;
			if($_REQUEST['sel']==""){
				$emsg .= errorpre("enonewscat");
				$ok = -1;
			}
			
			if($ok == 1){
				$start = mktime($_REQUEST['s_hour'],$_REQUEST['s_minute'],$_REQUEST['s_second'],$_REQUEST['s_month'],$_REQUEST['s_day'],$_REQUEST['s_year']);
				
				if( (!empty($_REQUEST['e_month'])) && (!empty($_REQUEST['e_year'])) && (!empty($_REQUEST['e_day'])) ){
					$end = mktime($_REQUEST['e_hour'],$_REQUEST['e_minute'],$_REQUEST['e_second'],$_REQUEST['e_month'],$_REQUEST['e_day'],$_REQUEST['e_year']);
				} else {
					$end = 1;
				}
				
				if( (!empty($_REQUEST['etop_month'])) && (!empty($_REQUEST['etop_year'])) && (!empty($_REQUEST['etop_day'])) ){
					$end_top = mktime($_REQUEST['etop_hour'],$_REQUEST['etop_minute'],$_REQUEST['etop_second'],$_REQUEST['etop_month'],$_REQUEST['etop_day'],$_REQUEST['etop_year']);
				} else {
					$end_top = 1;
				}
			}
			
			
			if($_REQUEST['autobr1']==1){ $text1 = nl2br(nicehtml($_REQUEST['text1'],"")); } else { $text1 = nicehtml($_REQUEST['text1'],""); }
			if($_REQUEST['autobr2']==1){ $text2 = nl2br(nicehtml($_REQUEST['text2'],"")); } else { $text2 = nicehtml($_REQUEST['text2'],""); }
			
			$insert .= "UPDATE ".PREFIX."_news SET ";
			$insert .= "links='".str_replace(array("\r\n","\n"), ";", $_REQUEST['links'])."',";
			$insert .= "product='".$_REQUEST['product']."',";
			$insert .= "manufacturer='".$_REQUEST['manufacturer']."',";
			$insert .= "matchword='".$_REQUEST['matchword']."',";
			$insert .= "shortintro='".$_REQUEST['shortintro']."',";
			$insert .= "newscat='".$_REQUEST['sel']."',";
			$insert .= "time_start='".$start."',";
			$insert .= "time_end='".$end."',";
			$insert .= "ispublic='".$status."',";
			$insert .= "title='".nicehtml(strip_tags($_REQUEST['title']),"")."',";
			$insert .= "text='".$text1."',";
			$insert .= "textmore='".$text2."',";
			$insert .= "image='".$_REQUEST['images']."',";
			$insert .= "image_top='".$_REQUEST['images_top']."',";
			$insert .= "image_align='".$_REQUEST['image_align']."',";
			$insert .= "cansearch='".$_REQUEST['cansearch']."',";
			$insert .= "canvote='".$_REQUEST['canvote']."',";
			$insert .= "cancomment='".$_REQUEST['cancomment']."',";
			$insert .= "isontop='".$_REQUEST['isontop']."',";
			$insert .= "allareas='".$_REQUEST['allareas']."',";
			$insert .= "galids='".implode(",", $_REQUEST['galids'])."',";
			$insert .= "end_top='".$end_top."',";
			$insert .= "inline_screens='".addslashes(base64_decode($_REQUEST['inline_screens']))."'" ;
			$insert .= " WHERE newsid='".$_REQUEST['id']."'";
			$sql =& new dbc($insert);
			
			if($_REQUEST['close']==1){ 
				$redir_string = base64_decode($_REQUEST['redir']);
				$redir_string = str_replace('&amp;', '&', $redir_string);
				echo "<script>window.close();" ; 
				echo "window.opener.location.href='".$redir_string."';";
				echo "</script>";
				exit;
			} 
			
			header("Location:news.php?area=".$_REQUEST['area']."&cat=".$_REQUEST['cat']."&subaction=".$_REQUEST['subaction']."");
			exit();
		}
		
		$sql =& new dbc("SELECT uid FROM ".PREFIX."_news WHERE newsid='".$_REQUEST['id']."'");
		$row = $sql->fetchrow();
		
		if($row->uid!=$UID && !permission("editnewsall")){ echo error($langadmin['nopermeditnews']); exit();}
		
		
		
		$sql =& new dbc("SELECT * FROM ".PREFIX."_news WHERE newsid='".$_REQUEST['id']."'");
		$row = $sql->fetchrow();
		
		include("forms/newsinit.php");
		
	}
	
	
	
	
	
	// =================================================================
	// <<-- News löschen -->>
	// =================================================================
	if($_REQUEST['action']=="del"){
		if(!permission("editnewsall")){ echo error($langadmin['nopermdelnews']); exit(); }
		$sql =& new dbc("DELETE FROM ".PREFIX."_news WHERE newsid='".$_REQUEST['id']."'");
		$sql =& new dbc("DELETE FROM ".PREFIX."_newscomments WHERE newsid='".$_REQUEST['id']."'");
		header("Location:news.php?area=".$_REQUEST['area']."&cat=".$_REQUEST['cat']."&page=".$_REQUEST['page']."&subaction=".$_REQUEST['subaction']."");
	}
	
	
	
	// =================================================================
	// <<-- News aktivieren & deaktivieren -->>
	// =================================================================
	if(($_REQUEST['action']=="fastclose") || ($_REQUEST['action']=="fastopen")){
		if(!permission("editnews")){ echo error($langadmin['noperms']); exit(); }
		
		$sql =& new dbc("SELECT uid FROM ".PREFIX."_news WHERE newsid='".$_REQUEST['id']."'");
		$row = $sql->fetchrow();
		
		if(($row->uid!=$UID) && (!permission("editnewsall"))){ echo error($langadmin['nopermeditnews']); exit();}
		
		switch($_REQUEST['action']){
			case fastclose:
			$sql =& new dbc("UPDATE ".PREFIX."_news SET ispublic='0' WHERE newsid='".$_REQUEST['id']."'");
			break;
			case fastopen:
			$sql =& new dbc("UPDATE ".PREFIX."_news SET ispublic='1' WHERE newsid='".$_REQUEST['id']."'");
			break;
		}
		header("Location:news.php?area=".$_REQUEST['area']."&page=".$_REQUEST['page']."&subaction=".$_REQUEST['subaction']."&autor=".$_REQUEST['autor']."&title=".$_REQUEST['title']."&text=".$_REQUEST['text']."&cat=".$_REQUEST['cat']."");
	}
	
	
	
	
	
	// =================================================================
	// <<-- Newsübersicht -->>
	// =================================================================
	if(!isset($_REQUEST['action'])){
		
		switch($_REQUEST['offon']){
			case 1:
			$q3 = " && ispublic='0'";
			break;
			case 2:
			$q3 = " && ispublic='1'";
			break;
			default:
			$q3 = "";
		}
		
		$only = " (area_id='".$area."' $q3) ";
		if($_REQUEST['cat']!=""){
			$q = "AND (newscat='".$_REQUEST['cat']."')";
		}
		
		switch($_REQUEST['ud']){
			case DESC:
			$ad = " DESC";
			break;
			case ASC:
			$ad = " ASC";
			break;
			default:
			$ad = " DESC";
		}
		
		if($_REQUEST['selby']!=""){
			switch ($_REQUEST['selby']){
				case hits:
				$q_2 = " ORDER BY hits";
				break;
				case  printed:
				$q_2 = " ORDER BY printed";
				break;
				case ntime:
				$q_2 = " ORDER BY ntime";
				break;
				case autor:
				$q_2 = " ORDER BY uid";
				break;
				case title:
				$q_2 = " ORDER BY title";
				break;
			}
		}
		
		if(!$q_2){
			$q_2 = "ORDER BY ntime";
		}
		
		
		
		// Wenn Benutzer nur eigene News bearbeiten darf,
		// nur News ausgeben, die auch von im sind...
		
		if(!permission("editnewsall")){
			/*
			$only = " (uid='".$UID."' && area_id='".$area."' $q3)";
			*/
			if($_REQUEST['cat']!=""){
				$q = " AND (uid='".$UID."' && newscat='".$_REQUEST['cat']."' $q3)";
			}
		}
		if($_REQUEST['cat']!=""){
			$q5 = " AND (newscat='".$_REQUEST['cat']."' $q3)";
		}
		
		
		// Es wird nach News gesucht, also normale Variablen leeren...
		if( ($_REQUEST['subaction']=="searchnews") && ($_REQUEST['mode']==1) ){
			$news = array();
			unset($only,$q,$q_2,$ad);
			
			if(
			$_REQUEST['title']=="" &&
			$_REQUEST['newsid']=="" &&
			$_REQUEST['autor']=="" &&
			$_REQUEST['text']==""
			){
				echo error($langadmin['nothingfound']);
				exit();
			} else {
				if(!empty($_REQUEST['title'])){
					$q = "title like '%".mysql_escape_string($_REQUEST['title'])."%' OR";
				}
				$q .= " newsid='".$_REQUEST['newsid']."'";
				$q .= " OR uid='".fetchuserid($_REQUEST['autor'])."'";
				
				if(!empty($_REQUEST['text'])){
					$q .= " OR text like '%".$_REQUEST['text']."%'";
					$q .= " OR textmore like '%".$_REQUEST['text']."%'";
				}
				
			}
			
		}
		
		if($_REQUEST['subaction']=="searchnews"){
			$limit = 150;
		}
		
		$sql2 =& new dbc("SELECT * FROM ".PREFIX."_news WHERE $only $q $q5  $q_2 $ad");
		$num = $sql2->numrows();
		
		$seiten = ceil($num / $limit);
		$a = prepage() * $limit - $limit;
		
		$sql =& new dbc("SELECT * FROM ".PREFIX."_news WHERE $only  $q $q5  $q_2 $ad limit  $a,$limit");
		$erg = $sql->numrows();
		
		if($erg==0){
			$thepage = $_REQUEST['page']-1;
			
			if($num<$limit){
				$thepage = 1;
			}
			
			$seiten = ceil($num / $limit);
			$a = $thepage * $limit - $limit;
			
			$sql =& new dbc("SELECT * FROM ".PREFIX."_news WHERE $only  $q $q_2 $ad limit  $a,$limit");
			$erg = $sql->numrows();
		}
		
		if((!$erg) && ($_REQUEST['subaction']=="searchnews")){
			echo error($langadmin['nothingfound']);
			exit();
		}
		
		if($_REQUEST['subaction']!="searchnews" && $_REQUEST['seloption']!="moveto"){
			
			//===============================================
			// <<-- Newsübersicht -->>
			//===============================================
			
			$cat_sel = array();
			$news = array();
			
			$sql_c =& new dbc("SELECT * FROM ".PREFIX."_newscat WHERE area='".$area."'");
			while ($row_c = $sql_c->fetchrow()) {
				unset($sel,$num_d);
				if($_REQUEST['cat'] == $row_c->catid){
					$sel = "selected";
				}
				
				$sql_d =& new dbc("SELECT newsid FROM ".PREFIX."_news  WHERE newscat='".$row_c->catid."' AND area_id='".$area."'");
				$row_c->sel = $sel;
				$row_c->num = $sql_d->numrows();
				array_push($cat_sel, $row_c);
				
			}
		}
		
		// =============================================
		// <<-- Newskategorie und Kommentare auslesen -->>
		// =============================================
		while ($row = $sql->fetchrow()){
			unset($newscat);
			unset($comments);
			
			
			
			$sql1 = new dbc("SELECT catname FROM ".PREFIX."_newscat WHERE catid='".$row->newscat."' ");
			$row1 = $sql1->fetchrow();
			$sql1->close();
			
			$sql2 = new dbc("SELECT newsid FROM ".PREFIX."_newscomments WHERE newsid='".$row->newsid."' ");
			$row->comments = $sql2->numrows();
			
			$row->user = fetchusername($row->uid);
			$row->ntime = mygmt($row->ntime);
			$row->newscat_op = $row->newscat;
			$row->newscat = htmlspecialchars($row1->catname);
			
			$row->isp = $row->ispublic;
			
			array_push($news, $row);
			
		}
		
		
		
		
		if($_REQUEST['subaction']=="searchnews"){ 
			$tmpl->assign("switchnorm", 1);
		}
		
		
		
		// ==================================================
		// <<-- Navigation -->>
		// ==================================================
		if($num>$limit){
			
			$tmpl->assign("shownavi", 1);
			
			if($_REQUEST['offon']!=""){
				$offon="offon=".$_REQUEST['offon']."&";
			}
			
			if($_REQUEST['subaction']=="searchnews"){
				$navi = pagenav($seiten, $lang['aktpos'], " <a class=\"pnav\" href=\"news.php?subaction=searchnews&text=".$_REQUEST['text']."&autor=".$_REQUEST['autor']."&area=".$area."&title=".$_REQUEST['title']."&pp=".$limit."&page={s}\">{t}</a> ");
			} else {
				$navi = pagenav($seiten, $lang['aktpos'], " <a class=\"pnav\" href=\"news.php?".$offon."area=".$area."&cat=".$_REQUEST['cat']."&ud=".$_REQUEST['ud']."&selby=".$_REQUEST['selby']."&show=".$_REQUEST['show']."&pp=".$limit."&page={s}\">{t}</a> ");
			}
		}
		
		// ==================================================
		// <<-- Template "Newsübersicht" ausgeben -->>
		// ==================================================
		$tmpl->assign("lang", $langadmin);
		$tmpl->assign("area", $area);
		$tmpl->assign("news", $news);
		$tmpl->assign("cat_sel", $cat_sel);
		$tmpl->assign("donow", $donow);
		$tmpl->assign("editall", permission("editnewsall"));
		$tmpl->assign("category", $_REQUEST['cat']);
		$tmpl->assign("special", base64_encode($special));
		$tmpl->assign("subaction", $_REQUEST['subaction']);
		$tmpl->assign("num", $num);
		$tmpl->assign("pp", $_REQUEST['pp']);
		$tmpl->assign("navi", $navi);
		$tmpl->assign("selby", $_REQUEST['selby']);
		$tmpl->assign("ud", $_REQUEST['ud']);
		$tmpl->assign("offon", $_REQUEST['offon']);
		$tmpl->assign("cat", $_REQUEST['cat']);
		$tmpl->assign("addtolink", $addtolink);
		$tmpl->assign("special", $special);
		$tmpl->assign("page", $page);
		
		$tmpl->display("newsshow.tpl");
	}
} 
?>