SMMP/public_html/index.php

160 lines
4.3 KiB
PHP
Raw Normal View History

<?php
2016-07-02 15:10:41 +00:00
require_once '../autoloader.php';
2016-10-18 14:03:03 +00:00
use \router\core\Router;
2017-01-30 17:39:21 +00:00
use \api\core\Request;
use \api\core\Response;
use \database\core\DatabaseDriver;
use \api\core\AuthSystemDefault;
2016-10-18 14:03:03 +00:00
2016-07-02 16:35:34 +00:00
/*******************************************/
/* DEBUGGER */
/*******************************************/
debug();
/*******************************************/
/* DEBUGGER */
/*******************************************/
/* [1] Gestion des authentifications et des droits
=========================================================*/
/* (1) On met à jour l'authentification et les permissions */
$auth = AuthSystemDefault::auth();
2017-01-30 17:39:21 +00:00
Request::setAuthSystem(new AuthSystemDefault);
/* (2) On définit la page d'accueil */
if( $auth == 2 ) define('__REDIRECT__', 'Location: /history/'); // Connecté -> Accès
elseif( $auth == 1 ) define('__REDIRECT__', 'Location: /admin/'); // Pas identifié -> Identification
else define('__REDIRECT__', 'Location: /warehouse/'); // Pas localisé -> Localisation
2016-07-16 11:05:24 +00:00
/* [2] Gestion du routage
=========================================================*/
/* (1) On initialise le routeur
---------------------------------------------------------*/
$R = new Router( $_GET['url'] );
2016-07-16 11:05:24 +00:00
/* (2) Gestion des SVG avec couleur modifiée */
// path/to/resource/filename-HEXADE.svg
$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\tfill: #".$matches[1]." !important;\n";
$stylesheet .= "\t\tfill-opacity: 1 !important;\n";
$stylesheet .= "\t}\n";
$stylesheet .= "\t#stroke-stylisable{\n";
$stylesheet .= "\t\tstroke: #".$matches[1]." !important;\n";
$stylesheet .= "\t\tstroke-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;
});
/* (3) On cree les regles de routage QUAND ON EST CONNECTE
---------------------------------------------------------*/
/* (2) Si on est connecté */
if( $auth == 2 ){
// logout from admin
$R->get('logout/?', function(){
$_SERVER['REQUEST_METHOD'] = 'POST';
$req = new Request('authentificationDefault/admin', ['username' => '0', 'password' => '']);
$res = $req->dispatch();
header('Location: /');
});
// nomPage/arg1/arg2 -> inclusion de la page
$R->get('(.*)', function($m){
// Liste des pages du site
$page_list = [ 'history', 'profile', 'machines', 'users', 'groups', 'analytics', 'settings' ];
2016-07-16 11:05:24 +00:00
if( !preg_match('#^(?:'.implode('|', $page_list).')(?:/[\w-]+)*/?$#i', $m[0]) )
header(__REDIRECT__);
else
2016-10-18 14:03:03 +00:00
include __PUBLIC__.'/view/view.php';
});
/* (3) Si on est pas authentifié */
}else if( $auth == 1 ){
// warehouse logout
$R->get('logout/?', function(){
$_SERVER['REQUEST_METHOD'] = 'POST';
2017-01-30 17:39:21 +00:00
(new Request('authentificationDefault/warehouse', ['name' => '000', 'password' => '']))->dispatch();
header('Location: /');
});
// admin login page
$R->get('(.*)', function($m){
if( !preg_match('#^admin/$#', $m[0]) ) header(__REDIRECT__);
2016-10-18 14:03:03 +00:00
else include __PUBLIC__.'/view/admin.php';
});
}else{
2016-10-18 14:03:03 +00:00
$R->get('(.*)', function($m){
if( !preg_match('#^warehouse/$#', $m[0]) ) header(__REDIRECT__);
2016-10-18 14:03:03 +00:00
else include __PUBLIC__.'/view/warehouse.php';
});
}
/* (4) api/module/method -> Api */
2017-01-30 17:39:21 +00:00
$R->post('api(?:(/.*))/?', function($url){
$request = Request::remote($url[0], $_POST);
$answer = $request->dispatch();
2016-07-02 15:10:41 +00:00
// Si c'est une réponse (et non un download)
2017-01-30 17:39:21 +00:00
if( $answer instanceof Response ){
2016-08-06 09:32:50 +00:00
header('Content-Type: application/json; charset=UTF-8');
echo $answer->serialize();
2016-08-06 09:32:50 +00:00
}
});
2016-07-02 15:10:41 +00:00
/* (6) N'importe -> page d'accueil */
$R->get('.+', function(){ header(__REDIRECT__); });
$R->post('.+', function(){ header(__REDIRECT__); });
/* (3) On lance le routeur
---------------------------------------------------------*/
$R->run();
2016-07-02 15:10:41 +00:00
?>