Subversion Repositories eFlore/Applications.del

Rev

Rev 1821 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1821 Rev 1922
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 $conteneur;
22
	private $methode = null;
22
	private $methode = null;
23
	private $sousServiceNom = null;
23
	private $sousServiceNom = null;
24
	private $cheminCourant = null;
24
	private $cheminCourant = null;
25
 
25
 
26
	private $serviceNom = 'syndication';
26
	private $serviceNom = 'syndication';
27
	private $format = 'atom';
27
	private $format = 'atom';
28
	private $squelette_dossier = null;
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->conteneur = new Conteneur();
35
		$this->cheminCourant = dirname(__FILE__).DS;
35
		$this->cheminCourant = dirname(__FILE__).DS;
36
		$this->squelette_dossier = dirname(__FILE__).DS.$this->serviceNom.DS.'squelettes'.DS;
36
		$this->squelette_dossier = dirname(__FILE__).DS.$this->serviceNom.DS.'squelettes'.DS;
37
		$this->formats_autorises = $this->conteneur->getParametreTableau('syndication.formats');
37
		$this->formats_autorises = $this->conteneur->getParametreTableau('syndication.formats');
38
	}
38
	}
39
 
39
 
40
	public function consulter($ressources, $parametres) {
40
	public function consulter($ressources, $parametres) {
41
		$this->methode = 'consulter';
41
		$this->methode = 'consulter';
42
		$this->initialiserRessourcesEtParametres($ressources, $parametres);
42
		$this->initialiserRessourcesEtParametres($ressources, $parametres);
43
		$this->verifierRessourcesEtParametres();
43
		$this->verifierRessourcesEtParametres();
44
		$this->format = isset($this->parametres['format']) ? $this->parametres['format'] : $this->format;
44
		$this->format = isset($this->parametres['format']) ? $this->parametres['format'] : $this->format;
45
		return $this->executerService();
45
		return $this->executerService();
46
	}
46
	}
47
 
47
 
48
	private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
48
	private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
49
		$this->ressources = $ressources;
49
		$this->ressources = $ressources;
50
		$this->parametres = $parametres;
50
		$this->parametres = $parametres;
51
	}
51
	}
52
 
52
 
53
	private function verifierRessourcesEtParametres() {
53
	private function verifierRessourcesEtParametres() {
54
		if (isset($this->parametres['format']) && !in_array($this->parametres['format'], $this->formats_autorises)) {
54
		if (isset($this->parametres['format']) && !in_array($this->parametres['format'], $this->formats_autorises)) {
55
			$msg = "Vous devez indiquer un format de flux valide.\n".$this->getDoc();
55
			$msg = "Vous devez indiquer un format de flux valide.\n".$this->getDoc();
56
			throw new Exception($msg, RestServeur::HTTP_CODE_ERREUR);
56
			throw new Exception($msg, RestServeur::HTTP_CODE_ERREUR);
57
		}
57
		}
58
	}
58
	}
59
 
59
 
60
	private function executerService() {
60
	private function executerService() {
61
		$reponseHttp = new ReponseHttp();
61
		$reponseHttp = new ReponseHttp();
62
		try {
62
		try {
63
			$donnees = $this->traiterRessources();
63
			$donnees = $this->traiterRessources();
64
			$resultat = $this->creerResultatService($donnees);
64
			$resultat = $this->creerResultatService($donnees);
65
			$reponseHttp->setResultatService($resultat);
65
			$reponseHttp->setResultatService($resultat);
66
		} catch (Exception $e) {
66
		} catch (Exception $e) {
67
			$reponseHttp->ajouterErreur($e);
67
			$reponseHttp->ajouterErreur($e);
68
		}
68
		}
69
		$reponseHttp->emettreLesEntetes();
69
		$reponseHttp->emettreLesEntetes();
70
		$corps = $reponseHttp->getCorps();
70
		$corps = $reponseHttp->getCorps();
71
		return $corps;
71
		return $corps;
72
	}
72
	}
73
 
73
 
74
	private function traiterRessources() {
74
	private function traiterRessources() {
75
		$this->analyserRessources();
75
		$this->analyserRessources();
76
		$retour = $this->initialiserService();
76
		$retour = $this->initialiserService();
77
		return $retour;
77
		return $retour;
78
	}
78
	}
79
 
79
 
80
	private function analyserRessources() {
80
	private function analyserRessources() {
81
		if ($this->methode == 'consulter') {
81
		if ($this->methode == 'consulter' && isset($this->ressources[0])) {
-
 
82
			if (preg_match('/^tags|votes-?-Par-?Tag|tags-?Par-?Protocole$/i', $this->ressources[0])) {
-
 
83
				$this->sousServiceNom = 'tags';
-
 
84
			} else if (preg_match('/^votes|votes-?Par-?Protocole$/i', $this->ressources[0])){
-
 
85
				$this->sousServiceNom = 'votes';
82
			if (isset($this->ressources[0])) {
86
			} else if ($this->ressources[0] == 'commentaires') {
83
				$this->sousServiceNom = $this->ressources[0];
87
				$this->sousServiceNom = 'commentaires';
84
			}
88
			}
85
		}
89
		}
86
		if ($this->sousServiceNom == null) {
90
		if ($this->sousServiceNom == null) {
87
			$this->lancerMessageErreurRessource();
91
			$this->lancerMessageErreurRessource();
88
		}
92
		}
89
	}
93
	}
90
 
94
 
