<?php
require_once '../vendor/autoload.php';

ini_set('display_errors', '1');

/**
 * Améliore la sortie print
 *
 * @author  Tatane http://www.tatane.info/index.php/print_rn
 * @author http://www.blog.cactuscrew.com/77-print_rn.html
 * @param $data (array) tableau ou variable à examiner
 * @param $name (string) nom a afficher
 * @return false affiche les clef valeur du tableau $data
 * @example n_print($array, 'Tableau de valeur');
 */
function n_print($data, $name = '') {
    $aBackTrace = debug_backtrace();
    echo '<h2>', $name, '</h2>';
    echo '<fieldset style="border: 1px solid orange; padding: 5px;color: #333; background-color: #fff;">';
    echo '<legend style="border:1px solid orange;padding: 1px;background-color:#eee;color:orange;">
    ', basename($aBackTrace[0]['file']), ' ligne => ', $aBackTrace[0]['line'], '
  </legend>';
    echo '<pre>', htmlentities(print_r($data, 1)), '</pre>';
    echo '
</fieldset>
<br />
';
}

use App\Router;
use App\Utils\Error;
use Noodlehaus\Config;

$router = new Router();

$router->addRoute(['/', '/index.php', '/index'], 'App\Controllers\Home', 'GET');
$router->addRoute('/api', 'App\Controllers\Result', 'GET');
$router->addRoute('/hmac', 'App\Controllers\GenHmac', 'GET');
//$router->addRoute('/debug', 'App\Bin\ThumbShoter', 'GET');
$router->addRoute('/backend', 'App\Controllers\Backend', 'GET');
$router->addRoute('/logout', 'App\Controllers\Logout', 'GET');

$conf = (object)Config::load([__DIR__ . '/../datas/config.default.json', '?' . __DIR__ . '/../datas/config.json'])->all();

if (!file_exists(__DIR__ . '/../datas/config.json')) {
    session_start();
    $conf->webPage = true;
    $_SESSION['login'] = 1;
    $_SESSION['firstRun'] = 1;
    if ($_SERVER['REQUEST_URI'] !== '/backend?page=settings') {
        header("Location:/backend?page=settings");
        exit();
    }
}

if ($conf->onlyLocalServer === true && $_SERVER['REMOTE_ADDR'] !== '127.0.0.1') {
    $error = new Error();
    $error->index((object)['status' => 404, 'message' => 'The page that you have requested could not be found.']);
    exit();
}

if ($router->match()) {
    $router->render($conf);
} else {
    $error = new Error();
    $error->index((object)['status' => 404, 'message' => 'The page that you have requested could not be found.']);
}
