<?php
ob_start();
require_once("init.php"); 
if ( (!permission('staticnew')) && (!permission('staticedit') )) { echo error($langadmin['noperms']); exit; }

$tmpl = new Koobi("templates/" . ADMINTHEME . "/static/");
$tmpl->assign("theme", ADMINTHEME);
$tmpl->assign("langadmin", $langadmin);
$tmpl->assign("area", $area);
$tmpl->register_function("permission", "permission");

$filedir = "../static/";

switch ($_REQUEST["action"]) {
	
	case "view":
	
	switch($_REQUEST['cat']){
		case '':
		$dbfirst = "";
		break;
		
		default :
		$dbfirst = "WHERE categ=$_REQUEST[cat] "; 
		break;
	}
	
	switch($_REQUEST['ud']){
		case DESC:
		$ad = " DESC";
		break;
		case ASC:
		$ad = " ASC";
		break;
		default:
		$ad = " ASC";
	}
		
	if($_REQUEST['selby']!=""){
		switch ($_REQUEST['selby']){
			case hits:
			$q_2 = " ORDER BY hits";
			break;
			case ntime:
			$q_2 = " ORDER BY ctime";
			break;
			case autor:
			$q_2 = " ORDER BY uid";
			break;
			case title:
			$q_2 = " ORDER BY title";
			break;
		}
	}
		
	if(!$q_2){
		$q_2 = "ORDER BY title";
	}
	
	$limit = ($_REQUEST['pp']!="") ? (int)$_REQUEST['pp'] : 10 ;
	
	$static = array();
	$qs = "SELECT id FROM ".PREFIX."_static $dbfirst $q_2 $ad";
	$sql = new dbc($qs);
	$num = $sql->numrows();
	
	$seiten = ceil($num / $limit);
	$a = prepage() * $limit - $limit;
	
	$qs = "SELECT * FROM ".PREFIX."_static $dbfirst $q_2 $ad limit $a,$limit";
	$sql = new dbc($qs);
	
	
	while($row = $sql->fetchrow()){
		$sql_c = new dbc("SELECT name FROM ".PREFIX."_static_categ WHERE id=$row->categ"); 
		$row_c = $sql_c->fetchrow();
		$row->user = fetchusername($row->uid);
		$row->ctime = mygmt($row->ctime);
		$row->catname = $row_c->name;
		array_push($static,$row);
	}
	
	// <<-- navi -->>
	if($num > $limit){
		$tmpl->assign('navi', pagenav($seiten, $lang['aktpos'], " <a class=\"pnav\" href=\"static.php?action=view&amp;selby=$_REQUEST[selby]&amp;ud=$_REQUEST[ud]&amp;cat=$_REQUEST[cat]&amp;area=$area&amp;pp=$limit&amp;page={s}\">{t}</a> "));
	}
	
	// <<-- kategorien (4.0) -->>
	$categs = array();
	$sql_c = new dbc("SELECT * FROM ".PREFIX."_static_categ");
	while($row_c = $sql_c->fetchrow()){ array_push($categs,$row_c); };
	$tmpl->assign('categs',$categs);
	
	$tmpl->assign('categs',$categs);
	$tmpl->assign('row',$row);
	$tmpl->assign('static',$static);
	$tmpl->display('staticshow.tpl');
	break;
	
	// ======================================================================
	// <<-- KATEGORIEN -->>
	// ======================================================================
	case "categ":
	if($_REQUEST['do']=="newcateg"){
		if (!permission('staticnew')) { echo error($langadmin['noperms']); exit; }
		$sql = new dbc("INSERT INTO ".PREFIX."_static_categ (id,name) VALUES ('','".strip_tags($_REQUEST['name'])."')");
		header("Location:static.php?action=categ");
	}
	
	if($_REQUEST['do']=="delcateg"){
		if (!permission('staticedit')) { echo error($langadmin['noperms']); exit; }
		$sql = new dbc("DELETE FROM ".PREFIX."_static_categ WHERE id=$_REQUEST[id]");
		$sql = new dbc("UPDATE ".PREFIX."_static set categ=1 WHERE categ=$_REQUEST[id]");
		header("Location:static.php?action=categ");
	}
	
	if($_REQUEST['do']=="save"){
		if (!permission('staticedit')) { echo error($langadmin['noperms']); exit; }
		if($_POST['name']>=1){
		foreach ($_POST['name'] as $id => $name) {
			if( $name != "")  {
				$result = new dbc("UPDATE ".PREFIX."_static_categ SET name='$name' WHERE id = '$id'");
			}
		}
	}
	header("Location:static.php?action=categ");
	}
	
	$categs = array();
	$sql_c = new dbc("SELECT * FROM ".PREFIX."_static_categ");
	while($row_c = $sql_c->fetchrow()){ array_push($categs,$row_c); };
	$tmpl->assign('categs',$categs);
	$tmpl->display('categs.tpl');
	break;
	
	// ======================================================================
	// <<-- NEUE DATEI ANLEGEN -->>
	// ======================================================================
	case "new":
	
	// <<-- kategorien (4.0) -->>
	$categs = array();
	$sql_c = new dbc("SELECT * FROM ".PREFIX."_static_categ");
	while($row_c = $sql_c->fetchrow()){ array_push($categs,$row_c); };
	$tmpl->assign('categs',$categs);
	
	if(!permission('staticnew')) { echo error($langadmin['noperms']); exit;  } 
	if($_REQUEST['save']==1){
		alog("alog_adstatic", ": $_REQUEST[pagename].tpl");
		$text = $_REQUEST['text2'];
		if($_REQUEST['autobr2']==1) $text = nl2br($text);
		
		$pagename = trim($_REQUEST['pagename']);
		$pagename = str_replace(" ", "", $pagename);
		$pagename =  strtolower($pagename);
		
		
		if( ($pagename!="") && ($_REQUEST['title']!="") ){
			$filename = $pagename.".tpl";
			if(file_exists($filedir.$filename)){ echo error($langadmin['staticerror_exits']); exit; }
			if($fd = fopen($filedir.$filename, "w")){
				fwrite($fd, stripslashes($text));
				fclose($fd);
				@chmod($filedir.$filename,0777);
				$sql = new dbc("INSERT INTO ".PREFIX."_static (id,title,pagename,area,uid,ctime,categ,content) VALUES ('','".strip_tags($_REQUEST['title'])."','$pagename','".$_REQUEST['area']."','".UID."','".time()."','$_REQUEST[categ]','$text')");
			}
		}
		header("Location:static.php?area=".$_REQUEST['area']."&action=view");
	}
	
	
	// <<-- EDITOR INITIALISIEREN -->>
	$fck_value = "";
	include("forms/forminit.php");
	
	if(!is_writeable("../static")) {
		$tmpl->assign('error',1);
	}
	$tmpl->display('form.tpl');
	break;
	
	// ======================================================================
	// <<-- DATEI BEARBEITEN -->>
	// ======================================================================
	case "edit":
	if(!permission('staticedit')) { echo error($langadmin['noperms']); exit;  } 
	
	if($_REQUEST['save']==1){
		//alog("alog_editstatic", ": $_REQUEST[pagename]");
		$text = $_REQUEST['text2'];
		if($_REQUEST['autobr2']==1) $text = nl2br($text);
		$text = str_replace("&#8217;","'", $text);
		
		$categ = ($_REQUEST['categ']!='') ? (int)$_REQUEST['categ'] : 1; 
		$sql = new dbc("UPDATE ".PREFIX."_static SET title='".strip_tags($_REQUEST['title'])."', categ='$categ', content='".trim($text)."' WHERE id='".$_REQUEST['id']."'");
		
		
		$fd = @fopen($filedir.$_REQUEST['pagename'], "w");
		@fwrite($fd, stripslashes($text));
		@fclose($fd);
		header("Location:static.php?area=".$_REQUEST['area']."&action=view");
	}
	
	// <<-- kategorien (4.0) -->>
	$categs = array();
	$sql_c = new dbc("SELECT * FROM ".PREFIX."_static_categ");
	while($row_c = $sql_c->fetchrow()){ array_push($categs,$row_c); };
	$tmpl->assign('categs',$categs);
	
	// <<-- dokument auslesen -->>
	$sql = new dbc("SELECT * FROM ".PREFIX."_static WHERE id='".$_REQUEST['id']."'");
	$row = $sql->fetchrow();
	
	if(!file_exists($filedir.$row->pagename.".tpl")){ echo error($langadmin['static_openerror']); exit; }
	if ($fd = @fopen($filedir.$row->pagename.".tpl", "r")) {
		$content = @fread($fd, filesize($filedir.$row->pagename.".tpl"));
		fclose($fd);
	}
	
	
	// <<-- EDITOR INITIALISIEREN -->>
	$fck_value = $content;
	include("forms/forminit.php");
	$tmpl->assign('ed', $ed);
	
	$tmpl->assign('row',$row);
	$tmpl->display('form.tpl');
	break;
	
	// ======================================================================
	// <<-- LOESCHEN -->>
	// ======================================================================
	case "delete":
	if(!permission('staticedit')) { echo error($langadmin['noperms']); exit;  } 
	alog("alog_delstatic", ": $_REQUEST[file].tpl");
	$sql = new dbc("DELETE FROM ".PREFIX."_static WHERE id='".$_REQUEST['id']."'");
	@unlink($filedir.$_REQUEST['file'].".tpl");
	header("Location:static.php?area=".$_REQUEST['area']."&action=view");
	break;
}
?>