<?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 ('/','\\',',','.','#',':',';','\'','"','[',']','{','}',')','(','|','`','~','!','@','%','$','^','&','*','=','?','+',' ','_');
	$konten_judul		= mysqli_real_escape_string($koneksi, $_POST['konten_judul']);
	$konten_sdesc		= mysqli_real_escape_string($koneksi, $_POST['konten_sdesc']);
	$konten_desc		= mysqli_real_escape_string($koneksi, $_POST['konten_desc']);
	$konten_author		= mysqli_real_escape_string($koneksi, $_POST['konten_author']);
	$konten_status		= mysqli_real_escape_string($koneksi, $_POST['konten_status']);
	$konten_seo			= strtolower(str_replace($stringreplace,'-',$konten_judul));
	$konten_metatitle	= mysqli_real_escape_string($koneksi, $_POST['konten_metatitle']);
	$konten_metadesc	= mysqli_real_escape_string($koneksi, $_POST['konten_metadesc']);
	$konten_metakey		= mysqli_real_escape_string($koneksi, $_POST['konten_metakey']);
	
	$kontenkat_id		= mysqli_real_escape_string($koneksi, $_POST['kontenkat_id']);
	$kontenkat_judul	= mysqli_real_escape_string($koneksi, $_POST['kontenkat_judul']);
	$kontenkat_seo		= strtolower(str_replace($stringreplace,'-',$kontenkat_judul));
	
	$code		   		= md5(uniqid(rand()));
	$gencode			= substr($code, 0, 5);
	$replace			= strtolower(str_replace($stringreplace,'_',$konten_judul));
	$nama_file 			= $_FILES["konten_image"]["name"];
	$tipe_file 			= $_FILES["konten_image"]["type"];
	$alamat 			= $_FILES["konten_image"]["tmp_name"];
	if ($kontenkat_id == "2") {
		$nama_baru 			= "info_".$replace."_".$gencode.".".end((explode(".", $nama_file)));
	} else {
		$nama_baru 			= "artikel_".$replace."_".$gencode.".".end((explode(".", $nama_file)));
	}
	$tujuan 			= "assets/images/konten/$nama_baru";
	
	$ndb = mysqli_query($koneksi, "SELECT MAX(konten_order) AS NoTerakhir FROM cmas_konten WHERE kontenkat_id='$kontenkat_id'");
	$ndt = mysqli_fetch_array($ndb);
	$lastno = $ndt['NoTerakhir'];
	$nourut = (int) $lastno;
	$nourut++;
	
	$konten_order		= $nourut;
	$konten_date		= mysqli_real_escape_string($koneksi, $_POST['konten_date']);
	
	$postdated			= date("Y-m-d H:i:s");
	$updated			= date("Y-m-d H:i:s");
	
	if(empty($nama_file))
	{
		mysqli_query($koneksi, "INSERT INTO cmas_konten (konten_id, konten_judul, konten_sdesc, konten_desc, konten_author, konten_status, konten_seo, konten_metatitle, konten_metadesc, konten_metakey, konten_image, konten_imageid, konten_header, konten_headerid, konten_order, konten_date, kontenkat_id, kontenkat_judul, kontenkat_seo, postdated, updated, posted_id, posted_username, posted_nama, updated_id, updated_username, updated_nama) VALUES(NULL, '$konten_judul', '$konten_sdesc', '$konten_desc', '$konten_author', '$konten_status', '$konten_seo', '$konten_metatitle', '$konten_metadesc', '$konten_metakey', '$konten_image', '$konten_imageid', '$konten_header', '$konten_headerid', '$konten_order', '$konten_date', '$kontenkat_id', '$kontenkat_judul', '$kontenkat_seo', '$$postdated', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]', '$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: $konten_judul', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
	
		echo "<meta http-equiv='refresh' content='0; url=?m=konten_indexkat&id=$kontenkat_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" AND $tipe_file != "image/webp") {
			$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);
			if ($kontenkat_id == "2") {
				resize_crop_image(900, 600, $tujuan, $tujuan);
			} else {}
			mysqli_query($koneksi, "INSERT INTO cmas_konten (konten_id, konten_judul, konten_sdesc, konten_desc, konten_author, konten_status, konten_seo, konten_metatitle, konten_metadesc, konten_metakey, konten_image, konten_imageid, konten_header, konten_headerid, konten_order, konten_date, kontenkat_id, kontenkat_judul, kontenkat_seo, postdated, updated, posted_id, posted_username, posted_nama, updated_id, updated_username, updated_nama) VALUES(NULL, '$konten_judul', '$konten_sdesc', '$konten_desc', '$konten_author', '$konten_status', '$konten_seo', '$konten_metatitle', '$konten_metadesc', '$konten_metakey', '$nama_baru', '1', '$konten_header', '$konten_headerid', '$konten_order', '$konten_date', '$kontenkat_id', '$kontenkat_judul', '$kontenkat_seo', '$$postdated', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]', '$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 & Upload File', 'Konten: $konten_judul', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
			
			echo "<meta http-equiv='refresh' content='0; url=?m=konten_indexkat&id=$kontenkat_id&is=is'>";
		}
	}
?>