<?php
session_start();
require_once(dirname(__DIR__, 2) . '/config.php');
require_once(BASE_PATH . '/db/idl_connect.php');

echo '<meta charset="utf-8" />';
if(isset($_SESSION['user_droits'])) {
	$droits = substr($_SESSION['user_droits'],3,1);
	
	if ($droits!=2&&$droits!=5&&$droits!=6)
	{
		echo '<p>'.NOMODULE.' <a href="./index.php">'.ACCUEIL.'</a>.</p>';
	}
	else {
		$mimes = array('text/csv',
		'text/plain',
		'application/csv',
		'text/comma-separated-values',
		'application/excel',
		'application/vnd.ms-excel',
		'application/vnd.msexcel',
		'text/anytext',
		'application/octet-stream',
		'application/txt',);	
		if(@in_array($_FILES['file']['type'],$mimes)){
			// Recherche de l'encodage
			$content = file_get_contents($_FILES['file']['tmp_name']);
			$encodage = mb_detect_encoding($content, ['UTF-8','ISO-8859-1','ISO-8859-15','Windows-1252'], true);
			if ($encodage === false || $encodage === 'ASCII') {
			    $encodage = 'Windows-1252';
			}
			if ($encodage !== 'UTF-8') {
			    $content = iconv($encodage, 'UTF-8//IGNORE', $content);
			}
			// Suppression d'une éventuelle BOM UTF-8
			$content = preg_replace('/^\xEF\xBB\xBF/', '', $content);
			$encoding = (mb_detect_encoding($content, ['UTF-8'], true) !== 'UTF-8') ? 1 : 0;
			// Création d’un fichier temporaire avec le contenu converti
			$tmp_utf8_file = tempnam(sys_get_temp_dir(), 'utf8_');
			file_put_contents($tmp_utf8_file, $content);
			// Suppression des retards
			mysqli_query($bdd,"DELETE FROM ".$prefix."_retards");
			echo '<p>'.FREQ_GEST7.'</p>';
			$_SESSION['nblines']=0;
			if (($handle = fopen($tmp_utf8_file, "r")) !== false) {
				while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
					if($encoding==1) {
						$nom = mb_convert_encoding(mysqli_real_escape_string($bdd,$data[1]),"UTF-8",$encodage);
						$prenom = mb_convert_encoding(mysqli_real_escape_string($bdd,$data[2]),"UTF-8",$encodage);
					}
					else {
						$nom = mysqli_real_escape_string($bdd,$data[1]);
						$prenom = mysqli_real_escape_string($bdd,$data[2]);
					}
					$eleve = mysqli_query($bdd,"SELECT eleves_id FROM ".$prefix."_eleves WHERE nom = '".$nom."' AND prenom = '".$prenom."'");
					$eleve_nb = mysqli_num_rows($eleve);
					if(@is_numeric($data[21])) {
						$date = mysqli_real_escape_string($bdd,$data[20]);
						if($eleve_nb==1) {
							if($encoding==1) {
								$titre = mb_convert_encoding(mysqli_real_escape_string($bdd,$data[17]),"UTF-8",$encodage);
							}
							else {
								$titre = mysqli_real_escape_string($bdd,$data[17]);
							}
							$alerte = mysqli_real_escape_string($bdd,$data[21]);
							$eleve_info = mysqli_fetch_assoc($eleve);
							mysqli_query($bdd,"INSERT INTO ".$prefix."_retards (retards_id, eleves_id, titre, retour_date, niv_alerte) VALUES ('', '".$eleve_info['eleves_id']."', '".$titre."', '".substr($date,6,4)."/".substr($date,3,2)."/".substr($date,0,2)."', '".$alerte."')");
						}	
						else {
							$_SESSION['nblines']++;	
							$line=$_SESSION['nblines'];
							if($encoding==1) { 
								$_SESSION['allname'.$line]=mb_convert_encoding(mysqli_real_escape_string($bdd,$data[1]).' '.mysqli_real_escape_string($bdd,$data[2]),"UTF-8",$encodage);
								$_SESSION['titre'.$line]=mb_convert_encoding(mysqli_real_escape_string($bdd,$data[17]),"UTF-8",$encodage);
							}
							else {
								$_SESSION['allname'.$line]=mysqli_real_escape_string($bdd,$data[1]).' '.mysqli_real_escape_string($bdd,$data[2]);
								$_SESSION['titre'.$line]=mysqli_real_escape_string($bdd,$data[17]);
							}
							$_SESSION['alerte'.$line]=mysqli_real_escape_string($bdd,$data[21]);
							$_SESSION['date'.$line] = mysqli_real_escape_string($bdd,$data[20]);
						}
					}
					else if(@is_numeric($data[22])) {
						$date = mysqli_real_escape_string($bdd,$data[21]);
						if($eleve_nb==1) {
							if($encoding==1) {
								$titre = mb_convert_encoding(mysqli_real_escape_string($bdd,$data[18]),"UTF-8",$encodage);
							}
							else {
								$titre = mysqli_real_escape_string($bdd,$data[18]);
							}
							$alerte = mysqli_real_escape_string($bdd,$data[22]);
							$eleve_info = mysqli_fetch_assoc($eleve);
							mysqli_query($bdd,"INSERT INTO ".$prefix."_retards (retards_id, eleves_id, titre, retour_date, niv_alerte) VALUES ('', '".$eleve_info['eleves_id']."', '".$titre."', '".substr($date,6,4)."/".substr($date,3,2)."/".substr($date,0,2)."', '".$alerte."')");
						}	
						else {
							$_SESSION['nblines']++;	
							$line=$_SESSION['nblines'];
							if($encoding==1) { 
								$_SESSION['allname'.$line]=mb_convert_encoding(mysqli_real_escape_string($bdd,$data[1]).' '.mysqli_real_escape_string($bdd,$data[2]),"UTF-8",$encodage);
								$_SESSION['titre'.$line]=mb_convert_encoding(mysqli_real_escape_string($bdd,$data[18]),"UTF-8",$encodage);
							}
							else {
								$_SESSION['allname'.$line]=mysqli_real_escape_string($bdd,$data[1]).' '.mysqli_real_escape_string($bdd,$data[2]);
								$_SESSION['titre'.$line]=mysqli_real_escape_string($bdd,$data[18]);
							}
							$_SESSION['alerte'.$line]=mysqli_real_escape_string($bdd,$data[22]);
							$_SESSION['date'.$line] = mysqli_real_escape_string($bdd,$data[21]);
						}
					}
					else if(@is_numeric($data[24])) {
						$date = mysqli_real_escape_string($bdd,$data[23]);
						if($eleve_nb==1) {
							if($encoding==1) {
								$titre = mb_convert_encoding(mysqli_real_escape_string($bdd,$data[17]),"UTF-8",$encodage);
							}
							else {
								$titre = mysqli_real_escape_string($bdd,$data[17]);
							}
							$alerte = mysqli_real_escape_string($bdd,$data[24]);
							$eleve_info = mysqli_fetch_assoc($eleve);
							mysqli_query($bdd,"INSERT INTO ".$prefix."_retards (retards_id, eleves_id, titre, retour_date, niv_alerte) VALUES ('', '".$eleve_info['eleves_id']."', '".$titre."', '".substr($date,6,4)."/".substr($date,3,2)."/".substr($date,0,2)."', '".$alerte."')");
						}	
						else {
							$_SESSION['nblines']++;	
							$line=$_SESSION['nblines'];
							if($encoding==1) { 
								$_SESSION['allname'.$line]=mb_convert_encoding(mysqli_real_escape_string($bdd,$data[1]).' '.mysqli_real_escape_string($bdd,$data[2]),"UTF-8",$encodage);
								$_SESSION['titre'.$line]=mb_convert_encoding(mysqli_real_escape_string($bdd,$data[17]),"UTF-8",$encodage);
							}
							else {
								$_SESSION['allname'.$line]=mysqli_real_escape_string($bdd,$data[1]).' '.mysqli_real_escape_string($bdd,$data[2]);
								$_SESSION['titre'.$line]=mysqli_real_escape_string($bdd,$data[17]);
							}
							$_SESSION['alerte'.$line]=mysqli_real_escape_string($bdd,$data[24]);
							$_SESSION['date'.$line] = mysqli_real_escape_string($bdd,$data[23]);
						}
					}
				}
				fclose($handle);
				unlink($tmp_utf8_file);
			}
			echo '<script language="Javascript">
			<!--
			document.location.replace("../../freq.php?page=retards");
			// -->
			</script>';
		}
		else {
			die(NOCSV);
		}
	}
}
else {
	include('include/noaccess.php');
	echo '<a href="../../index.php">'.ACCUEIL.'</a>.</p>';
}
mysqli_close($bdd);
?>
