Subversion Repositories eFlore/Applications.del

Rev

Rev 1661 | Rev 1820 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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