<?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['filers']['type'],$mimes)){
			// Recherche de l'encodage
			$content = file_get_contents($_FILES['filers']['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);

			// 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."_sigbresas");
			echo '<p>'.FREQ_GEST6.'</p>';
			$_SESSION['nblinesr']=0;
			if (($handle = fopen($tmp_utf8_file, "r")) !== false) {
				while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
					if($data[0]!=""&&$data[2]!=""&&$data[4]!="") {
						$date = mysqli_real_escape_string($bdd,$data[5]);
						if(is_numeric(substr($date,0,2))&&$data[0]!="Réservations: En cours"&&$data[2]!="Emprunteur") {
							$sep_pos = strrpos($data[2], ' ');
							if($encoding==1) {
								$nom = mb_convert_encoding(mysqli_real_escape_string($bdd,substr($data[2], 0, $sep_pos)),"UTF-8",$encodage);
								$prenom = mb_convert_encoding(mysqli_real_escape_string($bdd,substr($data[2], $sep_pos+1)),"UTF-8",$encodage);
							}
							else { 
								$nom = mysqli_real_escape_string($bdd,substr($data[2], 0, $sep_pos));
								$prenom = mysqli_real_escape_string($bdd,substr($data[2], $sep_pos+1));
							}
							$eleve = mysqli_query($bdd,"SELECT eleves_id FROM ".$prefix."_eleves WHERE nom = '".$nom."' AND prenom = '".$prenom."'");
							$eleve_nb = mysqli_num_rows($eleve);
							echo $nom.' '.$prenom.' '.$eleve_nb.'<br>';
							if($eleve_nb==1) {
								if($encoding==1) {
									$titre = mb_convert_encoding(mysqli_real_escape_string($bdd,$data[0]),"UTF-8",$encodage);
									$statut = mb_convert_encoding(mysqli_real_escape_string($bdd,$data[6]),"UTF-8",$encodage);
								}
								else {
									$titre = mysqli_real_escape_string($bdd,$data[0]);
									$statut = mysqli_real_escape_string($bdd,$data[6]);
								}
								$eleve_info = mysqli_fetch_assoc($eleve);
								mysqli_query($bdd,"INSERT INTO ".$prefix."_sigbresas (resas_id, eleves_id, titre, statut, date_statut) VALUES ('', '".$eleve_info['eleves_id']."', '".$titre."', '".$statut."', '".substr($date,6,4)."-".substr($date,3,2)."-".substr($date,0,2)."')");
							}	
							else {
								$_SESSION['nblinesr']++;	
								$line=$_SESSION['nblinesr'];
								if($encoding==1) { 
									$_SESSION['allnamer'.$line]=mb_convert_encoding(mysqli_real_escape_string($bdd,$data[2]),"UTF-8",$encodage);
									$_SESSION['titrer'.$line]=mb_convert_encoding(mysqli_real_escape_string($bdd,$data[0]),"UTF-8",$encodage);
									$_SESSION['statutr'.$line]=mb_convert_encoding(mysqli_real_escape_string($bdd,$data[6]),"UTF-8",$encodage);
								}
								else {
									$_SESSION['allnamer'.$line]=mysqli_real_escape_string($bdd,$data[2]);
									$_SESSION['titrer'.$line]=mysqli_real_escape_string($bdd,$data[0]);
									$_SESSION['statutr'.$line]=mysqli_real_escape_string($bdd,$data[6]);
								}
								$_SESSION['dater'.$line] = mysqli_real_escape_string($bdd,$data[5]);
							}	
						}
					}			
				}
				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);
?>
