<?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_id			= mysqli_real_escape_string($koneksi, $_POST['konten_id']);
	$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";
	
	$konten_date		= mysqli_real_escape_string($koneksi, $_POST['konten_date']);
	
	$updated			= date("Y-m-d H:i:s");
		
	if(empty($nama_file))
	{
		mysqli_query($koneksi, "UPDATE cmas_konten SET
		konten_judul		= '$konten_judul',
		konten_sdesc		= '$konten_sdesc',
		konten_desc			= '$konten_desc',
		konten_author		= '$konten_author',
		konten_status		= '$konten_status',
		konten_seo			= '$konten_seo',
		konten_metatitle	= '$konten_metatitle',
		konten_metadesc		= '$konten_metadesc',
		konten_metakey		= '$konten_metakey',
		konten_date			= '$konten_date',
		kontenkat_id		= '$kontenkat_id',
		kontenkat_judul		= '$kontenkat_judul',
		kontenkat_seo		= '$kontenkat_seo',
		updated				= '$updated',
		updated_id			= '$udt[admin_id]',
		updated_username	= '$udt[admin_username]',
		updated_nama		= '$udt[admin_nama]'
		WHERE konten_id		= '$konten_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, 'Update', '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&us=us'>";
	}
	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, 'Update & 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_edit&id=$konten_id&ufe=ufe'>";
		} else { 
			$db = mysqli_query($koneksi, "SELECT * FROM cmas_konten WHERE konten_id='$konten_id'");
			if (mysqli_num_rows($db) > 0 ) {
				$dt = mysqli_fetch_array($db);
				@unlink('assets/images/konten/'.$dt['konten_image']);
				mysqli_query($koneksi, "UPDATE cmas_konten SET konten_image='', konten_imageid='0' WHERE konten_id='$konten_id'");
			}
			$gambar_baru = move_uploaded_file($alamat, $tujuan);
			if ($kontenkat_id == "2") {
				resize_crop_image(900, 600, $tujuan, $tujuan);
			} else {}
			mysqli_query($koneksi, "UPDATE cmas_konten SET
			konten_judul		= '$konten_judul',
			konten_sdesc		= '$konten_sdesc',
			konten_desc			= '$konten_desc',
			konten_author		= '$konten_author',
			konten_status		= '$konten_status',
			konten_seo			= '$konten_seo',
			konten_metatitle	= '$konten_metatitle',
			konten_metadesc		= '$konten_metadesc',
			konten_metakey		= '$konten_metakey',
			konten_image		= '$nama_baru',
			konten_imageid		= '1',
			konten_date			= '$konten_date',
			kontenkat_id		= '$kontenkat_id',
			kontenkat_judul		= '$kontenkat_judul',
			kontenkat_seo		= '$kontenkat_seo',
			updated				= '$updated',
			updated_id			= '$udt[admin_id]',
			updated_username	= '$udt[admin_username]',
			updated_nama		= '$udt[admin_nama]'
			WHERE konten_id		= '$konten_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, 'Update & 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&us=us'>";
		}
	}
?>