<?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";
	
	$kontenimg_judul		= mysqli_real_escape_string($koneksi, $_POST['kontenimg_judul']);
	
	$konten_id			= mysqli_real_escape_string($koneksi, $_POST['konten_id']);
	$konten_judul		= mysqli_real_escape_string($koneksi, $_POST['konten_judul']);
	
	$stringreplace 		= array ('/','\\',',','.','#',':',';','\'','"','[',']','{','}',')','(','|','`','~','!','@','%','$','^','&','*','=','?','+',' ','_','-');
	$code		   		= md5(uniqid(rand()));
	$gencode			= substr($code, 0, 5);
	$replace			= strtolower(str_replace($stringreplace,'_',$kontenimg_judul));
	$nama_file 			= $_FILES["kontenimg_image"]["name"];
	$tipe_file 			= $_FILES["kontenimg_image"]["type"];
	$alamat 			= $_FILES["kontenimg_image"]["tmp_name"];
	$nama_baru 			= "addimg_".$replace."_".$gencode.".".end((explode(".", $nama_file)));
	$tujuan 			= "assets/images/konten/img/$nama_baru";
	
	$postdated			= date("Y-m-d H:i:s");
	
	if(empty($nama_file))
	{
		mysqli_query($koneksi, "INSERT INTO cmas_kontenimg (kontenimg_id, kontenimg_judul, kontenimg_image, konten_id, konten_judul, postdated, posted_id, posted_username, posted_nama) VALUES(NULL, '$kontenimg_judul', '$kontenimg_image', '$konten_id', '$konten_judul', '$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', 'Konten Image: $kontenimg_judul > $konten_judul', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
		
		echo "<meta http-equiv='refresh' content='0; url=?m=kontenimg_input&id=$konten_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', 'Konten: $konten_judul, Error!', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
			
			echo "<meta http-equiv='refresh' content='0; url=?m=konten_input&ufe=ufe'>";
		} else { 
	
			$gambar_baru = move_uploaded_file($alamat, $tujuan);
			// resize_crop_image(1200, 800, $tujuan, $tujuan);
			mysqli_query($koneksi, "INSERT INTO cmas_kontenimg (kontenimg_id, kontenimg_judul, kontenimg_image, konten_id, konten_judul, postdated, posted_id, posted_username, posted_nama) VALUES(NULL, '$kontenimg_judul', '$nama_baru', '$konten_id', '$konten_judul', '$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', 'Konten Image: $kontenimg_judul > $konten_judul', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
			
			echo "<meta http-equiv='refresh' content='0; url=?m=kontenimg_input&id=$konten_id&is=is'>";
		}
	}
?>