NxTIC/public_html/index.php

147 lines
3.9 KiB
PHP
Raw Permalink Normal View History

<?php define('__ROOT__', dirname(dirname(__FILE__)) );
require_once __ROOT__.'/autoloader.php';
use \router\core\Router;
2016-12-17 23:12:23 +00:00
use \http\core\HttpRequest;
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();
/*******************************************/
2016-06-02 16:52:12 +00:00
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 ){
2016-04-17 16:01:23 +00:00
$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';
}
2016-04-04 12:04:36 +00:00
/* (3) Log-Out management */
2016-04-13 13:20:57 +00:00
$logout_vars = isset($_POST['logout-sub']);
2016-06-02 16:52:12 +00:00
// Si on se deconnecte
2016-04-13 13:20:57 +00:00
if( $logout_vars ){
$logout = new ModuleRequest('user/logout');
2016-04-13 13:20:57 +00:00
$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';
2016-04-13 13:20:57 +00:00
}
2016-04-17 16:01:23 +00:00
/* [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
2016-06-02 16:52:12 +00:00
$R->get('/?', function(){ header(__REDIRECT__); });
2016-04-04 12:04:36 +00:00
/* [5] Manage websites pages
2016-04-04 12:04:36 +00:00
=========================================================*/
$temporaryMenuInflater = new MenuManager();
$views = $temporaryMenuInflater->getCategories();
2016-04-04 12:04:36 +00:00
// 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){
2016-12-17 23:12:23 +00:00
$http = new HttpRequest();
$_POST = $http->POST();
$request = ModuleRequest::fromPost($url, $_POST);
$answer = $request->dispatch();
// Si c'est une réponse
2016-06-04 09:06:36 +00:00
if( $answer instanceof ModuleResponse )
echo $answer->serialize();
});
/* [7] Any other URL -> homepage
=========================================================*/
$R->get('.+', function(){ header(__REDIRECT__); });
2016-06-02 16:52:12 +00:00
$R->post('.+', function(){ header(__REDIRECT__); });
/* [8] Launch Router
===================================================*/
$R->run();
?>