999 |
aurelien |
1 |
<?php
|
|
|
2 |
Class CelEfloreImages extends Cel {
|
|
|
3 |
|
1000 |
aurelien |
4 |
private $formats_supportes = array('image/jpeg');
|
|
|
5 |
|
999 |
aurelien |
6 |
function getElement($uid) {
|
|
|
7 |
return getRessource();
|
|
|
8 |
}
|
|
|
9 |
|
|
|
10 |
function getRessource(){
|
|
|
11 |
|
|
|
12 |
$parametres = $this->recupererParametresBruts();
|
1000 |
aurelien |
13 |
$header = '';
|
|
|
14 |
|
1001 |
aurelien |
15 |
if (!$erreurs = $this->parametresOntErreurs($parametres)) {
|
999 |
aurelien |
16 |
|
1000 |
aurelien |
17 |
$num_nom = $parametres['masque.nn'];
|
|
|
18 |
$retour = isset($parametres['retour']) ? $parametres['retour'] : 'image/jpeg';
|
|
|
19 |
$format = isset($parametres['retour.format']) ? $parametres['retour.format'] : 'M';
|
|
|
20 |
|
|
|
21 |
$id_image_a_renvoyer = $this->obtenirImageAuHasardPourNumNom($num_nom);
|
|
|
22 |
|
|
|
23 |
$header = $id_image_a_renvoyer ? "HTTP/1.0 200 OK" : "HTTP/1.0 404 Not Found";
|
|
|
24 |
$erreurs = $id_image_a_renvoyer ? false : 'Aucune image ne corespond au numéro numenclatural '.$num_nom."\n";
|
|
|
25 |
} else {
|
|
|
26 |
$header = "HTTP/1.0 400 Bad Request";
|
|
|
27 |
}
|
999 |
aurelien |
28 |
|
1000 |
aurelien |
29 |
header($header);
|
|
|
30 |
|
|
|
31 |
$erreurs ? $this->renvoyerTexteSimple($erreurs) : $this->renvoyerImageExistanteOuGeneree($id_image_a_renvoyer, $format);
|
|
|
32 |
exit;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
private function renvoyerTexteSimple($erreurs) {
|
|
|
36 |
header("Content-type: text/plain");
|
|
|
37 |
echo $erreurs;
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
private function renvoyerImageExistanteOuGeneree($id_image, $format = 'M') {
|
|
|
41 |
|
999 |
aurelien |
42 |
$generateur_image = new ImageRecreation($this->config);
|
|
|
43 |
|
1000 |
aurelien |
44 |
header("Content-type: image/jpeg");
|
|
|
45 |
if ($this->estUnFormatDejaCree($format)) {
|
|
|
46 |
$nom = $generateur_image->convertirIdBddVersNomFichier($id_image, $format);
|
|
|
47 |
$dossier = $generateur_image->obtenirDossierPourFormat($id_image, $format);
|
|
|
48 |
$chemin_image = $dossier.'/'.$nom;
|
|
|
49 |
echo file_get_contents($chemin_image);
|
|
|
50 |
} else {
|
|
|
51 |
$infos_image = $generateur_image->obtenirImageEtInfosPourId($id_image);
|
|
|
52 |
$image_a_renvoyer = $generateur_image->creerMiniatureImageSelonFormat($infos_image, $format);
|
|
|
53 |
imagejpeg($image_a_renvoyer);
|
999 |
aurelien |
54 |
}
|
|
|
55 |
}
|
|
|
56 |
|
1000 |
aurelien |
57 |
private function obtenirImageAuHasardPourNumNom($numero_nomenclatural) {
|
|
|
58 |
|
999 |
aurelien |
59 |
$requete = 'SELECT cim.ci_id_image as id_image '.
|
|
|
60 |
'FROM cel_obs_images AS coi '.
|
|
|
61 |
'LEFT JOIN cel_inventory AS ci '.
|
|
|
62 |
'ON (coi.coi_ce_observation = ci.ordre AND coi.coi_ce_utilisateur = ci.identifiant) '.
|
|
|
63 |
'LEFT JOIN cel_images AS cim '.
|
|
|
64 |
'ON (coi.coi_ce_image = cim.ci_id_image AND coi.coi_ce_utilisateur = cim.ci_ce_utilisateur) '.
|
|
|
65 |
'WHERE ci.transmission = 1 '.
|
|
|
66 |
' AND num_nom_sel = '.$this->proteger($numero_nomenclatural).
|
|
|
67 |
' AND ci.identifiant = cim.ci_ce_utilisateur ';
|
|
|
68 |
|
|
|
69 |
$resultat_images = $this->executerRequete($requete);
|
1000 |
aurelien |
70 |
$id_image_hasard = (is_array($resultat_images) && count($resultat_images) > 0) ? $resultat_images[array_rand($resultat_images)]['id_image'] : false;
|
|
|
71 |
|
999 |
aurelien |
72 |
return $id_image_hasard;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
private function nettoyerParametres(Array $parametres) {
|
|
|
76 |
// Pas besoin d'utiliser urldecode car déjà fait par php pour les clés et valeur de $_GET
|
|
|
77 |
if (isset($parametres) && count($parametres) > 0) {
|
1000 |
aurelien |
78 |
foreach ($parametres as $cle => $valeur) {
|
|
|
79 |
$verifier = array('NULL', "\n", "\r", "\\", "'", '"', "\x00", "\x1a", ';');
|
|
|
80 |
$parametres[$cle] = strip_tags(str_replace($verifier, '', $valeur));
|
|
|
81 |
}
|
999 |
aurelien |
82 |
}
|
|
|
83 |
return $parametres;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
private function recupererParametresBruts() {
|
|
|
87 |
$parametres_bruts = array();
|
|
|
88 |
if (!empty($_SERVER['QUERY_STRING'])) {
|
1000 |
aurelien |
89 |
$paires = explode('&', $_SERVER['QUERY_STRING']);
|
|
|
90 |
foreach ($paires as $paire) {
|
|
|
91 |
$nv = explode('=', $paire);
|
|
|
92 |
$nom = urldecode($nv[0]);
|
|
|
93 |
$valeur = urldecode($nv[1]);
|
|
|
94 |
$parametres_bruts[$nom] = $valeur;
|
|
|
95 |
}
|
|
|
96 |
$parametres_bruts = $this->nettoyerParametres($parametres_bruts);
|
999 |
aurelien |
97 |
}
|
|
|
98 |
return $parametres_bruts;
|
|
|
99 |
}
|
|
|
100 |
|
1000 |
aurelien |
101 |
private function parametresOntErreurs($parametres) {
|
|
|
102 |
$erreurs = false;
|
|
|
103 |
if (!isset($parametres['masque.nn'])) {
|
|
|
104 |
$erreurs .= 'Le paramètre masque.nn est obligatoire '."\n";
|
|
|
105 |
} else {
|
1001 |
aurelien |
106 |
if (!is_numeric($parametres['masque.nn'])) {
|
1000 |
aurelien |
107 |
$erreurs .= 'Le paramètre masque.nn est mal formé '."\n";
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
if (isset($parametres['retour']) && !$this->retourEstSupporte($parametres['retour'])) {
|
|
|
112 |
$erreurs .= 'Le type de retour '.$parametres['retour'].' n\'est pas supporté '."\n";
|
|
|
113 |
}
|
|
|
114 |
|
1001 |
aurelien |
115 |
if (isset($parametres['retour.format']) && !$this->formatEstSupporte($parametres['retour.format'])) {
|
1000 |
aurelien |
116 |
$erreurs .= 'Le type de format '.$parametres['retour.format'].' n\'est pas supporté '."\n";
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
return $erreurs;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
private function getFormats() {
|
|
|
123 |
$formats = array();
|
|
|
124 |
|
|
|
125 |
foreach ($this->config['cel_db'] as $param_config => $valeur) {
|
1001 |
aurelien |
126 |
if (substr_count($param_config,'format_') != 0) {
|
1000 |
aurelien |
127 |
$formats[$param_config] = $valeur;
|
|
|
128 |
}
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
return $formats;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
private function retourEstSupporte($retour = 'image/jpeg') {
|
|
|
135 |
return in_array($retour, $this->formats_supportes);
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
private function formatEstSupporte($format = 'M') {
|
|
|
139 |
|
|
|
140 |
$format_supporte = true;
|
|
|
141 |
|
|
|
142 |
// cas du format déjà existant (S, M, L etc...)
|
|
|
143 |
$format_supporte = $this->estUnFormatDejaCree($format);
|
|
|
144 |
|
|
|
145 |
//TODO: décommenter les lignes suivantes lorsque les formats "à la demande" seront implémentés
|
|
|
146 |
// cas du coté maximal de l'image demandé
|
|
|
147 |
//$format_supporte = is_numeric($format) ? true : false;
|
|
|
148 |
// cas du format exact demande Lxl
|
|
|
149 |
//$format_supporte = preg_match('/[0-9]*x[0-9]*/', $format) ? true : false;
|
|
|
150 |
|
|
|
151 |
return $format_supporte;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
private function estUnFormatDejaCree($format = 'M') {
|
|
|
155 |
$formats = $this->getFormats();
|
|
|
156 |
$format_supporte = isset($formats['format_'.$format]) ? true : false;
|
|
|
157 |
|
|
|
158 |
return $format_supporte;
|
|
|
159 |
}
|
999 |
aurelien |
160 |
}
|
|
|
161 |
?>
|