91
	private function lancerMessageErreurRessource() {
95
	private function lancerMessageErreurRessource() {
92
		$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
96
		$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
93
		$message = "La ressource demandée '$ressource' ".
97
		$message = "La ressource demandée '$ressource' ".
94
			"n'est pas disponible pour le service {$this->serviceNom} !\n".
98
			"n'est pas disponible pour le service {$this->serviceNom} !\n".
95
			$this->getDoc();
99
			$this->getDoc();
96
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
100
		$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
97
		throw new Exception($message, $code);
101
		throw new Exception($message, $code);
98
	}
102
	}
99
 
103
 
100
	public function getDoc() {
104
	public function getDoc() {
101
		$formatsAutorises = implode(', ', $this->formats_autorises);
105
		$formatsAutorises = implode(', ', $this->formats_autorises);
102
		return "Les URLs disponibles pour ce service sont :\n".
106
		return "Les URLs disponibles pour ce service sont :\n".
103
			" * en GET :\n".
107
			" * en GET :\n".
104
			" - syndication/commentaires\n".
108
			" - syndication/commentaires\n".
105
			" - syndication/tags\n".
109
			" - syndication/tags\n".
106
			" - syndication/votes-par-protocole\n".
110
			" - syndication/votes-par-protocole\n".
107
			" Paramètres : \n".
111
			" Paramètres : \n".
108
			"  - format : $formatsAutorises";
112
			"  - format : $formatsAutorises";
109
	}
113
	}
110
 
114
 
111
	private function initialiserService() {
115
	private function initialiserService() {
112
		$classe = $this->obtenirNomClasseService($this->sousServiceNom);
116
		$classe = $this->obtenirNomClasseService();
113
		$chemins = array();
117
		$chemins = array();
114
		$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
118
		$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
115
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
119
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
116
		$retour = '';
120
		$retour = '';
117
		$service = null;
121
		$service = null;
118
 
122
 
119
		foreach ($chemins as $chemin) {
123
		foreach ($chemins as $chemin) {
120
			if (file_exists($chemin)) {
124
			if (file_exists($chemin)) {
121
				require_once $chemin;
125
				require_once $chemin;
122
				$service = new $classe($this->conteneur);
126
				$service = new $classe($this->conteneur);
123
				if ($this->methode == 'consulter') {
127
				if ($this->methode == 'consulter') {
124
					$retour = $service->consulter();
128
					$retour = $service->consulter();
125
				} else {
129
				} else {
126
					$message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ".
130
					$message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ".
127
						"ne possède pas de méthode '{$this->methode}' !";
131
						"ne possède pas de méthode '{$this->methode}' !";
128
					$code = RestServeur::HTTP_NON_IMPLEMENTE;
132
					$code = RestServeur::HTTP_NON_IMPLEMENTE;
129
					throw new Exception($message, $code);
133
					throw new Exception($message, $code);
130
				}
134
				}
131
			}
135
			}
132
		}
136
		}
133
 
137
 
134
		if (is_null($service)) {
138
		if (is_null($service)) {
135
			$ressource = $this->serviceNom.'/'.implode('/', $this->ressources);
139
			$ressource = $this->serviceNom.'/'.implode('/', $this->ressources);
136
			$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
140
			$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
137
				"est introuvable par le service '{$this->serviceNom}' !\n".
141
				"est introuvable par le service '{$this->serviceNom}' !\n".
138
				$this->getDoc();
142
				$this->getDoc();
139
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
143
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
140
			throw new Exception($message, $code);
144
			throw new Exception($message, $code);
141
		}
145
		}
142
		return $retour;
146
		return $retour;
143
	}
147
	}
144
 
148
 
145
	private function obtenirNomClasseService($mot) {
149
	private function obtenirNomClasseService() {
146
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
-
 
147
		return $classeNom;
150
		return str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $this->sousServiceNom))));
148
	}
151
	}
149
 
152
 
150
	private function creerResultatService($donnees) {
153
	private function creerResultatService($donnees) {
151
		$resultat = new ResultatService();
154
		$resultat = new ResultatService();
152
		$resultat->mime = $this->getTypeMime();
155
		$resultat->mime = $this->getTypeMime();
153
		$resultat->corps = SquelettePhp::analyser($this->squelette_dossier.$this->format.'.tpl.xml', $donnees);
156
		$resultat->corps = SquelettePhp::analyser($this->squelette_dossier.$this->format.'.tpl.xml', $donnees);
154
		return $resultat;
157
		return $resultat;
155
	}
158
	}
156
 
159
 
157
	private function getTypeMime() {
160
	private function getTypeMime() {
158
		$mime = '';
161
		$mime = '';
159
		switch ($this->format) {
162
		switch ($this->format) {
160
			case 'atom' :
163
			case 'atom' :
161
				$mime = 'application/atom+xml';
164
				$mime = 'application/atom+xml';
162
				break;
165
				break;
163
			case 'rss1' :
166
			case 'rss1' :
164
			case 'rss2' :
167
			case 'rss2' :
165
				$mime = 'application/rss+xml';
168
				$mime = 'application/rss+xml';
166
				break;
169
				break;
167
			case 'opml' :
170
			case 'opml' :
168
				$mime = 'text/x-opml';
171
				$mime = 'text/x-opml';
169
				break;
172
				break;
170
			default:
173
			default:
171
				$mime = 'text/html';
174
				$mime = 'text/html';
172
		}
175
		}
173
		return $mime;
176
		return $mime;
174
	}
177
	}
175
}
178
}