2016-07-04 13:45:29 +00:00
|
|
|
<?php
|
2016-02-02 22:29:30 +00:00
|
|
|
|
|
|
|
namespace manager;
|
2016-07-04 13:45:29 +00:00
|
|
|
|
2016-02-02 22:29:30 +00:00
|
|
|
|
2016-02-04 20:15:43 +00:00
|
|
|
class ResourceDispatcher{
|
2016-02-02 22:29:30 +00:00
|
|
|
|
2016-02-04 18:23:16 +00:00
|
|
|
// Constantes
|
2016-02-03 07:34:27 +00:00
|
|
|
public static $extension_config_path = '/config/dispatcher-extensions.json';
|
|
|
|
public static $parents_config_path = '/config/dispatcher-tree.json';
|
|
|
|
|
|
|
|
public static $supported_extensions;
|
|
|
|
public static $supported_parents;
|
2016-02-02 22:29:30 +00:00
|
|
|
|
|
|
|
// Attributs prives utiles (initialisation)
|
|
|
|
private $header;
|
|
|
|
private $path;
|
|
|
|
private $flags;
|
2016-02-16 22:42:07 +00:00
|
|
|
private $stylesheet;
|
2016-02-04 20:15:43 +00:00
|
|
|
public $error;
|
2016-02-02 22:29:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* CONSTRUCTEUR & AMORCAGE DU DISPATCHER
|
|
|
|
*
|
|
|
|
* @url<String> L'url courante
|
2016-02-08 21:43:35 +00:00
|
|
|
* @view<Boolean> Si VRAI, retourne header+contenu, sinon cree juste l'objet
|
2016-02-02 22:29:30 +00:00
|
|
|
*
|
|
|
|
* @return status<Boolean> Retourne si oui ou non tout s'est bien passe
|
|
|
|
*
|
|
|
|
*/
|
2016-02-08 21:43:35 +00:00
|
|
|
public function __construct($url, $view=false){
|
|
|
|
$this->error = ManagerError::Success;
|
|
|
|
|
|
|
|
|
2016-02-03 07:34:27 +00:00
|
|
|
/* [0] On met a jour la configuration
|
|
|
|
=====================================================*/
|
|
|
|
// Extensions supportees
|
2016-02-08 21:43:35 +00:00
|
|
|
$extensions_conf = json_decode( file_get_contents(__ROOT__.self::$extension_config_path), true );
|
2016-02-04 18:23:16 +00:00
|
|
|
|
|
|
|
// Gestion de l'erreur de parsage
|
2016-02-04 20:15:43 +00:00
|
|
|
if( $extensions_conf == null ){
|
|
|
|
$this->error = ManagerError::ParsingFailed;
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-04 18:23:16 +00:00
|
|
|
|
2016-02-03 09:13:31 +00:00
|
|
|
self::$supported_extensions = $extensions_conf;
|
2016-02-03 07:34:27 +00:00
|
|
|
|
|
|
|
// Dossiers supportes
|
2016-02-08 21:43:35 +00:00
|
|
|
$parents_conf = json_decode( file_get_contents(__ROOT__.self::$parents_config_path), true );
|
2016-07-04 13:45:29 +00:00
|
|
|
|
2016-02-04 18:23:16 +00:00
|
|
|
// Gestion de l'erreur de parsage
|
2016-02-04 20:15:43 +00:00
|
|
|
if( $parents_conf == null ){
|
|
|
|
$this->error = ManagerError::ParsingFailed;
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-04 18:23:16 +00:00
|
|
|
|
2016-02-03 09:13:31 +00:00
|
|
|
self::$supported_parents = $parents_conf;
|
2016-02-03 07:34:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-02 22:29:30 +00:00
|
|
|
/* [1] On recupere les donnees de l'URL
|
|
|
|
==================================================*/
|
|
|
|
$serialFlags = array_slice( explode('/',$url), 1 );
|
|
|
|
|
|
|
|
/* [2] On check/cree les drapeaux avec ces donnees
|
|
|
|
==================================================*/
|
2016-02-04 20:15:43 +00:00
|
|
|
if( !$this->createFlags($serialFlags) ){ // Creation des drapeaux
|
|
|
|
$this->error = ManagerError::InvalidFlags;
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-02 22:29:30 +00:00
|
|
|
|
|
|
|
/* [3] On construit le chemin a partir des tags
|
|
|
|
==================================================*/
|
2016-02-04 20:15:43 +00:00
|
|
|
if( !$this->buildPath() ){ // Construction du chemin
|
2016-02-04 22:45:03 +00:00
|
|
|
$this->error = ManagerError::UnreachableResource;
|
2016-02-04 20:15:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-02-02 22:29:30 +00:00
|
|
|
|
2016-02-08 21:43:35 +00:00
|
|
|
/* [4] On gere l'affichage pour l'appel externe/interne
|
2016-02-02 22:29:30 +00:00
|
|
|
==================================================*/
|
2016-02-08 21:43:35 +00:00
|
|
|
if( $view ) // Appel externe
|
|
|
|
$this->view();
|
2016-02-02 22:29:30 +00:00
|
|
|
|
2016-07-04 13:45:29 +00:00
|
|
|
|
2016-02-02 22:29:30 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-04 18:23:16 +00:00
|
|
|
/* INCLUSION PHP D'UNE RESSOURCE UTILISANT LE DISPATCHER
|
|
|
|
*
|
|
|
|
* @route<String> Route associee a une ressource
|
|
|
|
*
|
|
|
|
* @return content<*> Retourne le contenu de la ressource
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getResource($route){
|
2016-02-08 21:43:35 +00:00
|
|
|
$instance = new ResourceDispatcher($route);
|
|
|
|
return $instance->getContent();
|
2016-02-04 18:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-02 22:29:30 +00:00
|
|
|
|
|
|
|
/* FONCTION QUI VERIFIE LES DRAPEAUX
|
|
|
|
*
|
|
|
|
* @serialFlags<Array> Tableau a indice numerique
|
|
|
|
*
|
|
|
|
* @return correct<Boolean> Retourne si oui ou non les drapeaux sont corrects
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private function createFlags($serialFlags){
|
|
|
|
/* [1] Verification des flags (version serialisee)
|
|
|
|
======================================================*/
|
|
|
|
|
|
|
|
$correct = true;
|
|
|
|
|
|
|
|
// Verification du nombre de drapeaux () au moins 3
|
|
|
|
$correct = $correct && count($serialFlags) >= 3;
|
|
|
|
|
|
|
|
// Verification que l'extension est correcte
|
2016-02-03 09:13:31 +00:00
|
|
|
$correct = $correct && array_key_exists($serialFlags[0], self::$supported_extensions);
|
2016-02-02 22:29:30 +00:00
|
|
|
|
|
|
|
// Verification du filename
|
2016-02-16 22:42:07 +00:00
|
|
|
$correct = $correct && preg_match('/^[\w_-]+$/i', $serialFlags[1]);
|
2016-02-02 22:29:30 +00:00
|
|
|
|
|
|
|
// Verification du parent
|
2016-02-03 09:13:31 +00:00
|
|
|
$correct = $correct && array_key_exists($serialFlags[2], self::$supported_parents);
|
2016-02-02 22:29:30 +00:00
|
|
|
|
|
|
|
// Verification du sous-parent (optionnel)
|
|
|
|
$opt_subParent = count($serialFlags) >= 4;
|
|
|
|
|
|
|
|
if( $opt_subParent )
|
2016-02-16 22:42:07 +00:00
|
|
|
$correct = $correct && preg_match('/^[\w_-]+$/i', $serialFlags[3]);
|
|
|
|
|
|
|
|
// Verification de la couleur (optionnel)
|
2016-02-16 23:15:47 +00:00
|
|
|
$opt_color = count($serialFlags) >= 5 && preg_match('/^[\da-f]{6}+$/i', $serialFlags[4]);
|
2016-02-02 22:29:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
if( !$correct )
|
|
|
|
return false;
|
|
|
|
|
2016-07-04 13:45:29 +00:00
|
|
|
|
|
|
|
|
2016-02-02 22:29:30 +00:00
|
|
|
/* [2] Creation (non serialisee) des flags
|
|
|
|
======================================================*/
|
|
|
|
// Si tout se deroule bien, on cree les flags
|
2016-07-04 13:45:29 +00:00
|
|
|
$this->flags = [
|
2016-02-02 22:29:30 +00:00
|
|
|
'extension' => $serialFlags[0],
|
|
|
|
'filename' => $serialFlags[1],
|
|
|
|
'parent' => $serialFlags[2]
|
2016-07-04 13:45:29 +00:00
|
|
|
];
|
2016-02-02 22:29:30 +00:00
|
|
|
|
|
|
|
// Ajout du sous-parent optionnel
|
|
|
|
if( $opt_subParent )
|
|
|
|
$this->flags['subparent'] = $serialFlags[3];
|
|
|
|
|
2016-02-16 22:42:07 +00:00
|
|
|
// Ajout du color optionnel
|
|
|
|
if( $opt_color )
|
2016-02-16 23:15:47 +00:00
|
|
|
$this->flags['color'] = '#'.$serialFlags[4];
|
2016-02-16 22:42:07 +00:00
|
|
|
|
2016-02-02 22:29:30 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* FONCTION QUI CONSTRUIT LE CHEMIN A PARTIR DU PATH
|
|
|
|
*
|
|
|
|
* @return fileExists<Boolean> Retourne si oui ou non le fichier cible existe
|
|
|
|
*
|
|
|
|
* @format
|
|
|
|
*
|
|
|
|
* f/extension/filename/parent/:subparent:/ (:OPT:)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private function buildPath(){
|
|
|
|
/* [1] On recupere le HEADER associe a l'extension
|
|
|
|
==========================================================*/
|
2016-02-03 09:13:31 +00:00
|
|
|
// Si aucun header pour cet cle, on retourne une erreur
|
|
|
|
if( !isset(self::$supported_extensions[$this->flags['extension']]) ) return false;
|
2016-02-02 22:29:30 +00:00
|
|
|
|
|
|
|
// On recupere le header associe
|
2016-02-03 09:13:31 +00:00
|
|
|
$header = self::$supported_extensions[$this->flags['extension']];
|
2016-02-02 22:29:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] On recupere le chemin associe au parent
|
|
|
|
==========================================================*/
|
|
|
|
// Si aucun dossier pour cet indice, on retourne une erreur
|
2016-02-03 09:13:31 +00:00
|
|
|
if( !isset(self::$supported_parents[$this->flags['parent']]) ) return false;
|
2016-02-02 22:29:30 +00:00
|
|
|
|
|
|
|
// On recupere le dossier associe
|
2016-02-03 09:13:31 +00:00
|
|
|
$parent = self::$supported_parents[$this->flags['parent']];
|
2016-02-02 22:29:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [3] Gestion du sous-parent optionnel
|
|
|
|
==========================================================*/
|
|
|
|
$opt_subParent = (isset($this->flags['subparent'])) ? $this->flags['subparent'].'/' : '';
|
|
|
|
|
|
|
|
|
2016-02-16 22:42:07 +00:00
|
|
|
/* [4] Gestion du color optionnel
|
|
|
|
=========================================================*/
|
|
|
|
$this->stylesheet = "";
|
|
|
|
|
|
|
|
// si le color est defini
|
|
|
|
if( isset($this->flags['color']) ){
|
|
|
|
$this->stylesheet = "\n<style type='text/css'>\n";
|
|
|
|
$this->stylesheet .= "\t#stylisable{\n";
|
|
|
|
$this->stylesheet .= "\t\tfill: ".$this->flags['color']." !important;\n";
|
|
|
|
$this->stylesheet .= "\t\tfill-opacity: 1 !important;\n";
|
|
|
|
$this->stylesheet .= "\t}\n";
|
|
|
|
$this->stylesheet .= "</style>";
|
|
|
|
}
|
2016-07-04 13:45:29 +00:00
|
|
|
|
2016-02-16 22:42:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [5] On definit le header
|
2016-02-02 22:29:30 +00:00
|
|
|
==========================================================*/
|
|
|
|
$this->header = $header;
|
|
|
|
|
2016-07-04 13:45:29 +00:00
|
|
|
/* [6] On construit le chemin
|
2016-02-02 22:29:30 +00:00
|
|
|
==========================================================*/
|
|
|
|
$this->path = __ROOT__.$parent.'/'.$opt_subParent.$this->flags['filename'].'.'.$this->flags['extension'];
|
|
|
|
|
2016-02-16 22:42:07 +00:00
|
|
|
/* [7] On retourne si le fichier existe ou non
|
2016-02-02 22:29:30 +00:00
|
|
|
==========================================================*/
|
2016-02-08 21:43:35 +00:00
|
|
|
return @file_get_contents( $this->path ) != false;
|
2016-02-02 22:29:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* FUNCTION QUI AFFICHE LA RESSOURCE EN QUESTION
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function view(){
|
2016-02-08 21:43:35 +00:00
|
|
|
// S'il y a eu une erreur en amont
|
|
|
|
if( $this->error != ManagerError::Success )
|
|
|
|
return false; // on retourne faux
|
|
|
|
|
2016-02-02 22:29:30 +00:00
|
|
|
// On definit le header
|
|
|
|
header('Content-Type: '.$this->header);
|
|
|
|
|
2016-02-16 23:15:47 +00:00
|
|
|
// On recupere le contenu
|
|
|
|
$svg_content = file_get_contents($this->path);
|
2016-02-16 22:42:07 +00:00
|
|
|
|
2016-02-16 23:15:47 +00:00
|
|
|
// On affiche tout
|
|
|
|
echo str_replace( '</svg>', $this->stylesheet.'</svg>', $svg_content );
|
2016-02-08 21:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* FUNCTION QUI RETOURNE LE CONTENU DE LA RESSOURCE EN QUESTION
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function getContent(){
|
|
|
|
// S'il y a eu une erreur en amont
|
|
|
|
if( $this->error != ManagerError::Success )
|
|
|
|
return false; // on retourne faux
|
|
|
|
|
|
|
|
|
|
|
|
// On inclut le contenu
|
2016-02-16 23:15:47 +00:00
|
|
|
$svg_content = file_get_contents($this->path);
|
2016-02-16 22:42:07 +00:00
|
|
|
|
2016-02-16 23:15:47 +00:00
|
|
|
// On retourne tout
|
|
|
|
return str_replace( '</svg>', $this->stylesheet.'</svg>', $svg_content );
|
2016-02-02 22:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-07-04 13:45:29 +00:00
|
|
|
?>
|