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
|
1632 |
jpm |
104 |
* http://localhost/del/services/0.1/images/#idImg/votes => tous les votes d'une image (#idImg) classés par protocole : classe VotesImage
|
1604 |
jpm |
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 :
|
1632 |
jpm |
110 |
* http://localhost/del/services/0.1/images/#idImg => ajouter un vote sur une image donnée (#idImg)
|
1631 |
jpm |
111 |
*
|
|
|
112 |
* POST :
|
1632 |
jpm |
113 |
* http://localhost/del/services/0.1/images/#idImg => modifier un vote sur une image donnée (#idImg)
|
1631 |
jpm |
114 |
*
|
|
|
115 |
* DELETE :
|
1632 |
jpm |
116 |
* http://localhost/del/services/0.1/images/#idImg => supprimer une image donnée (#idImg)
|
|
|
117 |
* http://localhost/del/services/0.1/images/#idImg/votes/#idVote => supprimer un vote (#idVote) d'une image donnée (#idImg)
|
1604 |
jpm |
118 |
*/
|
|
|
119 |
private function analyserRessources() {
|
1631 |
jpm |
120 |
if ($this->methode == 'consulter') {
|
|
|
121 |
$this->analyserRessoucesConsultation();
|
|
|
122 |
} else if ($this->methode == 'modifier' || $this->methode == 'ajouter') {
|
|
|
123 |
$this->analyserRessoucesModification();
|
1604 |
jpm |
124 |
} else if ($this->methode == 'supprimer') {
|
1631 |
jpm |
125 |
$this->analyserRessoucesSuppression();
|
1604 |
jpm |
126 |
}
|
702 |
gduche |
127 |
}
|
|
|
128 |
|
1631 |
jpm |
129 |
private function analyserRessoucesConsultation() {
|
1604 |
jpm |
130 |
if (count($this->ressources) == 0) {
|
|
|
131 |
$this->sousServiceNom = 'liste-images';
|
|
|
132 |
} else if (count($this->ressources) == 2) {
|
|
|
133 |
if ($this->etreRessourceIdentifiant(0) && $this->verifierRessourceValeur(1, 'votes')) {
|
|
|
134 |
$this->sousServiceNom = 'votes-image';
|
758 |
delphine |
135 |
}
|
702 |
gduche |
136 |
}
|
1604 |
jpm |
137 |
|
|
|
138 |
if ($this->sousServiceNom == null) {
|
|
|
139 |
$this->lancerMessageErreurRessource();
|
|
|
140 |
}
|
702 |
gduche |
141 |
}
|
1604 |
jpm |
142 |
|
1631 |
jpm |
143 |
private function analyserRessoucesModification() {
|
|
|
144 |
if (count($this->ressources) == 2) {
|
|
|
145 |
if ($this->etreRessourceIdentifiant(0) && $this->verifierRessourceValeur(1, 'votes')) {
|
|
|
146 |
$this->sousServiceNom = 'votes-image';
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
if ($this->sousServiceNom == null) {
|
|
|
151 |
$this->lancerMessageErreurRessource();
|
|
|
152 |
}
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
private function analyserRessoucesSuppression() {
|
1604 |
jpm |
156 |
if (count($this->ressources) == 1 && $this->etreRessourceIdentifiant(0)) {
|
|
|
157 |
$this->sousServiceNom = 'liste-images';
|
1632 |
jpm |
158 |
} else if (count($this->ressources) == 3) {
|
|
|
159 |
if ($this->etreRessourceIdentifiant(0) && $this->verifierRessourceValeur(1, 'votes') && $this->etreRessourceIdentifiant(2) ) {
|
|
|
160 |
$this->sousServiceNom = 'votes-image';
|
|
|
161 |
}
|
758 |
delphine |
162 |
}
|
1604 |
jpm |
163 |
|
|
|
164 |
if ($this->sousServiceNom == null) {
|
|
|
165 |
$this->lancerMessageErreurRessource();
|
|
|
166 |
}
|
758 |
delphine |
167 |
}
|
1604 |
jpm |
168 |
|
758 |
delphine |
169 |
private function etreRessourceIdentifiant($num) {
|
|
|
170 |
$presenceId = false;
|
1604 |
jpm |
171 |
if (isset($this->ressources[$num]) && is_numeric($this->ressources[$num])) {
|
758 |
delphine |
172 |
$presenceId = true;
|
754 |
delphine |
173 |
}
|
758 |
delphine |
174 |
return $presenceId;
|
754 |
delphine |
175 |
}
|
702 |
gduche |
176 |
|
1604 |
jpm |
177 |
private function verifierRessourceValeur($num, $valeur) {
|
|
|
178 |
$ok = false;
|
|
|
179 |
if (isset($this->ressources[$num]) && $this->ressources[$num] == $valeur) {
|
|
|
180 |
$ok = true;
|
|
|
181 |
}
|
|
|
182 |
return $ok;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
private function lancerMessageErreurRessource() {
|
|
|
186 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
187 |
$message = "La ressource demandée '$ressource' ".
|
|
|
188 |
"n'est pas disponible pour le service ".$this->serviceNom." !\n".
|
|
|
189 |
"Les URLs disponibles sont : \n".
|
|
|
190 |
" - en GET : images, images/#id/votes \n".
|
|
|
191 |
" - en DELETE : images/#id";
|
|
|
192 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
193 |
throw new Exception($message, $code);
|
|
|
194 |
}
|
|
|
195 |
|
702 |
gduche |
196 |
private function initialiserService() {
|
1604 |
jpm |
197 |
$classe = $this->obtenirNomClasseService($this->sousServiceNom);
|
|
|
198 |
//echo $this->sousServiceNom.':'.$classe."\n";
|
|
|
199 |
//echo 'Ressources :'.print_r($this->ressources, true);
|
|
|
200 |
//echo 'Parametres :'.print_r($this->parametres, true);
|
702 |
gduche |
201 |
$chemins = array();
|
1604 |
jpm |
202 |
$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
|
718 |
gduche |
203 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
702 |
gduche |
204 |
$retour = '';
|
|
|
205 |
$service = null;
|
|
|
206 |
foreach ($chemins as $chemin) {
|
|
|
207 |
if (file_exists($chemin)) {
|
1604 |
jpm |
208 |
$this->conteneur->chargerConfiguration('config_'.$this->serviceNom.'.ini');
|
751 |
delphine |
209 |
require_once $chemin;
|
724 |
gduche |
210 |
$service = new $classe($this->conteneur);
|
782 |
delphine |
211 |
if ($this->methode == 'consulter') {
|
|
|
212 |
$retour = $service->consulter($this->ressources, $this->parametres);
|
1631 |
jpm |
213 |
} elseif ($this->methode == 'ajouter') {
|
|
|
214 |
$retour = $service->ajouter($this->ressources, $this->parametres);
|
|
|
215 |
} elseif ($this->methode == 'modifier') {
|
|
|
216 |
$retour = $service->modifier($this->ressources, $this->parametres);
|
1472 |
aurelien |
217 |
} elseif ($this->methode == 'supprimer') {
|
|
|
218 |
$retour = $service->supprimer($this->ressources);
|
782 |
delphine |
219 |
}
|
702 |
gduche |
220 |
}
|
|
|
221 |
}
|
1604 |
jpm |
222 |
|
702 |
gduche |
223 |
if (is_null($service)) {
|
1604 |
jpm |
224 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
225 |
$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
|
|
|
226 |
"n'existe pas dans le service '{$this->serviceNom}' !";
|
702 |
gduche |
227 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
228 |
throw new Exception($message, $code);
|
|
|
229 |
}
|
|
|
230 |
return $retour;
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
private function obtenirNomClasseService($mot) {
|
|
|
234 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
235 |
return $classeNom;
|
|
|
236 |
}
|
|
|
237 |
}
|
|
|
238 |
?>
|