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

/* The following code will save the full sync file in a particular folder and send a notification to destination with the path */
error_log("\npost: ".print_r($_POST, true), 3, "my-errors.log");
if (isset($_POST['name']) && isset($_POST['friend_email'])){
	//get name and friends email
	$name = $_POST['name'];
	$friend_email = $_POST['friend_email'];
	error_log("\nname: ".$name." friend: ".$friend_email, 3, "my-errors.log");
	
	//set save path of file
	$file_path = "../FullSyncFiles/";
	$file_name = basename( $_FILES['zip_file']['name']);
	$file_path = $file_path . $file_name; 
	error_log("\nfile: ".$file_path, 3, "my-errors.log");
	
	//move file to be synced to save path
	if(move_uploaded_file($_FILES['zip_file']['tmp_name'], $file_path)){	
	    error_log("\nmoved", 3, "my-errors.log");
		
		// include db config class
		require_once __DIR__ . '/db_config.php';
	
		// connecting to db
	    	$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
	    	mysqli_set_charset($mysqli, "utf8");
			
		//get reg id of friend
		if($stmt = $mysqli->prepare("SELECT REG_ID FROM PROFILE WHERE USER_ID = (SELECT USER_ID FROM USER_ID WHERE EMAIL LIKE ?)")){
			// Bind a variable to the parameter as a string. 
			$stmt->bind_param("s", $friend_email);
			    		
			// Execute the statement.
			$stmt->execute();  
			    	
			//get rows for the friend if any		    	
			$stmt->bind_result($reg_id);
				
			while ($stmt->fetch()){ 
				$friend_reg_id[] = $reg_id;
			}
				
			if(isset($friend_reg_id)){
				//send notification to friend
				//API Key
				$api_key = 'AIzaSyBcrrQw9HFn3wnFPzsE7ogjpqsI_drfbj4';	
						
				// Set other POST variables for GCM
				$url = 'https://fcm.googleapis.com/fcm/send';
				
				$headers = array("Authorization: key=$api_key", "Content-Type: application/json");
						
				$fields = array("content_available"=>true, "registration_ids" => $friend_reg_id, "data" => array("name"=>$name, "syncFileName"=>$file_name, "type"=>'full sync', "action"=>'request'), "priority"=>"high");
						
				// Open connection
				$ch = curl_init();
						 
				// Set the url, number of POST vars, POST data
				curl_setopt($ch, CURLOPT_URL, $url);	 
				curl_setopt($ch, CURLOPT_POST, true);
				curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
						        
				// Disabling SSL Certificate support temporarly
				curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
				 
				curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
						 
				// Execute post
				$result = curl_exec($ch);
						
				if ($result === FALSE) {		
					die('Curl failed: ' . curl_error($ch));
				}
						 
				// Close connection
				curl_close($ch);
			}
			else{
				echo "Failed to get reg id";
			}
					
			// Close the prepared statement.
			$stmt->close();									
		}
		else{    						 			
			echo "Failed to get reg id";
		}
			    		    	
	    	mysqli_close($mysqli); 
	}
	else{
		echo "File not uploaded";
	}
}
else{
    echo "Required field(s) is missing"; 
}


?>