<?php
// Récupération de l'action
$log = isset ($_GET['log']) ? $_GET['log'] : '';     // Nom du fichier à afficher

// Extraction des parametres: Entete, nom du fichier
if ($log != ''){$parm = explode(',',$log);
                $titre   = $parm[0];    //echo $titre.'<br>';
                $fichier = $parm[1];    //echo $fichier.'*<br>';
    }else{ echo $log ='Pas de nom de fichier?' ;}
?>

<!-- *************************************************************************************************
ATTENTION: LES INSERTIONS PHP DES NOMS DE FICHIER DOIVENT ETRE COLLE AU CODE HTML  ( AVANT ET APRES )
    POUR EVITER L'AJOUT DE BLANC QUI MODIFIE LE FILENAME:  TRES TRES IMPORTANT
    IDEM pour le contenu de la TextArea
******************************************************************************************************    
-->
<!DOCTYPE HTML>
<html>
<meta charset="utf-8" />
<title>Visionnage histo des visiteurs</title>
<head>
<link rel="stylesheet" type="text/css" href="../../CSS/Enpa.css" />
<script type="text/javascript">
    function reloadWin() {
        window.location.reload();
    }

    /* auto-size a textarea in height - la hauteur de la textearea est ajustée à la taille du contenu */
    /* Cela évite le double ascenseur si la textearea est contenue dans une page dejà scrollée   MR 22-10-2022 */
    function autoHeightCL(CLelement){               /* version Class  */
    /*    https://stackoverflow.com/questions/4548088/how-to-auto-size-a-textarea-in-height     */
        const txtarea = document.querySelector('.' + CLelement);  /*mettre class=classname dans def textarea */
        txtarea.style.height = 'auto';
        txtarea.style.height = txtarea.scrollHeight + 'px';
    }

    /* auto-size a textarea in height  */
    function autoHeightID(IDelement){              /* version ID*/
    /*    https://stackoverflow.com/questions/4548088/how-to-auto-size-a-textarea-in-height     */
        const txtarea = document.querySelector('#' + IDelement);   /*mettre Id=idname dans def textarea */
        txtarea.style.height = 'auto';
        txtarea.style.height = txtarea.scrollHeight + 'px';
    }
</script>
</head>

<body>
<div id="content" class="container">
    <a style="float: left;" href="logVisiteurs.php"><img src="../../Animations/Fleches/Boutongauche.gif" style="vertical-align:text-bottom;" /></a>
    <h1><?php echo $titre ?></h1>
    <hr />
    <div class="centreimg">

<?php
    // print_r($_POST);
    function has_newline($string){
    return (strpos($string, "\r") !== false || strpos($string, "\n") !== false);
    }

    if(isset($_POST['Rec'])) {
        // Ouverture ou création du nouveau fichier. En mode w on ouvre le fichier qu'en écriture puis on le réécrit entièrement.
        $data     = "$_POST[modif]";
        // strpos ne detecte pas la position mais l'existence, donc on extrait du caractere CR ou LF en pos 12
        $Record = trim($data);

        $Startstr = substr( $data , 11 , 1);        // Extraction caractere pos 12  pour tester s'il s'agit d'un CR ou LF cad d'un champ vide  ( date par defaut )

        if ( has_newline($Startstr)  == true )  {
            $Record = substr( $data, 13 , strlen($data) - 13);   // champ vide uniquement la date mise par defaut, on supprime la premiere ligne
            }
            else {
            $Record = $data ;                                     // Cette ligne est utilisée donc à sauvegarder
            }
        $ouverture=fopen("$fichier","w");
        fwrite($ouverture,trim($Record) . PHP_EOL);       // ecriture
        fclose($ouverture);                     // fermeture du fichier
        }

?>

    <form method="post" action="">

        <!--***********************************************************************************************************  -->
        <!--  Attention de bien coller les déclarations php aux balises html sinon insertion d'espace dans le texte      -->
        <!--***********************************************************************************************************  -->
        <!--Remplissage de la Textarea-->
        <textarea class="zoneTexte" id="mytxtarea" name="modif"  ><?php if(is_file($fichier)) echo trim(file_get_contents($fichier)); ?>  </textarea>

        <br/>
        <input type="button" value="Refresh" onclick=reloadWin() style="display:inline; position:absolute; left: 5%;">
        <input type="submit" name="Rec" value="Enregistrer">

    </form>
         <script>  autoHeightID('mytxtarea');  </script>
    </div>
</div>
</body>
</html>