2016-04-04 09:47:17 +00:00
|
|
|
<?php define('__ROOT__', dirname(__FILE__) );
|
2016-04-18 06:42:26 +00:00
|
|
|
|
2016-04-04 09:47:17 +00:00
|
|
|
require_once __ROOT__.'/manager/autoloader.php';
|
|
|
|
|
|
|
|
use \router\Router;
|
|
|
|
use \manager\ResourceDispatcher;
|
|
|
|
use \manager\ModuleRequest;
|
2016-04-12 15:46:34 +00:00
|
|
|
use \manager\ManagerError;
|
|
|
|
use \manager\Database;
|
2016-04-13 15:06:16 +00:00
|
|
|
use \manager\MenuManager;
|
2016-04-12 15:46:34 +00:00
|
|
|
|
|
|
|
|
2016-04-04 09:47:17 +00:00
|
|
|
|
|
|
|
/*******************************************/
|
|
|
|
/* DEBUGGER */
|
|
|
|
/*******************************************/
|
|
|
|
debug();
|
|
|
|
/*******************************************/
|
|
|
|
|
|
|
|
|
2016-06-02 16:52:12 +00:00
|
|
|
define('__REDIRECT__', 'Location: /dashboard/');
|
|
|
|
|
2016-04-13 11:36:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [x] Gestion des utilisateurs
|
2016-04-12 15:46:34 +00:00
|
|
|
=========================================================*/
|
|
|
|
/* (1) Valeurs par defaut */
|
|
|
|
if( !isset($_SESSION['userid']) ) $_SESSION['userid'] = null;
|
|
|
|
if( !isset($_SESSION['username']) ) $_SESSION['username'] = null;
|
|
|
|
if( !isset($_SESSION['permission']) ) $_SESSION['permission'] = array();
|
|
|
|
|
2016-04-13 11:36:49 +00:00
|
|
|
|
2016-04-12 15:46:34 +00:00
|
|
|
/* (2) Gestion de la connection */
|
2016-04-13 13:20:57 +00:00
|
|
|
$login_vars = isset($_POST['login-sub']);
|
2016-04-17 16:01:23 +00:00
|
|
|
$login_vars = $login_vars && isset($_POST['login']) && Database::check('varchar(3,50)', $_POST['login']);
|
2016-04-12 15:46:34 +00:00
|
|
|
$login_vars = $login_vars && isset($_POST['password']) && Database::check('text', $_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
|
|
|
|
2016-04-14 10:45:54 +00:00
|
|
|
$login = new ModuleRequest('user/login', array( 'login' => $_POST['login'], 'password' => $_POST['password'] ));
|
2016-04-12 15:46:34 +00:00
|
|
|
$login_ans = $login->dispatch();
|
|
|
|
|
2016-06-02 16:52:12 +00:00
|
|
|
// Si aucune erreur, on dit qu'on vient de se connecter
|
2016-04-12 22:29:18 +00:00
|
|
|
if( $login_ans->error == ManagerError::Success )
|
2016-04-12 15:46:34 +00:00
|
|
|
$_SESSION['login_status'] = 'logged';
|
2016-04-13 11:36:49 +00:00
|
|
|
else // Si on n'a pas les bonnes donnees
|
2016-04-12 15:46:34 +00:00
|
|
|
$_SESSION['login_status'] = 'error';
|
|
|
|
}
|
2016-04-04 12:04:36 +00:00
|
|
|
|
|
|
|
|
2016-04-13 11:36:49 +00:00
|
|
|
/* (3) Gestion de le deconnexion */
|
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 ){
|
2016-04-14 10:45:54 +00:00
|
|
|
$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 11:36:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-04-17 16:01:23 +00:00
|
|
|
|
2016-04-04 09:47:17 +00:00
|
|
|
/* [0] On initialise le routeur
|
|
|
|
===================================================*/
|
|
|
|
$R = new Router( $_GET['url'] );
|
2016-04-13 11:36:49 +00:00
|
|
|
|
2016-04-04 09:47:17 +00:00
|
|
|
|
|
|
|
/* [1] On cree les regles de routage
|
|
|
|
===================================================*/
|
|
|
|
// Racine -> page d'accueil
|
2016-06-02 16:52:12 +00:00
|
|
|
$R->get('/?', function(){ header(__REDIRECT__); });
|
2016-04-04 09:47:17 +00:00
|
|
|
|
2016-04-04 12:04:36 +00:00
|
|
|
|
|
|
|
/* [2] On recupere la liste des pages du site
|
|
|
|
=========================================================*/
|
2016-04-13 15:06:16 +00:00
|
|
|
$temporaryMenuInflater = new MenuManager();
|
|
|
|
$views = $temporaryMenuInflater->getCategories();
|
2016-04-04 12:04:36 +00:00
|
|
|
|
2016-04-04 09:47:17 +00:00
|
|
|
|
|
|
|
// nomPage/arg1/arg2 -> inclusion de la page
|
2016-04-04 12:04:36 +00:00
|
|
|
$R->get('(?:'.implode('|', $views).')(?:/[\w-]+)*/?', function(){ include __ROOT__.'/view.php'; });
|
2016-04-04 09:47:17 +00:00
|
|
|
|
2016-04-13 11:36:49 +00:00
|
|
|
|
2016-04-04 09:47:17 +00:00
|
|
|
// Dispatcher
|
2016-04-04 12:04:36 +00:00
|
|
|
$R->get('f(?:/([\w-]+))*/?', function(){ new ResourceDispatcher($_GET['url'], true); });
|
2016-04-04 09:47:17 +00:00
|
|
|
|
2016-04-13 11:36:49 +00:00
|
|
|
|
2016-04-04 09:47:17 +00:00
|
|
|
// Api
|
2016-06-03 08:06:47 +00:00
|
|
|
$R->post('api(?:/(.*))?', function($url){
|
|
|
|
$request = ModuleRequest::fromPost($url, $_POST);
|
2016-06-03 08:58:01 +00:00
|
|
|
$answer = $request->dispatch();
|
|
|
|
|
|
|
|
// Si c'est une réponse
|
|
|
|
if( $answer instanceof ModuleResponse )
|
|
|
|
echo $answer->serialize();
|
2016-04-18 06:42:26 +00:00
|
|
|
|
2016-04-04 09:47:17 +00:00
|
|
|
});
|
|
|
|
|
2016-04-13 11:36:49 +00:00
|
|
|
|
2016-04-04 09:47:17 +00:00
|
|
|
// N'importe -> page d'accueil
|
2016-06-02 16:52:12 +00:00
|
|
|
$R->get('.+', function(){ header(__REDIRECT__); });
|
|
|
|
$R->post('.+', function(){ header(__REDIRECT__); });
|
2016-04-18 06:42:26 +00:00
|
|
|
|
2016-04-04 09:47:17 +00:00
|
|
|
|
|
|
|
|
2016-04-13 11:36:49 +00:00
|
|
|
|
2016-04-04 12:04:36 +00:00
|
|
|
/* [3] On lance le routeur
|
2016-04-04 09:47:17 +00:00
|
|
|
===================================================*/
|
|
|
|
$R->run();
|
|
|
|
|
2016-04-18 06:42:26 +00:00
|
|
|
?>
|