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