NxTIC/public_html/index.php

143 lines
3.8 KiB
PHP

<?php define('__ROOT__', dirname(dirname(__FILE__)) );
require_once __ROOT__.'/autoloader.php';
use \router\core\Router;
use \api\core\ModuleRequest;
use \api\core\ModuleResponse;
use \manager\ManagerError;
use \database\core\DatabaseDriver;
use \api\core\Checker;
use \manager\MenuManager;
// var_dump($_SESSION);
/*******************************************/
/* DEBUGGER */
/*******************************************/
debug();
/*******************************************/
define('__REDIRECT__', 'Location: /dashboard/');
/* [2] Identity Management
=========================================================*/
/* (1) Session initialization */
if( !isset($_SESSION['userid']) ) $_SESSION['userid'] = null;
if( !isset($_SESSION['username']) ) $_SESSION['username'] = null;
if( !isset($_SESSION['permission']) ) $_SESSION['permission'] = [];
/* (2) Sign-In management */
$login_vars = isset($_POST['login-sub']) && isset($_POST['login']) && isset($_POST['password']);
// Status de login
$_SESSION['login_status'] = 'no';
// Si les parametres de connection, on essaie de connecter
if( $login_vars ){
$login = new ModuleRequest('user/login', [ 'login' => $_POST['login'], 'password' => $_POST['password'] ]);
$login_ans = $login->dispatch();
// Si aucune erreur, on dit qu'on vient de se connecter
if( $login_ans->error == ManagerError::Success )
$_SESSION['login_status'] = 'logged';
// Si on n'a pas les bonnes donnees
else
$_SESSION['login_status'] = 'error';
}
/* (3) Log-Out management */
$logout_vars = isset($_POST['logout-sub']);
// Si on se deconnecte
if( $logout_vars ){
$logout = new ModuleRequest('user/logout');
$logout_ans = $logout->dispatch();
// Si aucune erreur, on dit qu'on vient de se deconnecter
if( $logout_ans->error == ManagerError::Success )
$_SESSION['login_status'] = 'unlogged';
}
/* [3] On initialise le routeur
===================================================*/
/* (1) New Router */
$R = new Router( $_GET['url'] );
/* (2) path/to/resource/filename@0hexa0.svg -> update svg content */
$R->get('(.+)@([a-f0-9]{6})(\.svg)', function($matches){
$path = __PUBLIC__.'/'.$matches[0].$matches[2];
header('Content-Type: image/svg+xml; charset=utf-8');
// On crée la partie ajoutée
$stylesheet = "\n<style type='text/css'>\n";
$stylesheet .= "\t#stylisable{\n";
$stylesheet .= "\t\tfill: #".$matches[1]." !important;\n";
$stylesheet .= "\t\tfill-opacity: 1 !important;\n";
$stylesheet .= "\t}\n";
$stylesheet .= "</style></svg>";
// On récupère le fichier
$file = file_get_contents($path);
// On ajoute le style
$file = str_replace('</svg>', $stylesheet, $file);
echo $file;
});
/* [4] / -> homepage
===================================================*/
// Racine -> page d'accueil
$R->get('/?', function(){ header(__REDIRECT__); });
/* [5] Manage websites pages
=========================================================*/
$temporaryMenuInflater = new MenuManager();
$views = $temporaryMenuInflater->getCategories();
// pagename/arg1/arg2 -> include view
$R->get('(?:'.implode('|', $views).')(?:/[\w-]+)*/?', function(){ include __PUBLIC__.'/view.php'; });
/* [6] host.xxx/api/{module}/{method} -> api
=========================================================*/
$R->post('api(?:/(.*))?', function($url){
$request = ModuleRequest::fromPost($url, $_POST);
$answer = $request->dispatch();
// Si c'est une réponse
if( $answer instanceof ModuleResponse )
echo $answer->serialize();
});
/* [7] Any other URL -> homepage
=========================================================*/
$R->get('.+', function(){ header(__REDIRECT__); });
$R->post('.+', function(){ header(__REDIRECT__); });
/* [8] Launch Router
===================================================*/
$R->run();
?>