2017-12-10 19:33:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace api\core;
|
|
|
|
use \database\core\DatabaseDriver;
|
|
|
|
use \api\core\AuthSystem;
|
|
|
|
use \api\core\ModuleFactory;
|
2017-12-11 17:01:49 +00:00
|
|
|
use \api\core\Config;
|
2017-12-10 19:33:18 +00:00
|
|
|
use \error\core\Error;
|
|
|
|
use \error\core\Err;
|
|
|
|
|
|
|
|
|
|
|
|
class Request{
|
|
|
|
|
|
|
|
// Constantes
|
|
|
|
private static $default_options = [ 'download' => false ];
|
|
|
|
private static $authsystem = null;
|
|
|
|
|
|
|
|
|
|
|
|
// Attributs prives utiles (initialisation)
|
2017-12-11 11:15:05 +00:00
|
|
|
private $id; // chemin extrait de l'URI
|
2017-12-10 20:24:39 +00:00
|
|
|
private $raw_params; // paramètres reçus
|
|
|
|
private $params; // paramètres donnés à la fonction
|
2017-12-10 19:33:18 +00:00
|
|
|
private $options; // options
|
|
|
|
private $http_method; // methode HTTP appelante
|
|
|
|
|
|
|
|
// Contiendra la reponse a la requete
|
|
|
|
public $answer;
|
|
|
|
|
|
|
|
// Contiendra l'etat de la requete
|
|
|
|
public $error;
|
|
|
|
|
|
|
|
|
|
|
|
/* (0) Constructeur d'une requete de module
|
|
|
|
*
|
|
|
|
* @uri<String> URI relative de l'appel
|
|
|
|
* @param<Array> Tableau associatif contenant les parametres utiles au traitement
|
|
|
|
* @forced_method<String> Méthode demandée (optionnel)
|
|
|
|
*
|
|
|
|
* @return instance<Request> Instance crée
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
public function __construct($uri=null, $params=null, $forced_method=null){
|
2017-12-11 17:01:49 +00:00
|
|
|
debug();
|
2017-12-10 19:33:18 +00:00
|
|
|
return $this->buildRequestObject($uri, $params, $forced_method);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (1) Constructeur d'une requete de module (delegation)
|
|
|
|
*
|
|
|
|
* @uri<String> URI relative de l'appel
|
|
|
|
* @param<Array> Tableau associatif contenant les parametres utiles au traitement
|
|
|
|
* @forced_method<String> Méthode demandée (optionnel)
|
|
|
|
*
|
|
|
|
* @return status<Boolean> Retourne si oui ou non tout s'est bien passe
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
private function buildRequestObject($uri=null, $params=null, $forced_method=null){
|
|
|
|
|
|
|
|
/* (1) Initialisation
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Erreur par défaut */
|
|
|
|
$this->error = new Error(Err::Success);
|
|
|
|
|
|
|
|
/* (2) Si pas parametre manquant, on quitte */
|
2017-12-11 11:15:05 +00:00
|
|
|
if( is_null($uri) )
|
2017-12-10 19:33:18 +00:00
|
|
|
return $this->error->set(Err::MissingPath);
|
|
|
|
|
|
|
|
|
2017-12-11 11:15:05 +00:00
|
|
|
|
2017-12-11 17:01:49 +00:00
|
|
|
/* (2) On vérifie la configuration
|
2017-12-10 19:33:18 +00:00
|
|
|
---------------------------------------------------------*/
|
2017-12-11 17:01:49 +00:00
|
|
|
/* (1) Dispatch if error */
|
|
|
|
if( Config::get()->error->get() != Err::Success )
|
|
|
|
return ($this->error = Config::get()->error);
|
2017-12-10 19:33:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (3) Verification des types des parametres
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Si path est une <string> */
|
|
|
|
if( !is_string($uri) ) // Si le type est incorrect
|
|
|
|
return $this->error->set(Err::WrongPathModule);
|
|
|
|
|
2017-12-11 11:15:05 +00:00
|
|
|
/* (2) Add slash at the beginning of URI */
|
|
|
|
if( !preg_match('@^\/@', $uri) )
|
|
|
|
$uri = "/$uri";
|
|
|
|
|
|
|
|
/* (3) Formattage @params en tableau */
|
2017-12-10 20:24:39 +00:00
|
|
|
$this->raw_params = (is_array($params)) ? $params : [];
|
2017-12-10 19:33:18 +00:00
|
|
|
|
2017-12-11 11:15:05 +00:00
|
|
|
/* (4) On définit en constante la méthode HTTP */
|
2017-12-10 19:33:18 +00:00
|
|
|
if( !isset($_SERVER['REQUEST_METHOD']) && !is_string($forced_method) )
|
|
|
|
return $this->error->set(Err::UnknownHttpMethod);
|
|
|
|
|
|
|
|
$this->http_method = is_string($forced_method) ? strtoupper($forced_method) : strtoupper($_SERVER['REQUEST_METHOD']);
|
|
|
|
|
|
|
|
|
|
|
|
/* (4) Verification du chemin (existence module+methode)
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if( !$this->checkURI($uri) ) // Verification de la coherence du chemin + attribution
|
|
|
|
return false; // checkURI() sets the error itself
|
|
|
|
|
|
|
|
|
|
|
|
/* (5) Verification des permissions
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if( !$this->checkPermission() ) // Si on a pas les droits
|
|
|
|
return false; // checkPermission() sets the error itself
|
|
|
|
|
|
|
|
|
|
|
|
/* (6) Verification des parametres (si @type est defini)
|
|
|
|
---------------------------------------------------------*/
|
2017-12-10 20:24:39 +00:00
|
|
|
if( !$this->checkParams() ) // Verification de tous les types
|
2017-12-10 19:33:18 +00:00
|
|
|
return false; // checkParams() sets the error itself
|
|
|
|
|
|
|
|
|
|
|
|
/* (7) Récupèration des options
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
$this->buildOptions();
|
|
|
|
|
|
|
|
|
|
|
|
/* (8) Construction de l'objet (add http method to params)
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
$this->params['HTTP_METHOD'] = $this->http_method;
|
|
|
|
$this->error->set(Err::Success);
|
|
|
|
|
|
|
|
return true; // On retourne que tout s'est bien passe
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Definit le systeme d'authentification
|
|
|
|
*
|
|
|
|
* @instance<AuthSystem> Instance de type AuthSystem
|
|
|
|
*
|
|
|
|
* @return success<Boolean> Whether the AuthSystem is valid or not
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
public static function setAuthSystem($instance=null){
|
|
|
|
/* (1) Check instance type */
|
|
|
|
if( !($instance instanceof AuthSystem) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (2) Store instance */
|
|
|
|
self::$authsystem = $instance;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 17:01:49 +00:00
|
|
|
/* (3) Verification du format et de la coherence du chemin specifie
|
2017-12-10 19:33:18 +00:00
|
|
|
*
|
2017-12-11 11:15:05 +00:00
|
|
|
* @URI<String> URI d'appel (commence par /)
|
2017-12-10 19:33:18 +00:00
|
|
|
*
|
|
|
|
* @return validity<Boolean> Retourne si oui ou non l'objet est correct
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
private function checkURI($uri){
|
|
|
|
|
|
|
|
/* (1) Verification format general
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) If wrong format -> exit */
|
2017-12-11 11:15:05 +00:00
|
|
|
if( !preg_match('@^\/[\w-]+(\/[\w-]+)*\/?$@', $uri, $matches) && $uri != '/' )
|
2017-12-10 19:33:18 +00:00
|
|
|
return $this->error->set(Err::WrongPathModule);
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Verification de l'existence du chemin (conf)
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Check if begins with each indexed @path */
|
|
|
|
$exists_size = 0;
|
|
|
|
$path = null;
|
|
|
|
|
2017-12-11 17:01:49 +00:00
|
|
|
foreach(Config::get()->index as $key=>$void){
|
2017-12-10 19:33:18 +00:00
|
|
|
$match_size = strlen($key);
|
|
|
|
|
|
|
|
/* (1.1) Look for the longer match */
|
|
|
|
if( $match_size > $exists_size && substr($uri, 0, $match_size) == $key ){
|
|
|
|
$exists_size = $match_size;
|
|
|
|
$path = $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) If @path not found -> exit */
|
|
|
|
if( is_null($path) )
|
|
|
|
return $this->error->set(Err::UnknownModule);
|
|
|
|
|
|
|
|
|
2017-12-10 20:24:39 +00:00
|
|
|
/* (3) Extract URI parameters
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Extract URI string after @path */
|
|
|
|
$uri_end = substr($uri, $exists_size);
|
|
|
|
|
|
|
|
/* (2) If invalid format, return error */
|
|
|
|
if( !preg_match('@^((?:\/[^\/]+)*)\/?$@', $uri_end, $uri_match) )
|
|
|
|
return $this->error->set(Err::InvalidURI);
|
|
|
|
|
|
|
|
/* (3) Add each URI parameter to the parameter store */
|
|
|
|
$uri_args = array_slice( explode('/', $uri_match[1]), 1);
|
|
|
|
|
|
|
|
foreach($uri_args as $index=>$value)
|
|
|
|
$this->raw_params["URL$index"] = $value;
|
|
|
|
|
|
|
|
/* (4) Verification de l'existence de la methode (conf)
|
2017-12-10 19:33:18 +00:00
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Check if HTTP method is in allowed methods */
|
2017-12-11 17:01:49 +00:00
|
|
|
if( !in_array($this->http_method, Config::$allowed_http_methods) )
|
2017-12-10 19:33:18 +00:00
|
|
|
return $this->error->set(Err::UnknownHttpMethod, $this->http_method);
|
|
|
|
|
|
|
|
/* (2) Check if HTTP method is defined for this @path */
|
2017-12-11 17:01:49 +00:00
|
|
|
if( !isset(Config::get()->index[$path][$this->http_method]) )
|
2017-12-10 19:33:18 +00:00
|
|
|
return $this->error->set(Err::UnknownMethod, $this->http_method);
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-10 20:24:39 +00:00
|
|
|
/* (5) Enregistrement du chemin et renvoi de SUCCESS
|
2017-12-10 19:33:18 +00:00
|
|
|
---------------------------------------------------------*/
|
2017-12-11 11:15:05 +00:00
|
|
|
$this->id = [
|
2017-12-10 19:33:18 +00:00
|
|
|
'path'=> $path,
|
|
|
|
'method'=> $this->http_method
|
|
|
|
];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 17:01:49 +00:00
|
|
|
/* (4) Retourne si on a la permission d'executer cette methode
|
2017-12-10 19:33:18 +00:00
|
|
|
*
|
|
|
|
* @return permission<bool> Retourne si on a les droits ou pas pour executer cette methode
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
private function checkPermission(){
|
|
|
|
|
|
|
|
/* (1) On recupere les informations utiles
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
// On recupere le nom de la methode
|
2017-12-11 17:01:49 +00:00
|
|
|
$method = Config::get()->index[$this->id['path']][$this->id['method']];
|
2017-12-10 19:33:18 +00:00
|
|
|
|
|
|
|
// Si aucune permission n'est definie
|
|
|
|
if( !isset($method['permissions']) || !is_array($method['permissions']) || count($method['permissions']) < 1 )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* (2) Vérification des permissions et de l'authentification
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
// if no AuthSystem set up, use the default one
|
|
|
|
if( !is_object(self::$authsystem) || !self::$authsystem instanceof AuthSystem ){
|
|
|
|
|
|
|
|
// try to load default AuthSystem
|
|
|
|
if( !file_exists(__BUILD__.'/api/core/AuthSystemDefault.php') )
|
|
|
|
return $this->error->set(Err::UnreachableResource);
|
|
|
|
|
|
|
|
// load default AuthSystem class
|
|
|
|
$classname = '\\api\\core\\AuthSystemDefault';
|
|
|
|
self::$authsystem = new $classname();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check permission using user-implemented AuthSystem
|
|
|
|
$granted = self::$authsystem::permission( $method['permissions'] );
|
|
|
|
|
|
|
|
/* (1) On retourne FAUX si aucun droit n'a ete trouve */
|
|
|
|
if( $granted->get() !== Err::Success ){
|
|
|
|
$this->error = $granted;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* On retourne VRAI si la permission est ok */
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 17:01:49 +00:00
|
|
|
/* (5) Verification du type des parametres envoyes
|
2017-12-10 19:33:18 +00:00
|
|
|
*
|
|
|
|
* @return correct<bool> Retourne si oui ou non les parametres ont le bon type
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2017-12-10 20:24:39 +00:00
|
|
|
private function checkParams(){
|
2017-12-10 19:33:18 +00:00
|
|
|
|
|
|
|
/* (1) On verifie qu'il ne manque aucun parametre
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Si @params n'est pas un tableau */
|
2017-12-10 20:24:39 +00:00
|
|
|
if( !is_array($this->raw_params) )
|
2017-12-10 19:33:18 +00:00
|
|
|
return $this->error->set(Err::MissingParam);
|
|
|
|
|
|
|
|
/* (2) On récupère les données de la méthode */
|
2017-12-11 17:01:49 +00:00
|
|
|
$method = Config::get()->index[$this->id['path']][$this->id['method']];
|
2017-12-10 19:33:18 +00:00
|
|
|
|
|
|
|
/* (3) Si pas 'parameters' dans la config */
|
|
|
|
if( !isset($method['parameters']) || !is_array($method['parameters']) )
|
|
|
|
return $this->error->set(Err::ConfigError);
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Si le type est defini, pour chaque param, on teste
|
|
|
|
---------------------------------------------------------*/
|
2017-12-10 20:24:39 +00:00
|
|
|
foreach($method['parameters'] as $name=>$config){
|
2017-12-10 19:33:18 +00:00
|
|
|
|
|
|
|
/* (2.1) Vérification des données
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Si @name n'est pas une string */
|
|
|
|
if( !is_string($name) )
|
|
|
|
return $this->error->set(Err::ConfigError);
|
|
|
|
|
2017-12-10 20:24:39 +00:00
|
|
|
/* (2) Si @config n'est pas un tableau */
|
|
|
|
if( !is_array($config) )
|
2017-12-10 19:33:18 +00:00
|
|
|
return $this->error->set(Err::ConfigError);
|
|
|
|
|
2017-12-10 20:24:39 +00:00
|
|
|
/* (3) So @config['type] manquant ou incorrect */
|
|
|
|
if( !isset($config['type']) || !is_string($config['type']) )
|
2017-12-10 19:33:18 +00:00
|
|
|
return $this->error->set(Err::ConfigError);
|
|
|
|
|
|
|
|
|
|
|
|
/* (2.2) Gestion des spécifications
|
|
|
|
---------------------------------------------------------*/
|
2017-12-10 20:24:39 +00:00
|
|
|
/* (1) On récupère le paramètre RENAME */
|
|
|
|
$rename = $name;
|
|
|
|
if( isset($config['rename']) && is_string($config['rename']) && preg_match('@^\w+$@', $config['rename']) )
|
|
|
|
$rename = $config['rename'];
|
|
|
|
|
2017-12-10 19:33:18 +00:00
|
|
|
/* (1) On récupère si le paramètre est optionnel ou pas */
|
2017-12-10 20:24:39 +00:00
|
|
|
$optional = isset($config['optional']) && $config['optional'] === true;
|
2017-12-10 19:33:18 +00:00
|
|
|
|
|
|
|
/* (2) Si de type 'FILE' + fichier existe => on enregistre la ref. */
|
2017-12-10 20:24:39 +00:00
|
|
|
if( $config['type'] == 'FILE' && isset($_FILES[$name]) )
|
|
|
|
$this->params[$rename] = &$_FILES[$name];
|
2017-12-10 19:33:18 +00:00
|
|
|
|
|
|
|
/* (3) Si param obligatoire et manquant -> erreur */
|
2017-12-10 20:24:39 +00:00
|
|
|
if( !isset($this->raw_params[$name]) && !$optional )
|
2017-12-10 19:33:18 +00:00
|
|
|
return $this->error->set(Err::MissingParam, $name);
|
|
|
|
|
|
|
|
|
|
|
|
/* (2.3) Gestion des valeurs
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Si le paramètre est optionnel et manquant */
|
2017-12-10 20:24:39 +00:00
|
|
|
if( $optional && !isset($this->raw_params[$name]) ){
|
2017-12-10 19:33:18 +00:00
|
|
|
|
|
|
|
// On le crée le param optionnel avec la valeur NULL
|
2017-12-10 20:24:39 +00:00
|
|
|
$this->params[$rename] = null;
|
2017-12-10 19:33:18 +00:00
|
|
|
|
|
|
|
/* (2) Si le paramètre est renseigné (sauf FILE) */
|
2017-12-10 20:24:39 +00:00
|
|
|
}elseif( $config['type'] != 'FILE'){
|
2017-12-10 19:33:18 +00:00
|
|
|
|
|
|
|
// Si la verification est fausse, on retourne faux
|
2017-12-10 20:24:39 +00:00
|
|
|
if( !Checker::run($config['type'], $this->raw_params[$name]) )
|
|
|
|
return $this->error->set(Err::WrongParam, $name, $config['type']);
|
|
|
|
|
|
|
|
// Sinon, on ajoute aux params qu'on enverra à l'appel
|
|
|
|
else
|
|
|
|
$this->params[$rename] = $this->raw_params[$name];
|
2017-12-10 19:33:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) Gestion du retour, si tout s'est bien passe
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 17:01:49 +00:00
|
|
|
/* (6) Ajout des options a partir de la configuration
|
2017-12-10 19:33:18 +00:00
|
|
|
*
|
|
|
|
* @return correct<bool> Retourne FAUS en cas d'erreur
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
private function buildOptions(){
|
|
|
|
|
|
|
|
/* (1) On récupère les options de la méthode en cours
|
|
|
|
---------------------------------------------------------*/
|
2017-12-11 17:01:49 +00:00
|
|
|
$method = Config::get()->index[$this->id['path']][$this->id['method']];
|
2017-12-10 19:33:18 +00:00
|
|
|
|
|
|
|
/* (1) Si 'option' n'est pas défini (ou incorrect), on met les valeurs par défaut */
|
|
|
|
if( !isset($method['options']) || !is_array($method['options']) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* (2) Par défaut on définit les options par défaut */
|
|
|
|
$this->options = self::$default_options;
|
|
|
|
|
|
|
|
/* (3) On récupère les options données */
|
|
|
|
$options = $method['options'];
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Gestion des différentes options
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
foreach($options as $option=>$value){
|
|
|
|
|
|
|
|
/* (1) On ne prend en compte l'option que si elle est dans les options par défaut */
|
|
|
|
if( !isset(self::$default_options[$option]) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* (2) Le type de la valeur doit être le même que celui de la valeur par défaut */
|
|
|
|
if( gettype($value) != gettype(self::$default_options[$option]) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* (3) Si tout est bon, on définit la valeur */
|
|
|
|
|
|
|
|
$this->options[$option] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 17:01:49 +00:00
|
|
|
/* (7) Execute le traitement associe et remplie la reponse
|
2017-12-10 19:33:18 +00:00
|
|
|
*
|
|
|
|
* @return answer<Response> Retourne une reponse de type <Response> si tout s'est bien passe
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
public function dispatch(){
|
|
|
|
|
|
|
|
/* (1) On verifie qu'aucune erreur n'a ete signalee
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if( $this->error->get() !== Err::Success ) // si il y a une erreur
|
|
|
|
return new Response($this->error); // on la passe a la reponse
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) On essaie d'instancier le module
|
|
|
|
---------------------------------------------------------*/
|
2017-12-11 11:15:05 +00:00
|
|
|
$instance = ModuleFactory::getModule($this->id['path']);
|
2017-12-10 19:33:18 +00:00
|
|
|
|
|
|
|
if( $instance instanceof Error ){
|
2017-12-11 11:15:05 +00:00
|
|
|
$this->error->set(Err::UncallableModule, $this->id['path']);
|
2017-12-10 19:33:18 +00:00
|
|
|
return new Response($this->error);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) On verifie que la methode est amorcable
|
|
|
|
---------------------------------------------------------*/
|
2017-12-11 11:15:05 +00:00
|
|
|
if( !is_callable([$instance, $this->id['method']]) ){
|
|
|
|
$this->error->set(Err::UncallableMethod, $this->id['method']);
|
2017-12-10 19:33:18 +00:00
|
|
|
return new Response($this->error);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (4) On amorce la methode
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) On lance la fonction */
|
2017-12-11 11:15:05 +00:00
|
|
|
$returned = call_user_func( [$instance, $this->id['method']], $this->params );
|
2017-12-10 19:33:18 +00:00
|
|
|
|
|
|
|
/* (2) On appelle le destructeur (si défini) */
|
|
|
|
$instance = null;
|
|
|
|
|
|
|
|
|
|
|
|
/* (5) Gestion de la reponse
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) S'il s'agit d'un téléchargement -> on dispatch */
|
|
|
|
if( $this->options['download'] === true )
|
|
|
|
return $this->download();
|
|
|
|
|
|
|
|
/* (2) On construit la réponse avec l'erreur */
|
|
|
|
$response = new Response($this->error);
|
|
|
|
|
|
|
|
/* (3) On ajoute les données */
|
|
|
|
$response->appendAll($returned);
|
|
|
|
|
|
|
|
/* (4) On retourne la réponse */
|
|
|
|
return $response;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-11 17:01:49 +00:00
|
|
|
/* (8) Gestion d'un téléchargement HTTP
|
2017-12-10 19:33:18 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function download(){
|
|
|
|
|
|
|
|
/* (1) Vérification des erreurs et paramètres
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Si retourne 'error' et n'est pas SUCCESS -> error */
|
|
|
|
if( isset($returned['error']) && $returned['error'] instanceof Error && $returned['error']->get() != Err::Success ){
|
|
|
|
$this->error = $returned['error'];
|
|
|
|
return new Response($this->error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Vérification du contenu, si pas défini */
|
|
|
|
if( !isset($returned['body']) ){
|
|
|
|
$this->error->set(Err::MissingBody);
|
|
|
|
return new Response($this->error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Si @headers n'est pas défini on met par défaut */
|
|
|
|
if( !isset($returned['headers']) || !is_array($returned['headers']) ){
|
|
|
|
$this->error->set(Err::MissingHeaders);
|
|
|
|
return new Response($this->error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Détermine si téléchargement AJAX/DIRECT */
|
|
|
|
$from_ajax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Gestion du téléchargement direct (HTTP)
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if( !$from_ajax ){
|
|
|
|
|
|
|
|
/* (1) On définit les headers */
|
|
|
|
foreach($returned['headers'] as $header=>$value)
|
|
|
|
header($header.': '.$value);
|
|
|
|
|
|
|
|
/* (2) On affiche le contenu */
|
|
|
|
echo $returned['body'];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) Gestion du téléchargement différé (AJAX)
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) On génère les noms de fichiers utiles */
|
|
|
|
$target_fname = '/tmp/download_'.uniqid().'.php'; // cible
|
|
|
|
$buffer_fname = __ROOT__.'/tmp/content_'.uniqid().'.php'; // buffer
|
|
|
|
|
|
|
|
/* (2) On écrit le BODY dans un fichier buffer */
|
|
|
|
$buffer_file = fopen($buffer_fname, 'w');
|
|
|
|
fwrite($buffer_file, $returned['body']);
|
|
|
|
fclose($buffer_file);
|
|
|
|
|
|
|
|
/* (3) On crée le fichier cible */
|
|
|
|
$target_fnameroot = __PUBLIC__.$target_fname;
|
|
|
|
$taret_file = fopen($target_fnameroot, 'w');
|
|
|
|
fwrite($taret_file, '<?php'.PHP_EOL);
|
|
|
|
|
|
|
|
|
|
|
|
/* (4) Script qui écrira les headers */
|
|
|
|
foreach($returned['headers'] as $header=>$value)
|
|
|
|
fwrite($taret_file, "header(\"$header: $value\");".PHP_EOL);
|
|
|
|
|
|
|
|
/* (5) Script qui écrira le contenu du buffer */
|
|
|
|
chmod($buffer_fname, 0775);
|
|
|
|
fwrite($taret_file, "readfile('$buffer_fname');".PHP_EOL);
|
|
|
|
|
|
|
|
/* (6) Script qui supprimera les fichiers: buffer+target */
|
|
|
|
fwrite($taret_file, "unlink('$buffer_fname');".PHP_EOL);
|
|
|
|
fwrite($taret_file, "unlink(__FILE__);".PHP_EOL);
|
|
|
|
fwrite($taret_file, '?>'.PHP_EOL);
|
|
|
|
|
|
|
|
/* (7) On ferme le fichier cible */
|
|
|
|
fclose($taret_file);
|
|
|
|
chmod($target_fnameroot, 0775);
|
|
|
|
|
|
|
|
/* (8) On envoie la réponse contenant le lien du fichier cible */
|
|
|
|
$response = new Response($this->error);
|
|
|
|
$response->append('link', $target_fname);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|