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