<?php
ob_start();
require_once("init.php");
$incdir = dirname(__FILE__);
define("INC", $incdir);
define("FORMS", 1);
$target = "../uploads/articlecat_icons/";

$area = ( (!isset($area)) || ($_REQUEST['area']=="") ) ? 1 : $_REQUEST['area'] ;

if( (!permission('newarticle')) && (!permission('editarticle')) && (!permission('editarticle_own')) ) { echo error($langadmin['noperms']); exit(); }

$tmpl = new Koobi("templates/".ADMINTHEME."/articles");
$tmpl->assign("langadmin", $langadmin);
$tmpl->assign("theme", ADMINTHEME);
$tmpl->assign('pref', $pref);
$tmpl->assign('permission', 'permission');
$tmpl->assign('uid', UID);


// ===============================================
// <<-- KOMMENTARE -->>
// ===============================================
if($_REQUEST['action']=="comments"){
	if(!permission("edit_article_comments")) { echo error($langadmin['noperms']); exit(); }
	
	if($_REQUEST['subaction']=="save") {
		foreach ($_POST["title"] as $id => $title) {
			$sql = new dbc("UPDATE " . PREFIX . "_articlecomments  SET title='".$_POST['title'][$id]."', comment='".$_POST['comment'][$id]."' WHERE id = $id");
			alog("alog_edit_comment_article"  , " " . $sql->getid());
		}
		
		foreach ($_POST["del"] as $id => $del) {
			$sql = new dbc("DELETE FROM " . PREFIX . "_articlecomments  WHERE id = $id");
			alog("alog_delete_comment_article"  , " " . $sql->getid());
		}
		
		header("Location:articles.php?area=".$_REQUEST['area']."");
	}
	
	if($_REQUEST['subaction']=="del") {
		$sql = new dbc("DELETE FROM ".PREFIX."_articlecomments   WHERE id='".$_REQUEST['nid']."'");
		alog("alog_delete_comment_article"  , " " . $sql->getid());
	}
	
	$sql = new dbc("SELECT * FROM ".PREFIX."_articlecomments  WHERE  articleid='".$_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("comments.tpl");
}



$limit = (isset($_REQUEST['pp']) && $_REQUEST['pp']!="") ? $_REQUEST['pp'] : 10;
switch($_REQUEST['action']){
	
	
	
	// <<-- ARTIKEL BEARBEITEN -->>
	case "edit" :
	if($_REQUEST['save']==1){
		
		$descr = ($_REQUEST['autobr2']==1) ? nl2br($_REQUEST['text2']) : $_REQUEST['text2'];
		
		$end_t = explode(".", $_REQUEST['date15']);
		$end_top = mktime($_REQUEST['e_date_top_hour'],$_REQUEST['e_date_top_minute'],0,$end_t[1],$end_t[0],$end_t[2]);
		
		$time_s = explode(".", $_REQUEST['date13']);
		$time_start = mktime($_REQUEST['s_hour'],$_REQUEST['s_minute'],0,$time_s[1],$time_s[0],$time_s[2]);
		
		$time_e = explode(".", $_REQUEST['date14']);
		$time_end = mktime($_REQUEST['e_hour'],$_REQUEST['e_minute'],0,$time_e[1],$time_e[0],$time_e[2]);
		
		if(permission('changetypeofdoc')) { $sql_in = "type = '".$_REQUEST['type_ofdoc']."',"; }
		$sql = new dbc("UPDATE ".PREFIX."_articles set 
		$sql_in
		articlecat = '".$_REQUEST['sel']."',
		time_start = '".$time_start."',
		time_end = '".$time_end."',
		title = '".strip_tags($_REQUEST['title'])."',
		subtitle = '".strip_tags($_REQUEST['subtitle'])."',
		asin = '".strip_tags($_REQUEST['asin'])."',
		release = '".strip_tags($_REQUEST['release'])."',
		genre = '".$_REQUEST['genre']."',
		publisher = '".$_REQUEST['publisher']."',
		manufacturer = '".$_REQUEST['manufacturer']."',
		voting = '".$_REQUEST['voting']."',
		shopurl = '".strip_tags($_REQUEST['shopurl'])."',
		shop = '".strip_tags($_REQUEST['shop'])."',
		price = '".strip_tags($_REQUEST['price'])."',
		inline_screens = '".addslashes(base64_decode($_REQUEST['inline_screens']))."',
		platform = '".@implode(";", $_REQUEST['platforms'])."',
		gallery = '".@implode(";", $_REQUEST['galids'])."',
		links = '".str_replace(array("\r\n","\n"), ";", $_REQUEST['links'])."',
		artvotes = '".str_replace(array("\r\n","\n"), ";", $_REQUEST['artvotes'])."',
		top = '".$_REQUEST['top']."',
		flop = '".$_REQUEST['flop']."',
		minimum = '".$_REQUEST['minimum']."',
		optimum = '".$_REQUEST['optimum']."',
		boxshot = '".$_REQUEST['images']."',
		active = '".$_REQUEST['active']."',
		cansearch = '".$_REQUEST['cansearch']."',
		isontop = '".$_REQUEST['isontop']."',
		image_top = '".$_REQUEST['images_top']."',
		uid = '".UID."',
		end_top = '".$end_top."',
		content = '".$descr."',
		product = '".$_REQUEST['product']."',
		matchword = '".$_REQUEST['matchword']."',
		showonpage = '".$_REQUEST['showonpage']."',
		password = '".$_REQUEST['password']."',
		canvote = '".$_REQUEST['canvote']."',
		cancomment = '".$_REQUEST['cancomment']."'
		WHERE id='".$_REQUEST['id']."'");
		
		alog("alog_article_edit" ," " . strip_tags($_REQUEST['title']));
		header("Location:articles.php?action=overview&area=".$_REQUEST['area']."");
	}
	
	
	$sql = new dbc("SELECT * FROM ".PREFIX."_articles  where id='".$_REQUEST['id']."'  AND area='$area' ");
	$row = $sql->fetchrow();
	// <<-- EDITOR INITIALISIEREN -->>
	$fck_value = $row->content;
	include("forms/forminit.php");
	include("forms/articleinit.php");
	include("forms/dropdown.php");
	$tmpl->assign('edit', 1);
	$tmpl->assign('row', $row);
	
	$tmpl->display('article_form.tpl');
	break;
	
	// <<-- NEUER ARTIKEL -->>
	case "new" :
	if($_REQUEST['save']==1){
		
		$descr = $_REQUEST['text2'];
		if($_REQUEST['autobr2']==1){
			$descr = nl2br($_REQUEST['text2']);
		}
		
		$end_t = explode(".", $_REQUEST['date15']);
		$end_top = mktime($_REQUEST['e_date_top_hour'],$_REQUEST['e_date_top_minute'],0,$end_t[1],$end_t[0],$end_t[2]);
		
		$time_s = explode(".", $_REQUEST['date13']);
		$time_start = mktime($_REQUEST['s_hour'],$_REQUEST['s_minute'],0,$time_s[1],$time_s[0],$time_s[2]);
		
		$time_e = explode(".", $_REQUEST['date14']);
		$time_end = mktime($_REQUEST['e_hour'],$_REQUEST['e_minute'],0,$time_e[1],$time_e[0],$time_e[2]);
		
		$sql = new dbc("INSERT INTO ".PREFIX."_articles ( 
		area,
		id,
		articlecat,
		time_start,
		time_end,
		title,
		subtitle,
		asin,
		release,
		genre,
		publisher,
		manufacturer,
		voting,
		shopurl,
		shop,
		price,
		inline_screens,
		platform,
		gallery,
		links,
		artvotes,
		top,
		flop,
		minimum,
		optimum,
		boxshot,
		active,
		cansearch,
		isontop,
		image_top,
		uid,
		ctime,
		content,
		type,
		end_top,
		product,
		matchword,
		showonpage,
		password,
		canvote,
		cancomment
		) VALUES (
		'$_REQUEST[area]',
		'',
		'".$_REQUEST['sel']."',
		'".$time_start."',
		'".$time_end."',
		'".strip_tags($_REQUEST['title'])."',
		'".strip_tags($_REQUEST['subtitle'])."',
		'".strip_tags($_REQUEST['asin'])."',
		'".strip_tags($_REQUEST['release'])."',
		'".$_REQUEST['genre']."',
		'".$_REQUEST['publisher']."',
		'".$_REQUEST['manufacturer']."',
		'".$_REQUEST['voting']."',
		'".strip_tags($_REQUEST['shopurl'])."',
		'".strip_tags($_REQUEST['shop'])."',
		'".strip_tags($_REQUEST['price'])."',
		'".addslashes(base64_decode($_REQUEST['inline_screens']))."',
		'".@implode(";", $_REQUEST['platforms'])."',
		'".@implode(";", $_REQUEST['galids'])."',
		'".str_replace(array("\r\n","\n"), ";", $_REQUEST['links'])."',
		'".str_replace(array("\r\n","\n"), ";", $_REQUEST['artvotes'])."',
		'".$_REQUEST['top']."',
		'".$_REQUEST['flop']."',
		'".$_REQUEST['minimum']."',
		'".$_REQUEST['optimum']."',
		'".$_REQUEST['images']."',
		'".$_REQUEST['active']."',
		'".$_REQUEST['cansearch']."',
		'".$_REQUEST['isontop']."',
		'".$_REQUEST['images_top']."',
		'".UID."',
		'".time()."',
		'".$descr."',
		'".$_REQUEST['type']."',
		'".$end_top."',
		'".$_REQUEST['product']."',
		'".$_REQUEST['matchword']."',
		'".$_REQUEST['showonpage']."',
		'".$_REQUEST['password']."',
		'".$_REQUEST['canvote']."',
		'".$_REQUEST['cancomment']."'
		)");
		
		alog("alog_new_article" , strip_tags($_REQUEST['title']));
		header("Location:articles.php?area=".$_REQUEST['area']."&type=".$_REQUEST['type']."");
	}
	
	$sql_p = new dbc("SELECT id,title FROM ".PREFIX."_products WHERE area='$area' order by title desc");
	$items = array();
	while( $row_p=$sql_p->fetchrow()){
		array_push($items,$row_p);
	}
	
	if($_REQUEST['fillform']==1){
		$sql = new dbc("SELECT * FROM ".PREFIX."_products  where id='".$_REQUEST['fill_id']."'  AND area='$area'");
		$row = $sql->fetchrow();
		$row->content = $row->descr;
	} 
	
	// <<-- EDITOR INITIALISIEREN -->>
	$fck_value = $row->content;
	include("forms/forminit.php");
	
	include("forms/articleinit.php");
	include("forms/dropdown.php");
	$tmpl->assign('row', $row);
	$tmpl->assign('items', $items);
	$tmpl->assign('dateexpire', date("Y")+20);
	$tmpl->assign('dateexpire_top', date("m")+1);
	$tmpl->display('article_form.tpl');
	
	break;
	
	// <<-- ARTIKEL LOESCHEN -->>
	case "del" :
	if (check_action('editarticle','articles','id',$_REQUEST['id'])){
		$result = new dbc("DELETE FROM ".PREFIX."_articles WHERE id = '".$_REQUEST['id']."'");
		alog("alog_delete_article" , $_REQUEST['id'] );
	}
	header("Location:articles.php?area=".$_REQUEST['area']."");
	break;
	
	// <<-- OEFFNEN EINZELN -->>
	case "fastopen" :
	if (check_action('editarticle','articles','id',$_REQUEST['id'])){
		$result = new dbc("UPDATE ".PREFIX."_articles SET active='1' WHERE id = '".$_REQUEST['id']."'");
		alog("alog_open_article" , $_REQUEST['id'] );
	}
	header("Location:articles.php?area=".$_REQUEST['area']."");
	break;
	
	// <<-- SCHLIESSEN EINZELN -->>
	case "fastclose" :
	if (check_action('editarticle','articles','id',$_REQUEST['id'])){
		$result = new dbc("UPDATE ".PREFIX."_articles SET active='0' WHERE id = '".$_REQUEST['id']."'");
		alog("alog_close_article" , $_REQUEST['id'] );
	}
	header("Location:articles.php?area=".$_REQUEST['area']."");
	break;
	
	// <<-- UEBERSICHT -->>
	case "":
	case "overview" :
	
	
	switch($_REQUEST['seloption']){
		
		// <<-- IN EINE ANDERE KATEGORIE VERSCHIEBEN -->>
		case "moveto":
		
		$moveto = array();
		if($_REQUEST['mode']=="once"){
			$sql = new dbc("SELECT id, title FROM ".PREFIX."_articles WHERE id='".$_REQUEST['id']."'");
			$row = $sql->fetchrow();
			array_push($moveto, $row);
		} else {
			if($_POST['sel']<1) { header("Location:articles.php?area=".$_REQUEST['area'].""); } 
			
			foreach ($_POST['sel'] as $id => $sel) {
				if( $sel != "")  {
					if($_REQUEST['final']==1){
						$sql = new dbc("UPDATE ".PREFIX."_articles SET articlecat='".$_REQUEST['articlecat']."' WHERE id='".$id."'");
						alog("alog_move_article" , " " . $id );
						header("Location:articles.php?area=".$_REQUEST['area']."");
					} else {
						$sql = new dbc("SELECT id, title FROM ".PREFIX."_articles WHERE id='".$id."'");
						$row = $sql->fetchrow();
						array_push($moveto, $row);
					}
				}
			}
		}
		
		$article= array();
		getArticlecat(0, "", $article, $area);
		$tmpl->assign('article', $article);
		$tmpl->assign('moveto', $moveto);
		$tmpl->assign('move', 1);
		$tmpl->display("moveto.tpl");
		exit;
		break;
		
		// <<-- LOESCHEN MASSEN -->>
		case  "massdel":
		if($_POST['sel']>1){
			foreach ($_POST['sel'] as $id => $sel) {
				if( $sel != "")  {
					if (check_action('editarticle','articles','id',$id)){
						$result = new dbc("DELETE FROM ".PREFIX."_articles  WHERE id = '$id'");
						
					}
				}
			}
		}
		break;
		
		// <<-- OEFFNEN MASSEN -->>
		case "open" :
		if($_POST['sel']>1){
			foreach ($_POST['sel'] as $id => $sel) {
				if( $sel != "")  {
					if (check_action('editarticle','articles','id',$id)){
						$result = new dbc("UPDATE ".PREFIX."_articles  set active='1' WHERE id = '$id'");
						
					}
				}
			}
		}
		alog("alog_open_article" , " " .$_REQUEST['id'] );
		break;
		
		// <<-- SCHLIESSEN MASSEN -->>
		case "close": 
		if($_POST['sel']>1){
			foreach($_POST['sel'] as $id => $sel) {
				if( $sel != "")  {
					if (check_action('editarticle','articles','id',$id)){
						$result = new dbc("UPDATE ".PREFIX."_articles SET active='0' WHERE id = '$id'");
						
					}
				}
			}
		}
		alog("alog_close_article" ," " . $_REQUEST['id'] );
		break;
	}
	
	
	if($_REQUEST['optsort']==1){
		switch($_REQUEST['cat']){
			case "" : $optsort_q = ""; break;
			default : $optsort_q = " AND ( articlecat='".$_REQUEST['cat']."' )" ; break;
		}
	}
	switch($_REQUEST['offon']){
		case 1: $off_on = " AND (active='0')"; break;
		case 2: $off_on = " AND (active='1')"; break;
		default: $off_on = ""; break;
	}
	
	switch ($_REQUEST['selby']){
		case hits : $order_first = " ORDER BY hits" ;break;
		case  printed : $order_first = " ORDER BY printed" ;break;
		case ntime : $order_first = " ORDER BY ctime" ;break;
		case autor : $order_first = " ORDER BY uid"; break;
		case title : $order_first = " ORDER BY title"; break;
		default : $order_first = " ORDER BY ctime"; break;
	}
	
	switch($_REQUEST['ud']){
		case DESC: $ascdesc = " DESC"; break;
		case ASC: $ascdesc = " ASC"; break;
		default: $ascdesc = " DESC";
	}
	
	switch($_REQUEST['type']){
		case "" : $type_query = ""; break;
		default : $type_query = "type='".$_REQUEST['type']."' AND "; break;
	}
	
	$sql = new dbc("SELECT id FROM ".PREFIX."_articles WHERE $type_query area='".$area."' $optsort_q $off_on  $order_first $ascdesc");
	$num = $sql->numrows();
	
	$seiten = ceil($num / $limit);
	$a = prepage() * $limit - $limit;
	
	$sql = new dbc("SELECT * FROM ".PREFIX."_articles WHERE $type_query area='".$area."' $optsort_q $off_on  $order_first $ascdesc limit $a,$limit");
	$erg = $sql->numrows();
	$navi = pagenav($seiten, $lang['aktpos'], " <a class=\"pnav\" href=\"articles.php?type=".$_REQUEST['type']."&offon=".$_REQUEST['offon']."&area=".$area."&cat=".$_REQUEST['cat']."&ud=".$_REQUEST['ud']."&selby=".$_REQUEST['selby']."&show=".$_REQUEST['show']."&pp=".$limit."&page={s}\">{t}</a> ");
	
	$articles = array();
	while($row = $sql->fetchrow()){
		$sql2 = new dbc("SELECT catname FROM ".PREFIX."_articlecat WHERE catid='".$row->articlecat."'");
		$row2 = $sql2->fetchrow();
		
		$row->user = fetchusername($row->uid);
		$row->ctime = mygmt($row->ctime);
		$row->catname = $row2->catname;
		$sql2 = new dbc("SELECT id FROM ".PREFIX."_articlecomments WHERE  articleid='".$row->id."' ");
		$row->comments = $sql2->numrows();
		array_push($articles,$row);	
	}
	
	$cat_sel= array();
	getArticlecat(0, "", $cat_sel, $area);
	
	if(permission('editarticle')) { $tmpl->assign('editall', 1); }
	if($num>$limit) { $tmpl->assign('navi', $navi); }
	
	$tmpl->assign('cat_sel', $cat_sel);
	$tmpl->assign('articles', $articles);
	$tmpl->assign('num', $sql->numrows());
	$tmpl->display('overview.tpl');
	
	break;
	
	
	// <<-- KATEGORIE- UEBERSICHT -->>
	case "category" :
	if(!permission('editcateg')){  echo error($langadmin['noperms']); exit();  }
	$article= array();
	getArticlecat(0, "", $article, $area);
	
	$tmpl->assign('article', $article);
	$tmpl->assign('catid', $_REQUEST['id']);
	$tmpl->assign('row', $row );
	$tmpl->display("articlecat.tpl");
	break;
	
	
	// <<-- KATEGORIE ORDNEN -->>
	case "saveposi" :
	if(!permission('editcateg')){  echo error($langadmin['noperms']); exit();  }
	foreach ($_POST['posi'] as $id => $posi) {
		if( ($posi != '') && (is_numeric($posi)) ) {
			$query = "UPDATE " . PREFIX . "_articlecat SET posi=$posi WHERE catid = $id";
			$result = new dbc($query);
		}
	}
	header("Location:articles.php?action=category&area=".$_REQUEST['area']."");
	break;
	
	// <<-- KATEGORIE LOESCHEN -->>
	case "catdel" :
	if(!permission('editcateg')){  echo error($langadmin['noperms']); exit();  }
	artcatdel($_REQUEST['id']);
	alog("alog_delete_article_cat" , " " .$_REQUEST['id'] );
	header("Location:articles.php?action=category&area=".$_REQUEST['area']."");
	break;
	
	
	// <<-- KATEGORIE ANLEGEN -->>
	case "addcateg" :
	if(!permission('editcateg')){  echo error($langadmin['noperms']); exit();  }
	if($_REQUEST['editsave']==1){
		$descr = $_REQUEST['autobr']==1 ? nl2br($_REQUEST['descr']) : $descr = $_REQUEST['descr'];
		
		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 = "$fupload_name";
		}
		$sql = new dbc("INSERT INTO ".PREFIX."_articlecat (
		area,
		catid,
		icon,
		catname,
		catdesc,
		parent_id
		) VALUES (
		'$_REQUEST[area]',
		'',
		'$sql_in',
		'".strip_tags($_REQUEST['title'])."',
		'".$descr."',
		'".$_REQUEST['articlecat']."'
		)");
		
		alog("alog_new_article_cat" , " " . strip_tags($_REQUEST['title']) );
		header("Location:articles.php?action=category&area=".$_REQUEST['area']."");
	}
	$article= array();
	getArticlecat(0, "", $article, $area);
	$tmpl->assign('article', $article);
	$tmpl->display("category.tpl");
	break;
	
	
	
	// <<-- KATEGORIE BEARBEITEN -->>
	case "editcateg" :
	if(!permission('editcateg')){  echo error($langadmin['noperms']); exit();  }
	
	// AKTUALISIEREN
	if($_REQUEST['editsave']==1){
		$descr = $_REQUEST['autobr']==1 ? nl2br($_REQUEST['descr']) : $descr = $_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."_articlecat SET $sql_in catname='".strip_tags($_REQUEST['title'])."', catdesc='".$descr."' WHERE catid='".$_REQUEST['id']."' ");
		header("Location:articles.php?action=category&area=".$_REQUEST['area']."");
	}
	
	// <<-- DATENSATZ AUSLESEN -->>
	$sql = new dbc("SELECT * FROM ".PREFIX."_articlecat WHERE catid=".$_REQUEST['id']."");
	$row = $sql->fetchrow();
	
	if($row->icon!="") {
		if(is_file($target.$row->icon)){
			$tmpl->assign('icon', $target . $row->icon);
		} 
	}
	$tmpl->assign('row', $row);
	$tmpl->display("category.tpl");
	break;
}
?>