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


if($_REQUEST['action']=="new") {
	$butt = $langadmin['addgroup'];
	$form = "groups.php?action=new&send=1";
	$title = $langadmin['addgroup'];
	}else{
	$butt = $langadmin['saveperms'];
	$form = "groups.php?action=edit&id=".$_REQUEST['id']."&send=1";
	$title = $langadmin['editgroup'];
}

$tmpl = new Koobi("templates/" . ADMINTHEME . "/groups/");
$tmpl->assign("form", $form);
$tmpl->assign("butt", $butt);
$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);
// ==========================================
// <<-- Gruppenübersicht -->>
// ==========================================
if (!isset($_REQUEST['action'])){ 
	$tmpl->assign("theme", ADMINTHEME);
	$tmpl->assign("lang", $lang);
	$tmpl->assign("langadmin", $langadmin);
	$tmpl->assign("area", $area);
	
	
	$group_array = array();	
	
	$sql = new dbc("SELECT * FROM ".PREFIX."_usergroup ORDER BY ugroup ASC");
	while ($row=$sql->fetchrow()){
		
		$sql2 = new dbc("SELECT * FROM ".PREFIX."_user WHERE ugroup='".$row->ugroup."'");
		$row1 = $sql2->fetchrow();
		$row->user = $sql2->numrows();
		
		array_push($group_array, $row);
		
	}
	$tmpl->assign("group_array", $group_array);
	$tmpl->assign("row", $row);
	$tmpl->display("groups.tpl");
	
}

// ==========================================
// <<-- Gruppe bearbeiten -->>
// ==========================================
if(isset($_REQUEST['action']) && $_REQUEST['action']=="edit") {
	unset($row,$adminrights,$siterights);
	
	
	if(!permission("editgroups")) { echo error($langadmin['nopermeditgroup']); exit();}
	
	if(!empty($_REQUEST['gname'])){
		
		$target = "../uploads/avatars/default/";
		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") {
			
			$filesize = @filesize($_FILES['file']['tmp_name']);
			$fupload_name = @ktrim($_FILES['file']['name'],1);
			$fupload_name = @rand_tostring($target ,$fupload_name);
			
			if( $_REQUEST['delav']!=1 ){
				@move_uploaded_file($_FILES['file']['tmp_name'], $target . $fupload_name);
				@chmod($target . $fupload_name,0777);
				$avatar = "default_avatar = '$fupload_name',";
				
				$sql_old = new dbc("SELECT default_avatar FROM " . PREFIX . "_usergroup WHERE ugroup='$_REQUEST[id]'");
				$row_old = $sql_old->fetchrow();
				@unlink($target . $row_old->default_avatar);
				$avatar .= "default_avatar='$fupload_name',";
				
			}
		}
		
		
		if($_REQUEST['delav']==1){
			$sql_old = new dbc("SELECT default_avatar  FROM " . PREFIX . "_usergroup WHERE ugroup='$_REQUEST[id]'");
			$row_old = $sql_old->fetchrow();
			@unlink($target . $row_old->user_avatar);
			$avatar .= "default_avatar='',";
		}
		
		$sql = new dbc("UPDATE ".PREFIX."_usergroup SET 
		$avatar 
		maxpicdownload='".$_REQUEST['maxpicdownload']."', 
		maxcommlength='".$_REQUEST['maxcommlength']."', 
		groupname='".strip_tags($_REQUEST['gname'])."',
		groupname_single ='".strip_tags($_REQUEST['gname_show'])."',
		groupmaxpn='".$_REQUEST['groupmaxpn']."', 
		maxpnlength='".$_REQUEST['maxpnlength']."',
		groupmaxsig='".$_REQUEST['groupmaxsig']."',
		avatar_size ='".$_REQUEST['avatar_size']."',
		avatar_width ='".$_REQUEST['avatar_width']."',
		avatar_height ='".$_REQUEST['avatar_height']."' ,
		set_default_avatar = '$_REQUEST[set_default_avatar]',
		maxlength_post = '$_REQUEST[maxlength_post]',
		max_attachments = '$_REQUEST[max_attachments]'
		WHERE ugroup='".$_REQUEST['id']."'");
		header("location:groups.php");
	}
	
	
	
	$sql = new dbc("SELECT * FROM ".PREFIX."_usergroup WHERE ugroup='".$_REQUEST['id']."'");
	$row = $sql->fetchrow();
	$maxpn = $row->groupmaxpn;
	$maxpnlength = $row->maxpnlength;
	$groupmaxsig = $row->groupmaxsig;
	$maxcommlength = $row->maxcommlength;
	$maxpicdownload = $row->maxpicdownload;
	$maxlength_post = $row->maxlength_post;
	$max_attachments = $row->max_attachments;
	
	if($row->default_avatar != ""){
		$tmpl->assign('avatar', "<img src=\"../uploads/avatars/default/$row->default_avatar\" alt=\"\" />");
	}
	
	$action = $_REQUEST['action'];
	
	if(!$maxpn){ $maxpn = "50"; }
	if(!$maxpnlength){ $maxpnlength = "5000"; }
	if(!$groupmaxsig) { $groupmaxsig = "100"; } 
	if(!$maxcommlength){ $maxcommlength = "100"; } 
	if(!$maxpicdownload){ $maxpicdownload = "5"; } 
	if(!maxlength_post){ $maxlength_post = "50000"; } 
	if(!max_attachments) { $max_attachments = "5"; }
	
	$tmpl->assign("maxlength_post", $maxlength_post);
	$tmpl->assign("maxpicdownload", $maxpicdownload);
	$tmpl->assign("maxpn", $maxpn);
	$tmpl->assign("maxpnlength", $maxpnlength);
	$tmpl->assign("groupmaxsig", $groupmaxsig);
	$tmpl->assign("maxcommlength", $maxcommlength);
	$tmpl->assign("maxlength_post", $maxlength_post);
	$tmpl->assign("max_attachments", $max_attachments);
	$tmpl->assign("row", $row);
	$tmpl->assign("action", $action);
	$tmpl->display("permform.tpl");
}


