2017-01-06 09:27:30 +00:00
< ? php
2016-07-02 15:10:41 +00:00
2017-09-19 14:38:16 +00:00
require_once '../vendor/autoload.php' ;
2016-02-01 22:09:35 +00:00
2016-10-18 14:03:03 +00:00
use \router\core\Router ;
2017-11-09 11:56:00 +00:00
use \api\core\Loader ;
2017-01-30 17:39:21 +00:00
use \api\core\Request ;
use \api\core\Response ;
2016-11-05 13:57:35 +00:00
use \database\core\DatabaseDriver ;
2017-01-30 18:59:06 +00:00
use \api\core\AuthSystemDefault ;
2017-10-27 16:02:11 +00:00
use \token\core\TreeToken ;
use \log\core\Log ;
$page_log = Log :: get ( 'router' );
$session_guard = new TreeToken ( 1000 );
2016-10-18 14:03:03 +00:00
2016-02-13 17:41:19 +00:00
2016-02-02 22:29:30 +00:00
/*******************************************/
/* DEBUGGER */
/*******************************************/
2016-02-04 22:45:03 +00:00
debug ();
2016-02-02 22:29:30 +00:00
/*******************************************/
2016-07-04 09:04:49 +00:00
/* DEBUGGER */
/*******************************************/
2016-07-08 13:18:23 +00:00
/* [ 1 ] Gestion des authentifications et des droits
2016-07-07 15:59:31 +00:00
=========================================================*/
2016-07-08 13:18:23 +00:00
/* (1) On met à jour l'authentification et les permissions */
2017-02-19 11:14:03 +00:00
Request :: setAuthSystem ( new AuthSystemDefault );
2017-01-30 18:59:06 +00:00
$auth = AuthSystemDefault :: auth ();
2017-01-30 17:39:21 +00:00
2016-07-04 09:04:49 +00:00
2016-07-10 13:23:53 +00:00
/* (2) On définit la page d'accueil */
2016-07-21 10:23:18 +00:00
if ( $auth == 2 ) define ( '__REDIRECT__' , 'Location: /history/' ); // Connecté -> Accès
2016-07-10 13:23:53 +00:00
elseif ( $auth == 1 ) define ( '__REDIRECT__' , 'Location: /admin/' ); // Pas identifié -> Identification
else define ( '__REDIRECT__' , 'Location: /warehouse/' ); // Pas localisé -> Localisation
2016-07-04 09:04:49 +00:00
2016-07-16 11:05:24 +00:00
2016-07-04 09:04:49 +00:00
/* [ 2 ] Gestion du routage
=========================================================*/
/* ( 1 ) On initialise le routeur
---------------------------------------------------------*/
$R = new Router ( $_GET [ 'url' ] );
2016-02-01 22:09:35 +00:00
2016-07-03 12:20:42 +00:00
2016-07-16 11:05:24 +00:00
/* (2) Gestion des SVG avec couleur modifiée */
$R -> get ( '(.+)@([a-f0-9]{6})(\.svg)' , function ( $matches ){
2016-10-18 14:03:03 +00:00
$path = __PUBLIC__ . '/' . $matches [ 0 ] . $matches [ 2 ];
2016-07-16 11:05:24 +00:00
header ( 'Content-Type: image/svg+xml' );
// On crée la partie ajoutée
$stylesheet = " \n <style type='text/css'> \n " ;
$stylesheet .= " \t #stylisable { \n " ;
$stylesheet .= " \t \t fill: # " . $matches [ 1 ] . " !important; \n " ;
$stylesheet .= " \t \t fill-opacity: 1 !important; \n " ;
$stylesheet .= " \t } \n " ;
2017-01-15 16:27:02 +00:00
$stylesheet .= " \t #stroke-stylisable { \n " ;
$stylesheet .= " \t \t stroke: # " . $matches [ 1 ] . " !important; \n " ;
$stylesheet .= " \t \t stroke-opacity: 1 !important; \n " ;
$stylesheet .= " \t } \n " ;
2016-07-16 11:05:24 +00:00
$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 ;
});
2017-10-27 16:02:11 +00:00
/* (3) Si ....css.map n'existe pas ne cherche pas */
$R -> get ( '(.+).css.map' , function ( $matches ){
die ();
});
2017-11-05 10:17:53 +00:00
/* (4) serverinfo.js -> generate it with no cache */
$R -> get ( 'serverinfo.js' , function ( $matches ){
global $auth ;
2017-11-12 10:21:09 +00:00
// session timeout warning
$session_warn = 5 ; // 5 minutes before expiration
2017-11-05 10:17:53 +00:00
// {1} Disable cache //
header ( 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0' );
header ( 'Cache-Control: post-check=0, pre-check=0' , false );
header ( 'Pragma: no-cache' );
header ( 'Content-Type: text/javascript' );
// {2} Generate content //
echo " var SERVER = { \n " ;
echo " \t module: { \n " ;
// if connected to warehouse
if ( $auth >= 1 ){
$m_id = array_keys ( $_SESSION [ 'WAREHOUSE' ][ 'modules' ]);
for ( $m = 0 ; $m < count ( $m_id ) ; $m ++ ){
if ( $m > 0 )
echo " , \n " ;
echo " \t \t ' " . $_SESSION [ 'WAREHOUSE' ][ 'modules' ][ $m_id [ $m ]] . " ': true " ;
}
}
2017-11-12 10:21:09 +00:00
// session timeout
echo " \n \t }, \n " ;
echo " \t session: { \n " ;
2017-11-12 10:45:47 +00:00
echo " \t \t timeout: ' " . ( ( ini_get ( 'session.gc_maxlifetime' ) - $session_warn * 60 ) * 1000 ) . " ' \n " ;
2017-11-05 10:17:53 +00:00
echo " \n \t } \n " ;
2017-11-12 10:21:09 +00:00
echo " }; \n \n " ;
// {3} Function that reloads or logout the user
echo " var KEEP_SESSION = function(keep) { \n " ;
2017-11-12 10:45:47 +00:00
echo " \t document.location = ( keep ) ? '' : '/logout'; \n " ;
2017-11-12 10:21:09 +00:00
echo " }; \n \n " ;
// {4} Function called at session timeout
echo " var SESSION_TIMEOUT_FUNC = function() { \n " ;
// {4.1} setup POPUP window
echo " \t var popup = new Popup(); \n " ;
echo " \t var popup_content = { \n " ;
echo " \t \t title: 'Expiration de connexion', \n " ;
2017-11-12 10:45:47 +00:00
echo " \t \t content: 'Vous n \ 'avez rien fait depuis un moment. Vous serez déconnecté dans <b> " . round ( . 5 + $session_warn ) . " minutes</b>. Vous pouvez recharger la page pour garder votre connexion.', \n " ;
2017-11-12 10:21:09 +00:00
echo " \t \t type: 'search', \n " ;
echo " \t \t action: 'Recharger la page' \n " ;
echo " \t } \n " ;
// {4.2} Show popup dialog -> on reload: reload page ; on cancel: logout
echo " \t popup.ask(popup_content, KEEP_SESSION); \n " ;
// {4.3} Logout by default 5min if no popup response
2017-11-12 10:45:47 +00:00
echo " \t setTimeout(function() { KEEP_SESSION(false); }, (.5+ $session_warn )*60*1000); \n " ;
2017-11-12 10:21:09 +00:00
echo " }; \n \n " ;
// {5} Set the timeout
echo " var SESSION_TIMEOUT = setTimeout(SESSION_TIMEOUT_FUNC, SERVER.session.timeout); \n " ;
2017-11-05 10:17:53 +00:00
die ();
});
2016-07-16 11:05:24 +00:00
2016-07-08 13:18:23 +00:00
/* ( 3 ) On cree les regles de routage QUAND ON EST CONNECTE
2016-07-04 09:04:49 +00:00
---------------------------------------------------------*/
2016-07-08 13:18:23 +00:00
/* (2) Si on est connecté */
if ( $auth == 2 ){
2017-01-13 17:17:31 +00:00
// logout from admin
$R -> get ( 'logout/?' , function (){
2017-10-27 16:02:11 +00:00
$GLOBALS [ 'page_log' ] -> log ( 'admin.logout' );
$GLOBALS [ 'session_guard' ] -> init_parent ();
2017-02-17 07:27:34 +00:00
$_SERVER [ 'REQUEST_METHOD' ] = 'POST' ;
2017-10-19 12:07:38 +00:00
$req = new Request ( 'authenticationDefault/admin' , [ 'username' => '-' , 'password' => '' ]);
2017-02-16 17:34:38 +00:00
$res = $req -> dispatch ();
2017-01-13 17:17:31 +00:00
header ( 'Location: /' );
});
2016-07-08 13:18:23 +00:00
2017-10-27 16:02:11 +00:00
// nomPage/arg1/arg2 -> page correcte
$page_list = [ 'history' , 'profile' , 'machines' , 'users' , 'groups' , 'options' , 'settings' ];
2016-07-16 11:05:24 +00:00
2017-10-27 16:02:11 +00:00
$R -> get ( '((?:' . implode ( '|' , $page_list ) . ')(?:/[\w-]+)*/?)' , function ( $m ){
$GLOBALS [ 'page_log' ] -> log ( " admin.page(/ ${ m[0] } ) " );
$GLOBALS [ 'session_guard' ] -> init_parent ();
2016-07-10 13:23:53 +00:00
2017-10-27 16:02:11 +00:00
include __PUBLIC__ . '/view/view.php' ;
2016-07-10 13:23:53 +00:00
});
2017-10-27 16:02:11 +00:00
/* (3) Si on est pas admin, juste warehouse */
2016-07-10 13:23:53 +00:00
} else if ( $auth == 1 ){
2017-01-13 17:17:31 +00:00
// warehouse logout
$R -> get ( 'logout/?' , function (){
2017-10-27 16:02:11 +00:00
$GLOBALS [ 'page_log' ] -> log ( 'warehouse.logout' );
$GLOBALS [ 'session_guard' ] -> init_parent ();
2017-02-17 07:27:34 +00:00
$_SERVER [ 'REQUEST_METHOD' ] = 'POST' ;
2017-10-19 12:07:38 +00:00
( new Request ( 'authenticationDefault/warehouse' , [ 'name' => '---' , 'password' => '' ])) -> dispatch ();
2017-01-13 17:17:31 +00:00
header ( 'Location: /' );
});
// admin login page
2017-10-27 16:02:11 +00:00
$R -> get ( 'admin/?' , function (){
$GLOBALS [ 'page_log' ] -> log ( 'admin.login_page' );
$GLOBALS [ 'session_guard' ] -> init_parent ();
include __PUBLIC__ . '/view/admin.php' ;
2016-07-10 13:23:53 +00:00
});
2016-07-08 13:18:23 +00:00
2017-10-27 16:02:11 +00:00
/* (4) Si on est pas co */
2016-07-10 13:23:53 +00:00
} else {
2016-10-18 14:03:03 +00:00
2017-10-27 16:02:11 +00:00
// warehouse login page
$R -> get ( 'warehouse/?' , function (){
$GLOBALS [ 'page_log' ] -> log ( 'warehouse.login_page' );
$GLOBALS [ 'session_guard' ] -> init_parent ();
include __PUBLIC__ . '/view/warehouse.php' ;
2016-07-10 13:23:53 +00:00
});
2016-07-08 13:18:23 +00:00
2016-07-10 13:23:53 +00:00
}
2016-02-01 22:09:35 +00:00
2016-02-03 22:22:18 +00:00
2016-02-02 22:29:30 +00:00
2016-07-04 09:04:49 +00:00
/* (4) api/module/method -> Api */
2017-01-30 17:39:21 +00:00
$R -> post ( 'api(?:(/.*))/?' , function ( $url ){
2017-10-27 16:15:37 +00:00
if ( isset ( $_SERVER [ 'HTTP_X_TREE_TOKEN' ]) )
$GLOBALS [ 'page_log' ] -> log ( 'api.call(' . $_SERVER [ 'HTTP_X_TREE_TOKEN' ] . ')' );
2017-10-27 16:17:07 +00:00
else
$GLOBALS [ 'page_log' ] -> log ( 'api.call(NO_TOKEN)' );
2017-10-27 16:02:11 +00:00
header ( 'Content-Type: application/json; charset=UTF-8' );
2017-10-27 16:15:37 +00:00
// {1} Allow authed SATS not to be checked by session_guard.child //
if ( $GLOBALS [ 'auth' ] < 3 || ! isset ( $_SERVER [ 'PHP_AUTH_DIGEST' ]) )
if ( ! $GLOBALS [ 'session_guard' ] -> init_child () )
die ( json_encode ([ 'error' => 100 , 'ErrorDescription' => 'session_guard.child error' ]));
2017-10-27 16:02:11 +00:00
2017-11-09 11:56:00 +00:00
$request = Loader :: remote ( $url [ 0 ]);
2016-07-04 09:04:49 +00:00
$answer = $request -> dispatch ();
2016-07-02 15:10:41 +00:00
2016-07-04 09:04:49 +00:00
// Si c'est une réponse (et non un download)
2017-10-27 16:02:11 +00:00
if ( $answer instanceof Response )
2016-07-04 09:04:49 +00:00
echo $answer -> serialize ();
});
2016-02-04 22:45:03 +00:00
2016-07-02 15:10:41 +00:00
2017-01-13 17:17:31 +00:00
/* (6) N'importe -> page d'accueil */
2017-10-27 16:02:11 +00:00
$R -> get ( '.*' , function (){ $GLOBALS [ 'page_log' ] -> log ( 'get.redirect' ); $GLOBALS [ 'session_guard' ] -> init_parent (); header ( __REDIRECT__ ); });
$R -> post ( '.*' , function (){ $GLOBALS [ 'page_log' ] -> log ( 'post.redirect' ); header ( __REDIRECT__ ); });
2016-02-01 22:09:35 +00:00
2016-02-02 10:09:48 +00:00
2016-07-04 09:04:49 +00:00
/* ( 3 ) On lance le routeur
---------------------------------------------------------*/
$R -> run ();
2016-02-01 22:09:35 +00:00
2016-07-02 15:10:41 +00:00
?>