Subversion Repositories eFlore/Applications.del

Rev

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

Rev 1047 Rev 1049
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
25
	private $format = null;
26
	private $formats_autorises = array('rss1','rss2','atom');
26
 
27
 
27
	private $conteneur;
28
	private $conteneur;
28
	
29
	
29
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
30
	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
30
	protected $utilisationParametresBruts = true;
31
	protected $utilisationParametresBruts = true;
31
 
32
 
32
	public function __construct() {
33
	public function __construct() {
33
		$this->cheminCourant = dirname(__FILE__).DS;
34
		$this->cheminCourant = dirname(__FILE__).DS;
34
		$this->squelette_dossier = dirname(__FILE__).DIRECTORY_SEPARATOR.'syndication'.DIRECTORY_SEPARATOR.'squelettes'.DIRECTORY_SEPARATOR;
35
		$this->squelette_dossier = dirname(__FILE__).DIRECTORY_SEPARATOR.'syndication'.DIRECTORY_SEPARATOR.'squelettes'.DIRECTORY_SEPARATOR;
35
	}
36
	}
36
 
37
 
37
	public function consulter($ressources, $parametres) {
38
	public function consulter($ressources, $parametres) {
38
		$this->methode = 'consulter';
39
		$this->methode = 'consulter';
39
		$resultat = '';
40
		$resultat = '';
40
		$reponseHttp = new ReponseHttp();
41
		$reponseHttp = new ReponseHttp();
41
		try {
42
		try {
42
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
43
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
43
			$this->verifierRessourcesEtParametres();
44
			$this->verifierRessourcesEtParametres();
44
			$this->conteneur = new Conteneur($this->parametres);
45
			$this->conteneur = new Conteneur($this->parametres);
45
			$resultat = $this->traiterRessources();
46
			$resultat = $this->traiterRessources();
46
			$reponseHttp->setResultatService($this->creerResultatService($resultat));
47
			$reponseHttp->setResultatService($this->creerResultatService($resultat));
47
		} catch (Exception $e) {
48
		} catch (Exception $e) {
48
			$reponseHttp->ajouterErreur($e);
49
			$reponseHttp->ajouterErreur($e);
49
		}
50
		}
50
		$reponseHttp->emettreLesEntetes();
51
		$reponseHttp->emettreLesEntetes();
51
		$corps = $reponseHttp->getCorps();
52
		$corps = $reponseHttp->getCorps();
52
		return $corps;
53
		return $corps;
53
	}
54
	}
54
	
55
	
55
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
56
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
56
		$this->ressources = $ressources;
57
		$this->ressources = $ressources;
57
		$this->parametres = $parametres;
58
		$this->parametres = $parametres;
58
	}
59
	}
59
	
60
	
60
	private function verifierRessourcesEtParametres() {
61
	private function verifierRessourcesEtParametres() {
61
		if (!isset($this->ressources[1])) {
62
		if (!isset($this->ressources[1]) || !in_array($this->ressources[1], $this->formats_autorises)) {
62
			$message = "Vous devez indiquer un format de flux";
63
			$message = "Vous devez indiquer un format de flux valide, les formats acceptés sont ".implode(', ',$this->formats_autorises);
63
			$code = RestServeur::HTTP_CODE_ERREUR;
64
			$code = RestServeur::HTTP_CODE_ERREUR;
64
			throw new Exception($message, $code);
65
			throw new Exception($message, $code);
65
		} else {
66
		} else {
66
			$this->format = $this->ressources[1];
67
			$this->format = $this->ressources[1];
67
		}
68
		}
68
	}
69
	}
69
 
70
 
70
	private function traiterRessources() {
71
	private function traiterRessources() {
71
		$retour = '';
72
		$retour = '';
72
		$this->initialiserProjet();
73
		$this->initialiserProjet();
73
		$retour = $this->initialiserService();
74
		$retour = $this->initialiserService();
74
 
75
 
75
		return $retour;
76
		return $retour;
76
	}
77
	}
77
	
78
	
78
	private function creerResultatService($donnees) {
79
	private function creerResultatService($donnees) {
79
		$resultat = new ResultatService();
80
		$resultat = new ResultatService();
80
		$resultat->mime = $this->getTypeMime();
81
		$resultat->mime = $this->getTypeMime();
81
		$resultat->corps = SquelettePhp::analyser($this->squelette_dossier.$this->format.'.tpl.xml', $donnees);
82
		$resultat->corps = SquelettePhp::analyser($this->squelette_dossier.$this->format.'.tpl.xml', $donnees);
82
		
83
		
83
		return $resultat;
84
		return $resultat;
84
	}
85
	}
85
 
86
 
