<?php
include('includes/functions.php'); # general functions
include('includes/imgFunctions.php'); # functions for resize, watermark

define  ("MAX_SIZE","2000000");
function getExtension($str) {
	$i = strrpos ($str,".");
	if (!$i) { return ""; }
	$l = strlen ($str) - $i;
   	$ext = substr ($str,$i+1,$l);
    return $ext;
}

if(isset($_SESSION['adminId']) && trim($_SESSION['adminId']) >0) {
	$id = trim($_SESSION['adminId']);
	$query = mysqli_query($con, "SELECT * FROM tbl_admin WHERE admin_id='$id'");
	$resEdit = mysqli_fetch_array($query);	
	$ed_admin_name = $resEdit['name'];
	$ed_admin_email = $resEdit['email'];	
	
	//get data from settings table
	$setRec = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM tbl_settings WHERE id = '1'"));
	
	$adminRec = mysqli_fetch_array(mysqli_query($con, "SELECT email FROM tbl_admin WHERE admin_id = '1'"));
	$admin_email = $adminRec['email'];
}

if(isset($_POST['update'])) {
	
	if(isset($_POST['smtp_password']) && trim($_POST['smtp_password']) !=''){
	$smtp_password = mysqli_real_escape_string($con, $_POST['smtp_password']); } else {
	$error = 'Enter SMTP Password'; }
	
	if(isset($_POST['smtp_username']) && trim($_POST['smtp_username']) !=''){
	$smtp_username = mysqli_real_escape_string($con, $_POST['smtp_username']); } else {
	$error = 'Enter SMTP Username'; }
	
	if(isset($_POST['smtp_port']) && trim($_POST['smtp_port']) !=''){
	$smtp_port = mysqli_real_escape_string($con, $_POST['smtp_port']); } else {
	$error = 'Enter SMTP Port'; }
	
	if(isset($_POST['smtp_server_name']) && trim($_POST['smtp_server_name']) !=''){
	$smtp_server_name = mysqli_real_escape_string($con, $_POST['smtp_server_name']); } else {
	$error = 'Enter SMTP Server Name'; }
	
	
	if(isset($_POST['currency_symbol']) && trim($_POST['currency_symbol']) !=''){
	$currency_symbol = mysqli_real_escape_string($con, $_POST['currency_symbol']); } else {
	$currency_symbol = ''; }
	
	if(isset($_POST['currency_name']) && trim($_POST['currency_name']) !=''){
	$currency_name = mysqli_real_escape_string($con, $_POST['currency_name']); } else {
	$error = 'Enter Currency Name'; }
	
	if(isset($_POST['sms']) && trim($_POST['sms']) !=''){
	$sms = mysqli_real_escape_string($con, $_POST['sms']); } else {
	$error = 'Select SMS'; }	
		
	if(isset($_POST['app_type']) && trim($_POST['app_type']) !=''){
	$app_type = mysqli_real_escape_string($con, $_POST['app_type']); } else {
	$error = 'Select App Type'; }
	
	if(empty($error)) {
													
		$update1 = mysqli_query($con, "UPDATE tbl_settings SET app_type = '".$app_type."',
															   sms = '".$sms."',
															   currency_name = '".$currency_name."', 
															   currency_symbol = '".$currency_symbol."',
															   smtp_server_name = '".$smtp_server_name."',
															   smtp_port = '".$smtp_port."',
															   smtp_username = '".$smtp_username."',
															   smtp_password = '".$smtp_password."' WHERE id = '1'");
		$result = '';
		if($update1 != false) {
			$result =  true;
		}		

		if($result == true) {
		?>
			<script language="javascript">
			alert("Updated successfully");
			window.location="index.php?page=configuration";			
			</script>
		<?php //exit;		
		}
	}
}
?>