<?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');
	$tableuser = new PoTable('users');
	$users = $tableuser->findAll('id_user', 'ASC');
	foreach($users as $user){
		$user = $user->id_user;
	}
	$id_user = $user + 1;
	$table = new PoTable('users');
	$table->save(array(
		'id_user' => $id_user,
		'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');
$iduser = $val->validasi($_POST['iduser'],'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');
$data = $_POST[bio];
$eutf = htmlspecialchars($data,ENT_QUOTES);

$extensionList = array("jpg", "jpeg");
$fileName = $_FILES['fupload']['name'];
$tmpName  = $_FILES['fupload']['tmp_name'];
$fileType = $_FILES['fupload']['type'];
$fileSize = $_FILES['fupload']['size'];
$pecah = explode(".", $fileName);
$ekstensi = $pecah[1];
$nama_file_unik = $iduser.'.'.$ekstensi;
$picture = 'user-'.$nama_file_unik;

if(empty($tmpName)){
	if (empty($_POST[newpassword])) {
		$data = array(
			'nama_lengkap' => $namalengkap,
			'email' => $email,
			'blokir' => $blokir,  
			'no_telp' => $telp,
			'bio' => $data
		);
		$table = new PoTable('users');
		$table->updateBy('id_session', $id, $data);
	}else{
		$pass=md5($_POST[newpassword]);
		$data = array(
			'password' => $pass,
			'nama_lengkap' => $namalengkap,
			'email' => $email,
			'blokir' => $blokir,
			'no_telp' => $telp,
			'bio' => $data
		);
		$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[iduser] = $currentUser->id_user;
	$_SESSION[namauser] = $currentUser->username;
	$_SESSION[namalengkap] = $currentUser->nama_lengkap;
	$_SESSION[passuser] = $currentUser->password;
	$_SESSION[leveluser] = $currentUser->level;
	header('location:../../admin.php?mod='.$mod);
}else{
	if (empty($_POST[newpassword])) {
		$fileimage = "../../../po-content/po-upload/user-$iduser.jpg";
		if (file_exists("$fileimage")){
			unlink("../../../po-content/po-upload/user-$iduser.jpg");
		}
		UploadUser($nama_file_unik);
		$data = array(
			'nama_lengkap' => $namalengkap,
			'email' => $email,
			'blokir' => $blokir,  
			'no_telp' => $telp,
			'userpicture' => $nama_file_unik,
			'bio' => $data
		);
		$table = new PoTable('users');
		$table->updateBy('id_session', $id, $data);
	}else{
		$fileimage = "../../../po-content/po-upload/user-$iduser.jpg";
		if (file_exists("$fileimage")){
			unlink("../../../po-content/po-upload/user-$iduser.jpg");
		}
		UploadUser($nama_file_unik);
		$pass=md5($_POST[newpassword]);
		$data = array(
			'password' => $pass,
			'nama_lengkap' => $namalengkap,
			'email' => $email,
			'blokir' => $blokir,
			'no_telp' => $telp,
			'userpicture' => $nama_file_unik,
			'bio' => $data
		);
		$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[iduser] = $currentUser->id_user;
	$_SESSION[namauser] = $currentUser->username;
	$_SESSION[namalengkap] = $currentUser->nama_lengkap;
	$_SESSION[passuser] = $currentUser->password;
	$_SESSION[leveluser] = $currentUser->level;
	header('location:../../admin.php?mod='.$mod);
}
}
}
?>