1047 |
aurelien |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Description :
|
|
|
4 |
* Classe principale de chargement des services de syndication
|
|
|
5 |
*
|
|
|
6 |
* Encodage en entrée : utf8
|
|
|
7 |
* Encodage en sortie : utf8
|
|
|
8 |
* @package del-service
|
|
|
9 |
* @author Aurélien PERONNET <aurelien@tela-botanica.org>
|
|
|
10 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
11 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
12 |
* @version 0.1
|
|
|
13 |
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
|
|
|
14 |
*/
|
|
|
15 |
class Syndication extends RestService {
|
|
|
16 |
|
|
|
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;
|
|
|
28 |
|
|
|
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;
|
|
|
34 |
$this->squelette_dossier = dirname(__FILE__).DIRECTORY_SEPARATOR.'syndication'.DIRECTORY_SEPARATOR.'squelettes'.DIRECTORY_SEPARATOR;
|
|
|
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 |
}
|
|
|
55 |
|
|
|
56 |
private function initialiserRessourcesEtParametres($ressources, $parametres) {
|
|
|
57 |
$this->ressources = $ressources;
|
|
|
58 |
$this->parametres = $parametres;
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
private function verifierRessourcesEtParametres() {
|
1057 |
aurelien |
62 |
|
|
|
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 |
}
|
|
|
69 |
|
|
|
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 |
|
|
|
85 |
return $retour;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
private function creerResultatService($donnees) {
|
|
|
89 |
$resultat = new ResultatService();
|
|
|
90 |
$resultat->mime = $this->getTypeMime();
|
|
|
91 |
$resultat->corps = SquelettePhp::analyser($this->squelette_dossier.$this->format.'.tpl.xml', $donnees);
|
|
|
92 |
|
|
|
93 |
return $resultat;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
/*------------------------------------------------------------------------------------------------------------------
|
|
|
97 |
CONFIGURATION DU PROJET
|
|
|
98 |
------------------------------------------------------------------------------------------------------------------*/
|
|
|
99 |
private function initialiserProjet() {
|
|
|
100 |
$this->projetNom = 'syndication';
|
|
|
101 |
$this->chargerConfigProjet();
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
private function chargerConfigProjet() {
|
|
|
105 |
$projet = $this->projetNom;
|
|
|
106 |
$chemin = Config::get('chemin_configurations')."config_$projet.ini";
|
|
|
107 |
Config::charger($chemin);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
/*------------------------------------------------------------------------------------------------------------------
|
|
|
111 |
CONFIGURATION DU SERVICE
|
|
|
112 |
------------------------------------------------------------------------------------------------------------------*/
|
|
|
113 |
private function initialiserService() {
|
|
|
114 |
$this->chargerNomService();
|
|
|
115 |
|
|
|
116 |
$classe = $this->obtenirNomClasseService($this->serviceNom);
|
|
|
117 |
$chemins = array();
|
|
|
118 |
$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
|
|
|
119 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
|
|
120 |
$retour = '';
|
|
|
121 |
$service = null;
|
|
|
122 |
foreach ($chemins as $chemin) {
|
|
|
123 |
if (file_exists($chemin)) {
|
|
|
124 |
$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
|
|
|
125 |
require_once $chemin;
|
|
|
126 |
$service = new $classe($this->conteneur);
|
|
|
127 |
if ($this->methode == 'consulter') {
|
|
|
128 |
$retour = $service->consulter($this->ressources, $this->parametres);
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
if (is_null($service)) {
|
|
|
134 |
$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
|
|
|
135 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
136 |
throw new Exception($message, $code);
|
|
|
137 |
}
|
|
|
138 |
return $retour;
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
private function chargerNomService() {
|
|
|
142 |
if (!isset($this->ressources[0])) {
|
|
|
143 |
$message = "Vous devez indiquer un nom de service";
|
|
|
144 |
$code = RestServeur::HTTP_CODE_ERREUR;
|
|
|
145 |
throw new Exception($message, $code);
|
|
|
146 |
} else {
|
|
|
147 |
$this->serviceNom = $this->ressources[0];
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
private function obtenirNomClasseService($mot) {
|
|
|
152 |
$classeNom = 'Syndication'.ucwords($mot);
|
|
|
153 |
return $classeNom;
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
private function getTypeMime() {
|
|
|
157 |
$mime = '';
|
|
|
158 |
switch ($this->format) {
|
|
|
159 |
case 'atom' :
|
|
|
160 |
$mime = 'application/atom+xml';
|
|
|
161 |
break;
|
|
|
162 |
case 'rss1' :
|
|
|
163 |
case 'rss2' :
|
|
|
164 |
$mime = 'application/rss+xml';
|
|
|
165 |
break;
|
|
|
166 |
case 'opml' :
|
|
|
167 |
$mime = 'text/x-opml';
|
|
|
168 |
break;
|
|
|
169 |
default:
|
|
|
170 |
$mime = 'text/html';
|
|
|
171 |
}
|
|
|
172 |
return $mime;
|
|
|
173 |
}
|
|
|
174 |
}
|
|
|
175 |
?>
|