<?php
$encoding = (mb_detect_encoding($content, ['UTF-8'], true) !== 'UTF-8') ? 1 : 0;

$id = 1;
$cdata5 = $cdata6 = $cdata7 = $cdata8 = 0;

if (($handle = fopen($_FILES['file']['tmp_name'], "r")) !== false) {
    while (($data = fgetcsv($handle, 1000, ";")) !== false) {
        include('proc/database_datainput_check.php');

        // Sécuriser les variables manquantes
        $att_code = isset($att_code) ? $att_code : '';
        $att_ident = isset($att_ident) ? $att_ident : '';
        $att_mdp = isset($att_mdp) ? $att_mdp : '';
        $att_mail = isset($att_mail) ? $att_mail : '';
        $att_resp_nom = isset($att_resp_nom) ? $att_resp_nom : '';
        $att_add1 = isset($att_add1) ? $att_add1 : '';
        $att_add2 = isset($att_add2) ? $att_add2 : '';
        $att_cpostal = isset($att_cpostal) ? $att_cpostal : '';
        $att_ville = isset($att_ville) ? $att_ville : '';

        if ($encoding === 1) {
            $att0 = @mb_convert_encoding(mysqli_real_escape_string($bdd, $data[0]), "UTF-8", $encodage);
            $att1 = @mb_convert_encoding(mysqli_real_escape_string($bdd, $data[1]), "UTF-8", $encodage);
            $att2 = @mb_convert_encoding(mysqli_real_escape_string($bdd, $data[4]), "UTF-8", $encodage);
        } else {
            $att0 = @mysqli_real_escape_string($bdd, $data[0]);
            $att1 = @mysqli_real_escape_string($bdd, $data[1]);
            $att2 = @mysqli_real_escape_string($bdd, $data[4]);
        }

        // Nettoyage des identifiants déjà existants
        if (mysqli_num_rows(mysqli_query($bdd, "SELECT * FROM {$prefix}_user WHERE login = '$att_code'")) > 0 || $att_code == 'reserver') {
            $att_code = '';
        }
        if (mysqli_num_rows(mysqli_query($bdd, "SELECT * FROM {$prefix}_user WHERE login = '$att_ident'")) > 0 || $att_ident == 'reserver') {
            $att_ident = '';
        }

        // Date de naissance
        $att3 = @mysqli_real_escape_string($bdd, $data[2]);
        $jour = substr($att3, 0, 2);
        $mois = substr($att3, 3, 2);
        $annee = substr($att3, 6, 4);

        // Sexe
        $att4 = @mysqli_real_escape_string($bdd, $data[3]);
        if (strtolower($att4) === 'f') {
            $att4 = 'F';
        } elseif (in_array(strtolower($att4), ['m', 'g'])) {
            $att4 = 'M';
        }

        // Mot de passe
        if (empty($att_mdp)) {
            $att_mdp = @mb_strtolower(substr(strToNoAccent($att1), 0, 1), 'UTF-8') . $jour . $mois . $annee;
        }

        do {
            $sel = bin2hex(random_bytes(16));
        } while ($sel === '0');

        $mdp_avec_sel = $att_mdp . $sel;
        $hashed_password = hash('sha256', $mdp_avec_sel);

        // Insertion
        mysqli_query($bdd,
            "INSERT INTO {$prefix}_elevesmaj (
                nom, prenom, division, naissance, sexe, code, mdp, sel, mail, id_perso, resp_nom, add1, add2, cpostal, ville, categ
            ) VALUES (
                '$att0', '$att1', '$att2', '$annee-$mois-$jour', '$att4',
                '$att_code', '$hashed_password', '$sel', '$att_mail', '$att_ident', '$att_resp_nom', '$att_add1', '$att_add2', '$att_cpostal', '$att_ville', '1'
            )"
        );

        $id++;

        if (!empty($data[5])) $cdata5 = 1;
        if (!empty($data[6])) $cdata6 = 1;
        if (!empty($data[7])) $cdata7 = 1;
        if (!empty($data[8])) $cdata8 = 1;
    }
    fclose($handle);
}
?>