<?php

	if ( $mode_simulation ) {
	//if ( true ) {
		
		echo("AQCM_update_firmware_from_PC : mode simulation..." . "</br>");
		
	}
	else {

		echo ("- la sonde tente de charger le firmware :</br></br>");
		
		$flag_restore_script = false;
		$errors = []; // Store errors here

		{
			
			$currentDirectory = getcwd();
			$uploadDirectory = "../tmp_files/";

			$fileExtensionsAllowed = ['zip']; // These will be the only file extensions allowed 

			$fileName 		= $_FILES['the_file']['name'];
			$fileSize 		= $_FILES['the_file']['size'];
			$fileTmpName  	= $_FILES['the_file']['tmp_name'];
			$fileType 		= $_FILES['the_file']['type'];
			$koko  			= explode('.',$fileName);
			$koko1 			= end($koko);
			$fileExtension 	= strtolower($koko1);
			$koko2				= basename($fileName);


			//$uploadPath = $uploadDirectory . basename($fileName); 
			$uploadPath = "/home/firmware/AQCM05_SERVER_firmware_from_PC.zip";

			/*
			echo("fileName : "); print_r($fileName);echo("</br>");
			echo("\$fileSize : "); echo($fileSize);echo("</br>");
			echo("extension : "); echo($fileExtension);echo("</br>");
			echo("basename(\$fileName) : "); echo($koko2);echo("</br>");
			echo("\$fileTmpName : "); echo($fileTmpName);echo("</br>");
			echo("\$uploadPath : "); echo($uploadPath);echo("</br>");
			*/

			if ( file_exists($uploadPath) ) {
				$cmd_1 = "sudo chmod 666 " . $uploadPath ;
				$result_1 = exec($cmd_1, $exec_output_1, $exec_return_1);
			}

			if (isset($_POST['submit'])) {

				if ( ! in_array($fileExtension,$fileExtensionsAllowed) ) {
					$errors[] = "Extension non autorisée ! (*.zip) </br>";
				}

				if ($fileSize > 8000000) {
					$errors[] = "La taille du fichier dépasse 8 Mo !";
				}

				if ( empty($errors) ) {
					
					$didUpload = move_uploaded_file($fileTmpName, $uploadPath);

					/*
					echo($fileTmpName);echo("</br>");
					echo($uploadPath);echo("</br>");
					echo $didUpload . " ; " . $fileTmpName . " ; " . $uploadPath ; echo "</br>";
					*/
					
				}
				else {
					echo "<b>→</b>&nbsp;&nbsp;une erreur est survenue !!!</br>";
					$didUpload = False;
					foreach ($errors as $error) {
						echo "&nbsp;erreur : " . $error . "</br>";
					}
				}
				
			}
			else {
				$didUpload = False;
				echo "<b>→</b>&nbsp;&nbsp;une erreur est survenue : veuillez contacter votre administrateur";
			}
		}
		

		echo "&nbsp;&nbsp;&nbsp;" . basename($fileName) . "</br></br>";

		if ( $didUpload ) {
			
			echo "- le fichier a été chargé par la sonde... </br></br>";

			echo ('<div align="center">');
			echo ('<a class="a_1" style="color: red" href="./AQCM_update_firmware_from_PC_1_wait.php"><b>→&nbsp;&nbsp;lancer la mise à jour...</b></a>');
			echo ('<br></div>');
			/*
			*/
			
		} else {
			
			echo "le fichier n'a pas pu être chargé !!!</br>";
			
		} 
		
	}






?>