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

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

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

$tmpl = new Koobi("templates/".ADMINTHEME."/links/");
$tmpl->assign("theme", ADMINTHEME);
$tmpl->assign("langadmin", $langadmin);
$tmpl->assign("area", $area);
$tmpl->assign("kversion", KVERSION);
$tmpl->assign("rmode", $_REQUEST['mode']);
$tmpl->assign('uid', UID);
$tmpl->assign('pref', $pref);
$tmpl->register_function("permission", "permission");
$tmpl->assign('mupload', 'Max. ' . @ini_get("upload_max_filesize")*1024 . " Kb");

if( (!permission('link_new')) && (!permission('link_edit')) && (!permission('link_cat_new')) && (!permission('link_comments_edit')) && (!permission('link_cat_edit')) ) { echo error($langadmin['noperms']); exit(); }

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



switch($_REQUEST['seloption']){
	// ============================================================================
	// <<-- IN EINE ANDERE KATEGORIE VERSCHIEBEN -->>
	// ============================================================================
	case "moveto" :
	if(!permission('link_edit')) { echo error($langadmin['noperms']); exit(); }
	$moveto = array();
	if($_REQUEST['mode']=="once"){
		$sql = new dbc("SELECT id, title FROM ".PREFIX."_links WHERE id='$_REQUEST[id]'");
		$row = $sql->fetchrow();
		array_push($moveto, $row);
	} else {
		if($_POST['sel']<1) { header("Location:links.php?area=$_REQUEST[area]&action=overview"); } 
		
		foreach ($_POST['sel'] as $id => $sel) {
			if( $sel != "")  {
				if($_REQUEST['final']==1){
					$sql = new dbc("UPDATE ".PREFIX."_links SET catid='$_REQUEST[newcat]' WHERE id='$id'");
					header("Location:links.php?area=$_REQUEST[area]&action=overview");
				} else {
					$sql = new dbc("SELECT id, title FROM ".PREFIX."_links WHERE id='$id'");
					$row = $sql->fetchrow();
					array_push($moveto, $row);
				}
			}
		}
	}
	
	$cat_sel = array();
	getLinkcategs_simple(0, "", $cat_sel, $area);
	$tmpl->assign('cat_sel', $cat_sel);
	$tmpl->assign('moveto', $moveto);
	$tmpl->assign('move', 1);
	$tmpl->display('link_moveto.tpl');
	exit;
	break;
	
	// ============================================================================
	// <<-- LOESCHEN MASSEN -->>
	// ============================================================================
	case  "massdel":
	if(!permission('link_delete')) { echo error($langadmin['noperms']); exit(); }
	if($_POST['sel']>1){
		foreach ($_POST['sel'] as $id => $sel) {
			if( $sel != "")  {
				$sql1 = new dbc("DELETE FROM ".PREFIX."_links WHERE id = '$id'");
				$sql2 = new dbc("DELETE FROM ".PREFIX."_linkcomments WHERE dlid = '$id'");
			}
		}
	}
	header("Location:links.php?area=$_REQUEST[area]&action=overview");
	break;
	
	// ============================================================================
	// <<-- OEFFNEN MASSEN -->>
	// ============================================================================
	case "open" :
	if(!permission('link_edit')) { echo error($langadmin['noperms']); exit(); }
	if($_POST['sel']>1){
		foreach ($_POST['sel'] as $id => $sel) {
			if( $sel != "")  {
				$result = new dbc("UPDATE ".PREFIX."_links SET active='1' WHERE id = '$id'");
			}
		}
	}
	break;
	
	// ============================================================================
	// <<-- SCHLIESSEN MASSEN -->>
	// ============================================================================
	case "close": 
	if(!permission('link_edit')) { echo error($langadmin['noperms']); exit(); }
	if($_POST['sel']>1){
		foreach($_POST['sel'] as $id => $sel) {
			if( $sel != "")  {
				$result = new dbc("UPDATE ".PREFIX."_links SET active='0' WHERE id = '$id'");
			}
		}
	}
	break;
}

