3 |
jpm |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Description :
|
|
|
4 |
* Classe principale de chargement des services d'eFlore.
|
|
|
5 |
*
|
|
|
6 |
* Encodage en entrée : utf8
|
|
|
7 |
* Encodage en sortie : utf8
|
|
|
8 |
* @package eflore-projets
|
|
|
9 |
* @author Jennifer DHÉ <jennifer.dhe@tela-botanica.org>
|
|
|
10 |
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
|
|
|
11 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
12 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
13 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
14 |
* @version 0.1
|
|
|
15 |
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
|
|
|
16 |
*/
|
|
|
17 |
class Projets extends RestService {
|
|
|
18 |
|
|
|
19 |
/** Contients les paramètres.*/
|
|
|
20 |
private $parametres = array();
|
|
|
21 |
/** Contients les ressources.*/
|
|
|
22 |
private $ressources = array();
|
|
|
23 |
/** Contients les erreurs HTTP levées par le service.*/
|
|
|
24 |
private $erreursHttp = array();
|
|
|
25 |
|
|
|
26 |
/** Nom du projet courrant. */
|
|
|
27 |
private $projetNom = array();
|
|
|
28 |
/** Nom du service demandé. */
|
|
|
29 |
private $serviceNom = array();
|
|
|
30 |
/** Chemin vers le dossier courrant. */
|
|
|
31 |
private $cheminCourrant = null;
|
|
|
32 |
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres brutes. */
|
|
|
33 |
protected $utilisationParametresBruts = true;
|
|
|
34 |
|
|
|
35 |
public function __construct() {
|
|
|
36 |
$this->cheminCourrant = dirname(__FILE__).DS;
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
public function consulter($ressources, $parametres) {
|
|
|
40 |
$resultat = '';
|
|
|
41 |
try {
|
|
|
42 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
|
|
43 |
$resultat = $this->traiterRessources();
|
|
|
44 |
} catch (Exception $e) {
|
|
|
45 |
$this->ajouterErreurHttp($e->getCode(), $e->getMessage());
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
$corps = $this->genererReponseHttp($resultat);
|
|
|
49 |
return $corps;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
private function initialiserRessourcesEtParametres($ressources, $parametres) {
|
|
|
53 |
$this->ressources = $ressources;
|
|
|
54 |
$this->parametres = $parametres;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
private function traiterRessources() {
|
|
|
58 |
$retour = '';
|
|
|
59 |
if ($this->avoirRessources()) {
|
|
|
60 |
if ($this->avoirRessourceProjet()) {
|
|
|
61 |
$this->initialiserProjet();
|
|
|
62 |
if ($this->avoirRessourceService()) {
|
|
|
63 |
$retour = $this->initialiserService();
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
return $retour;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
private function avoirRessources() {
|
|
|
71 |
$presenceDeRessources = false;
|
|
|
72 |
if (isset($this->ressources) && count($this->ressources) > 0) {
|
|
|
73 |
$presenceDeRessources = true;
|
|
|
74 |
} else {
|
|
|
75 |
$message = "Aucune ressource n'a été indiquée.\n".
|
|
|
76 |
"Veuillez indiquer au moins un code de projet et un type de service.";
|
|
|
77 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
|
|
78 |
throw new Exception($message, $code);
|
|
|
79 |
}
|
|
|
80 |
return $presenceDeRessources;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
private function avoirRessourceProjet() {
|
|
|
84 |
$presenceRessourceProjet = false;
|
|
|
85 |
$projet = $this->ressources[0];
|
|
|
86 |
$projetsDispo = Outils::recupererTableauConfig('projetsDispo');
|
|
|
87 |
if (in_array($projet, $projetsDispo)) {
|
|
|
88 |
$presenceRessourceProjet = true;
|
|
|
89 |
} else {
|
|
|
90 |
$message = "La ressource '$projet' n'indique pas un projet existant.\n".
|
|
|
91 |
"Les projets existant sont :\n".implode(', ', $projetsDispo);
|
|
|
92 |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
|
|
|
93 |
throw new Exception($message, $code);
|
|
|
94 |
}
|
|
|
95 |
return $presenceRessourceProjet;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
private function initialiserProjet() {
|
|
|
99 |
$this->chargerNomDuProjet();
|
|
|
100 |
$this->chargerConfigProjet();
|
|
|
101 |
// Enregistrement en première position des autoload de la méthode gérant les classes des services
|
|
|
102 |
spl_autoload_register(array($this, 'chargerClasseProjet'), true , true);
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
private function chargerNomDuProjet() {
|
|
|
106 |
$this->projetNom = $this->ressources[0];
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
private function chargerConfigProjet() {
|
|
|
110 |
$projet = $this->projetNom;
|
|
|
111 |
$chemin = Config::get('chemin_configurations')."config_$projet.ini";
|
|
|
112 |
Config::charger($chemin);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
private function chargerClasseProjet($classe) {
|
|
|
116 |
if (class_exists($classe)) {
|
|
|
117 |
return null;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
$chemins = array();
|
|
|
121 |
$chemins[] = $this->cheminCourrant.$this->projetNom.DS;
|
|
|
122 |
$chemins[] = $this->cheminCourrant.'commun'.DS;
|
|
|
123 |
$chemins[] = Config::get('chemin_bibliotheque');
|
|
|
124 |
|
|
|
125 |
foreach ($chemins as $chemin) {
|
|
|
126 |
$chemin = $chemin.$classe.'.php';
|
|
|
127 |
if (file_exists($chemin)) {
|
|
|
128 |
require_once $chemin;
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
private function avoirRessourceService() {
|
|
|
134 |
$presenceRessourceService = false;
|
|
|
135 |
$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
|
|
|
136 |
$service = $this->ressources[1];
|
|
|
137 |
if (in_array($service, $servicesDispo)) {
|
|
|
138 |
$presenceRessourceService = true;
|
|
|
139 |
} else {
|
|
|
140 |
$message = "La service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
|
|
|
141 |
"Les services disponibles sont : ".implode(', ', $servicesDispo);
|
|
|
142 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
143 |
throw new Exception($message, $code);
|
|
|
144 |
}
|
|
|
145 |
return $presenceRessourceService;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
private function initialiserService() {
|
|
|
149 |
$this->chargerNomDuService();
|
|
|
150 |
$classe = $this->obtenirNomClasse($this->serviceNom);
|
|
|
151 |
$chemins = array();
|
|
|
152 |
$chemins[] = $this->cheminCourrant.$this->projetNom.DS.$classe.'.php';
|
|
|
153 |
$chemins[] = $this->cheminCourrant.'commun'.DS.$classe.'.php';
|
|
|
154 |
|
|
|
155 |
$retour = '';
|
|
|
156 |
$service = null;
|
|
|
157 |
foreach ($chemins as $chemin) {
|
|
|
158 |
if (file_exists($chemin)) {
|
|
|
159 |
$service = new $classe($this->getBdd());
|
|
|
160 |
$ressourcesPourService = $this->filtrerRessourcesPourService();
|
|
|
161 |
$retour = $service->consulter($ressourcesPourService, $this->parametres);
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
if (is_null($service)) {
|
|
|
165 |
$message = "La service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
|
|
|
166 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
167 |
throw new Exception($message, $code);
|
|
|
168 |
}
|
|
|
169 |
return $retour;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
private function chargerNomDuService() {
|
|
|
173 |
$this->serviceNom = $this->ressources[1];
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
private function obtenirNomClasse($mot) {
|
|
|
177 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
178 |
return $classeNom;
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
private function filtrerRessourcesPourService() {
|
|
|
182 |
$ressourcesPourService = array();
|
|
|
183 |
$nbreDeRessources = count($this->ressources);
|
|
|
184 |
for ($i = 2; $i < $nbreDeRessources; $i++) {
|
|
|
185 |
$ressourcesPourService[] = $this->ressources[$i];
|
|
|
186 |
}
|
|
|
187 |
return $ressourcesPourService;
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
private function ajouterErreurHttp($entete, $message) {
|
|
|
191 |
$this->erreursHttp[] = array('entete' => $entete, 'message' => $message);
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
private function etreEnErreurHttp() {
|
|
|
195 |
$enErreur = false;
|
|
|
196 |
if (count($this->erreursHttp) > 0) {
|
|
|
197 |
$enErreur = true;
|
|
|
198 |
}
|
|
|
199 |
return $enErreur;
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
private function genererReponseHttp($resultat) {
|
|
|
203 |
$this->genererEnteteHttp();
|
|
|
204 |
$corps = $this->genererCorpsHttp($resultat);
|
|
|
205 |
return $corps;
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
private function genererEnteteHttp($mime = 'application/json', $encodage= 'utf-8') {
|
|
|
209 |
// Si aucune erreur n'a été établie (donc un tableau de résultat a bien ete renvoyé...)
|
|
|
210 |
if ($this->etreEnErreurHttp()) {
|
|
|
211 |
$this->emettreEntete($this->erreursHttp[0]['entete'], 'text/html', $encodage);
|
|
|
212 |
} else {
|
|
|
213 |
$this->emettreEntete(RestServeur::HTTP_CODE_OK, $mime, $encodage);
|
|
|
214 |
}
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
private function emettreEntete($entete, $mime, $encodage) {
|
|
|
218 |
// Gestion du type de contenu
|
|
|
219 |
if (!is_null($mime) && !is_null($encodage)) {
|
|
|
220 |
header("Content-Type: $mime; charset=$encodage");
|
|
|
221 |
} else if (!is_null($mime) && is_null($encodage)) {
|
|
|
222 |
header("Content-Type: $mime");
|
|
|
223 |
}
|
|
|
224 |
// Envoie de l'entête
|
|
|
225 |
RestServeur::envoyerEnteteStatutHttp($entete);
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
private function genererCorpsHttp($resultats) {
|
|
|
229 |
$corps = '';
|
|
|
230 |
if ($this->etreEnErreurHttp()) {
|
|
|
231 |
$corps = $this->erreursHttp[0]['message'];
|
|
|
232 |
} else {
|
|
|
233 |
$corps = json_encode($resultats);
|
|
|
234 |
}
|
|
|
235 |
return $corps;
|
|
|
236 |
}
|
|
|
237 |
}
|
|
|
238 |
?>
|