<?php
ini_set('display_errors', 'On');
error_reporting(E_ERROR);

/*The following code will upload the images after renaming them to the FuelBuddyImages folder*/

// array for JSON response (default to failure)
$response = array();
$response["success"] = 0;
$response["message"] = "Failed";

//get json string
$json = file_get_contents('php://input');

//get json array
$jsonArr = json_decode($json, true);

// check for required fields
if (isset($jsonArr['email']) && isset($jsonArr['img_name']) && isset($jsonArr['img_type']) && isset($jsonArr['img_file'])) {
	$email = $jsonArr['email'];
	$img_name = $jsonArr['img_name'];
	$img_type = $jsonArr['img_type'];
	
	if(!empty($img_name)){
		//change picPath to only name and append email id
		$img_name = str_replace(" ", "_", $img_name);
		$img_name = $email . "." . basename($img_name);

		//set save path of file and change append email id to image name
		if(strcmp($img_type, "image") == 0){		
			$file_path = "../FBImagesTemp/";
		}
		else{
			$file_path = "../FBReceiptsTemp/";
		}	
		$file_path = $file_path . $img_name; 
			
		file_put_contents($file_path, base64_decode($jsonArr['img_file']));		
		//move_uploaded_file($_FILES['img_file']['tmp_name'], $file_path);
		
		$response["success"] = 1;
		$response["message"] = "Success";
				
		// echoing JSON response
		echo json_encode($response);
	}
	
	//else send generic failure message
	if($response["success"] == 0){
		$response["success"] = 0;
		$response["message"] = "Failed";
								 
		// echoing JSON response
		echo json_encode($response);
	}
}
else{
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
 
    // echoing JSON response
    echo json_encode($response);
}

?>