<?php
if ( !defined('IN_MFA') )
		die("Hacking attempt");

$user->DieNotAdmin();

$layoutadminargs['admin_start'] = $lang['admin_start'];
$layoutadminargs['general_config'] = $lang['general_config'];
$layoutadminargs['user_management'] = $lang['user_management'];
$layoutadminargs['maintenance'] = $lang['maintenance'];
$layoutadminargs['back'] = $lang['back'];


if((IsSet($_POST['subnew'])) || (IsSet($_POST['subedit'])))
{
	if($_POST['user_name'] == '')
		$escape = "Kein Benutzername angegeben";
	if(($_POST['user_password'] == '') && (!IsSet($_POST['subedit']))) 
		$escape = "Kein Passwort angegeben";
	if($_POST['user_password'] != $_POST['user_password2'])
		$escape= "Passwortangaben sind nicht identisch";

	//user_root_dir in die richtige Form bringen
	if(substr($_POST['user_root_dir'], -1) != "/") 
		$_POST['user_root_dir'] .= "/";
	if(trim($_POST['user_root_dir']) == "/")
		$_POST['user_root_dir'] = "";
	if(substr($_POST['user_root_dir'], 0, 1) == "/") 
		$_POST['user_root_dir']= substr($_POST['user_root_dir'], 1);
	
	if(!file_exists(ROOT_DIR."/".$_POST['user_root_dir']))
		$escape = "The new home directory does not exist";
	
	if(IsSet($escape))
		$show_after_header .= "<p class='error'>".$escape."</p>";
	else
	{	
		//userrechte verabreiten				
		if($_POST['user_level'] == 1)
			$user_rights = "11111"; //Admin darf automatisch alles
		else
		{
			$user_rights = "";
			
			$user_rights .=(! $_POST['allowupload'])? "0":"1"; //STANDART setzen
			$user_rights .=(! $_POST['allowdelete'])? "0":"1";
			$user_rights .=(! $_POST['allowview'])? "0":"1";
			$user_rights .=(! $_POST['allowedit'])? "0":"1";
			$user_rights .=(! $_POST['allowscripting'])? "0":"1";
		}	
		if(IsSet($_POST['subnew']))
		{
			$newid = 1;
			foreach($users as $usr)  // werden in neuer list im FORMAT [id],[username],[password],[user_level],[user_rights]; gespeichert
			{
				$usr = new users($usr);
				if($usr->user_id >= $newid)
					$newid = $usr->user_id + 1;
				if(strtoupper($usr->user_name) == strtoupper($_POST['user_name']) || strtoupper($_POST['user_name']) == "ANONYMOUS")
					message_die("User already exists","This user already exists","Warning",2,"admin_users");
			}
			//neuen User in Liste aufnehmen
			$new_user = array($newid, $_POST['user_name'],crypt($_POST['user_password']),$_POST['user_level'],$user_rights,"english",0,"default",$_POST['user_root_dir'],"codepress");
		
			if(!write_userdata($new_user))
				message_die("Error","Couldn`t write Userdata","Critical Error");
		}
		
		if(IsSet($_POST['subedit']))
		{
			$edituser = new users($_GET['object']);
			if($edituser->user_id == -1)
				message_die("Error","Invalid Username","Critical Error");
			if($_POST['user_name'] != $edituser->user_name)
			{
				$user_file = new file(SCRIPT_PATH."/work/data/user_".strtolower($edituser->user_name).".php");
				$user_file->rename(SCRIPT_PATH."/work/data/user_".strtolower($_POST['user_name']).".php");
			}
			if($_POST['user_password'] != '')//wenn auch ein neues Passwort angegeben wurde
				$edituser->user_password = crypt($_POST['user_password']);
					
			$edituser->user_name = $_POST['user_name'];
			$edituser->user_normallevel = $_POST['user_level'];
			$edituser->user_rights = $user_rights;
			$edituser->user_root_dir = $_POST['user_root_dir'];
			$edituser->writeuser();
		}
		//message_die("Job successfully done","The job, requested by you was successfully done. You will be redirected in one second", "Message",2,"admin_users");
	}	
} 
//User löschen
if(IsSet($_GET['confirm']))
{
	$deluser = new users($_GET['object']);
	if(!$deluser->delete())
		message_die("Error","Couldn`t delete User","Error");
}


$layoutadminargs['info_text'] = $lang['admin_users_text'];

