Subversion Repositories eFlore/Applications.del

Rev

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

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