Subversion Repositories eFlore/Applications.del

Rev

Rev 724 | Rev 751 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 724 Rev 746
1
<?php
1
<?php
2
/**
2
/**
3
* Description :
3
* Description :
4
* Classe principale de chargement des services d'eFlore.
4
* Classe principale de chargement des services d'eFlore.
5
*
5
*
6
* Encodage en entrée : utf8
6
* Encodage en entrée : utf8
7
* Encodage en sortie : utf8
7
* Encodage en sortie : utf8
8
* @package eflore-projets
8
* @package eflore-projets
9
* @author Jennifer DHÉ <jennifer.dhe@tela-botanica.org>
9
* @author Jennifer DHÉ <jennifer.dhe@tela-botanica.org>
10
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
10
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
11
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
11
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
12
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
12
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
13
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
13
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
14
* @version 0.1
14
* @version 0.1
15
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
15
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
16
*/
16
*/
17
class Del extends RestService {
17
class Images extends RestService {
-
 
18
 
-
 
19
	
-
 
20
	/*
-
 
21
	 * url possibles : 
-
 
22
	 * http://localhost/del/services/0.1/images/ => toutes les images, appel à la classe ListeImages
-
 
23
	 * http://localhost/del/services/0.1/images/#id => une image donnée => non utilisé sur DEL pour l'instant
-
 
24
	 * http://localhost/del/services/0.1/images/#id/votes/ => tous les votes d'une image classés par protocole
-
 
25
	 * http://localhost/del/services/0.1/images/#id/votes/protocole/#id => tous les votes d'une image et d'un protocole donné
-
 
26
	 * http://localhost/del/services/0.1/images/#id/votes/#id => un vote donné pour une image donnée.
-
 
27
	 * */
18
 
28
	
19
	private $parametres = array();
29
	private $parametres = array();
20
	private $ressources = array();
30
	private $ressources = array();
21
	private $projetNom = array();
31
	private $projetNom = array();
22
	private $serviceNom = array();
32
	private $serviceNom = array();
23
	private $cheminCourant = null;
33
	private $cheminCourant = null;
24
 
34
 
25
	private $conteneur;
35
	private $conteneur;
26
	
36
	
27
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
37
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
28
	protected $utilisationParametresBruts = true;
38
	protected $utilisationParametresBruts = true;
29
 
39
 
30
	public function __construct() {
40
	public function __construct() {
31
		$this->cheminCourant = dirname(__FILE__).DS;
41
		$this->cheminCourant = dirname(__FILE__).DS;
32
	}
42
	}
33
 
43
 
34
	public function consulter($ressources, $parametres) {
44
	public function consulter($ressources, $parametres) {
35
		
45
		
36
		$resultat = '';
46
		$resultat = '';
37
		$reponseHttp = new ReponseHttp();
47
		$reponseHttp = new ReponseHttp();
38
		try {
48
		try {
39
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
49
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
40
			$this->conteneur = new Conteneur($this->parametres);
50
			$this->conteneur = new Conteneur($this->parametres);
41
			$resultat = $this->traiterRessources();
51
			$resultat = $this->traiterRessources();
42
			$reponseHttp->setResultatService($resultat);
52
			$reponseHttp->setResultatService($resultat);
43
		} catch (Exception $e) {
53
		} catch (Exception $e) {
44
			$reponseHttp->ajouterErreur($e);
54
			$reponseHttp->ajouterErreur($e);
45
		}
55
		}
46
		$reponseHttp->emettreLesEntetes();
56
		$reponseHttp->emettreLesEntetes();
47
		$corps = $reponseHttp->getCorps();
57
		$corps = $reponseHttp->getCorps();
48
		return $corps;
58
		return $corps;
49
	}
59
	}
50
 
60
 
51
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
61
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
52
		$this->ressources = $ressources;
62
		$this->ressources = $ressources;
53
		$this->parametres = $parametres;
63
		$this->parametres = $parametres;
54
	}
64
	}
55
 
65
 
56
	private function traiterRessources() {
66
	private function traiterRessources() {
57
		$retour = '';
67
		$retour = '';
58
		if ($this->avoirRessources()) {
68
		if ($this->avoirRessources()) {
59
			if ($this->avoirRessourceProjet()) {
69
			//if ($this->avoirRessourceProjet()) {
60
				$this->initialiserProjet();
70
				$this->initialiserProjet();
61
				if ($this->avoirRessourceService()) {
71
				if ($this->avoirRessourceService()) {
62
					$retour = $this->initialiserService();
72
					$retour = $this->initialiserService();
63
				}
73
				}
64
			}
74
			//}
65
		}
75
		}
66
		return $retour;
76
		return $retour;
67
	}
77
	}
