<?php 
	echo "<title>Loading...</title><div id='loading'><div class='loader'></div><br>Loading...</div>";
	date_default_timezone_set('Asia/Jakarta');
	
	$metadata_id			= mysqli_real_escape_string($koneksi, $_POST['metadata_id']);
	$metadata_title			= mysqli_real_escape_string($koneksi, $_POST['metadata_title']);
	$metadata_description	= mysqli_real_escape_string($koneksi, $_POST['metadata_description']);
	$metadata_keyword		= mysqli_real_escape_string($koneksi, $_POST['metadata_keyword']);
	$metadata_url			= mysqli_real_escape_string($koneksi, $_POST['metadata_url']);
	$metadata_author		= mysqli_real_escape_string($koneksi, $_POST['metadata_author']);
	$metadata_copyright		= mysqli_real_escape_string($koneksi, $_POST['metadata_copyright']);
	$metadata_powered		= mysqli_real_escape_string($koneksi, $_POST['metadata_powered']);
	$metadata_charset		= mysqli_real_escape_string($koneksi, $_POST['metadata_charset']);
	$metadata_generator		= mysqli_real_escape_string($koneksi, $_POST['metadata_generator']);
	$metadata_robots		= mysqli_real_escape_string($koneksi, $_POST['metadata_robots']);
	$metadata_developer		= mysqli_real_escape_string($koneksi, $_POST['metadata_developer']);
	$metadata_viewport		= mysqli_real_escape_string($koneksi, $_POST['metadata_viewport']);
	$metadata_appid			= mysqli_real_escape_string($koneksi, $_POST['metadata_appid']);
	$metadata_type			= mysqli_real_escape_string($koneksi, $_POST['metadata_type']);
	$metadata_googlesite	= mysqli_real_escape_string($koneksi, $_POST['metadata_googlesite']);
	$metadata_path			= mysqli_real_escape_string($koneksi, $_POST['metadata_path']);
	$metadata_pathdomain	= mysqli_real_escape_string($koneksi, $_POST['metadata_pathdomain']);
	$metadata_subdomain		= mysqli_real_escape_string($koneksi, $_POST['metadata_subdomain']);
	$metadata_addthisid		= mysqli_real_escape_string($koneksi, $_POST['metadata_addthisid']);
	$metadata_addthisclass	= mysqli_real_escape_string($koneksi, $_POST['metadata_addthisclass']);
	$metadata_tagmanagerid	= mysqli_real_escape_string($koneksi, $_POST['metadata_tagmanagerid']);
	
	$code		   			= md5(uniqid(rand()));
	$gencode				= substr($code, 0, 3);
	$nama_file 				= $_FILES["metadata_gambar"]["name"];
	$tipe_file 				= $_FILES["metadata_gambar"]["type"];
	$alamat 				= $_FILES["metadata_gambar"]["tmp_name"];
	$nama_baru 				= "favicon_".$gencode.".".end((explode(".", $nama_file)));
	$tujuan 				= "assets/images/$nama_baru";
	
	$updated				= date("Y-m-d H:i:s");
	
	if(empty($nama_file))
	{
		mysqli_query($koneksi, "UPDATE cmas_metadata SET 
		metadata_title			= '$metadata_title',
		metadata_description	= '$metadata_description',
		metadata_keyword		= '$metadata_keyword',
		metadata_url			= '$metadata_url',
		metadata_author			= '$metadata_author',
		metadata_copyright		= '$metadata_copyright',
		metadata_powered		= '$metadata_powered',
		metadata_charset		= '$metadata_charset',
		metadata_generator		= '$metadata_generator',
		metadata_robots			= '$metadata_robots',
		metadata_developer		= '$metadata_developer',
		metadata_viewport		= '$metadata_viewport',
		metadata_appid			= '$metadata_appid',
		metadata_type			= '$metadata_type',
		metadata_googlesite		= '$metadata_googlesite',
		metadata_path			= '$metadata_path',
		metadata_pathdomain		= '$metadata_pathdomain',
		metadata_subdomain		= '$metadata_subdomain',
		metadata_addthisid		= '$metadata_addthisid',
		metadata_addthisclass	= '$metadata_addthisclass',
		metadata_tagmanagerid	= '$metadata_tagmanagerid',
		updated					= '$updated',
		updated_id				= '$udt[admin_id]',
		updated_username		= '$udt[admin_username]',
		updated_nama			= '$udt[admin_nama]'
		WHERE metadata_id		= '$metadata_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', 'Metadata', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
		
		echo "<meta http-equiv='refresh' content='0; url=?m=metadata_index&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', 'Metadata, Error!', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
			
			echo "<meta http-equiv='refresh' content='0; url=?m=metadata_edit&id=$metadata_id&ufe=ufe'>";
		} else { 
			$db = mysqli_query($koneksi, "SELECT * FROM cmas_metadata WHERE metadata_id='$metadata_id'");
			if(mysqli_num_rows($db) > 0 ) {
				$dt = mysqli_fetch_array($db);
				@unlink('assets/images/'.$dt['metadata_gambar']);
			}
			$gambar_baru = move_uploaded_file($alamat, $tujuan);
			mysqli_query($koneksi, "UPDATE cmas_metadata SET 
			metadata_title			= '$metadata_title',
			metadata_description	= '$metadata_description',
			metadata_keyword		= '$metadata_keyword',
			metadata_url			= '$metadata_url',
			metadata_author			= '$metadata_author',
			metadata_copyright		= '$metadata_copyright',
			metadata_powered		= '$metadata_powered',
			metadata_charset		= '$metadata_charset',
			metadata_generator		= '$metadata_generator',
			metadata_robots			= '$metadata_robots',
			metadata_developer		= '$metadata_developer',
			metadata_viewport		= '$metadata_viewport',
			metadata_appid			= '$metadata_appid',
			metadata_type			= '$metadata_type',
			metadata_googlesite		= '$metadata_googlesite',
			metadata_path			= '$metadata_path',
			metadata_pathdomain		= '$metadata_pathdomain',
			metadata_subdomain		= '$metadata_subdomain',
			metadata_addthisid		= '$metadata_addthisid',
			metadata_addthisclass	= '$metadata_addthisclass',
			metadata_tagmanagerid	= '$metadata_tagmanagerid',
			metadata_gambar			= '$nama_baru',
			updated					= '$updated',
			updated_id				= '$udt[admin_id]',
			updated_username		= '$udt[admin_username]',
			updated_nama			= '$udt[admin_nama]'
			WHERE metadata_id		= '$metadata_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', 'Metadata', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
		
			echo "<meta http-equiv='refresh' content='0; url=?m=metadata_index&us=us'>";
		}
	}	
?>