switch($_REQUEST['action']){
	
	case "category" :
	if(!permission('link_cat_new')) { echo error($langadmin['noperms']); exit(); }
	$downlodcat = array();
	getLinkcategs_simple(0, "", $downlodcat, $area);
	
	$tmpl->assign('downlodcat', $downlodcat);
	$tmpl->assign('row', $row );
	
	$tmpl->display('link_cat.tpl');
	break;
	
	
	// ==============================================================================
	// <<-- KATEGORIE ORDNEN -->>
	// ==============================================================================
	case "saveposi" :
	if(!permission('link_cat_edit')){  echo error($langadmin['noperms']); exit();  }
	foreach ($_POST['posi'] as $id => $posi) {
		if( ($posi != '') && (is_numeric($posi)) ) {
			$query = "UPDATE " . PREFIX . "_linkcat  SET posi=$posi WHERE catid = $id";
			$result = new dbc($query);
		}
	}
	header("Location:links.php?action=category&area=".$_REQUEST['area']."");
	break;
	
	
	// ==============================================================================
	// <<-- KATEGORIE LOESCHEN -->>
	// ==============================================================================
	case "catdel" :
	if(!permission('link_cat_edit')){  echo error($langadmin['noperms']); exit();  }
	linkcatdel($_REQUEST['id']);
	header("Location:links.php?action=category&area=".$_REQUEST['area']."");
	break;
	
	
	// ==============================================================================
	// <<-- KATEGORIE ANLEGEN -->>
	// ==============================================================================
	case "addcateg" :
	if(!permission('link_cat_new')){  echo error($langadmin['noperms']); exit();  }
	if($_REQUEST['editsave']==1){
		
		$descr = $_REQUEST['autobr']==1 ? nl2br($_REQUEST['descr']) : $descr = $_REQUEST['descr'];
		$group_id = (is_array($_REQUEST['group'])) ? implode(",", $_REQUEST['group']) : $_REQUEST['group'];
		
		$sql = new dbc("INSERT INTO ".PREFIX."_linkcat(
		catid,
		parent_id,
		catname,
		catdesc,
		area,
		group_id
		) VALUES (
		'',
		'$_REQUEST[linkcat]',
		'".strip_tags($_REQUEST['title'])."',
		'$descr',
		'$_REQUEST[area]',
		'$group_id'
		)");
		header("Location:links.php?action=category&area=$_REQUEST[area]");
	}
	
	$r_groups = new dbc("SELECT ugroup,groupname FROM " . PREFIX . "_usergroup");
	$groups = array();
	while ($group = $r_groups->fetchrow()) {
		array_push($groups, $group);
	}
	
	$downlodcat = array();
	getLinkcategs_simple(0, "", $downlodcat, $area);
	$tmpl->assign('groups', $groups);
	$tmpl->assign('downlodcat', $downlodcat);
	$tmpl->display('link_catform.tpl');
	break;
	
	
	// ==============================================================================
	// <<-- KATEGORIE BEARBEITEN -->>
	// ==============================================================================
	case "editcateg" :
	if(!permission('link_cat_edit')){  echo error($langadmin['noperms']); exit();  }
	
	if($_REQUEST['editsave']==1){
		$descr = $_REQUEST['autobr']==1 ? nl2br($_REQUEST['descr']) : $descr = $_REQUEST['descr'];
		$group_id = (is_array($_REQUEST['group'])) ? implode(",", $_REQUEST['group']) : $_REQUEST['group'];
		
		$sql = new dbc("UPDATE ".PREFIX."_linkcat SET 
		catname = '".strip_tags($_REQUEST['title'])."',
		catdesc = '$descr',
		group_id = '$group_id'
		WHERE 
		catid = '$_REQUEST[id]'
		");
		header("Location:links.php?action=category&area=$_REQUEST[area]");
	}
	
	
	$sql = new dbc("SELECT * FROM ".PREFIX."_linkcat  WHERE catid='$_REQUEST[id]'");
	$row = $sql->fetchrow();
	$row->group_id = ($row->group_id == "") ? array() : explode(",", $row->group_id);
	
	$r_groups = new dbc("SELECT * FROM " . PREFIX . "_usergroup");
	$groups = array();
	while ($group = $r_groups->fetchrow()) {
		array_push($groups, $group);
	}
	
	
	$tmpl->assign('groups', $groups);
	$tmpl->assign('row', $row);
	$tmpl->display('link_catform.tpl');
	break;
	
	
	case "new" :
	// ==============================================================================
	// <<-- LINK ANLEGEN -->>
	// ==============================================================================
	if(!permission('link_new')) { echo error($langadmin['noperms']); exit(); }
	
	define("FORMS", 1);
	if($_REQUEST['save']==1){
		$descr = ($_REQUEST['autobr2']==1) ? nl2br($_REQUEST['text2']) : $_REQUEST['text2'];
		$hitstoday = (is_numeric($_REQUEST['hits_today'])) ? $_REQUEST['hits_today'] : 0;
		$hits = (is_numeric($_REQUEST['hits'])) ? $_REQUEST['hits'] : 0;
		if($hitstoday > $hits) $hits = $hitstoday;
		
		$sql = new dbc("INSERT INTO ".PREFIX."_links(
		url,
		id,
		area,
		uid,
		ctime,
		title,
		text,
		catid,
		image,
		canvote,
		cancomment,
		hits_today,
		hits
		
		) VALUES (
		'$_REQUEST[url]',
		'',
		'$_REQUEST[area]',
		'".UID."',
		'".time()."',
		'".strip_tags($_REQUEST['title'])."',
		'$descr',
		'$_REQUEST[cat]',
		'$_REQUEST[images]',
		'$_REQUEST[canvote]',
		'$_REQUEST[cancomment]',
		'$hitstoday',
		'$hits'
		)");
		
		header("Location:links.php?action=overview&area=$_REQUEST[area]");
	}
	
	define("FORMS", 1);
	include("forms/linkinit.php");
	
	$tmpl->assign('row', $row);
	$tmpl->display('link_form.tpl');
	break;
	
	case "edit" :
	if(!permission('link_edit')) { echo error($langadmin['noperms']); exit(); }
	
	// ==============================================================================
	// <<-- SPEICHERN -->>
	// ==============================================================================
	if($_REQUEST['save']==1){
		define("FORMS", 1);
		
		if($_REQUEST['resetvote']==1) $votdedb = " ,votes='',  votes_data='',  votes_ip='',  votes_uid=''";
		$descr = ($_REQUEST['autobr2']==1) ? nl2br($_REQUEST['text2']) : $_REQUEST['text2'];
		$hitstoday = (is_numeric($_REQUEST['hits_today'])) ? $_REQUEST['hits_today'] : 0;
		$hits = (is_numeric($_REQUEST['hits'])) ? $_REQUEST['hits'] : 0;
		if($hitstoday > $hits) $hits = $hitstoday;
		
		$sql = new dbc("UPDATE ".PREFIX."_links SET 
		url = '$_REQUEST[url]',
		title = '".strip_tags($_REQUEST['title'])."',
		text = '$descr',
		catid = '$_REQUEST[cat]',
		image = '$_REQUEST[images]',
		canvote = '$_REQUEST[canvote]',
		cancomment = '$_REQUEST[cancomment]',
		hits_today = '$hitstoday',
		hits = '$hits'
		$votdedb
		WHERE id='$_REQUEST[id]'");
		
		header("Location:links.php?action=overview&area=$_REQUEST[area]");
	}
	
	// <<-- AUSGABE -->>
	define("FORMS", 1);
	$sql = new dbc("SELECT * FROM ".PREFIX."_links WHERE id='$_REQUEST[id]'");
	$row = $sql->fetchrow();
	include("forms/linkinit.php");
	
	$tmpl->assign('row', $row);
	$tmpl->display('link_form.tpl');
	break;
	
	// ============================================================================
	// <<-- LOESCHEN -->>
	// ============================================================================
	case "del" :
	if(!permission('download_delete')) { echo error($langadmin['noperms']); exit(); }
	$sql1 = new dbc("DELETE FROM ".PREFIX."_links WHERE id = '$_REQUEST[id]'");
	$sql2 = new dbc("DELETE FROM ".PREFIX."_linkcomments WHERE dlid = '$_REQUEST[id]'");
	header("Location:links.php?area=$_REQUEST[area]&action=overview");
	break;
	
	// ============================================================================
	// <<-- OEFFNEN + SCHLIESSEN EINZELN -->>
	// ============================================================================
	
	case "fastopen" :
	if(!permission('link_edit')) { echo error($langadmin['noperms']); exit(); }
	$result = new dbc("UPDATE ".PREFIX."_links SET active='1' WHERE id = '$_REQUEST[id]'");
	header("Location:links.php?area=$_REQUEST[area]&action=overview");
	break;
	
	case "fastclose" :
	if(!permission('link_edit')) { echo error($langadmin['noperms']); exit(); }
	$result = new dbc("UPDATE ".PREFIX."_links SET active='0' WHERE id = '$_REQUEST[id]'");
	header("Location:links.php?area=$_REQUEST[area]&action=overview");
	break;
	
	// ============================================================================
	// <<-- UEBERSICHT -->>
	// ============================================================================
	case "" :
	case "overview" :
	
	
	switch($_REQUEST['cat']){
		case "" : $optsort_q = ""; break;
		default : $optsort_q = " AND ( catid='".$_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 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 = " ASC";
	}
	$sql = new dbc("SELECT id FROM ".PREFIX."_links 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."_links WHERE $type_query area='$area' $optsort_q $off_on  $order_first $ascdesc");
	$erg = $sql->numrows();
	
	if($num > $limit) $tmpl->assign('navi',$navi = pagenav($seiten, $lang['aktpos'], " <a class=\"pnav\" href=\"links.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> "));
	
	
	$downloads = array();
	while($row = $sql->fetchrow()){
		$sql2 = new dbc("SELECT catname FROM ".PREFIX."_linkcat WHERE catid='".$row->catid."'");
		$row2 = $sql2->fetchrow();
		
		$row->user = fetchusername($row->uid);
		$row->ctime = mygmt($row->ctime);
		$row->catname = $row2->catname;
		$sql2 = new dbc("SELECT id FROM ".PREFIX."_linkcomments  WHERE  dlid='".$row->id."' ");
		$row->comments = $sql2->numrows();
		array_push($downloads,$row);	
	}
	
	$cat_sel = array();
	getLinkcategs_simple(0, "", $cat_sel, $area);
	
	$tmpl->assign('cat_sel', $cat_sel);
	$tmpl->assign('downloads', $downloads);
	$tmpl->assign('num', $sql->numrows());
	$tmpl->display('link_overview.tpl');
	break;
}
?>