From 6a8c024255b332e27e0a0b0c9b95357da64cb5c7 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 12 May 2016 23:29:25 +0200 Subject: [PATCH] =?UTF-8?q?Gestion=20=E2=80=8B=5Fautomatique=5F=E2=80=8B?= =?UTF-8?q?=20du=20download=20quand=20on=20ajoute=20l'option=20'options=20?= =?UTF-8?q?:=20{=20download:=20true=20}'=20dans=20'modules.json'=20(on=20p?= =?UTF-8?q?eut=20utiliser=20dispatch(),=20il=20effectuera=20le=20download?= =?UTF-8?q?=20automatiquement=20si=20l'option=20'download'=20vaut=20'true'?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- automate.php | 2 +- config/modules.json | 1 + manager/ModuleRequest.php | 120 +++++++++++++++++++++++++++++++++++- manager/module/download.php | 19 +++--- 4 files changed, 132 insertions(+), 10 deletions(-) diff --git a/automate.php b/automate.php index d288f1c..3fadbb8 100755 --- a/automate.php +++ b/automate.php @@ -86,7 +86,7 @@ - + // Lancement du téléchargement $req = new ModuleRequest('download/phone', array('subjects'=>[1])); $res = $req->dispatch(); diff --git a/config/modules.json b/config/modules.json index f51a22a..3f22848 100755 --- a/config/modules.json +++ b/config/modules.json @@ -239,6 +239,7 @@ "phone": { "description": "Download des données relatives à une enquête téléphonique.", "permissions": ["admin"], + "options": { "download": true }, "parameters": { "subjects": { "description": "Identifiants des sujets à intégrer.", "type": "array" } } diff --git a/manager/ModuleRequest.php b/manager/ModuleRequest.php index 3c93ceb..c2c5889 100755 --- a/manager/ModuleRequest.php +++ b/manager/ModuleRequest.php @@ -8,11 +8,15 @@ // Constantes public static $config_path = 'f/json/modules/conf'; + public static $default_options = array( + 'download' => false + ); // Attributs prives utiles (initialisation) private $path; private $params; private $modules; + private $options; // Contiendra la reponse a la requete public $answer; @@ -85,7 +89,12 @@ return false; } - /* [5] Construction de l'objet + /* [5] Récupèration des options + =========================================================*/ + $this->buildOptions(); + + + /* [6] Construction de l'objet =========================================================*/ $this->params = $params; $this->error = ManagerError::Success; @@ -102,6 +111,11 @@ * */ public function dispatch(){ + /* [0] Si c'est un download, on lance la methode `download()` + =========================================================*/ + if( $this->options['download'] === true ) + return $this->download(); + /* [1] On verifie qu'aucune erreur n'a ete signalee =========================================================*/ if( $this->error != ManagerError::Success ) // si il y a une erreur @@ -134,6 +148,67 @@ + + + + /* EXECUTE LE TRAITEMENT ASSOCIE ET RENVOIE UN FICHIER AVEC LE HEADER ET LE BODY SPECIFIE + * + */ + public function download(){ + /* [1] On verifie qu'aucune erreur n'a ete signalee + =========================================================*/ + if( $this->error != ManagerError::Success ) // si il y a une erreur + return new ModuleResponse($this->error); // on la passe a la reponse + + + /* [2] On verifie que la methode est amorcable + =========================================================*/ + if( !is_callable($this->getFunctionCaller()) ){ + $this->error = ManagerError::UncallableMethod; + return new ModuleResponse($this->error); + } + + + /* [3] On amorce la methode + =========================================================*/ + $returned = call_user_func( $this->getFunctionCaller(), $this->params ); + + + /* [4] Vérification des erreurs et paramètres + =========================================================*/ + /* (1) Vérification de l'erreur retournée, si pas Success, on retourne l'erreur */ + if( isset($returned['ModuleError']) && $returned['ModuleError'] != ManagerError::Success ){ + $this->error = $returned['ModuleError']; + return new ModuleResponse($this->error); + } + + /* (2) Vérification du contenu, si pas défini */ + if( !isset($returned['body']) ){ + $this->error = ManagerError::ParamError; + return new ModuleResponse($this->error); + } + + /* (3) Si @headers n'est pas défini on met par défaut */ + if( !isset($returned['headers']) || !is_array($returned['headers']) ) + $returned['headers'] = array(); + + /* [5] Gestion du download + =========================================================*/ + /* (1) On définit les headers */ + foreach($returned['headers'] as $header=>$value) + header($header.': '.$value); + + /* (2) On affiche le contenu */ + echo $returned['body']; + + return false; + } + + + + + + /* DESERIALISATION ET CREATION D'UN OBJET * * @jsonString Json au format string contenant les donnees @@ -388,6 +463,49 @@ + /* AJOUT DES OPTIONS A PARTIR DE LA CONFIGURATION + * + */ + private function buildOptions(){ + /* [0] On récupère les options de la méthode en cours + =========================================================*/ + $method = $this->modules[$this->path['module']][$this->path['method']]; + + /* (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']; + + + /* [1] 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; + + } + + + + + /* RENVOI LE CHEMIN D'AMORCAGE DE LA METHODE * * @return path Retourne le chemin d'amorcage de la requete diff --git a/manager/module/download.php b/manager/module/download.php index 72d7b20..d4063e5 100644 --- a/manager/module/download.php +++ b/manager/module/download.php @@ -15,7 +15,6 @@ // TODO: Implémenter proprement en utilisant le système de retour + utilisant la liste de sujets dans les paramètres - $file_name = sessionManager::sha1($subjects[0]); $phone_log = json_decode( file_get_contents(__ROOT__.'/src/upload/phone_storage/'.$file_name.'.json'), true ); @@ -102,15 +101,19 @@ $zip->close(); + /* [4] On lance le téléchargement =========================================================*/ - header("Content-type: application/zip"); - header("Content-Disposition: attachment; filename=phone_data.zip"); - header("Pragma: no-cache"); - header("Expires: 0"); - - readfile($fname); - exit(); + return array( + 'ModuleError' => ManagerError::Success, + 'headers' => array( + 'Content-Type' => 'application/zip', + 'Content-Disposition' => 'attachment; filename=phone_data.zip', + 'Pragma' => 'no-cache', + 'Expires' => '0' + ), + 'body' => file_get_contents($fname) + ); } }