<?php
	echo "<title>Loading...</title><div id='loading'><div class='loader'></div><br>Loading...</div>";
	date_default_timezone_set('Asia/Jakarta');
	include "libs/functionresize.php";
	
	$stringreplace 		= array ('/','\\',',','.','#',':',';','\'','"','[',']','{','}',')','(','|','`','~','!','@','%','$','^','&','*','=','?','+',' ','_');
	$bus_id				= mysqli_real_escape_string($koneksi, $_POST['bus_id']);
	$bus_nama			= mysqli_real_escape_string($koneksi, $_POST['bus_nama']);
	
	$code		   		= md5(uniqid(rand()));
	$gencode			= substr($code, 0, 5);
	$replace			= strtolower(str_replace($stringreplace,'_',$bus_nama));
	$nama_file 			= $_FILES["bus_header"]["name"];
	$tipe_file 			= $_FILES["bus_header"]["type"];
	$alamat 			= $_FILES["bus_header"]["tmp_name"];
	$nama_baru 			= "bus_".$replace."_".$gencode.".".end((explode(".", $nama_file)));
	$tujuan 			= "assets/images/bus/head/$nama_baru";
	
	$updated			= date("Y-m-d H:i:s");
	
	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, 'Upload', 'File Header 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=bushead_edit&id=$bus_id&ufe=ufe'>";
	} else { 
		$gambar_baru = move_uploaded_file($alamat, $tujuan);
		resize_crop_image(1920, 300, $tujuan, $tujuan);
		mysqli_query($koneksi, "UPDATE cmas_bus SET
		bus_header			= '$nama_baru',
		bus_headerid		= '1',
		updated					= '$updated',
		updated_id				= '$udt[admin_id]',
		updated_username		= '$udt[admin_username]',
		updated_nama			= '$udt[admin_nama]'
		WHERE bus_id		= '$bus_id' ") 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, 'Upload', 'File Header Bus: $bus_nama', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
		
		echo "<meta http-equiv='refresh' content='0; url=?m=bushead_edit&id=$bus_id&us=us'>";
	}
?>