702 |
gduche |
1 |
<?php
|
1795 |
jpm |
2 |
// declare(encoding='UTF-8');
|
702 |
gduche |
3 |
/**
|
1795 |
jpm |
4 |
* Classe principale de chargement des sous-services "images" de DEL.
|
1700 |
jpm |
5 |
*
|
|
|
6 |
* Cette classe se charge toujours de :
|
|
|
7 |
* - vérifier l'existance des ressources (services) demandés
|
|
|
8 |
* - vérifier la cohérence et le format des paramêtres passées dans l'url
|
|
|
9 |
* En fonction, de la compléxité du service, elle peut :
|
|
|
10 |
* - dans un premier temps, exécuter directement les actions : consulter, ajouter, modifier, supprimer.
|
|
|
11 |
* - dans un second temps, charger dynamiquement d'éventuelles sous-classes du service en fonction des ressources présentes dans l'URL.
|
|
|
12 |
*
|
1795 |
jpm |
13 |
* URLs possibles :
|
|
|
14 |
* GET :
|
|
|
15 |
* http://localhost/del/services/0.1/images/ => toutes les images : classe ListeImages
|
|
|
16 |
* http://localhost/del/services/0.1/images/#idImg/votes => tous les votes d'une image (#idImg) classés par protocole : classe VotesImage
|
1700 |
jpm |
17 |
*
|
1795 |
jpm |
18 |
* Non Implémenté : http://localhost/del/services/0.1/images/#id => une image donnée => en test pour remplacer les appels à eflore/cel
|
|
|
19 |
* 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é
|
|
|
20 |
*
|
|
|
21 |
* PUT :
|
|
|
22 |
* http://localhost/del/services/0.1/images/#idImg => ajouter un vote sur une image donnée (#idImg)
|
|
|
23 |
*
|
|
|
24 |
* POST :
|
|
|
25 |
* http://localhost/del/services/0.1/images/#idImg => modifier un vote sur une image donnée (#idImg)
|
|
|
26 |
*
|
|
|
27 |
* DELETE :
|
|
|
28 |
* http://localhost/del/services/0.1/images/#idImg => supprimer une image donnée (#idImg)
|
|
|
29 |
* http://localhost/del/services/0.1/images/#idImg/votes/#idVote => supprimer un vote (#idVote) d'une image donnée (#idImg)
|
|
|
30 |
*
|
|
|
31 |
* @category DEL
|
|
|
32 |
* @package Services
|
1700 |
jpm |
33 |
* @subpackage Images
|
1795 |
jpm |
34 |
* @version 0.1
|
|
|
35 |
* @author Mathias CHOUET <mathias@tela-botanica.org>
|
|
|
36 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
37 |
* @author Aurelien PERONNET <aurelien@tela-botanica.org>
|
|
|
38 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
39 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
40 |
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
|
1700 |
jpm |
41 |
*/
|
1604 |
jpm |
42 |
restore_error_handler();
|
|
|
43 |
restore_exception_handler();
|
|
|
44 |
error_reporting(E_ALL);
|
1795 |
jpm |
45 |
|
746 |
gduche |
46 |
class Images extends RestService {
|
1795 |
jpm |
47 |
|
702 |
gduche |
48 |
private $parametres = array();
|
|
|
49 |
private $ressources = array();
|
782 |
delphine |
50 |
private $methode = null;
|
1604 |
jpm |
51 |
private $serviceNom = 'images';
|
|
|
52 |
private $sousServiceNom = null;
|
718 |
gduche |
53 |
private $cheminCourant = null;
|
|
|
54 |
|
|
|
55 |
private $conteneur;
|
1604 |
jpm |
56 |
|
718 |
gduche |
57 |
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
|
702 |
gduche |
58 |
protected $utilisationParametresBruts = true;
|
|
|
59 |
|
|
|
60 |
public function __construct() {
|
718 |
gduche |
61 |
$this->cheminCourant = dirname(__FILE__).DS;
|
702 |
gduche |
62 |
}
|
|
|
63 |
|
|
|
64 |
public function consulter($ressources, $parametres) {
|
782 |
delphine |
65 |
$this->methode = 'consulter';
|
1604 |
jpm |
66 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
|
|
67 |
return $this->executerService();
|
|
|
68 |
}
|
|
|
69 |
|
1622 |
jpm |
70 |
public function ajouter($ressources, $requeteDonnees) {
|
|
|
71 |
$this->methode = 'ajouter';
|
|
|
72 |
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
|
|
|
73 |
return $this->executerService();
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
public function modifier($ressources, $requeteDonnees) {
|
1666 |
jpm |
77 |
$this->methode = 'modifier';
|
1622 |
jpm |
78 |
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
|
|
|
79 |
return $this->executerService();
|
|
|
80 |
}
|
|
|
81 |
|
1604 |
jpm |
82 |
public function supprimer($ressources) {
|
|
|
83 |
$this->methode = 'supprimer';
|
|
|
84 |
$this->initialiserRessourcesEtParametres($ressources);
|
|
|
85 |
return $this->executerService();
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
|
|
|
89 |
$this->ressources = $ressources;
|
|
|
90 |
$this->parametres = $parametres;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
private function executerService() {
|
702 |
gduche |
94 |
$reponseHttp = new ReponseHttp();
|
|
|
95 |
try {
|
718 |
gduche |
96 |
$this->conteneur = new Conteneur($this->parametres);
|
702 |
gduche |
97 |
$resultat = $this->traiterRessources();
|
|
|
98 |
$reponseHttp->setResultatService($resultat);
|
|
|
99 |
} catch (Exception $e) {
|
|
|
100 |
$reponseHttp->ajouterErreur($e);
|
|
|
101 |
}
|
|
|
102 |
$reponseHttp->emettreLesEntetes();
|
|
|
103 |
$corps = $reponseHttp->getCorps();
|
|
|
104 |
return $corps;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
private function traiterRessources() {
|
1604 |
jpm |
108 |
$this->chargerConfigService();
|
|
|
109 |
$this->analyserRessources();
|
|
|
110 |
$retour = $this->initialiserService();
|
702 |
gduche |
111 |
return $retour;
|
|
|
112 |
}
|
758 |
delphine |
113 |
|
1604 |
jpm |
114 |
private function chargerConfigService() {
|
|
|
115 |
$chemin = Config::get('chemin_configurations')."config_{$this->serviceNom}.ini";
|
|
|
116 |
Config::charger($chemin);
|
702 |
gduche |
117 |
}
|
|
|
118 |
|
1604 |
jpm |
119 |
private function analyserRessources() {
|
1666 |
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') {
|
1666 |
jpm |
125 |
$this->analyserRessoucesSuppression();
|
1604 |
jpm |
126 |
}
|
702 |
gduche |
127 |
}
|
|
|
128 |
|
1666 |
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 |
|
1666 |
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';
|
1666 |
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);
|
1666 |
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 |
}
|
1793 |
jpm |
237 |
}
|