68
 
78
 
69
	private function avoirRessources() {
79
	private function avoirRessources() {
70
		$presenceDeRessources = false;
80
		$presenceDeRessources = false;
71
		if (isset($this->ressources) && count($this->ressources) > 0) {
81
		if (isset($this->ressources) && count($this->ressources) > 0) {
72
			$presenceDeRessources = true;
82
			$presenceDeRessources = true;
73
		} else {
83
		} else {
74
			$message = "Aucune ressource n'a été indiquée.\n".
84
			$message = "Aucune ressource n'a été indiquée.\n".
75
				"Veuillez indiquer au moins un code de projet et un type de service.";
85
				"Veuillez indiquer au moins un code de projet et un type de service.";
76
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
86
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
77
			throw new Exception($message, $code);
87
			throw new Exception($message, $code);
78
		}
88
		}
79
		return $presenceDeRessources;
89
		return $presenceDeRessources;
80
	}
90
	}
81
 
-
 
82
	private function avoirRessourceProjet() {
-
 
83
		$presenceRessourceProjet = false;
-
 
84
		$projet = $this->ressources[0];
-
 
85
		$projetsDispo = Outils::recupererTableauConfig('projetsDispo');
-
 
86
		if (in_array($projet, $projetsDispo)) {
-
 
87
			$presenceRessourceProjet = true;
-
 
88
		} else {
-
 
89
			$message = "La ressource '$projet' n'indique pas un projet existant.\n".
-
 
90
				"Les projets existant sont :\n".implode(', ', $projetsDispo);
-
 
91
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
-
 
92
			throw new Exception($message, $code);
-
 
93
		}
-
 
94
		return $presenceRessourceProjet;
-
 
95
	}
-
 
96
 
91
 
97
	private function initialiserProjet() {
92
	private function initialiserProjet() {
98
		$this->chargerNomDuProjet();
93
		$this->chargerNomDuProjet();
99
		$this->chargerConfigProjet();
94
		$this->chargerConfigProjet();
100
 
95
 
101
		// php5.3 : Enregistrement en première position des autoload de la méthode gérant les classes des services
96
		// php5.3 : Enregistrement en première position des autoload de la méthode gérant les classes des services
102
		if (phpversion() < 5.3) {
97
		if (phpversion() < 5.3) {
103
			spl_autoload_register(array($this, 'chargerClasseProjet'));
98
			spl_autoload_register(array($this, 'chargerClasseProjet'));
104
		} else {
99
		} else {
105
			spl_autoload_register(array($this, 'chargerClasseProjet'), true , true);
100
			spl_autoload_register(array($this, 'chargerClasseProjet'), true , true);
106
		}
101
		}
107
	}
102
	}
108
 
103
 
109
	private function chargerNomDuProjet() {
104
	private function chargerNomDuProjet() {
110
		$this->projetNom = $this->ressources[0];
105
		$this->projetNom = 'images';
111
	}
106
	}
112
 
107
 
113
	private function chargerConfigProjet() {
108
	private function chargerConfigProjet() {
114
		$projet = $this->projetNom;
109
		$projet = $this->projetNom;
115
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
110
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
116
		Config::charger($chemin);
111
		Config::charger($chemin);
117
	}
112
	}
118
 
113
 
119
	private function chargerClasseProjet($classe) {
114
	private function chargerClasseProjet($classe) {
120
		if (class_exists($classe)) {
115
		if (class_exists($classe)) {
121
			return null;
116
			return null;
122
		}
117
		}
123
 
118
 
124
		$cheminBiblio = Config::get('chemin_bibliotheque');
119
		$cheminBiblio = Config::get('chemin_bibliotheque');
125
		$chemins = array();
120
		$chemins = array();
126
		$chemins[] = $this->cheminCourant.$this->projetNom.DS;
121
		$chemins[] = $this->cheminCourant.$this->projetNom.DS;
127
		$chemins[] = $this->cheminCourant.'commun'.DS;
122
		$chemins[] = $this->cheminCourant.'commun'.DS;
128
		$chemins[] = $cheminBiblio;
123
		$chemins[] = $cheminBiblio;
129
		$chemins[] = $cheminBiblio.'robots'.DS;
124
		$chemins[] = $cheminBiblio.'robots'.DS;
130
 
125
 
131
		foreach ($chemins as $chemin) {
126
		foreach ($chemins as $chemin) {
132
			$chemin = $chemin.$classe.'.php';
127
			$chemin = $chemin.$classe.'.php';
133
			if (file_exists($chemin)) {
128
			if (file_exists($chemin)) {
134
				require_once $chemin;
129
				require_once $chemin;
135
				break;
130
				break;
136
			}
131
			}
137
		}
132
		}
138
	}
133
	}
