<?php
	echo "<title>Loading...</title><div id='loading'><div class='loader'></div><br>Loading...</div>";
	date_default_timezone_set('Asia/Jakarta');
	
	$busimg_judul		= mysqli_real_escape_string($koneksi, $_POST['busimg_judul']);
	
	$bus_id				= mysqli_real_escape_string($koneksi, $_POST['bus_id']);
	$bus_nama			= mysqli_real_escape_string($koneksi, $_POST['bus_nama']);
	
	$stringreplace 		= array ('/','\\',',','.','#',':',';','\'','"','[',']','{','}',')','(','|','`','~','!','@','%','$','^','&','*','=','?','+',' ','_','-');
	$code		   		= md5(uniqid(rand()));
	$gencode			= substr($code, 0, 5);
	$replace			= strtolower(str_replace($stringreplace,'_',$busimg_judul));
	$nama_file 			= $_FILES["busimg_image"]["name"];
	$tipe_file 			= $_FILES["busimg_image"]["type"];
	$alamat 			= $_FILES["busimg_image"]["tmp_name"];
	$nama_baru 			= "addimg_".$replace."_".$gencode.".".end((explode(".", $nama_file)));
	$tujuan 			= "assets/images/bus/add/$nama_baru";
	
	$postdated			= date("Y-m-d H:i:s");
	
	if(empty($nama_file))
	{
		mysqli_query($koneksi, "INSERT INTO cmas_busimg (busimg_id, busimg_judul, busimg_image, bus_id, bus_nama, postdated, posted_id, posted_username, posted_nama) VALUES(NULL, '$busimg_judul', '$busimg_image', '$bus_id', '$bus_nama', '$postdated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')") or die (mysqli_error($koneksi));
		
		$notification_ip = $_SERVER['REMOTE_ADDR'];
		mysqli_query($koneksi, "INSERT INTO cmas_notification (notification_id, notification_status, notification_description, notification_ip, notification_read, postdated, posted_id, posted_username, posted_nama) VALUE(NULL, 'Input', 'Bus Image: $busimg_judul > $bus_nama', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
		
		echo "<meta http-equiv='refresh' content='0; url=?m=busimg_input&id=$bus_id&is=is'>";
	}
	elseif (!empty($nama_file))
	{
		if ($tipe_file != "image/gif" AND $tipe_file != "image/jpg" AND $tipe_file != "image/jpeg" AND $tipe_file != "image/png") {
			$notification_ip = $_SERVER['REMOTE_ADDR'];
			mysqli_query($koneksi, "INSERT INTO cmas_notification (notification_id, notification_status, notification_description, notification_ip, notification_read, postdated, posted_id, posted_username, posted_nama) VALUE(NULL, 'Input & Upload File', 'Bus: $bus_nama, Error!', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
			
			echo "<meta http-equiv='refresh' content='0; url=?m=bus_input&ufe=ufe'>";
		} else { 
	
			$gambar_baru = move_uploaded_file($alamat, $tujuan);
			mysqli_query($koneksi, "INSERT INTO cmas_busimg (busimg_id, busimg_judul, busimg_image, bus_id, bus_nama, postdated, posted_id, posted_username, posted_nama) VALUES(NULL, '$busimg_judul', '$nama_baru', '$bus_id', '$bus_nama', '$postdated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')") or die (mysqli_error($koneksi));
			
			$notification_ip = $_SERVER['REMOTE_ADDR'];
			mysqli_query($koneksi, "INSERT INTO cmas_notification (notification_id, notification_status, notification_description, notification_ip, notification_read, postdated, posted_id, posted_username, posted_nama) VALUE(NULL, 'Input', 'Bus Image: $busimg_judul > $bus_nama', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
			
			echo "<meta http-equiv='refresh' content='0; url=?m=busimg_input&id=$bus_id&is=is'>";
		}
	}
?>