<?php

	//-------------------------------------------------------------------------------------------------
	$firmware_filename_1 = "/var/www/html/v2/firmwares/AQCM05_firmware_OK.zip" ;
	$firmware_filename_2 = "/var/www/html/v2/documentation/_images_uSD/AQCM05_sonde_full_firmware.zip" ;

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

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

		echo ("- téléchargement du firmware des sondes :</br></br>");
		
		$flag_restore_script = false;
		$errors = []; // Store errors here

		if(array_key_exists('the_file', $_FILES)){
			
			if ($_FILES['the_file']['error'] === UPLOAD_ERR_OK) {
			   echo '&nbsp;&nbsp;&nbsp;&nbsp;- upload was successful</br></br>';
			} else {
			   echo("&nbsp;&nbsp;&nbsp;&nbsp;- upload failed with error code " . $_FILES['the_file']['error'] . "</br>" );
			   //die("Upload failed with error code " . $_FILES['the_file']['error']);
			}
		}

		{
			
			$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 = "/var/www/html/v2/firmwares/" . $koko2;

				/*
				echo("currentDirectory : "); print_r($currentDirectory);echo("</br>");
				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 > 10000000) {
					$errors[] = "La taille du fichier dépasse 10 Mo !";
				}

				if ( empty($errors) ) {
					
					$FLAG_1 = move_uploaded_file($fileTmpName, $uploadPath);
					$FLAG_2 = copy($uploadPath, $firmware_filename_1);
					$FLAG_3 = copy($uploadPath, $firmware_filename_2);

					$didUpload = $FLAG_1 && $FLAG_2 && $FLAG_3;

				}
				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 "- téléchargement du firmware des sondes : succès...</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 "- téléchargement du firmware des sondes : échec !!!</br></br>";
			
		} 
		
	}






?>