<?php

session_start();

$path_to_root = "";
include $path_to_root . 'database.php';

$db = new database();

$nama = strip_tags(trim($_POST['nama']));
$email = strip_tags(trim($_POST['email']));
$pass = strip_tags(trim($_POST['password']));
$pass2 = strip_tags(trim($_POST['cpassword']));

if ($pass != $pass2) {
	header("location:login.php?login=2");
} else {
	$sql = get_sql_cek_email_register($email);

	$result = $db->db_query($sql);
	$num_rows = $db->db_num_rows($result);

	if ($num_rows == 0) {
		$sql2 = get_sql_register($email, $pass);

		$result2 = $db->db_query($sql2);

		if ($result2) {
			$sql_get_profil = get_sql_cek_email_register($email);

			$result_profil = $db->db_query($sql_get_profil);

			while ($rows = $db->db_fetch_array($result_profil)) {
				$id_user = $rows['user_id'];

				$sql_insert_profil = "INSERT INTO `profil_user` (`id_user`, `nama`, `hp`, `email`, `alamat`, `foto`) VALUES ('".$id_user."', '".$nama."', 'kosong', '".$email."', 'kosong', 'kosong')";

				$result_insert = $db->db_query($sql_insert_profil);

				if ($result_insert) {
					header("location:login.php?login=4");
				}
			}
		} else {
			header("location:login.php?login=5");
		}
	} else {
		header("location:login.php?login=3");
	}
}

function get_sql_cek_email_register($email){
	$sql = "SELECT * FROM users WHERE email = '" . $email . "'";
	return $sql;
}

function get_sql_register($email, $pass){
	$sql = "INSERT INTO `users` (`email`, `password`, `level`) VALUES ('" . $email . "', MD5('" . $pass . "'), '2')";
	return $sql;
}

?>