Subversion Repositories eFlore/Applications.del

Rev

Rev 1049 | Rev 1661 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1049 Rev 1057
1
<?php
1
<?php
2
/**
2
/**
3
* Description :
3
* Description :
4
* Classe principale de chargement des services de syndication
4
* Classe principale de chargement des services de syndication
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 del-service
8
* @package del-service
9
* @author Aurélien PERONNET <aurelien@tela-botanica.org>
9
* @author Aurélien PERONNET <aurelien@tela-botanica.org>
10
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
10
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
11
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
11
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
12
* @version 0.1
12
* @version 0.1
13
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
13
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
14
*/
14
*/
15
class Syndication extends RestService {
15
class Syndication extends RestService {
16
 
16
 
17
	
17
	
18
	private $parametres = array();
18
	private $parametres = array();
19
	private $ressources = array();
19
	private $ressources = array();
20
	private $methode = null;
20
	private $methode = null;
21
	private $projetNom = array();
21
	private $projetNom = array();
22
	private $serviceNom = array();
22
	private $serviceNom = array();
23
	private $cheminCourant = null;
23
	private $cheminCourant = null;
24
	private $squelette_dossier = null;
24
	private $squelette_dossier = null;
25
	// TODO: si dans 1000 ans un nouveau format de flux apparaît, il faudra mettre ceci dans le fichier de config
-
 
26
	private $formats_autorises = array('rss1','rss2','atom');
25
	private $formats_autorises = null;
27
 
26
 
28
	private $conteneur;
27
	private $conteneur;
29
	
28
	
30
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
29
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
31
	protected $utilisationParametresBruts = true;
30
	protected $utilisationParametresBruts = true;
32
 
31
 
33
	public function __construct() {
32
	public function __construct() {
34
		$this->cheminCourant = dirname(__FILE__).DS;
33
		$this->cheminCourant = dirname(__FILE__).DS;
35
		$this->squelette_dossier = dirname(__FILE__).DIRECTORY_SEPARATOR.'syndication'.DIRECTORY_SEPARATOR.'squelettes'.DIRECTORY_SEPARATOR;
34
		$this->squelette_dossier = dirname(__FILE__).DIRECTORY_SEPARATOR.'syndication'.DIRECTORY_SEPARATOR.'squelettes'.DIRECTORY_SEPARATOR;
36
	}
35
	}
37
 
36
 
38
	public function consulter($ressources, $parametres) {
37
	public function consulter($ressources, $parametres) {
39
		$this->methode = 'consulter';
38
		$this->methode = 'consulter';
40
		$resultat = '';
39
		$resultat = '';
41
		$reponseHttp = new ReponseHttp();
40
		$reponseHttp = new ReponseHttp();
42
		try {
41
		try {
-
 
42
			$this->initialiserProjet();
43
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
43
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
44
			$this->verifierRessourcesEtParametres();
-
 
45
			$this->conteneur = new Conteneur($this->parametres);
44
			$this->conteneur = new Conteneur($this->parametres);
-
 
45
			$this->verifierRessourcesEtParametres();
46
			$resultat = $this->traiterRessources();
46
			$resultat = $this->traiterRessources();
47
			$reponseHttp->setResultatService($this->creerResultatService($resultat));
47
			$reponseHttp->setResultatService($this->creerResultatService($resultat));
48
		} catch (Exception $e) {
48
		} catch (Exception $e) {
49
			$reponseHttp->ajouterErreur($e);
49
			$reponseHttp->ajouterErreur($e);
50
		}
50
		}
51
		$reponseHttp->emettreLesEntetes();
51
		$reponseHttp->emettreLesEntetes();
52
		$corps = $reponseHttp->getCorps();
52
		$corps = $reponseHttp->getCorps();
53
		return $corps;
53
		return $corps;
54
	}
54
	}
55
	
55
	
56
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
56
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
57
		$this->ressources = $ressources;
57
		$this->ressources = $ressources;
58
		$this->parametres = $parametres;
58
		$this->parametres = $parametres;
59
	}
59
	}
