| 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 | 	private $parametres = array();
 | 
        
           |  |  | 18 | 	private $ressources = array();
 | 
        
           |  |  | 19 | 	private $methode = null;
 | 
        
           |  |  | 20 | 	private $projetNom = array();
 | 
        
           |  |  | 21 | 	private $serviceNom = array();
 | 
        
           |  |  | 22 | 	private $cheminCourant = null;
 | 
        
           |  |  | 23 | 	private $squelette_dossier = null;
 | 
        
           | 1057 | aurelien | 24 | 	private $formats_autorises = null;
 | 
        
           | 1047 | aurelien | 25 |   | 
        
           |  |  | 26 | 	private $conteneur;
 | 
        
           | 1661 | jpm | 27 |   | 
        
           | 1047 | aurelien | 28 | 	/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
 | 
        
           |  |  | 29 | 	protected $utilisationParametresBruts = true;
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 | 	public function __construct() {
 | 
        
           |  |  | 32 | 		$this->cheminCourant = dirname(__FILE__).DS;
 | 
        
           | 1661 | jpm | 33 | 		$this->squelette_dossier = dirname(__FILE__).DS.'syndication'.DS.'squelettes'.DS;
 | 
        
           | 1047 | aurelien | 34 | 	}
 | 
        
           |  |  | 35 |   | 
        
           |  |  | 36 | 	public function consulter($ressources, $parametres) {
 | 
        
           |  |  | 37 | 		$this->methode = 'consulter';
 | 
        
           |  |  | 38 | 		$resultat = '';
 | 
        
           |  |  | 39 | 		$reponseHttp = new ReponseHttp();
 | 
        
           |  |  | 40 | 		try {
 | 
        
           | 1057 | aurelien | 41 | 			$this->initialiserProjet();
 | 
        
           | 1047 | aurelien | 42 | 			$this->initialiserRessourcesEtParametres($ressources, $parametres);
 | 
        
           | 1057 | aurelien | 43 | 			$this->conteneur = new Conteneur($this->parametres);
 | 
        
           | 1047 | aurelien | 44 | 			$this->verifierRessourcesEtParametres();
 | 
        
           |  |  | 45 | 			$resultat = $this->traiterRessources();
 | 
        
           |  |  | 46 | 			$reponseHttp->setResultatService($this->creerResultatService($resultat));
 | 
        
           |  |  | 47 | 		} catch (Exception $e) {
 | 
        
           |  |  | 48 | 			$reponseHttp->ajouterErreur($e);
 | 
        
           |  |  | 49 | 		}
 | 
        
           |  |  | 50 | 		$reponseHttp->emettreLesEntetes();
 | 
        
           |  |  | 51 | 		$corps = $reponseHttp->getCorps();
 | 
        
           |  |  | 52 | 		return $corps;
 | 
        
           |  |  | 53 | 	}
 | 
        
           | 1661 | jpm | 54 |   | 
        
           | 1047 | aurelien | 55 | 	private function initialiserRessourcesEtParametres($ressources, $parametres) {
 | 
        
           |  |  | 56 | 		$this->ressources = $ressources;
 | 
        
           |  |  | 57 | 		$this->parametres = $parametres;
 | 
        
           |  |  | 58 | 	}
 | 
        
           | 1661 | jpm | 59 |   | 
        
           | 1047 | aurelien | 60 | 	private function verifierRessourcesEtParametres() {
 | 
        
           | 1057 | aurelien | 61 | 		$servicesDispos = Config::get('servicesDispo');
 | 
        
           |  |  | 62 | 		if (!isset($this->ressources[0]) || !in_array($this->ressources[0], explode(',',$servicesDispos))) {
 | 
        
           |  |  | 63 | 			$message = "Vous devez indiquer un nom de service valide, les services disponibles sont ".$servicesDispos;
 | 
        
           |  |  | 64 | 			$code = RestServeur::HTTP_CODE_ERREUR;
 | 
        
           |  |  | 65 | 			throw new Exception($message, $code);
 | 
        
           |  |  | 66 | 		}
 | 
        
           | 1661 | jpm | 67 |   | 
        
           | 1057 | aurelien | 68 | 		$chaineFormatsAutorises = Config::get('formatsRss');
 | 
        
           |  |  | 69 | 		$this->formats_autorises = explode(',', $chaineFormatsAutorises);
 | 
        
           | 1049 | aurelien | 70 | 		if (!isset($this->ressources[1]) || !in_array($this->ressources[1], $this->formats_autorises)) {
 | 
        
           | 1057 | aurelien | 71 | 			$message = "Vous devez indiquer un format de flux valide, les formats acceptés sont ".$chaineFormatsAutorises;
 | 
        
           | 1047 | aurelien | 72 | 			$code = RestServeur::HTTP_CODE_ERREUR;
 | 
        
           |  |  | 73 | 			throw new Exception($message, $code);
 | 
        
           |  |  | 74 | 		} else {
 | 
        
           |  |  | 75 | 			$this->format = $this->ressources[1];
 | 
        
           |  |  | 76 | 		}
 | 
        
           |  |  | 77 | 	}
 | 
        
           |  |  | 78 |   | 
        
           |  |  | 79 | 	private function traiterRessources() {
 | 
        
           |  |  | 80 | 		$retour = '';
 | 
        
           |  |  | 81 | 		$retour = $this->initialiserService();
 | 
        
           |  |  | 82 | 		return $retour;
 | 
        
           |  |  | 83 | 	}
 | 
        
           | 1661 | jpm | 84 |   | 
        
           | 1047 | aurelien | 85 | 	private function creerResultatService($donnees) {
 | 
        
           |  |  | 86 | 		$resultat = new ResultatService();
 | 
        
           |  |  | 87 | 		$resultat->mime = $this->getTypeMime();
 | 
        
           |  |  | 88 | 		$resultat->corps = SquelettePhp::analyser($this->squelette_dossier.$this->format.'.tpl.xml', $donnees);
 | 
        
           |  |  | 89 | 		return $resultat;
 | 
        
           |  |  | 90 | 	}
 | 
        
           |  |  | 91 |   | 
        
           |  |  | 92 | 	/*------------------------------------------------------------------------------------------------------------------
 | 
        
           |  |  | 93 | 										CONFIGURATION DU PROJET
 | 
        
           |  |  | 94 | 	------------------------------------------------------------------------------------------------------------------*/
 | 
        
           |  |  | 95 | 	private function initialiserProjet() {
 | 
        
           |  |  | 96 | 		$this->projetNom = 'syndication';
 | 
        
           |  |  | 97 | 		$this->chargerConfigProjet();
 | 
        
           |  |  | 98 | 	}
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 | 	private function chargerConfigProjet() {
 | 
        
           |  |  | 101 | 		$projet = $this->projetNom;
 | 
        
           |  |  | 102 | 		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
 | 
        
           |  |  | 103 | 		Config::charger($chemin);
 | 
        
           |  |  | 104 | 	}
 | 
        
           |  |  | 105 |   | 
        
           |  |  | 106 | 	/*------------------------------------------------------------------------------------------------------------------
 | 
        
           |  |  | 107 | 								CONFIGURATION DU SERVICE
 | 
        
           |  |  | 108 | 	------------------------------------------------------------------------------------------------------------------*/
 | 
        
           |  |  | 109 | 	private function initialiserService() {
 | 
        
           |  |  | 110 | 		$this->chargerNomService();
 | 
        
           | 1661 | jpm | 111 |   | 
        
           | 1047 | aurelien | 112 | 		$classe = $this->obtenirNomClasseService($this->serviceNom);
 | 
        
           |  |  | 113 | 		$chemins = array();
 | 
        
           |  |  | 114 | 		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
 | 
        
           |  |  | 115 | 		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
 | 
        
           |  |  | 116 | 		$retour = '';
 | 
        
           |  |  | 117 | 		$service = null;
 | 
        
           |  |  | 118 | 		foreach ($chemins as $chemin) {
 | 
        
           |  |  | 119 | 			if (file_exists($chemin)) {
 | 
        
           | 1661 | jpm | 120 | 				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
 | 
        
           | 1047 | aurelien | 121 | 				require_once $chemin;
 | 
        
           |  |  | 122 | 				$service = new $classe($this->conteneur);
 | 
        
           |  |  | 123 | 				if ($this->methode == 'consulter') {
 | 
        
           |  |  | 124 | 					$retour = $service->consulter($this->ressources, $this->parametres);
 | 
        
           |  |  | 125 | 				}
 | 
        
           |  |  | 126 | 			}
 | 
        
           |  |  | 127 | 		}
 | 
        
           | 1661 | jpm | 128 |   | 
        
           | 1047 | aurelien | 129 | 		if (is_null($service)) {
 | 
        
           |  |  | 130 | 			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
 | 
        
           |  |  | 131 | 			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
 | 
        
           |  |  | 132 | 			throw new Exception($message, $code);
 | 
        
           |  |  | 133 | 		}
 | 
        
           |  |  | 134 | 		return $retour;
 | 
        
           |  |  | 135 | 	}
 | 
        
           | 1661 | jpm | 136 |   | 
        
           | 1047 | aurelien | 137 | 	private function chargerNomService() {
 | 
        
           |  |  | 138 | 		if (!isset($this->ressources[0])) {
 | 
        
           |  |  | 139 | 			$message = "Vous devez indiquer un nom de service";
 | 
        
           |  |  | 140 | 			$code = RestServeur::HTTP_CODE_ERREUR;
 | 
        
           |  |  | 141 | 			throw new Exception($message, $code);
 | 
        
           |  |  | 142 | 		} else {
 | 
        
           |  |  | 143 | 			$this->serviceNom = $this->ressources[0];
 | 
        
           |  |  | 144 | 		}
 | 
        
           |  |  | 145 | 	}
 | 
        
           |  |  | 146 |   | 
        
           |  |  | 147 | 	private function obtenirNomClasseService($mot) {
 | 
        
           |  |  | 148 | 		$classeNom = 'Syndication'.ucwords($mot);
 | 
        
           |  |  | 149 | 		return $classeNom;
 | 
        
           |  |  | 150 | 	}
 | 
        
           | 1661 | jpm | 151 |   | 
        
           | 1047 | aurelien | 152 | 	private function getTypeMime() {
 | 
        
           |  |  | 153 | 		$mime = '';
 | 
        
           |  |  | 154 | 		switch ($this->format) {
 | 
        
           |  |  | 155 | 			case 'atom' :
 | 
        
           |  |  | 156 | 				$mime = 'application/atom+xml';
 | 
        
           |  |  | 157 | 				break;
 | 
        
           |  |  | 158 | 			case 'rss1' :
 | 
        
           |  |  | 159 | 			case 'rss2' :
 | 
        
           |  |  | 160 | 				$mime = 'application/rss+xml';
 | 
        
           |  |  | 161 | 				break;
 | 
        
           |  |  | 162 | 			case 'opml' :
 | 
        
           |  |  | 163 | 				$mime = 'text/x-opml';
 | 
        
           |  |  | 164 | 				break;
 | 
        
           |  |  | 165 | 			default:
 | 
        
           |  |  | 166 | 				$mime = 'text/html';
 | 
        
           |  |  | 167 | 		}
 | 
        
           |  |  | 168 | 		return $mime;
 | 
        
           |  |  | 169 | 	}
 | 
        
           |  |  | 170 | }
 | 
        
           |  |  | 171 | ?>
 |