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;
|
1049 |
aurelien |
25 |
// TODO: si dans 1000 ans un nouveau format de flux apparaît, il faudra mettre ceci dans le fichier de config
|
|
|
26 |
private $formats_autorises = array('rss1','rss2','atom');
|
1047 |
aurelien |
27 |
|
|
|
28 |
private $conteneur;
|
|
|
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() {
|
|
|
34 |
$this->cheminCourant = dirname(__FILE__).DS;
|
|
|
35 |
$this->squelette_dossier = dirname(__FILE__).DIRECTORY_SEPARATOR.'syndication'.DIRECTORY_SEPARATOR.'squelettes'.DIRECTORY_SEPARATOR;
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
public function consulter($ressources, $parametres) {
|
|
|
39 |
$this->methode = 'consulter';
|
|
|
40 |
$resultat = '';
|
|
|
41 |
$reponseHttp = new ReponseHttp();
|
|
|
42 |
try {
|
|
|
43 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
|
|
44 |
$this->verifierRessourcesEtParametres();
|
|
|
45 |
$this->conteneur = new Conteneur($this->parametres);
|
|
|
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() {
|
1049 |
aurelien |
62 |
if (!isset($this->ressources[1]) || !in_array($this->ressources[1], $this->formats_autorises)) {
|
|
|
63 |
$message = "Vous devez indiquer un format de flux valide, les formats acceptés sont ".implode(', ',$this->formats_autorises);
|
1047 |
aurelien |
64 |
$code = RestServeur::HTTP_CODE_ERREUR;
|
|
|
65 |
throw new Exception($message, $code);
|
|
|
66 |
} else {
|
|
|
67 |
$this->format = $this->ressources[1];
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
private function traiterRessources() {
|
|
|
72 |
$retour = '';
|
|
|
73 |
$this->initialiserProjet();
|
|
|
74 |
$retour = $this->initialiserService();
|
|
|
75 |
|
|
|
76 |
return $retour;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
private function creerResultatService($donnees) {
|
|
|
80 |
$resultat = new ResultatService();
|
|
|
81 |
$resultat->mime = $this->getTypeMime();
|
|
|
82 |
$resultat->corps = SquelettePhp::analyser($this->squelette_dossier.$this->format.'.tpl.xml', $donnees);
|
|
|
83 |
|
|
|
84 |
return $resultat;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/*------------------------------------------------------------------------------------------------------------------
|
|
|
88 |
CONFIGURATION DU PROJET
|
|
|
89 |
------------------------------------------------------------------------------------------------------------------*/
|
|
|
90 |
private function initialiserProjet() {
|
|
|
91 |
$this->projetNom = 'syndication';
|
|
|
92 |
$this->chargerConfigProjet();
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
private function chargerConfigProjet() {
|
|
|
96 |
$projet = $this->projetNom;
|
|
|
97 |
$chemin = Config::get('chemin_configurations')."config_$projet.ini";
|
|
|
98 |
Config::charger($chemin);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/*------------------------------------------------------------------------------------------------------------------
|
|
|
102 |
CONFIGURATION DU SERVICE
|
|
|
103 |
------------------------------------------------------------------------------------------------------------------*/
|
|
|
104 |
private function initialiserService() {
|
|
|
105 |
$this->chargerNomService();
|
|
|
106 |
|
|
|
107 |
$classe = $this->obtenirNomClasseService($this->serviceNom);
|
|
|
108 |
$chemins = array();
|
|
|
109 |
$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
|
|
|
110 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
|
|
111 |
$retour = '';
|
|
|
112 |
$service = null;
|
|
|
113 |
foreach ($chemins as $chemin) {
|
|
|
114 |
if (file_exists($chemin)) {
|
|
|
115 |
$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
|
|
|
116 |
require_once $chemin;
|
|
|
117 |
$service = new $classe($this->conteneur);
|
|
|
118 |
if ($this->methode == 'consulter') {
|
|
|
119 |
$retour = $service->consulter($this->ressources, $this->parametres);
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
if (is_null($service)) {
|
|
|
125 |
$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
|
|
|
126 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
127 |
throw new Exception($message, $code);
|
|
|
128 |
}
|
|
|
129 |
return $retour;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
private function chargerNomService() {
|
|
|
133 |
if (!isset($this->ressources[0])) {
|
|
|
134 |
$message = "Vous devez indiquer un nom de service";
|
|
|
135 |
$code = RestServeur::HTTP_CODE_ERREUR;
|
|
|
136 |
throw new Exception($message, $code);
|
|
|
137 |
} else {
|
|
|
138 |
$this->serviceNom = $this->ressources[0];
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
private function obtenirNomClasseService($mot) {
|
|
|
143 |
$classeNom = 'Syndication'.ucwords($mot);
|
|
|
144 |
return $classeNom;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
private function getTypeMime() {
|
|
|
148 |
$mime = '';
|
|
|
149 |
switch ($this->format) {
|
|
|
150 |
case 'atom' :
|
|
|
151 |
$mime = 'application/atom+xml';
|
|
|
152 |
break;
|
|
|
153 |
case 'rss1' :
|
|
|
154 |
case 'rss2' :
|
|
|
155 |
$mime = 'application/rss+xml';
|
|
|
156 |
break;
|
|
|
157 |
case 'opml' :
|
|
|
158 |
$mime = 'text/x-opml';
|
|
|
159 |
break;
|
|
|
160 |
default:
|
|
|
161 |
$mime = 'text/html';
|
|
|
162 |
}
|
|
|
163 |
return $mime;
|
|
|
164 |
}
|
|
|
165 |
}
|
|
|
166 |
?>
|