Subversion Repositories eFlore/Applications.del

Rev

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

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