<?php
header('Content-type: application/json');
include '../db/db.php';
session_start();
$log = htmlspecialchars(trim($_SESSION['user']), ENT_QUOTES, 'UTF-8');
$log_lang = htmlspecialchars(trim($_SESSION['lang']), ENT_QUOTES, 'UTF-8');

$menu = htmlspecialchars(trim($_SESSION['menu']), ENT_QUOTES, 'UTF-8');
$emp_id = htmlspecialchars(trim($_SESSION['emp_id']), ENT_QUOTES, 'UTF-8');

if (empty($log)) {
	$redirectUrl = "index.php";
	echo "<script type=\"text/javascript\">";
	echo "window.location.href = '$redirectUrl'";
	echo "</script>";

} else {

	$sqlu = "SELECT * FROM user_priv_q WHERE emp_id='$emp_id' AND user='$log' AND menu_id='$menu'";
	$resultu = mysqli_query($conn, $sqlu);
	$countu = mysqli_num_rows($resultu);

	if($countu == 1){
		$rowc = mysqli_fetch_assoc($resultu);
		$user_id = $rowc['user_id'];
		
		date_default_timezone_set('Asia/Colombo');
		$date_today = date("Y-m-d");

		/****************************** Insert *************************************/

		if(isset($_FILES['img_upload'])) {	
			if($_FILES["img_upload"]["name"] != '') {
				$group_id = htmlspecialchars(strip_tags(trim($_POST['group_id'])), ENT_QUOTES, 'UTF-8');
				
				$valid_extensions = array('png','jpg','jpeg'); // valid extensions
				$path = '../upload/news_img/'.$group_id.'/'; // upload directory	

				// get uploaded file's extension
				$ext = strtolower(pathinfo($_FILES["img_upload"]["name"], PATHINFO_EXTENSION));
				$tmp = $_FILES['img_upload']['tmp_name'];

				// check valid format
				if(in_array($ext, $valid_extensions)) 
				{					
					if(is_dir($path) == false){
						mkdir("$path", 0755);		// Create directory if it does not exist
						copy('../upload/news_img/.htaccess', $path.'.htaccess');
					}
					
					$img_name = rand(100000, 999999).'.'.$ext;	
					$path = $path.$img_name;	
						
					if (file_exists($path))
					{			
						$response['status'] = '2'; // File Exists
					
					} else if($log_lang == 1 && $group_id != '') {
						if(move_uploaded_file($tmp,$path)) 
						{		
							$sql = "INSERT INTO news_img VALUES (NULL, '$group_id', '$img_name', '$user_id', '0')";

							if(mysqli_query($conn, $sql)) {
								$response['status'] = '1'; // Success
							
							} else {
								$response['status'] = '3'; // Sql Error
							}
						
						} else{				
						  //exit("Error While uploading image on the server");
						  $response['status'] = '4'; // Uploading Error
						}
					}
				} 
				else 
				{
					$response['status'] = '5'; // Invaild File
				}
			 
			}
			echo json_encode($response);	
		}

		/****************************** Remove *************************************/
		
		if (array_key_exists('remove_img', $_POST)) {

			if($log_lang == 1) {
			
				$img_id = htmlspecialchars(strip_tags(trim($_POST['img_id'])), ENT_QUOTES, 'UTF-8');		
				$sql = "UPDATE news_img SET del=1 WHERE id='$img_id'";
				
				if(!empty($img_id)) {
				
					if(mysqli_query($conn, $sql)) {
						echo json_encode(array("msgType" => 1, "msg" => "Successfully Removed"));
					
					} else {
						echo json_encode(array("msgType" => 2, "msg" => "Sorry ! not deleted"));
					}
				
				} else {
					echo json_encode(array("msgType" => 2, "msg" => "Sorry ! empty id"));
				}
			
			}

		}	

		/******************************************************************/		

	} else{
		$redirectUrl = "index.php";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";	
	}
}
?>