60
	
60
	
61
	private function verifierRessourcesEtParametres() {
61
	private function verifierRessourcesEtParametres() {
-
 
62
		
-
 
63
		$servicesDispos = Config::get('servicesDispo');
-
 
64
		if (!isset($this->ressources[0]) || !in_array($this->ressources[0], explode(',',$servicesDispos))) {
-
 
65
			$message = "Vous devez indiquer un nom de service valide, les services disponibles sont ".$servicesDispos;
-
 
66
			$code = RestServeur::HTTP_CODE_ERREUR;
-
 
67
			throw new Exception($message, $code);
-
 
68
		}
-
 
69
		
-
 
70
		$chaineFormatsAutorises = Config::get('formatsRss');
-
 
71
		$this->formats_autorises = explode(',', $chaineFormatsAutorises);
62
		if (!isset($this->ressources[1]) || !in_array($this->ressources[1], $this->formats_autorises)) {
72
		if (!isset($this->ressources[1]) || !in_array($this->ressources[1], $this->formats_autorises)) {
63
			$message = "Vous devez indiquer un format de flux valide, les formats acceptés sont ".implode(', ',$this->formats_autorises);
73
			$message = "Vous devez indiquer un format de flux valide, les formats acceptés sont ".$chaineFormatsAutorises;
64
			$code = RestServeur::HTTP_CODE_ERREUR;
74
			$code = RestServeur::HTTP_CODE_ERREUR;
65
			throw new Exception($message, $code);
75
			throw new Exception($message, $code);
66
		} else {
76
		} else {
67
			$this->format = $this->ressources[1];
77
			$this->format = $this->ressources[1];
68
		}
78
		}
69
	}
79
	}
70
 
80
 
71
	private function traiterRessources() {
81
	private function traiterRessources() {
72
		$retour = '';
82
		$retour = '';
73
		$this->initialiserProjet();
-
 
74
		$retour = $this->initialiserService();
83
		$retour = $this->initialiserService();
75
 
84
 
76
		return $retour;
85
		return $retour;
77
	}
86
	}
78
	
87
	
79
	private function creerResultatService($donnees) {
88
	private function creerResultatService($donnees) {
80
		$resultat = new ResultatService();
89
		$resultat = new ResultatService();
81
		$resultat->mime = $this->getTypeMime();
90
		$resultat->mime = $this->getTypeMime();
82
		$resultat->corps = SquelettePhp::analyser($this->squelette_dossier.$this->format.'.tpl.xml', $donnees);
91
		$resultat->corps = SquelettePhp::analyser($this->squelette_dossier.$this->format.'.tpl.xml', $donnees);
83
		
92
		
84
		return $resultat;
93
		return $resultat;
85
	}
94
	}
86
 
95
 
87
	/*------------------------------------------------------------------------------------------------------------------
96
	/*------------------------------------------------------------------------------------------------------------------
88
										CONFIGURATION DU PROJET
97
										CONFIGURATION DU PROJET
89
	------------------------------------------------------------------------------------------------------------------*/
98
	------------------------------------------------------------------------------------------------------------------*/
90
	private function initialiserProjet() {
99
	private function initialiserProjet() {
91
		$this->projetNom = 'syndication';
100
		$this->projetNom = 'syndication';
92
		$this->chargerConfigProjet();
101
		$this->chargerConfigProjet();
93
	}
102
	}
94
 
103
 
95
	private function chargerConfigProjet() {
104
	private function chargerConfigProjet() {
96
		$projet = $this->projetNom;
105
		$projet = $this->projetNom;
97
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
106
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
98
		Config::charger($chemin);
107
		Config::charger($chemin);
99
	}
108
	}
100
 
109
 
