702 |
gduche |
1 |
<?php
|
|
|
2 |
/**
|
1604 |
jpm |
3 |
* Classe principale du service Images de DEL.
|
702 |
gduche |
4 |
*
|
1604 |
jpm |
5 |
* Cette classe se charge toujours de :
|
|
|
6 |
* - vérifier l'existance des ressources (services) demandés
|
|
|
7 |
* - vérifier la cohérence et le format des paramêtres passées dans l'url
|
|
|
8 |
* En fonction, de la compléxité du service, elle peut :
|
|
|
9 |
* - dans un premier temps, exécuter directement les actions : consulter, ajouter, modifier, supprimer.
|
|
|
10 |
* - dans un second temps, charger dynamiquement d'éventuelles sous-classes du service en fonction des ressources présentes dans l'URL.
|
|
|
11 |
*
|
702 |
gduche |
12 |
* Encodage en entrée : utf8
|
|
|
13 |
* Encodage en sortie : utf8
|
1604 |
jpm |
14 |
*
|
|
|
15 |
* @category DEL
|
|
|
16 |
* @package Images
|
|
|
17 |
* @version 0.1
|
702 |
gduche |
18 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
19 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
20 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
21 |
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
|
|
|
22 |
*/
|
1604 |
jpm |
23 |
restore_error_handler();
|
|
|
24 |
restore_exception_handler();
|
|
|
25 |
error_reporting(E_ALL);
|
746 |
gduche |
26 |
class Images extends RestService {
|
702 |
gduche |
27 |
private $parametres = array();
|
|
|
28 |
private $ressources = array();
|
782 |
delphine |
29 |
private $methode = null;
|
1604 |
jpm |
30 |
private $serviceNom = 'images';
|
|
|
31 |
private $sousServiceNom = null;
|
718 |
gduche |
32 |
private $cheminCourant = null;
|
|
|
33 |
|
|
|
34 |
private $conteneur;
|
1604 |
jpm |
35 |
|
718 |
gduche |
36 |
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
|
702 |
gduche |
37 |
protected $utilisationParametresBruts = true;
|
|
|
38 |
|
|
|
39 |
public function __construct() {
|
718 |
gduche |
40 |
$this->cheminCourant = dirname(__FILE__).DS;
|
702 |
gduche |
41 |
}
|
|
|
42 |
|
|
|
43 |
public function consulter($ressources, $parametres) {
|
782 |
delphine |
44 |
$this->methode = 'consulter';
|
1604 |
jpm |
45 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
|
|
46 |
return $this->executerService();
|
|
|
47 |
}
|
|
|
48 |
|
1623 |
mathias |
49 |
public function ajouter($ressources, $requeteDonnees) {
|
|
|
50 |
$this->methode = 'ajouter';
|
|
|
51 |
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
|
|
|
52 |
return $this->executerService();
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
public function modifier($ressources, $requeteDonnees) {
|
1631 |
jpm |
56 |
$this->methode = 'modifier';
|
1623 |
mathias |
57 |
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
|
|
|
58 |
return $this->executerService();
|
|
|
59 |
}
|
|
|
60 |
|
1604 |
jpm |
61 |
public function supprimer($ressources) {
|
|
|
62 |
$this->methode = 'supprimer';
|
|
|
63 |
$this->initialiserRessourcesEtParametres($ressources);
|
|
|
64 |
return $this->executerService();
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
|
|
|
68 |
$this->ressources = $ressources;
|
|
|
69 |
$this->parametres = $parametres;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
private function executerService() {
|
702 |
gduche |
73 |
$resultat = '';
|
|
|
74 |
$reponseHttp = new ReponseHttp();
|
|
|
75 |
try {
|
718 |
gduche |
76 |
$this->conteneur = new Conteneur($this->parametres);
|
702 |
gduche |
77 |
$resultat = $this->traiterRessources();
|
|
|
78 |
$reponseHttp->setResultatService($resultat);
|
|
|
79 |
} catch (Exception $e) {
|
|
|
80 |
$reponseHttp->ajouterErreur($e);
|
|
|
81 |
}
|
|
|
82 |
$reponseHttp->emettreLesEntetes();
|
|
|
83 |
$corps = $reponseHttp->getCorps();
|
|
|
84 |
return $corps;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
private function traiterRessources() {
|
1604 |
jpm |
88 |
$this->chargerConfigService();
|
|
|
89 |
$this->analyserRessources();
|
|
|
90 |
$retour = $this->initialiserService();
|
702 |
gduche |
91 |
return $retour;
|
|
|
92 |
}
|
758 |
delphine |
93 |
|
1604 |
jpm |
94 |
private function chargerConfigService() {
|
|
|
95 |
$chemin = Config::get('chemin_configurations')."config_{$this->serviceNom}.ini";
|
|
|
96 |
Config::charger($chemin);
|
702 |
gduche |
97 |
}
|
|
|
98 |
|
1604 |
jpm |
99 |
/**
|
1631 |
jpm |
100 |
* URLs possibles :
|
|
|
101 |
*
|
|
|
102 |
* GET :
|
1604 |
jpm |
103 |
* http://localhost/del/services/0.1/images/ => toutes les images : classe ListeImages
|
|
|
104 |
* http://localhost/del/services/0.1/images/#id/votes => tous les votes d'une image classés par protocole : classe VotesImage
|
|
|
105 |
*
|
|
|
106 |
* Non Implémenté : http://localhost/del/services/0.1/images/#id => une image donnée => en test pour remplacer les appels à eflore/cel
|
|
|
107 |
* Non Implémenté : http://localhost/del/services/0.1/images/#id/votes?protocole=#id => tous les votes d'une image et d'un protocole donné
|
|
|
108 |
*
|
1631 |
jpm |
109 |
* PUT :
|
|
|
110 |
* http://localhost/del/services/0.1/images/#id => ajouter un vote sur une image donnée
|
|
|
111 |
*
|
|
|
112 |
* POST :
|
|
|
113 |
* http://localhost/del/services/0.1/images/#id => modifier un vote sur une image donnée
|
|
|
114 |
*
|
|
|
115 |
* DELETE :
|
1604 |
jpm |
116 |
* http://localhost/del/services/0.1/images/#id => supprimer une image donnée
|
|
|
117 |
*/
|
|
|
118 |
private function analyserRessources() {
|
1631 |
jpm |
119 |
if ($this->methode == 'consulter') {
|
|
|
120 |
$this->analyserRessoucesConsultation();
|
|
|
121 |
} else if ($this->methode == 'modifier' || $this->methode == 'ajouter') {
|
|
|
122 |
$this->analyserRessoucesModification();
|
1604 |
jpm |
123 |
} else if ($this->methode == 'supprimer') {
|
1631 |
jpm |
124 |
$this->analyserRessoucesSuppression();
|
1604 |
jpm |
125 |
}
|
702 |
gduche |
126 |
}
|
|
|
127 |
|
1631 |
jpm |
128 |
private function analyserRessoucesConsultation() {
|
1604 |
jpm |
129 |
if (count($this->ressources) == 0) {
|
|
|
130 |
$this->sousServiceNom = 'liste-images';
|
|
|
131 |
} else if (count($this->ressources) == 2) {
|
|
|
132 |
if ($this->etreRessourceIdentifiant(0) && $this->verifierRessourceValeur(1, 'votes')) {
|
|
|
133 |
$this->sousServiceNom = 'votes-image';
|
758 |
delphine |
134 |
}
|
702 |
gduche |
135 |
}
|
1604 |
jpm |
136 |
|
|
|
137 |
if ($this->sousServiceNom == null) {
|
|
|
138 |
$this->lancerMessageErreurRessource();
|
|
|
139 |
}
|
702 |
gduche |
140 |
}
|
1604 |
jpm |
141 |
|
1631 |
jpm |
142 |
private function analyserRessoucesModification() {
|
|
|
143 |
if (count($this->ressources) == 2) {
|
|
|
144 |
if ($this->etreRessourceIdentifiant(0) && $this->verifierRessourceValeur(1, 'votes')) {
|
|
|
145 |
$this->sousServiceNom = 'votes-image';
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
if ($this->sousServiceNom == null) {
|
|
|
150 |
$this->lancerMessageErreurRessource();
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
private function analyserRessoucesSuppression() {
|
1604 |
jpm |
155 |
if (count($this->ressources) == 1 && $this->etreRessourceIdentifiant(0)) {
|
|
|
156 |
$this->sousServiceNom = 'liste-images';
|
758 |
delphine |
157 |
}
|
1604 |
jpm |
158 |
|
|
|
159 |
if ($this->sousServiceNom == null) {
|
|
|
160 |
$this->lancerMessageErreurRessource();
|
|
|
161 |
}
|
758 |
delphine |
162 |
}
|
1604 |
jpm |
163 |
|
758 |
delphine |
164 |
private function etreRessourceIdentifiant($num) {
|
|
|
165 |
$presenceId = false;
|
1604 |
jpm |
166 |
if (isset($this->ressources[$num]) && is_numeric($this->ressources[$num])) {
|
758 |
delphine |
167 |
$presenceId = true;
|
754 |
delphine |
168 |
}
|
758 |
delphine |
169 |
return $presenceId;
|
754 |
delphine |
170 |
}
|
702 |
gduche |
171 |
|
1604 |
jpm |
172 |
private function verifierRessourceValeur($num, $valeur) {
|
|
|
173 |
$ok = false;
|
|
|
174 |
if (isset($this->ressources[$num]) && $this->ressources[$num] == $valeur) {
|
|
|
175 |
$ok = true;
|
|
|
176 |
}
|
|
|
177 |
return $ok;
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
private function lancerMessageErreurRessource() {
|
|
|
181 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
182 |
$message = "La ressource demandée '$ressource' ".
|
|
|
183 |
"n'est pas disponible pour le service ".$this->serviceNom." !\n".
|
|
|
184 |
"Les URLs disponibles sont : \n".
|
|
|
185 |
" - en GET : images, images/#id/votes \n".
|
|
|
186 |
" - en DELETE : images/#id";
|
|
|
187 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
188 |
throw new Exception($message, $code);
|
|
|
189 |
}
|
|
|
190 |
|
702 |
gduche |
191 |
private function initialiserService() {
|
1604 |
jpm |
192 |
$classe = $this->obtenirNomClasseService($this->sousServiceNom);
|
|
|
193 |
//echo $this->sousServiceNom.':'.$classe."\n";
|
|
|
194 |
//echo 'Ressources :'.print_r($this->ressources, true);
|
|
|
195 |
//echo 'Parametres :'.print_r($this->parametres, true);
|
702 |
gduche |
196 |
$chemins = array();
|
1604 |
jpm |
197 |
$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
|
718 |
gduche |
198 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
702 |
gduche |
199 |
$retour = '';
|
|
|
200 |
$service = null;
|
|
|
201 |
foreach ($chemins as $chemin) {
|
|
|
202 |
if (file_exists($chemin)) {
|
1604 |
jpm |
203 |
$this->conteneur->chargerConfiguration('config_'.$this->serviceNom.'.ini');
|
751 |
delphine |
204 |
require_once $chemin;
|
724 |
gduche |
205 |
$service = new $classe($this->conteneur);
|
782 |
delphine |
206 |
if ($this->methode == 'consulter') {
|
|
|
207 |
$retour = $service->consulter($this->ressources, $this->parametres);
|
1631 |
jpm |
208 |
} elseif ($this->methode == 'ajouter') {
|
|
|
209 |
$retour = $service->ajouter($this->ressources, $this->parametres);
|
|
|
210 |
} elseif ($this->methode == 'modifier') {
|
|
|
211 |
$retour = $service->modifier($this->ressources, $this->parametres);
|
1472 |
aurelien |
212 |
} elseif ($this->methode == 'supprimer') {
|
|
|
213 |
$retour = $service->supprimer($this->ressources);
|
782 |
delphine |
214 |
}
|
702 |
gduche |
215 |
}
|
|
|
216 |
}
|
1604 |
jpm |
217 |
|
702 |
gduche |
218 |
if (is_null($service)) {
|
1604 |
jpm |
219 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
220 |
$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
|
|
|
221 |
"n'existe pas dans le service '{$this->serviceNom}' !";
|
702 |
gduche |
222 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
223 |
throw new Exception($message, $code);
|
|
|
224 |
}
|
|
|
225 |
return $retour;
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
private function obtenirNomClasseService($mot) {
|
|
|
229 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
230 |
return $classeNom;
|
|
|
231 |
}
|
|
|
232 |
}
|
|
|
233 |
?>
|