// ==========================================
// <<-- Gruppe löschen -->>
// ==========================================
if(isset($_REQUEST['action']) && $_REQUEST['action']=="delete") { 
	if(!permission("delgroups")) { echo error($langadmin['nopermdelgroup']); exit();}
	
	if($_REQUEST['id'] == 1 || $_REQUEST['id'] == 2 || $_REQUEST['id'] == 3 || $_REQUEST['id'] == 4){
		echo error($langadmin['groupscantdelete']);
		exit();
	}
	
	if($_REQUEST['send']==1){
		$sql = new dbc("UPDATE ".PREFIX."_user SET ugroup='".$_REQUEST['newgroup']."' WHERE ugroup='".$_REQUEST['id']."'");
		$sql = new dbc("DELETE FROM ".PREFIX."_usergroup WHERE ugroup='".$_REQUEST['id']."'");
		$sql = new dbc("DELETE FROM ".PREFIX."_permissions WHERE group_id='".$_REQUEST['id']."'");
		header("Location:groups.php");
	}
	
	$dbs = new dbc("SELECT * FROM ".PREFIX."_usergroup WHERE ugroup!='2' ORDER BY ugroup ASC");
	
	$ugroup = array();
	
	while ($row = $dbs->fetchrow()){
		array_push($ugroup, $row);
	}
	
	$tmpl->assign("gname", $_REQUEST['gname']);
	$tmpl->assign("ugroup", $ugroup);
	$tmpl->display("delgroups.tpl");
}

if(isset($_REQUEST['action']) && $_REQUEST['action']=="new") { 
	if(!permission("editgroups")) { echo error($langadmin['nopermeditgroup']); exit();}
	if($_REQUEST['send']==1){
		
		$target = "../uploads/avatars/default/";
		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") {
			
			$filesize = @filesize($_FILES['file']['tmp_name']);
			$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);
			
		}
		
		$q = "INSERT INTO ".PREFIX."_usergroup (
		default_avatar,
		maxpicdownload,
		ugroup,
		groupname,
		groupname_single,
		groupmaxpn,
		maxpnlength,
		groupmaxsig,
		avatar_size,
		avatar_width,
		avatar_height,
		set_default_avatar,
		maxlength_post,
		max_attachments
		) VALUES (
		'$fupload_name',
		'".$_REQUEST['maxpicdownload']."',
		'',
		'".$_REQUEST['gname']."',
		'".$_REQUEST['gname_show']."',
		'".$_REQUEST['groupmaxpn']."',
		'".$_REQUEST['maxpnlength']."',
		'".$_REQUEST['groupmaxsig']."',
		'".$_REQUEST['avatar_size']."',
		'".$_REQUEST['avatar_width']."',
		'".$_REQUEST['avatar_height']."',
		'$_REQUEST[set_default_avatar]',
		'$_REQUEST[maxlength_post]',
		'$_REQUEST[max_attachments]'
		)";
		
		$sql = new dbc("$q");
		$getid = $sql->getid();
		
		// standardmaske fuer die rechte einer gruppe
		include("perm_mask.php");
		$sql = new dbc("SELECT id FROM " . PREFIX . "_f_forum");
		while($row = $sql->fetchrow()){
			$query = new dbc("INSERT INTO " . PREFIX . "_f_permissions (forum_id, group_id, permissions) VALUES ('$row->id', '" . $getid . "', '$default_mask')");
		}
		header("Location:groups.php");
	}
	
	
	$action = $_REQUEST['action'];
	$tmpl->assign("row", $row);
	$tmpl->assign("action", $action);
	$tmpl->display("permform.tpl");
}
?>