101
	/*------------------------------------------------------------------------------------------------------------------
110
	/*------------------------------------------------------------------------------------------------------------------
102
								CONFIGURATION DU SERVICE
111
								CONFIGURATION DU SERVICE
103
	------------------------------------------------------------------------------------------------------------------*/
112
	------------------------------------------------------------------------------------------------------------------*/
104
	private function initialiserService() {
113
	private function initialiserService() {
105
		$this->chargerNomService();
114
		$this->chargerNomService();
106
				
115
				
107
		$classe = $this->obtenirNomClasseService($this->serviceNom);
116
		$classe = $this->obtenirNomClasseService($this->serviceNom);
108
		$chemins = array();
117
		$chemins = array();
109
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
118
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
110
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
119
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
111
		$retour = '';
120
		$retour = '';
112
		$service = null;
121
		$service = null;
113
		foreach ($chemins as $chemin) {
122
		foreach ($chemins as $chemin) {
114
			if (file_exists($chemin)) {
123
			if (file_exists($chemin)) {
115
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');	
124
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');	
116
				require_once $chemin;
125
				require_once $chemin;
117
				$service = new $classe($this->conteneur);
126
				$service = new $classe($this->conteneur);
118
				if ($this->methode == 'consulter') {
127
				if ($this->methode == 'consulter') {
119
					$retour = $service->consulter($this->ressources, $this->parametres);
128
					$retour = $service->consulter($this->ressources, $this->parametres);
120
				}
129
				}
121
			}
130
			}
122
		}
131
		}
123
		
132
		
124
		if (is_null($service)) {
133
		if (is_null($service)) {
125
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
134
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
126
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
135
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
127
			throw new Exception($message, $code);
136
			throw new Exception($message, $code);
128
		}
137
		}
129
		return $retour;
138
		return $retour;
130
	}
139
	}
131
	
140
	
132
	private function chargerNomService() {
141
	private function chargerNomService() {
133
		if (!isset($this->ressources[0])) {
142
		if (!isset($this->ressources[0])) {
134
			$message = "Vous devez indiquer un nom de service";
143
			$message = "Vous devez indiquer un nom de service";
135
			$code = RestServeur::HTTP_CODE_ERREUR;
144
			$code = RestServeur::HTTP_CODE_ERREUR;
136
			throw new Exception($message, $code);
145
			throw new Exception($message, $code);
137
		} else {
146
		} else {
138
			$this->serviceNom = $this->ressources[0];
147
			$this->serviceNom = $this->ressources[0];
139
		}
148
		}
140
	}
149
	}
141
 
150
 
142
	private function obtenirNomClasseService($mot) {
151
	private function obtenirNomClasseService($mot) {
143
		$classeNom = 'Syndication'.ucwords($mot);
152
		$classeNom = 'Syndication'.ucwords($mot);
144
		return $classeNom;
153
		return $classeNom;
145
	}
154
	}
146
	
155
	
147
	private function getTypeMime() {
156
	private function getTypeMime() {
148
		$mime = '';
157
		$mime = '';
149
		switch ($this->format) {
158
		switch ($this->format) {
150
			case 'atom' :
159
			case 'atom' :
151
				$mime = 'application/atom+xml';
160
				$mime = 'application/atom+xml';
152
				break;
161
				break;
153
			case 'rss1' :
162
			case 'rss1' :
154
			case 'rss2' :
163
			case 'rss2' :
155
				$mime = 'application/rss+xml';
164
				$mime = 'application/rss+xml';
156
				break;
165
				break;
157
			case 'opml' :
166
			case 'opml' :
158
				$mime = 'text/x-opml';
167
				$mime = 'text/x-opml';
159
				break;
168
				break;
160
			default:
169
			default:
161
				$mime = 'text/html';
170
				$mime = 'text/html';
162
		}
171
		}
163
		return $mime;
172
		return $mime;
164
	}
173
	}
165
}
174
}
166
?>
175
?>