Subversion Repositories eFlore/Applications.del

Rev

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

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