<?php

use Helpers\Fichier\Fichier;
use Helpers\Outil\Sql;
use Skeleton\Application;

// Polyfill PHP 8
if (!function_exists('str_starts_with')) {
    function str_starts_with(string $haystack, string $needle)
    {
        return empty($needle) || strpos($haystack, $needle) === 0;
    }
}

$loader = require_once '../../../vendor/autoload.php';

$projectRoot = Application::getAppRoot();

/**
 * Initialisation de la Session
 */

\Helpers\Session\Session::initSessionActive();

/**
 * A supprimer après la disparition de la V1
 */
require_once 'application/webroot/php/include/version.php';

$horsConnexion = 0;

$devSession_NumClient = 0;

if (!isset($_SESSION['Session_NumClient'])) {

    /**
     * A supprimer après la disparition de la V1
     */
    require_once 'application/autoload/Outil/Class_Connecteur.php';
    require_once 'application/autoload/Class_Param.php';
    require_once 'application/config/boot_init.php';
    $horsConnexion = 1;

    $builderInit = new DI\ContainerBuilder();

    $builderInit->addDefinitions($projectRoot . '\config\configSessionInit.php');
    $containerSession = $builderInit->build();
    $_SESSION['Session_NumClient'] = Sql::getNumClient($containerSession->get(\PDO::class));
    /** @phpstan-ignore-next-line */
    setVersionArrayV2($containerSession->get(\PDO::class));
}

if (!isset($_SESSION['Session_Langue']) && ($_SESSION['connexionModeSSO'] ?? 0) != 1
    //  || (array_key_exists('OxaprocDeflangue', $_COOKIE) && $_COOKIE['OxaprocDeflangue'])
) {
    if (array_key_exists('OxaprocDeflangue', $_COOKIE) && $_COOKIE['OxaprocDeflangue']) {
        $_SESSION['Session_Langue'] = $_COOKIE['OxaprocDeflangue'];
    } else {
        if (!isset($containerSession)) {
            $builderInit = new DI\ContainerBuilder();
            $builderInit->addDefinitions($projectRoot . '\config\configSessionInit.php');
            $containerSession = $builderInit->build();
            $_SESSION['Session_NumClient'] = Sql::getNumClient($containerSession->get(\PDO::class));
            /** @phpstan-ignore-next-line */
            setVersionArrayV2($containerSession->get(\PDO::class));
        }
        $_SESSION['Session_Langue'] =  Sql::boolSql(Sql::paravrac($containerSession->get(\PDO::class), 'PARAMOXA', 'MULTILNGACTIF')) ? \Helpers\Outil\Roles::droit($containerSession->get(\PDO::class), 'LNGBDD', 'PARAMOXA', true) : '';
    }
}
/** Faux positif */
/** @phpstan-ignore-next-line */
if ($devSession_NumClient != 0) {
    $_SESSION['Session_NumClient'] = $devSession_NumClient;
}

$nameServer = $_SERVER['COMPUTERNAME'];

function writeFile($dir, $nameServer)
{
    if (@opendir($dir)) {
        $name_dir_tmp = $dir;
        $name_file_tmp = $name_dir_tmp . '/' . 'PHP_Test_Writing_'.$nameServer.'.txt';

        if ( $fp = @fopen($name_file_tmp, 'w+')) {
            
            fputs($fp, "date de création du fichier ".date('d-m-Y H:i:s'));
            fclose($fp);
            // if (@unlink($name_file_tmp) && @rmdir($name_dir_tmp)) {
                return 1;
            // } else {
            //     return 0;
            // }
        } else {
            return 0;
        }
    } else {
        return 0;
    }
}


if(writeFile($_SESSION['chemin_pj'], $nameServer))
{
    echo "Ecriture OK";
} else 
{
    echo "Ecriture KO ".$_SESSION['chemin_pj'].'toto/';
}

if($content = Fichier::getContentFile( 'PHP_Test_Writing_'.$nameServer.'.txt', $_SESSION['chemin_pj']))
{
    echo "<br />Lecture OK, contenu du fichier ".'PHP_Test_Writing_'.$nameServer.'.txt'." =========>>>>>>> ".$content;
} else {
    var_dump(Fichier::getContentFile( 'PHP_Test_Writing_'.$nameServer.'.txt', $_SESSION['chemin_pj']));
}
