6 |
jpm |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
|
|
|
4 |
class Images extends Commun {
|
|
|
5 |
|
|
|
6 |
protected $limite_requete = array('depart' => 0, 'limite' => 100);
|
|
|
7 |
protected $requete_champ = '*';
|
|
|
8 |
protected $requete_condition = null;
|
|
|
9 |
protected $requete_group_by = ' ORDER BY CAST( id_image AS DECIMAL )';
|
|
|
10 |
protected $entete_http;
|
|
|
11 |
protected $corps_http;
|
|
|
12 |
protected $table_retour;
|
|
|
13 |
protected $retour_format = 'max';
|
|
|
14 |
protected $format_reponse = 'images';
|
|
|
15 |
protected $table_param = array();
|
|
|
16 |
protected $total_resultat;
|
|
|
17 |
protected $table_ressources = array();
|
|
|
18 |
protected $html = 'txt';
|
|
|
19 |
protected $recherche = 'stricte';
|
|
|
20 |
protected $service = 'images';
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
public function consulter($ressources, $parametres) {
|
|
|
24 |
$resultat_formate = '';
|
|
|
25 |
$this->table[] = Config::get("bdd_images"); //." t1,".Config::get("bdd_triples")." t2, ".Config::get("bdd_pages")." p";
|
|
|
26 |
$this->traiterParametres($parametres);
|
|
|
27 |
$this->traiterRessources($ressources);
|
|
|
28 |
if ($this->corps_http == '' && $this->entete_http == '') {
|
|
|
29 |
$requete = $this->assemblerLaRequete();
|
|
|
30 |
$resultat = $this->getBdd()->recupererTous($requete);
|
|
|
31 |
$this->testerResultat($resultat, $resultat_formate, $requete);
|
|
|
32 |
}
|
|
|
33 |
return $this->formerReponseHTTP($resultat_formate);
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
public function testerResultat($resultat, &$resultat_formate, $requete) {
|
|
|
37 |
if ($resultat == '') { //cas ou coste/services/ ou la requete comporte des erreurs
|
|
|
38 |
$e = 'La requête SQL formée comporte une erreur!';
|
|
|
39 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE,$e);
|
|
|
40 |
Debug::printr($requete);
|
|
|
41 |
} elseif ($resultat) {
|
|
|
42 |
$resultat_formate = $this->retournerResultatFormate($resultat);
|
|
|
43 |
} else {
|
|
|
44 |
if ($this->format_reponse == 'images/relations') {
|
|
|
45 |
$resultat_formate = 'null';
|
|
|
46 |
} else {
|
|
|
47 |
$m = 'Données introuvables dans la base';
|
|
|
48 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE,$m);
|
|
|
49 |
Debug::printr($requete);
|
|
|
50 |
}
|
|
|
51 |
}
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
//-------------------------------------------------------TRAITER RESSOURCES----------------------------------------------
|
|
|
55 |
|
|
|
56 |
public function traiterRessources(&$ressources) {
|
|
|
57 |
if (isset($ressources) && !empty($ressources)) {
|
|
|
58 |
$this->table_ressources = $ressources;
|
|
|
59 |
//traiter ressources/id
|
|
|
60 |
if ($ressources[0] == 'relations') {
|
|
|
61 |
$this->traiterRelationsProjet($ressources);
|
|
|
62 |
$this->format_reponse .= '/relations';
|
|
|
63 |
} elseif (isset($this->table_ressources[0]) && !empty($this->table_ressources[0])) {
|
|
|
64 |
$this->traiterRessourceId($this->table_ressources[0]);
|
|
|
65 |
//traiter ressources/id/champ
|
|
|
66 |
if (isset($this->table_ressources[1]) && !empty($this->table_ressources[1])) {
|
|
|
67 |
$this->format_reponse .= '/id/champ';
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
public function traiterRelationsProjet($tab_ress) {
|
|
|
74 |
$projet = $tab_ress[1];
|
|
|
75 |
$num_nom = $tab_ress[2];
|
|
|
76 |
if (isset($tab_ress[3])) {
|
|
|
77 |
$e = "Pas de type pour le service images";
|
|
|
78 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
|
|
|
79 |
}
|
|
|
80 |
$this->requete_condition[] = "num_nom = ".$this->getBdd()->proteger($num_nom);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
public function traiterRessourceId($id) {
|
|
|
84 |
$this->format_reponse .= '/id';
|
|
|
85 |
$this->requete_condition[] = 'id_image LIKE '.$this->getBdd()->proteger($id.'.png');
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
|
|
|
89 |
//-------------------------------------------------------TRAITER PARAMETRES----------------------------------------------
|
|
|
90 |
|
|
|
91 |
public function traiterParametres($parametres) {
|
|
|
92 |
if (isset($parametres) && !empty($parametres)) {
|
|
|
93 |
$this->table_param = $parametres;
|
|
|
94 |
//print_r($parametres);
|
|
|
95 |
if (isset($parametres['recherche']) && $parametres['recherche'] != '') {
|
|
|
96 |
$this->recherche = $parametres['recherche'];
|
|
|
97 |
}
|
|
|
98 |
foreach ($parametres as $param => $val) {
|
|
|
99 |
switch ($param) {
|
|
|
100 |
case 'retour_format' : $this->retour_format = $val; break;
|
|
|
101 |
case 'masque_nn' : $this->ajouterLeFiltreMasque('num_nom', $val); break;
|
|
|
102 |
case 'masque_no' : $this->ajouterLeFiltreMasque('nom_photographe', $val); break;
|
|
|
103 |
case 'masque_ns' : $this->ajouterLeFiltreMasque('nom_sci', $val); break;
|
|
|
104 |
case 'masque_date' : $this->ajouterLeFiltreMasque('date', $val); break;
|
|
|
105 |
case 'masque_station' : $this->ajouterLeFiltreMasque('station', $val); break;
|
|
|
106 |
case 'navigation_depart': $this->limite_requete['depart'] = $val; break;
|
|
|
107 |
case 'navigation_limite': $this->limite_requete['limite'] = $val; break;
|
|
|
108 |
case 'recherche' : break;
|
|
|
109 |
default :
|
|
|
110 |
$e = 'Erreur dans les paramètres de recherche de votre requête : </br> Le parametre " '
|
|
|
111 |
.$param.' " n\'existe pas.';
|
|
|
112 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE,$e);
|
|
|
113 |
break;
|
|
|
114 |
}
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
public function ajouterLeFiltreMasque($masque, $valeur) {
|
|
|
120 |
if ($this->recherche == 'etendue') {
|
|
|
121 |
$valeur = '%'.str_replace(' ','%', $valeur).'%';
|
|
|
122 |
}
|
|
|
123 |
//$this->table .= ', '.Config::get("bdd_table").'_v0_1 c';
|
|
|
124 |
switch ($masque) {
|
|
|
125 |
case 'nom_sci' :
|
|
|
126 |
$this->requete_champ = 'cb.*';
|
|
|
127 |
$this->table[] = ' cb , '.Config::get("bdd_table").'_v0_1 c';
|
|
|
128 |
$this->requete_condition[] = 'c.nom_sci LIKE '
|
|
|
129 |
.$this->getBdd()->proteger($valeur);
|
|
|
130 |
$this->requete_condition[] = 'c.num_nom_coste = CONCAT(cb.id_image,\',.png\')';
|
|
|
131 |
$this->ajouterRechercheFloue('nom_sci', $valeur);
|
|
|
132 |
break;
|
|
|
133 |
case 'num_nom' :
|
|
|
134 |
$val = (strrpos($valeur, ',') !== false)? " IN (".str_replace(',', '.png,', $valeur).")" : " = ".$this->getBdd()->proteger($valeur.".png");
|
|
|
135 |
$this->requete_condition[] = "id_image $val";
|
|
|
136 |
break;
|
|
|
137 |
default :
|
|
|
138 |
break;
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
public function ajouterRechercheFloue($nom_champ, $id) {
|
|
|
143 |
if ($this->recherche == 'floue') {
|
|
|
144 |
$id = $this->getBdd()->proteger($id);
|
|
|
145 |
$this->requete_condition[array_search("$nom_champ LIKE $id",
|
|
|
146 |
$this->requete_condition)] =
|
|
|
147 |
"($nom_champ LIKE $id)".
|
|
|
148 |
" OR ( SOUNDEX($nom_champ) = SOUNDEX($id))".
|
|
|
149 |
" OR SOUNDEX(REVERSE($nom_champ)) = SOUNDEX(REVERSE($id))))";
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
|
|
|
154 |
|
|
|
155 |
//-----------------------------FONCTIONS DASSEMBLAGE DE LA REQUETE-----------------------------------------------------
|
|
|
156 |
|
|
|
157 |
public function assemblerLaRequete() {
|
|
|
158 |
$requete = ' SELECT '.$this->requete_champ.
|
|
|
159 |
' FROM '.implode(', ', $this->table)
|
|
|
160 |
.$this->formerRequeteCondition()
|
|
|
161 |
.$this->requete_group_by
|
|
|
162 |
.$this->formerRequeteLimite();
|
|
|
163 |
return $requete;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
public function formerRequeteCondition() {
|
|
|
167 |
$condition = '';
|
|
|
168 |
if ($this->requete_condition !== null) {
|
|
|
169 |
$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
|
|
|
170 |
}
|
|
|
171 |
return $condition;
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
public function formerRequeteLimite() {
|
|
|
175 |
if (in_array($this->format_reponse , array('images/id', 'images/id/relations'))) {
|
|
|
176 |
$this->requete_limite = '';
|
|
|
177 |
} elseif (($depart = $this->limite_requete['depart']) > ($this->total_resultat = $this->recupererTotalResultat())) {
|
|
|
178 |
$this->limite_requete['depart'] =
|
|
|
179 |
(($this->total_resultat - $this->limite_requete['limite']) < 0) ? 0 : ($this->total_resultat - $this->limite_requete['limite']);
|
|
|
180 |
$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite'];
|
|
|
181 |
} else {
|
|
|
182 |
$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite'];
|
|
|
183 |
}
|
|
|
184 |
return $this->requete_limite;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
/**
|
|
|
188 |
* Recupere le nombre total de résultat d'une requete lancée.
|
|
|
189 |
* Est utilisée lors de l'affichage du total dans le résultat retourné et pr déterminer les limites et départ
|
|
|
190 |
* @return nombre total de résultat
|
|
|
191 |
*/
|
|
|
192 |
public function recupererTotalResultat() {
|
|
|
193 |
$total = null;
|
|
|
194 |
$requete = 'SELECT count(*) as nombre FROM '.implode(', ', $this->table).$this->formerRequeteCondition().$this->requete_group_by;
|
|
|
195 |
$res = $this->getBdd()->recuperer($requete);
|
|
|
196 |
if ($res) {
|
|
|
197 |
$total = $res['nombre'];
|
|
|
198 |
} else {
|
|
|
199 |
$e = 'Données introuvables dans la base ou erreur dans la requête SQL';
|
|
|
200 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $e);
|
|
|
201 |
}
|
|
|
202 |
return $total;
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
//-------------------FONCTIONS POUR LE FORMATAGE EN JSON----------------------------------------------------------------
|
|
|
206 |
|
|
|
207 |
public function retournerResultatFormate($resultat) {
|
|
|
208 |
$reponse = '';
|
|
|
209 |
switch ($this->format_reponse) {
|
|
|
210 |
case 'images' : $reponse = $this->formaterImages($resultat); break;
|
|
|
211 |
case 'images/relations' : $reponse = $this->formaterImagesRelations($resultat[0]); break;
|
|
|
212 |
case 'images/id' : $reponse = $this->formaterImagesId($resultat[0]); break;
|
|
|
213 |
case 'images/id/champ' : $reponse = $this->formaterImagesIdChamp($resultat[0]); break;
|
|
|
214 |
default : break;
|
|
|
215 |
}
|
|
|
216 |
$reponse = json_encode($reponse);
|
|
|
217 |
return $reponse;
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
public function formaterImages($resultat) {
|
|
|
221 |
//print_r($resultat);
|
|
|
222 |
$this->ajouterEnteteResultat($resultat);
|
|
|
223 |
$reponse['entete'] = $this->table_retour;
|
|
|
224 |
$this->table_retour = array();
|
|
|
225 |
foreach ($resultat as $images) {
|
|
|
226 |
$id = str_replace('.png', '', $images['id_image']);
|
|
|
227 |
$id_coste = $this->recupererNumNom($id);
|
|
|
228 |
if ($id != '') {
|
|
|
229 |
$this->table_retour['url.image'] = Config::get('url_images').$images['id_image'];
|
|
|
230 |
$this->afficherDonnees('id', $id_coste);
|
|
|
231 |
$reponse['resultat'][$id] = $this->table_retour;
|
|
|
232 |
$this->table_retour = array();
|
|
|
233 |
$reponse['resultat'][$id]['href'] = $this->ajouterHref('images', $id);
|
|
|
234 |
}
|
|
|
235 |
}
|
|
|
236 |
return $reponse;
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
public function ajouterEnteteResultat($resultat) {
|
|
|
240 |
$this->table_retour['depart'] = $this->limite_requete['depart'];
|
|
|
241 |
$this->table_retour['limite'] = $this->limite_requete['limite'];
|
|
|
242 |
$this->table_retour['total'] = $this->total_resultat;
|
|
|
243 |
//formuler les urls
|
|
|
244 |
$url = $this->formulerUrl($this->total_resultat, '/'.$this->service);
|
|
|
245 |
if ($url['precedent'] != '') { $this->table_retour['href.precedent'] = $url['precedent']; }
|
|
|
246 |
if ($url['suivant'] != '') { $this->table_retour['href.suivant'] = $url['suivant']; }
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
|
|
|
250 |
public function formaterImagesRelations($resultat) {
|
|
|
251 |
$reponse['url.image'] = Config::get('url_images').$resultat['id_image'];
|
|
|
252 |
return $reponse;
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
|
|
|
256 |
public function formaterImagesId($resultat) {
|
|
|
257 |
$id = str_replace('.png', '', $resultat['id_image']);
|
|
|
258 |
$id_coste = $this->recupererNumNom($id);
|
|
|
259 |
$this->table_retour['url.image'] = Config::get('url_images').$resultat['id_image'];
|
|
|
260 |
$this->table_retour['id'] = $id;
|
|
|
261 |
$this->table_retour['auteur'] = $resultat['auteur'];
|
|
|
262 |
$this->table_retour['station.pays'] = "ISO-3166-1.FR";
|
|
|
263 |
$this->afficherDonnees('id', $id_coste);
|
|
|
264 |
$res = $this->ajouterRelationBdnff($resultat['num_nom']);
|
|
|
265 |
return $res;
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
|
|
|
269 |
|
|
|
270 |
public function recupererNumNom($num_nom_coste) {
|
|
|
271 |
$req = "SELECT num_nom FROM coste_v0_1 WHERE num_nom_coste = "
|
|
|
272 |
.$this->getBdd()->proteger($num_nom_coste)." AND num_nom = num_nom_retenu";
|
|
|
273 |
$res = $this->getBdd()->recuperer($req);
|
|
|
274 |
if ($res) {
|
|
|
275 |
$num_nom = $res['num_nom'];
|
|
|
276 |
}
|
|
|
277 |
return $num_nom;
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
public function ajouterRelationBdnff($num_nom) {
|
|
|
281 |
$res = $this->table_retour;
|
|
|
282 |
$this->table_retour = array();
|
|
|
283 |
$this->afficherDonnees('id', $num_nom, 'bdtfx');
|
|
|
284 |
$res['relations']['nom'] = $this->table_retour;
|
|
|
285 |
return $res;
|
|
|
286 |
}
|
|
|
287 |
|
|
|
288 |
public function formaterImagesIdChamp($resultat) {
|
|
|
289 |
//on recupère tous les resultats possibles
|
|
|
290 |
$reponse_id = $this->formaterImagesId($resultat);
|
|
|
291 |
$this->table_retour = array();
|
|
|
292 |
//on recupère les résultats demandés à partir du tableau de résultat complet
|
|
|
293 |
$this->table_retour['id'] = $resultat['tag'];
|
|
|
294 |
$champs = explode(' ', $this->table_ressources[1]);
|
|
|
295 |
foreach ($champs as $champ) {
|
|
|
296 |
if ($this->verifierValiditeChamp($champ, $reponse_id)) {
|
|
|
297 |
if (preg_match('/^[^.]+\.\*$/', $champ)) {
|
|
|
298 |
$this->afficherPointEtoile($champ, $reponse_id);
|
|
|
299 |
} else {
|
|
|
300 |
$this->table_retour[$champ] = $reponse_id[$champ];
|
|
|
301 |
}
|
|
|
302 |
}
|
|
|
303 |
}
|
|
|
304 |
return $this->table_retour;
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
public function afficherPointEtoile($champ, $reponse_id) {
|
|
|
308 |
preg_match('/^([^.]+\.)\*$/', $champ, $match);
|
|
|
309 |
foreach ($reponse_id as $chp => $valeur) {
|
|
|
310 |
if (strrpos($chp, $match[1]) !== false) {
|
|
|
311 |
$this->table_retour[$chp] = $valeur;
|
|
|
312 |
}
|
|
|
313 |
}
|
|
|
314 |
}
|
|
|
315 |
|
|
|
316 |
public function verifierValiditeChamp($champ, $reponse_id) {
|
|
|
317 |
$validite = false;
|
|
|
318 |
preg_match('/^([^.]+)(:?\.([^.]+))?$/', $champ, $match);
|
|
|
319 |
if (array_key_exists($match[1], $reponse_id)) {
|
|
|
320 |
$validite = true;
|
|
|
321 |
if (isset($match[2]) && !empty($match[2])) {
|
|
|
322 |
if ($match[1] == 'type') {
|
|
|
323 |
$suffixes = array('.code', '.href', '.*');
|
|
|
324 |
$validite = (in_array($match[2], $suffixes)) ? true : false;
|
|
|
325 |
} else {
|
|
|
326 |
$validite = false;
|
|
|
327 |
}
|
|
|
328 |
}
|
|
|
329 |
}
|
|
|
330 |
if (!$validite) {
|
|
|
331 |
$champs = implode('</li><li>', array_keys($reponse_id));
|
|
|
332 |
$e = 'Erreur dans votre requête : </br> Le champ "'.$champ.'" n\'existe pas. Les champs disponibles
|
|
|
333 |
sont : <li>'.$champs.'</li>';
|
|
|
334 |
$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $e);
|
|
|
335 |
}
|
|
|
336 |
return $validite;
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
|
|
|
340 |
//------------------------------------------------Fonction d'affichage ------------------------------------------------
|
|
|
341 |
|
|
|
342 |
public function afficherDonnees($champ, $valeur, $projet = null) {
|
|
|
343 |
if ($this->retour_format == 'min') {
|
|
|
344 |
$champ = ($champ == 'type') ? $champ.'.code' : $champ;
|
|
|
345 |
$this->table_retour[$champ] = $valeur;
|
|
|
346 |
} else {
|
|
|
347 |
$this->afficherDonneesMax($champ, $valeur, $projet);
|
|
|
348 |
}
|
|
|
349 |
}
|
|
|
350 |
|
|
|
351 |
public function afficherDonneesMax($champ, $valeur, $projet = null) {
|
|
|
352 |
if (!$projet) $projet = 'coste';
|
|
|
353 |
if ($champ == 'id') {
|
|
|
354 |
$this->table_retour['nom_sci.id'] = $projet.'.'.$valeur;
|
|
|
355 |
$fct = "recupererNomSci".ucfirst($projet);
|
|
|
356 |
$this->table_retour['nom_sci'] = $this->$fct($valeur);
|
|
|
357 |
$this->table_retour['nom_sci.href'] = $this->ajouterHrefAutreProjet('noms', '', $valeur, $projet);
|
|
|
358 |
} elseif (in_array($champ, array('type'))) {
|
|
|
359 |
$this->table_retour[$champ.'.code'] = $valeur;
|
|
|
360 |
$this->table_retour[$champ] = $this->recupererSignificationCode($valeur);
|
|
|
361 |
$this->table_retour[$champ.'.href'] = $this->ajouterHrefAutreProjet($champ, '', $valeur, 'commun');
|
|
|
362 |
} else {
|
|
|
363 |
$this->table_retour[$champ] = $valeur;
|
|
|
364 |
}
|
|
|
365 |
}
|
|
|
366 |
|
|
|
367 |
public function recupererSignificationCode($code) {
|
|
|
368 |
$url = $this->ajouterHrefAutreProjet('ontologies', 'texteType:', $code.'/nom', 'commun');
|
|
|
369 |
$res = $this->consulterHref($url);
|
|
|
370 |
return $res->nom;
|
|
|
371 |
}
|
|
|
372 |
|
|
|
373 |
public function recupererNomSciCoste($id) {
|
|
|
374 |
$req = 'SELECT nom_sci FROM coste_v0_1 WHERE num_nom = '.$id;
|
|
|
375 |
if ($this->html == 'htm') {
|
|
|
376 |
$req = str_replace('nom_sci', 'nom_sci_html as nom_sci', $req);
|
|
|
377 |
}
|
|
|
378 |
$res = $this->getBdd()->recuperer($req);
|
|
|
379 |
return $res['nom_sci'];
|
|
|
380 |
}
|
|
|
381 |
|
|
|
382 |
public function recupererNomSciBdtfx($id) {
|
|
|
383 |
$req = 'SELECT flore_bdnff_nom_sci FROM coste_v0_1 WHERE flore_bdnff_num = '.$id;
|
|
|
384 |
$res = $this->getBdd()->recuperer($req);
|
|
|
385 |
return $res['flore_bdnff_nom_sci'];
|
|
|
386 |
}
|
|
|
387 |
}
|
|
|
388 |
|
|
|
389 |
|
|
|
390 |
|
|
|
391 |
?>
|