<?php
//DB Data
include '../../ASEngine/AS.php';
if(!$login->isLoggedIn())
    header("Location: ../../login.php");
$user = new ASUser(ASSession::get("user_id"));
$userInfo = $user->getInfo();

//basic include files
require_once("../../../db.php");

$slide_link = $_POST['slide_link'];

if(isset($_POST['slide1'])) {	
	if(isset($_FILES['image'])) {
		$errors= array();
		$file_name = $_FILES['image']['name'];
		$file_size =$_FILES['image']['size'];
		$file_tmp =$_FILES['image']['tmp_name'];
		$file_type=$_FILES['image']['type'];   
		$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
		
		$expensions= array("jpeg","jpg","png"); 		
		if(in_array($file_ext,$expensions)=== false) {
			$errors[]="extension not allowed, please choose a JPEG or PNG file.";
		}
		if($file_size > 2097152) {
		$errors[]='File size must be excately 2 MB';
		}				
		if(empty($errors)==true) {
			move_uploaded_file($file_tmp,"../../../inc/images/carousel/".$file_name);
			
			mysqli_query($conn, "UPDATE general_settings SET settings_slide1 = '$file_name', settings_slide1_link = '$slide_link' WHERE settings_id = 1");
			
			mysqli_close($conn);
			header("Location: ../../customize.php?customizemsn=updatedslide");
		} else {
			mysqli_query($conn, "UPDATE general_settings SET settings_slide1_link = '$slide_link' WHERE settings_id = 1");
			
			mysqli_close($conn);
			header("Location: ../../customize.php?customizemsn=updatedslide");
		}
	} 
}

if(isset($_POST['slide2'])){	
	if(isset($_FILES['image'])) {
		$errors= array();
		$file_name = $_FILES['image']['name'];
		$file_size =$_FILES['image']['size'];
		$file_tmp =$_FILES['image']['tmp_name'];
		$file_type=$_FILES['image']['type'];   
		$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
		
		$expensions= array("jpeg","jpg","png"); 		
		if(in_array($file_ext,$expensions)=== false) {
			$errors[]="extension not allowed, please choose a JPEG or PNG file.";
		}
		if($file_size > 2097152) {
		$errors[]='File size must be excately 2 MB';
		}				
		if(empty($errors)==true) {
			move_uploaded_file($file_tmp,"../../../inc/images/carousel/".$file_name);
			
			mysqli_query($conn, "UPDATE general_settings SET settings_slide2 = '$file_name', settings_slide2_link = '$slide_link' WHERE settings_id = 1");
			
			mysqli_close($conn);
			header("Location: ../../customize.php?customizemsn=updatedslide2");
		} else {
			mysqli_query($conn, "UPDATE general_settings SET settings_slide2_link = '$slide_link' WHERE settings_id = 1");
			
			mysqli_close($conn);
			header("Location: ../../customize.php?customizemsn=updatedslide2");
		}
	} 
}


if(isset($_POST['slide3'])) {	
	if(isset($_FILES['image'])) {
		$errors= array();
		$file_name = $_FILES['image']['name'];
		$file_size =$_FILES['image']['size'];
		$file_tmp =$_FILES['image']['tmp_name'];
		$file_type=$_FILES['image']['type'];   
		$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
		
		$expensions= array("jpeg","jpg","png"); 		
		if(in_array($file_ext,$expensions)=== false) {
			$errors[]="extension not allowed, please choose a JPEG or PNG file.";
		}
		if($file_size > 2097152){
		$errors[]='File size must be excately 2 MB';
		}				
		if(empty($errors)==true) {
			move_uploaded_file($file_tmp,"../../../inc/images/carousel/".$file_name);
			
			mysqli_query($conn, "UPDATE general_settings SET settings_slide3 = '$file_name', settings_slide3_link = '$slide_link' WHERE settings_id = 1");
			
			mysqli_close($conn);
			header("Location: ../../customize.php?customizemsn=updatedslide3");
		} else {
			mysqli_query($conn, "UPDATE general_settings SET settings_slide3_link = '$slide_link' WHERE settings_id = 1");
			
			mysqli_close($conn);
			header("Location: ../../customize.php?customizemsn=updatedslide3");
		}
	} 
}

if(isset($_POST['slide4'])) {	
	if(isset($_FILES['image'])) {
		$errors= array();
		$file_name = $_FILES['image']['name'];
		$file_size =$_FILES['image']['size'];
		$file_tmp =$_FILES['image']['tmp_name'];
		$file_type=$_FILES['image']['type'];   
		$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
		
		$expensions= array("jpeg","jpg","png"); 		
		if(in_array($file_ext,$expensions)=== false) {
			$errors[]="extension not allowed, please choose a JPEG or PNG file.";
		}
		if($file_size > 2097152){
		$errors[]='File size must be excately 2 MB';
		}				
		if(empty($errors)==true) {
			move_uploaded_file($file_tmp,"../../../inc/images/carousel/".$file_name);
			//echo "Success";
			mysqli_query($conn, "UPDATE general_settings SET settings_slide4 = '$file_name', settings_slide4_link = '$slide_link' WHERE settings_id = 1");
			
			mysqli_close($conn);
			header("Location: ../../customize.php?customizemsn=updatedslide4");
		} else {
			mysqli_query($conn, "UPDATE general_settings SET settings_slide4_link = '$slide_link' WHERE settings_id = 1");
			
			mysqli_close($conn);
			header("Location: ../../customize.php?customizemsn=updatedslide4");
		}
	} 
}
?>