86
	/*------------------------------------------------------------------------------------------------------------------
87
	/*------------------------------------------------------------------------------------------------------------------
87
										CONFIGURATION DU PROJET
88
										CONFIGURATION DU PROJET
88
	------------------------------------------------------------------------------------------------------------------*/
89
	------------------------------------------------------------------------------------------------------------------*/
89
	private function initialiserProjet() {
90
	private function initialiserProjet() {
90
		$this->projetNom = 'syndication';
91
		$this->projetNom = 'syndication';
91
		$this->chargerConfigProjet();
92
		$this->chargerConfigProjet();
92
	}
93
	}
93
 
94
 
94
	private function chargerConfigProjet() {
95
	private function chargerConfigProjet() {
95
		$projet = $this->projetNom;
96
		$projet = $this->projetNom;
96
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
97
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
97
		Config::charger($chemin);
98
		Config::charger($chemin);
98
	}
99
	}
99
 
100
 
100
	/*------------------------------------------------------------------------------------------------------------------
101
	/*------------------------------------------------------------------------------------------------------------------
101
								CONFIGURATION DU SERVICE
102
								CONFIGURATION DU SERVICE
102
	------------------------------------------------------------------------------------------------------------------*/
103
	------------------------------------------------------------------------------------------------------------------*/
103
	private function initialiserService() {
104
	private function initialiserService() {
104
		$this->chargerNomService();
105
		$this->chargerNomService();
105
				
106
				
106
		$classe = $this->obtenirNomClasseService($this->serviceNom);
107
		$classe = $this->obtenirNomClasseService($this->serviceNom);
107
		$chemins = array();
108
		$chemins = array();
108
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
109
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
109
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
110
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
110
		$retour = '';
111
		$retour = '';
111
		$service = null;
112
		$service = null;
112
		foreach ($chemins as $chemin) {
113
		foreach ($chemins as $chemin) {
113
			if (file_exists($chemin)) {
114
			if (file_exists($chemin)) {
114
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');	
115
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');	
115
				require_once $chemin;
116
				require_once $chemin;
116
				$service = new $classe($this->conteneur);
117
				$service = new $classe($this->conteneur);
117
				if ($this->methode == 'consulter') {
118
				if ($this->methode == 'consulter') {
118
					$retour = $service->consulter($this->ressources, $this->parametres);
119
					$retour = $service->consulter($this->ressources, $this->parametres);
119
				}
120
				}
120
			}
121
			}
121
		}
122
		}
122
		
123
		
123
		if (is_null($service)) {
124
		if (is_null($service)) {
124
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
125
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
125
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
126
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
126
			throw new Exception($message, $code);
127
			throw new Exception($message, $code);
127
		}
128
		}
128
		return $retour;
129
		return $retour;
129
	}
130
	}
130
	
131
	
131
	private function chargerNomService() {
132
	private function chargerNomService() {
132
		if (!isset($this->ressources[0])) {
133
		if (!isset($this->ressources[0])) {
133
			$message = "Vous devez indiquer un nom de service";
134
			$message = "Vous devez indiquer un nom de service";
134
			$code = RestServeur::HTTP_CODE_ERREUR;
135
			$code = RestServeur::HTTP_CODE_ERREUR;
135
			throw new Exception($message, $code);
136
			throw new Exception($message, $code);
136
		} else {
137
		} else {
137
			$this->serviceNom = $this->ressources[0];
138
			$this->serviceNom = $this->ressources[0];
138
		}
139
		}
139
	}
140
	}
140
 
141
 
141
	private function obtenirNomClasseService($mot) {
142
	private function obtenirNomClasseService($mot) {
142
		$classeNom = 'Syndication'.ucwords($mot);
143
		$classeNom = 'Syndication'.ucwords($mot);
143
		return $classeNom;
144
		return $classeNom;
144
	}
145
	}
145
	
146
	
146
	private function getTypeMime() {
147
	private function getTypeMime() {
147
		$mime = '';
148
		$mime = '';
148
		switch ($this->format) {
149
		switch ($this->format) {
149
			case 'atom' :
150
			case 'atom' :
150
				$mime = 'application/atom+xml';
151
				$mime = 'application/atom+xml';
151
				break;
152
				break;
152
			case 'rss1' :
153
			case 'rss1' :
153
			case 'rss2' :
154
			case 'rss2' :
154
				$mime = 'application/rss+xml';
155
				$mime = 'application/rss+xml';
155
				break;
156
				break;
156
			case 'opml' :
157
			case 'opml' :
157
				$mime = 'text/x-opml';
158
				$mime = 'text/x-opml';
158
				break;
159
				break;
159
			default:
160
			default:
160
				$mime = 'text/html';
161
				$mime = 'text/html';
161
		}
162
		}
162
		return $mime;
163
		return $mime;
163
	}
164
	}
164
}
165
}
165
?>
166
?>