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();
|
1821 |
jpm |
21 |
private $conteneur;
|
1047 |
aurelien |
22 |
private $methode = null;
|
1821 |
jpm |
23 |
private $sousServiceNom = null;
|
1047 |
aurelien |
24 |
private $cheminCourant = null;
|
1821 |
jpm |
25 |
|
|
|
26 |
private $serviceNom = 'syndication';
|
|
|
27 |
private $format = 'atom';
|
1047 |
aurelien |
28 |
private $squelette_dossier = null;
|
|
|
29 |
|
|
|
30 |
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
|
|
|
31 |
protected $utilisationParametresBruts = true;
|
|
|
32 |
|
|
|
33 |
public function __construct() {
|
1821 |
jpm |
34 |
$this->conteneur = new Conteneur();
|
1047 |
aurelien |
35 |
$this->cheminCourant = dirname(__FILE__).DS;
|
1821 |
jpm |
36 |
$this->squelette_dossier = dirname(__FILE__).DS.$this->serviceNom.DS.'squelettes'.DS;
|
|
|
37 |
$this->formats_autorises = $this->conteneur->getParametreTableau('syndication.formats');
|
1047 |
aurelien |
38 |
}
|
|
|
39 |
|
|
|
40 |
public function consulter($ressources, $parametres) {
|
|
|
41 |
$this->methode = 'consulter';
|
1821 |
jpm |
42 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
|
|
43 |
$this->verifierRessourcesEtParametres();
|
|
|
44 |
$this->format = isset($this->parametres['format']) ? $this->parametres['format'] : $this->format;
|
|
|
45 |
return $this->executerService();
|
1047 |
aurelien |
46 |
}
|
1661 |
jpm |
47 |
|
1821 |
jpm |
48 |
private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
|
1047 |
aurelien |
49 |
$this->ressources = $ressources;
|
|
|
50 |
$this->parametres = $parametres;
|
|
|
51 |
}
|
1661 |
jpm |
52 |
|
1047 |
aurelien |
53 |
private function verifierRessourcesEtParametres() {
|
1821 |
jpm |
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();
|
|
|
56 |
throw new Exception($msg, RestServeur::HTTP_CODE_ERREUR);
|
1057 |
aurelien |
57 |
}
|
1821 |
jpm |
58 |
}
|
1661 |
jpm |
59 |
|
1821 |
jpm |
60 |
private function executerService() {
|
|
|
61 |
$reponseHttp = new ReponseHttp();
|
|
|
62 |
try {
|
|
|
63 |
$donnees = $this->traiterRessources();
|
|
|
64 |
$resultat = $this->creerResultatService($donnees);
|
|
|
65 |
$reponseHttp->setResultatService($resultat);
|
|
|
66 |
} catch (Exception $e) {
|
|
|
67 |
$reponseHttp->ajouterErreur($e);
|
1047 |
aurelien |
68 |
}
|
1821 |
jpm |
69 |
$reponseHttp->emettreLesEntetes();
|
|
|
70 |
$corps = $reponseHttp->getCorps();
|
|
|
71 |
return $corps;
|
1047 |
aurelien |
72 |
}
|
|
|
73 |
|
|
|
74 |
private function traiterRessources() {
|
1821 |
jpm |
75 |
$this->analyserRessources();
|
1047 |
aurelien |
76 |
$retour = $this->initialiserService();
|
|
|
77 |
return $retour;
|
|
|
78 |
}
|
1661 |
jpm |
79 |
|
1821 |
jpm |
80 |
private function analyserRessources() {
|
|
|
81 |
if ($this->methode == 'consulter') {
|
|
|
82 |
if (isset($this->ressources[0])) {
|
|
|
83 |
$this->sousServiceNom = $this->ressources[0];
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
if ($this->sousServiceNom == null) {
|
|
|
87 |
$this->lancerMessageErreurRessource();
|
|
|
88 |
}
|
1047 |
aurelien |
89 |
}
|
|
|
90 |
|
1821 |
jpm |
91 |
private function lancerMessageErreurRessource() {
|
|
|
92 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
93 |
$message = "La ressource demandée '$ressource' ".
|
|
|
94 |
"n'est pas disponible pour le service {$this->serviceNom} !\n".
|
|
|
95 |
$this->getDoc();
|
|
|
96 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
97 |
throw new Exception($message, $code);
|
1047 |
aurelien |
98 |
}
|
|
|
99 |
|
1821 |
jpm |
100 |
public function getDoc() {
|
|
|
101 |
$formatsAutorises = implode(', ', $this->formats_autorises);
|
|
|
102 |
return "Les URLs disponibles pour ce service sont :\n".
|
|
|
103 |
" * en GET :\n".
|
|
|
104 |
" - syndication/commentaires\n".
|
|
|
105 |
" - syndication/tags\n".
|
|
|
106 |
" - syndication/votes-par-protocole\n".
|
|
|
107 |
" Paramètres : \n".
|
|
|
108 |
" - format : $formatsAutorises";
|
1047 |
aurelien |
109 |
}
|
|
|
110 |
|
|
|
111 |
private function initialiserService() {
|
1821 |
jpm |
112 |
$classe = $this->obtenirNomClasseService($this->sousServiceNom);
|
1047 |
aurelien |
113 |
$chemins = array();
|
1821 |
jpm |
114 |
$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
|
1047 |
aurelien |
115 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
|
|
116 |
$retour = '';
|
|
|
117 |
$service = null;
|
1821 |
jpm |
118 |
|
1047 |
aurelien |
119 |
foreach ($chemins as $chemin) {
|
|
|
120 |
if (file_exists($chemin)) {
|
|
|
121 |
require_once $chemin;
|
|
|
122 |
$service = new $classe($this->conteneur);
|
|
|
123 |
if ($this->methode == 'consulter') {
|
1821 |
jpm |
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);
|
1047 |
aurelien |
130 |
}
|
|
|
131 |
}
|
|
|
132 |
}
|
1661 |
jpm |
133 |
|
1047 |
aurelien |
134 |
if (is_null($service)) {
|
1821 |
jpm |
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();
|
1047 |
aurelien |
139 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
140 |
throw new Exception($message, $code);
|
|
|
141 |
}
|
|
|
142 |
return $retour;
|
|
|
143 |
}
|
1661 |
jpm |
144 |
|
1047 |
aurelien |
145 |
private function obtenirNomClasseService($mot) {
|
1821 |
jpm |
146 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
1047 |
aurelien |
147 |
return $classeNom;
|
|
|
148 |
}
|
1661 |
jpm |
149 |
|
1821 |
jpm |
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 |
}
|
|
|
156 |
|
1047 |
aurelien |
157 |
private function getTypeMime() {
|
|
|
158 |
$mime = '';
|
|
|
159 |
switch ($this->format) {
|
|
|
160 |
case 'atom' :
|
|
|
161 |
$mime = 'application/atom+xml';
|
|
|
162 |
break;
|
|
|
163 |
case 'rss1' :
|
|
|
164 |
case 'rss2' :
|
|
|
165 |
$mime = 'application/rss+xml';
|
|
|
166 |
break;
|
|
|
167 |
case 'opml' :
|
|
|
168 |
$mime = 'text/x-opml';
|
|
|
169 |
break;
|
|
|
170 |
default:
|
|
|
171 |
$mime = 'text/html';
|
|
|
172 |
}
|
|
|
173 |
return $mime;
|
|
|
174 |
}
|
1821 |
jpm |
175 |
}
|