//FORMULARE ZUM BEARBEITEN/ANLEGEN DER ACCOUNTS
if(IsSet($_GET['admin_mode']))
{	
	if($_GET['admin_mode'] == "edit")
		$edituser = new users($_GET['object']);
	
	if(($_GET['admin_mode'] == "new") || ($_GET['admin_mode'] == "edit"))
	{
		//form tag
		$layoutadminargs['admin_main_content'] .= "<table border='0'><form action = 'index.php?mode=admin_users&amp;admin_execute=".$_GET['admin_mode']."";
		//wenn USER editiert werden soll
		if($_GET['admin_mode'] == 'edit') $layoutadminargs['admin_main_content'] .= "&amp;object=".$_GET['object'];
		$layoutadminargs['admin_main_content'] .= "' method='post'>
		
		<tr>
			<td><b>".$lang['username']."</b></td><td><input type='text' name='user_name'";
		if($edituser) 
			$layoutadminargs['admin_main_content'] .= "value='".$edituser->user_name."'";
		
		//Zugriffsreche im einzelnen
		$layoutadminargs['admin_main_content'] .= "></td>
			<td rowspan='5' class='box' valign='top'><b>".$lang['simple_user_only']."</b><br>";
				if($_GET['admin_mode'] == "new")
				{
					$layoutadminargs['admin_main_content'] .="
						<input type='checkbox' name='allowupload' value='1'>".$lang['allowupload']."<br>
						<input type='checkbox' name='allowdelete' value='1'>".$lang['allowdelete']."<br>
						<input type='checkbox' name='allowview' value='1'>".$lang['allowview']."<br>
						<input type='checkbox' name='allowedit' value='1'>".$lang['allowedit']."<br>
						<input type='checkbox' name='allowscripting' value='1'>".$lang['allowscripting'];
				}
				if($_GET['admin_mode'] == "edit")
				{
					//WENN USER DIE RECHTE SCHON HAT, DANN ANKREUZEN
					$layoutadminargs['admin_main_content'] .= "<input type='checkbox' name='allowupload' value='1' ".($edituser->has_right("upload")?"CHECKED":"").">".$lang['allowupload']."<br>";	
					$layoutadminargs['admin_main_content'] .= "<input type='checkbox' name='allowdelete' value='1' ".($edituser->has_right("delete")?"CHECKED":"").">".$lang['allowdelete']."<br>";
					$layoutadminargs['admin_main_content'] .= "<input type='checkbox' name='allowview' value='1' ".($edituser->has_right("view")?"CHECKED":"").">".$lang['allowview']."<br>";
					$layoutadminargs['admin_main_content'] .= "<input type='checkbox' name='allowedit' value='1' ".($edituser->has_right("edit")?"CHECKED":"").">".$lang['allowedit']."<br>";
					$layoutadminargs['admin_main_content'] .= "<input type='checkbox' name='allowscripting' value='1' ".($edituser->has_right("scripting")?"CHECKED":"").">".$lang['allowscripting']."<br>";
				}
					
		$layoutadminargs['admin_main_content'] .= 
			"</td>
		</tr>
		<tr>
			<td><b>Home Direcotry</b></td><td><input type='text' name='user_root_dir'";
		if($edituser) 
			$layoutadminargs['admin_main_content'] .= "value='".$edituser->user_root_dir."'";
		
		//Zugriffsreche im einzelnen
		$layoutadminargs['admin_main_content'] .= "></td>";
		$layoutadminargs['admin_main_content'] .=  "<tr>
			<td><b>".$lang['password']."</b></td><td><input type='password' name='user_password'></td>
		</tr>
		<tr>
			<td><b>".$lang['repeat']."</b></td><td><input type='password' name='user_password2'></td>
		</tr>
		<tr>";
		if($_GET['admin_mode'] == "new")
			$layoutadminargs['admin_main_content'] .= "<td><b>".$lang['rank']."</b></td><td><select size='1' name='user_level'><option value='1'>".$lang['admin']."<option value='0'>".$lang['normal_user']."</select></td>";
		else
		{
			if($edituser->user_normallevel == 1)
				$layoutadminargs['admin_main_content'] .= "<td><b>".$lang['rank']."</b></td><td><select size='1' name='user_level'><option value='1' SELECTED>".$lang['admin']."<option value='0'>".$lang['normal_user']."</select></td>";
			if($edituser->user_normallevel == 0)
				$layoutadminargs['admin_main_content'] .= "<td><b>".$lang['rank']."</b></td><td><select size='1' name='user_level'><option value='1'>".$lang['admin']."<option value='0' SELECTED>".$lang['normal_user']."</select></td>";
		}
		$layoutadminargs['admin_main_content'] .= "</tr>
		<tr><td colspan='3'><input type='submit' name='sub".$_GET['admin_mode']."' value='Eintragen'></td></tr>
		</form>
		</table>";
	}
	//LÖSCHEN(Bestätigung ob User sicher ist)
	if($_GET['admin_mode'] == "delete")
	{
		$layoutadminargs['admin_main_content'] .="<span class='error'>".$lang['sure']."</span> <br>";
		$layoutadminargs['admin_main_content'] .="<a href='index.php?mode=admin_users&amp;confirm=1&amp;object=".$_GET['object']."'>".$lang['yes']."</a>&nbsp;|&nbsp;<a href='index.php?mode=admin_users'>".$lang['no']."</a><br><br>";
	}
}

//Userliste anzeigen
$layoutadminargs['admin_main_content'] .= "<table>";

$users = getusers();

$admins = array();
$normals = array();
foreach($users as $usr) //sieht dann so aus:  [user_level],[user_name]
{	
	$usr = new users($usr);
	if($usr->user_normallevel == 0)
		$normals[] = $usr->user_name.",".$usr->user_normallevel;
	else
		$admins[] = $usr->user_name.",".$usr->user_normallevel;
}

sort($admins);
sort($normals);

$userlist = array_merge($admins,$normals);

foreach($userlist as $usr)
{
	$usr = explode(",", $usr);
	if($usr[1] == 1)
		$user_rank = $lang['admin'];
	else
		$user_rank = $lang['normal_user'];
	
	$layoutadminargs['admin_main_content'] .= "<tr><td><b>".$usr[0]."</b></td><td>".$user_rank."</td><td><a class='small' href='index.php?mode=admin_users&amp;admin_mode=edit&amp;object=".$usr[0]."'>".$lang['edit']."</a>&nbsp;|&nbsp;<a class='small' href='index.php?mode=admin_users&amp;admin_mode=delete&amp;object=".$usr[0]."'>".$lang['delete']."</a></td></tr>";
}
$layoutadminargs['admin_main_content'] .= "<tr><td colspan='3' align='center'><a class='small' href='index.php?mode=admin_users&amp;admin_mode=new'>".$lang['new_user']."</a></td></tr>
												</table>";

$show_after_header .= $template->layoutparser("adminpage",$layoutadminargs);
?>