Subversion Repositories eFlore/Applications.del

Rev

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

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