<?php
session_start();
if (empty($_SESSION['namauser']) AND empty($_SESSION['passuser'])){
	header('location:../../404.php');
}else{
include_once '../../../po-library/po-database.php';
include_once '../../../po-library/po-function.php';

$val = new Povalidasi;
$mod=$_POST[mod];
$act=$_POST[act];

// Input user
if ($mod=='user' AND $act=='input'){
$username = $val->validasi($_POST['username'],'xss');
$pass=md5($_POST[password]);
$namalengkap = $val->validasi($_POST['nama_lengkap'],'xss');
$email = $val->validasi($_POST['email'],'xss');
$telp = $val->validasi($_POST['no_telp'],'xss');

	$table = new PoTable('users');
	$table->save(array(
		'username' => $username,
		'password' => $pass,
		'nama_lengkap' => $namalengkap,
		'email' => $email, 
		'no_telp' => $telp,
		'id_session' => $pass
	));
	header('location:../../admin.php?mod='.$mod);
}

// Update user
elseif ($mod=='user' AND $act=='update'){
$id = $val->validasi($_POST['id'],'xss');
$namalengkap = $val->validasi($_POST['nama_lengkap'],'xss');
$email = $val->validasi($_POST['email'],'xss');
$telp = $val->validasi($_POST['no_telp'],'xss');
$blokir = $val->validasi($_POST['blokir'],'xss');

	if (empty($_POST[password])) {
		$data = array(
			'nama_lengkap' => $namalengkap,
			'email' => $email,
			'blokir' => $blokir,  
			'no_telp' => $telp
		);
		$table = new PoTable('users');
		$table->updateBy('id_session', $id, $data);
	}else{
		$pass=md5($_POST[password]);
		$data = array(
			'password' => $pass,
			'nama_lengkap' => $namalengkap,
			'email' => $email,
			'blokir' => $blokir,
			'no_telp' => $telp
		);
		$table = new PoTable('users');
		$table->updateBy('id_session', $id, $data);
	}
	$tableuser = new PoTable('users');
	$currentUser = $tableuser->findBy(username, $_SESSION['namauser']);
	$currentUser = $currentUser->current();
	session_start();
	session_register("namauser");
	session_register("passuser");
	session_register("namalengkap");
	session_register("leveluser");
	$_SESSION[namauser] = $currentUser->username;
	$_SESSION[namalengkap] = $currentUser->nama_lengkap;
	$_SESSION[passuser] = $currentUser->password;
	$_SESSION[leveluser] = $currentUser->level;
	header('location:../../admin.php?mod='.$mod);
}
}
?>