Subversion Repositories eFlore/Applications.cel

Rev

Rev 416 | Rev 946 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
// declare(encoding='UTF-8');
/**
* Classe modèle spécifique à l'application, donc d'accés au données, elle ne devrait pas être appelée de l'extérieur.
*
* @category             php5
* @package              Cel
* @author               Jean-Pascal MILCENT <jpm@tela-botanica.org>
* @copyright    2010 Tela-Botanica
* @license              http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
* @license              http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
* @version              SVN: $Id$
*/
class CelRestClient {
        const HTTP_URL_REQUETE_SEPARATEUR = '&';
        const HTTP_URL_REQUETE_CLE_VALEUR_SEPARATEUR = '=';
        private $http_methodes = array('GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS', 'CONNECT', 'TRACE');
        protected $parametres = null;
        private $url = null;
        private $reponse_entetes = null;
        
        //+----------------------------------------------------------------------------------------------------------------+
        // ACCESSEURS
        
        public function getReponseEntetes($cle) {
                return $this->reponse_entetes;
        }
        
        public function getParametre($cle) {
                $valeur = (isset($this->parametres[$cle])) ? $this->parametres[$cle] : null;
                return $valeur;
        }
        
        public function ajouterParametre($cle, $valeur) {
                $this->parametres[$cle] = $valeur;
        }
        
        public function supprimerParametre($cle) {
                unset($this->parametres[$cle]);
        }
        
        public function nettoyerParametres() {
                $this->parametres = null;
        }
        
        //+----------------------------------------------------------------------------------------------------------------+
        // MÉTHODES
        
        public function consulter($url) {
                $retour = $this->envoyerRequete($url, 'GET');
                return $retour;
        }
        
        public function ajouter($url, Array $donnees) {
                $retour = $this->envoyerRequete($url, 'PUT', $donnees);
                return $retour;
        }
        
        public function modifier($url, Array $donnees) {
                $retour = $this->envoyerRequete($url, 'POST', $donnees);
                return $retour;
        }
        
        public function supprimer($url) {
                $retour = $this->envoyerRequete($url, 'DELETE');
                return $retour;
        }
        
        public function envoyerRequete($url, $mode, Array $donnees = array()) {
                $this->url = $url;
                $contenu = false;
                if (! in_array($mode, $this->http_methodes)) {
                        $e = "Le mode de requête '$mode' n'est pas accepté!";
                        trigger_error($e, E_USER_WARNING);
                } else {
                        if ($mode == 'GET') {
                                $this->traiterUrlParametres();
                        }
                        $contexte = stream_context_create(array(
                                'http' => array(
                                'method' => $mode,
                                        'header' => "Content-type: application/x-www-form-urlencoded\r\n",
                                'content' => http_build_query($donnees, null, self::HTTP_URL_REQUETE_SEPARATEUR))));
                        $flux = @fopen($this->url, 'r', false, $contexte);
                        if (!$flux) {
                                $this->reponse_entetes = $http_response_header;
                                $e = "L'ouverture de l'url '{$this->url}' par la méthode HTTP '$mode' a échoué!";
                                trigger_error($e, E_USER_WARNING);
                        } else {
                                // Informations sur les en-têtes et métadonnées du flux
                                $this->reponse_entetes = stream_get_meta_data($flux);
                                
                                // Contenu actuel de $url
                                $contenu = stream_get_contents($flux);
                                
                                fclose($flux);
                        }
                        $this->traiterEntete();
                }
                $this->reinitialiser();
                return $contenu;
        }
        
        private function traiterUrlParametres() {
                $parametres = array();
                if (count($this->parametres) > 0) {
                        foreach ($this->parametres as $cle => $valeur) {
                                $cle = rawurlencode($cle);
                                $valeur = rawurlencode($valeur);
                                $parametres[] = $cle.self::HTTP_URL_REQUETE_CLE_VALEUR_SEPARATEUR.$valeur;
                        }
                        $url_parametres = implode(self::HTTP_URL_REQUETE_SEPARATEUR, $parametres);
                        $this->url = $this->url.'?'.$url_parametres;
                }
        }
        
        private function traiterEntete() {
                $infos = $this->analyserEntete();
                $this->traiterEnteteDebogage($infos);
        }
        
        private function analyserEntete() {
                $entetes = $this->reponse_entetes; 
                $infos = array('date' => null, 'uri' => $this->url, 'debogages' => null);
                
                if (isset($entetes['wrapper_data'])) {
                        $entetes = $entetes['wrapper_data'];
                }
                foreach ($entetes as $entete) {
                        if (preg_match('/^X_REST_DEBOGAGE_MESSAGES: (.+)$/', $entete, $match)) {
                                $infos['debogages'] = json_decode($match[1]);
                        }
                        if (preg_match('/^Date: .+ ([012][0-9]:[012345][0-9]:[012345][0-9]) .*$/', $entete, $match)) {
                                $infos['date'] = $match[1];
                        }
                }
                return $infos;
        }
        
        private function traiterEnteteDebogage($entetes_analyses) {
                if (isset($entetes['debogages'])) {
                        $date = $entetes['date'];
                        $uri = $entetes['uri'];
                        $debogages = $entetes['debogages'];
                        foreach ($debogages as $debogage) {
                                $e = "DEBOGAGE : $date - $uri :\n$debogage";
                                trigger_error($e, E_USER_NOTICE);
                        }
                }
        }
        
        private function reinitialiser() {
                $this->nettoyerParametres();
        }
}