139
 
134
 
140
	private function avoirRessourceService() {
135
	private function avoirRessourceService() {
141
		$presenceRessourceService = false;
136
		$presenceRessourceService = false;
142
		$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
137
		$servicesDispo = Outils::recupererTableauConfig('servicesDispo');
143
	
138
	
144
		if (isset($this->ressources[1])) {
139
		if (isset($this->ressources[0])) {
145
			$service = $this->ressources[1];
140
			$service = $this->ressources[0];
146
			if (in_array($service, $servicesDispo)) {
141
			if (in_array($service, $servicesDispo)) {
147
				$presenceRessourceService = true;
142
				$presenceRessourceService = true;
148
			} else {
143
			} else {
149
				$message = "La service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
144
				$message = "La service demandé '$service' n'est pas disponible pour le projet {$this->projetNom} !\n".
150
					"Les services disponibles sont : ".implode(', ', $servicesDispo);
145
					"Les services disponibles sont : ".implode(', ', $servicesDispo);
151
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
146
				$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
152
				throw new Exception($message, $code);
147
				throw new Exception($message, $code);
153
			}
148
			}
154
		} else {
149
		} else {
155
			$message = "Vous n'avez pas indiqué de service pour le projet {$this->projetNom} !\n".
150
			$message = "Vous n'avez pas indiqué de service pour le projet {$this->projetNom} !\n".
156
				"Les services disponibles sont : ".implode(', ', $servicesDispo);
151
				"Les services disponibles sont : ".implode(', ', $servicesDispo);
157
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
152
			$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE;
158
			throw new Exception($message, $code);
153
			throw new Exception($message, $code);
159
		}
154
		}
160
		return $presenceRessourceService;
155
		return $presenceRessourceService;
161
	}
156
	}
162
 
157
 
163
	private function initialiserService() {
158
	private function initialiserService() {
164
		$this->chargerNomDuService();
159
		$this->chargerNomDuService();
-
 
160
		
165
		$classe = $this->obtenirNomClasseService($this->serviceNom);
161
		$classe = $this->obtenirNomClasseService($this->serviceNom);
166
		$chemins = array();
162
		$chemins = array();
167
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
163
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
168
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
164
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
169
 
165
 
170
		$retour = '';
166
		$retour = '';
171
		$service = null;
167
		$service = null;
172
		foreach ($chemins as $chemin) {
168
		foreach ($chemins as $chemin) {
173
			if (file_exists($chemin)) {
169
			if (file_exists($chemin)) {
174
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
170
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
175
				$service = new $classe($this->conteneur);
171
				$service = new $classe($this->conteneur);
176
				$ressourcesPourService = $this->filtrerRessourcesPourService();
172
				$ressourcesPourService = $this->filtrerRessourcesPourService();
177
				$retour = $service->consulter($ressourcesPourService, $this->parametres);
173
				$retour = $service->consulter($ressourcesPourService, $this->parametres);
178
			}
174
			}
179
		}
175
		}
-
 
176
		
180
		if (is_null($service)) {
177
		if (is_null($service)) {
181
			$message = "La service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
178
			$message = "La service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
182
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
179
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
183
			throw new Exception($message, $code);
180
			throw new Exception($message, $code);
184
		}
181
		}
185
		return $retour;
182
		return $retour;
186
	}
183
	}
187
 
184
 
188
	private function chargerNomDuService() {
185
	private function chargerNomDuService() {
189
		$this->serviceNom = $this->ressources[1];
186
		$this->serviceNom = $this->ressources[0];
190
	}
187
	}
191
 
188
 
192
	private function obtenirNomClasseService($mot) {
189
	private function obtenirNomClasseService($mot) {
193
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
190
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
194
		return $classeNom;
191
		return $classeNom;
195
	}
192
	}
196
 
193
 
197
	private function filtrerRessourcesPourService() {
194
	private function filtrerRessourcesPourService() {
198
		$ressourcesPourService = array();
195
		$ressourcesPourService = array();
199
		$nbreDeRessources = count($this->ressources);
196
		$nbreDeRessources = count($this->ressources);
200
		for ($i = 2; $i < $nbreDeRessources; $i++) {
197
		for ($i = 2; $i < $nbreDeRessources; $i++) {
201
			$ressourcesPourService[] = $this->ressources[$i];
198
			$ressourcesPourService[] = $this->ressources[$i];
202
		}
199
		}
203
		return $ressourcesPourService;
200
		return $ressourcesPourService;
204
	}
201
	}
205
}
202
}
206
?>
203
?>