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

$tmpl = new Koobi("templates/" . ADMINTHEME . "/guestbook/");
$tmpl->assign('title', $title);
$tmpl->assign('id', $_REQUEST['id']);
$tmpl->assign('onkey', "if(event.keyCode < 48 || event.keyCode > 57) { event.returnValue = false; } else {if(event.which < 48 || event.which > 57) return false;}");
$tmpl->assign('langadmin', $langadmin);
$tmpl->assign('theme', ADMINTHEME);
$tmpl->assign('pref', $pref);


$sql = new dbc("SELECT * FROM " . PREFIX . "_guestbook_settings");
$row_settings = $sql->fetchrow();
$tmpl->assign('maxpostlength', $row_settings->maxpostlength);
$tmpl->assign('spamtime', $row_settings->spamprotect_time);
$tmpl->assign('mailbycomment', $row_settings->mailbycomment);
$tmpl->assign('spamprotect', $row_settings->spamprotect);

switch($_REQUEST['action']){
	case '':
	case 'show':
	
	// aendern...
	if($_REQUEST['do']=="save"){
	if(count($_POST['author'])>0){
	foreach ($_POST['author'] as $id => $author) {
			$sql = new dbc("
			UPDATE " . PREFIX . "_guestbook SET 
			author ='".$_POST['author'][$id]."', 
			comment ='".$_POST['comment'][$id]."' ,
			email ='".$_POST['email'][$id]."' ,
			web ='".$_POST['web'][$id]."' ,
			authfrom ='".$_POST['authfrom'][$id]."' 
			WHERE id = $id
			");
		}
	}
		
	// oder loeschen...
	if(count($_POST['del'])>0){
	foreach ($_POST['del'] as $id => $del) {
			$sql = new dbc("DELETE FROM " . PREFIX . "_guestbook WHERE id = $id");
			// auto_increment wieder zuruecksetzen
			$sql = new dbc("ALTER TABLE " . PREFIX . "_guestbook PACK_KEYS =0 CHECKSUM =0 DELAY_KEY_WRITE =0 AUTO_INCREMENT =1");
		}
	  }
	}
	
	$limit = ($_REQUEST['pp']!='') ? $_REQUEST['pp'] : '15';
	$sort = ($_REQUEST['sort']!='') ? escs($_REQUEST['sort']) : 'desc';
	$inserts = array();
	
	$sql = new dbc("SELECT id FROM " . PREFIX . "_guestbook");
	$num = $sql->numrows();
	
	$seiten = ceil($num / $limit);
	$a = prepage() * $limit - $limit;
	
	// Navi 
	if($num > $limit){
		$tmpl->assign('navi', pagenav($seiten, $lang['aktpos'], " <a class=\"pnav\" href=\"guestbook.php?pp=$limit&amp;sort=$_REQUEST[sort]&amp;page={s}\">{t}</a> "));
	}
			
	$sql = new dbc("SELECT * FROM " . PREFIX . "_guestbook order by id $sort limit $a,$limit");
	$erg = $sql->numrows();

while ( $row = $sql->fetchrow()){
		$row->ctime = mygmt($row->ctime);
		array_push($inserts, $row);
	}
	
	$tmpl->assign('comments_array', $inserts);

	$tmpl->display('entries.tpl');
	break;
	
	case 'settings':
	$sql = new dbc("UPDATE ".PREFIX."_guestbook_settings SET
	spamprotect = '$_REQUEST[spamprotect]',
	mailbycomment = '$_REQUEST[mailbycomment]',
	maxpostlength  = '$_REQUEST[maxpostlength]',
	spamprotect_time = '$_REQUEST[spamprotect_time]'
	");
	header("Location:guestbook.php");
	break;
}


?>