<?php 
	echo "<title>Loading...</title><div id='loading'><div class='loader'></div><br>Loading...</div>";
	date_default_timezone_set('Asia/Jakarta');
	
	$phpmail_id				= mysqli_real_escape_string($koneksi, $_POST['phpmail_id']);
	$phpmail_host			= mysqli_real_escape_string($koneksi, $_POST['phpmail_host']);
	$phpmail_username		= mysqli_real_escape_string($koneksi, $_POST['phpmail_username']);
	$phpmail_password		= mysqli_real_escape_string($koneksi, $_POST['phpmail_password']);
	$phpmail_charset		= mysqli_real_escape_string($koneksi, $_POST['phpmail_charset']);
	$phpmail_mailfrom		= mysqli_real_escape_string($koneksi, $_POST['phpmail_mailfrom']);
	$phpmail_mailname		= mysqli_real_escape_string($koneksi, $_POST['phpmail_mailname']);
	$phpmail_ccmail			= mysqli_real_escape_string($koneksi, $_POST['phpmail_ccmail']);
	$phpmail_ccname			= mysqli_real_escape_string($koneksi, $_POST['phpmail_ccname']);
	$phpmail_bccmail		= mysqli_real_escape_string($koneksi, $_POST['phpmail_bccmail']);
	$phpmail_bccname		= mysqli_real_escape_string($koneksi, $_POST['phpmail_bccname']);
	$phpmail_logoname		= mysqli_real_escape_string($koneksi, $_POST['phpmail_logoname']);
	
	$code		   			= md5(uniqid(rand()));
	$gencode				= substr($code, 0, 3);
	$nama_file 				= $_FILES["phpmail_logo"]["name"];
	$tipe_file 				= $_FILES["phpmail_logo"]["type"];
	$alamat 				= $_FILES["phpmail_logo"]["tmp_name"];
	$nama_baru 				= "logomail_".$gencode.".".end((explode(".", $nama_file)));
	$tujuan 				= "assets/images/$nama_baru";
	
	$captcha_sitekey		= mysqli_real_escape_string($koneksi, $_POST['captcha_sitekey']);
	$captcha_secretkey		= mysqli_real_escape_string($koneksi, $_POST['captcha_secretkey']);
	
	$updated				= date("Y-m-d H:i:s");
	$updated_id				= $udt['admin_id'];
	
	if(empty($nama_file))
	{
		mysqli_query($koneksi, "UPDATE cmas_phpmail SET 
		phpmail_host			= '$phpmail_host',
		phpmail_username		= '$phpmail_username',
		phpmail_password		= '$phpmail_password',
		phpmail_charset			= '$phpmail_charset',
		phpmail_mailfrom		= '$phpmail_mailfrom',
		phpmail_mailname		= '$phpmail_mailname',
		phpmail_ccmail			= '$phpmail_ccmail',
		phpmail_ccname			= '$phpmail_ccname',
		phpmail_bccmail			= '$phpmail_bccmail',
		phpmail_bccname			= '$phpmail_bccname',
		phpmail_logoname		= '$phpmail_logoname',
		captcha_sitekey			= '$captcha_sitekey',
		captcha_secretkey		= '$captcha_secretkey',
		updated					= '$updated',
		updated_id				= '$updated_id'
		WHERE phpmail_id		= '$phpmail_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', 'PHPMail', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
		
		echo "<meta http-equiv='refresh' content='0; url=?m=phpmail_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', 'PHPMail, Error!', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
			
			echo "<meta http-equiv='refresh' content='0; url=?m=phpmail_edit&id=$phpmail_id&ufe=ufe'>";
		} else { 
			$db = mysqli_query($koneksi, "SELECT * FROM cmas_phpmail WHERE phpmail_id='$phpmail_id'");
			if(mysqli_num_rows($db) > 0 ) {
				$dt = mysqli_fetch_array($db);
				@unlink('assets/images/'.$dt['phpmail_logo']);
			}
			$gambar_baru = move_uploaded_file($alamat, $tujuan);
			mysqli_query($koneksi, "UPDATE cmas_phpmail SET 
			phpmail_host			= '$phpmail_host',
			phpmail_username		= '$phpmail_username',
			phpmail_password		= '$phpmail_password',
			phpmail_charset			= '$phpmail_charset',
			phpmail_mailfrom		= '$phpmail_mailfrom',
			phpmail_mailname		= '$phpmail_mailname',
			phpmail_ccmail			= '$phpmail_ccmail',
			phpmail_ccname			= '$phpmail_ccname',
			phpmail_bccmail			= '$phpmail_bccmail',
			phpmail_bccname			= '$phpmail_bccname',
			phpmail_logoname		= '$phpmail_logoname',
			phpmail_logo			= '$nama_baru',
			phpmail_logoid			= '1',
			captcha_sitekey			= '$captcha_sitekey',
			captcha_secretkey		= '$captcha_secretkey',
			updated					= '$updated',
			updated_id				= '$updated_id'
			WHERE phpmail_id		= '$phpmail_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', 'PHPMail', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
		
			echo "<meta http-equiv='refresh' content='0; url=?m=phpmail_index&us=us'>";
		}
	}	
?>