Rev 1049 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?php
/**
* Description :
* Classe principale de chargement des services de syndication
*
* Encodage en entrée : utf8
* Encodage en sortie : utf8
* @package del-service
* @author Aurélien PERONNET <aurelien@tela-botanica.org>
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
* @version 0.1
* @copyright 1999-2011 Tela Botanica (accueil@tela-botanica.org)
*/
class Syndication extends RestService {
private $parametres = array();
private $ressources = array();
private $methode = null;
private $projetNom = array();
private $serviceNom = array();
private $cheminCourant = null;
private $squelette_dossier = null;
private $formats_autorises = null;
private $conteneur;
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
protected $utilisationParametresBruts = true;
public function __construct() {
$this->cheminCourant = dirname(__FILE__).DS;
$this->squelette_dossier = dirname(__FILE__).DIRECTORY_SEPARATOR.'syndication'.DIRECTORY_SEPARATOR.'squelettes'.DIRECTORY_SEPARATOR;
}
public function consulter($ressources, $parametres) {
$this->methode = 'consulter';
$resultat = '';
$reponseHttp = new ReponseHttp();
try {
$this->initialiserProjet();
$this->initialiserRessourcesEtParametres($ressources, $parametres);
$this->conteneur = new Conteneur($this->parametres);
$this->verifierRessourcesEtParametres();
$resultat = $this->traiterRessources();
$reponseHttp->setResultatService($this->creerResultatService($resultat));
} catch (Exception $e) {
$reponseHttp->ajouterErreur($e);
}
$reponseHttp->emettreLesEntetes();
$corps = $reponseHttp->getCorps();
return $corps;
}
private function initialiserRessourcesEtParametres($ressources, $parametres) {
$this->ressources = $ressources;
$this->parametres = $parametres;
}
private function verifierRessourcesEtParametres() {
$servicesDispos = Config::get('servicesDispo');
if (!isset($this->ressources[0]) || !in_array($this->ressources[0], explode(',',$servicesDispos))) {
$message = "Vous devez indiquer un nom de service valide, les services disponibles sont ".$servicesDispos;
$code = RestServeur::HTTP_CODE_ERREUR;
throw new Exception($message, $code);
}
$chaineFormatsAutorises = Config::get('formatsRss');
$this->formats_autorises = explode(',', $chaineFormatsAutorises);
if (!isset($this->ressources[1]) || !in_array($this->ressources[1], $this->formats_autorises)) {
$message = "Vous devez indiquer un format de flux valide, les formats acceptés sont ".$chaineFormatsAutorises;
$code = RestServeur::HTTP_CODE_ERREUR;
throw new Exception($message, $code);
} else {
$this->format = $this->ressources[1];
}
}
private function traiterRessources() {
$retour = '';
$retour = $this->initialiserService();
return $retour;
}
private function creerResultatService($donnees) {
$resultat = new ResultatService();
$resultat->mime = $this->getTypeMime();
$resultat->corps = SquelettePhp::analyser($this->squelette_dossier.$this->format.'.tpl.xml', $donnees);
return $resultat;
}
/*------------------------------------------------------------------------------------------------------------------
CONFIGURATION DU PROJET
------------------------------------------------------------------------------------------------------------------*/
private function initialiserProjet() {
$this->projetNom = 'syndication';
$this->chargerConfigProjet();
}
private function chargerConfigProjet() {
$projet = $this->projetNom;
$chemin = Config::get('chemin_configurations')."config_$projet.ini";
Config::charger($chemin);
}
/*------------------------------------------------------------------------------------------------------------------
CONFIGURATION DU SERVICE
------------------------------------------------------------------------------------------------------------------*/
private function initialiserService() {
$this->chargerNomService();
$classe = $this->obtenirNomClasseService($this->serviceNom);
$chemins = array();
$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
$retour = '';
$service = null;
foreach ($chemins as $chemin) {
if (file_exists($chemin)) {
$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
require_once $chemin;
$service = new $classe($this->conteneur);
if ($this->methode == 'consulter') {
$retour = $service->consulter($this->ressources, $this->parametres);
}
}
}
if (is_null($service)) {
$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
throw new Exception($message, $code);
}
return $retour;
}
private function chargerNomService() {
if (!isset($this->ressources[0])) {
$message = "Vous devez indiquer un nom de service";
$code = RestServeur::HTTP_CODE_ERREUR;
throw new Exception($message, $code);
} else {
$this->serviceNom = $this->ressources[0];
}
}
private function obtenirNomClasseService($mot) {
$classeNom = 'Syndication'.ucwords($mot);
return $classeNom;
}
private function getTypeMime() {
$mime = '';
switch ($this->format) {
case 'atom' :
$mime = 'application/atom+xml';
break;
case 'rss1' :
case 'rss2' :
$mime = 'application/rss+xml';
break;
case 'opml' :
$mime = 'text/x-opml';
break;
default:
$mime = 'text/html';
}
return $mime;
}
}
?>