* @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 Dao { const ORDRE_ASCENDANT = 'ASC'; const ORDRE_DESCENDANT = 'DESC'; const HTTP_REQUETE_SEPARATEUR = '&'; protected $distinction = '0'; protected $limite_debut = null; protected $limite_nbre = null; protected $ordre = null; //+----------------------------------------------------------------------------------------------------------------+ // ACCESSEURS public function setDistinction($distinct) { $this->distinction = $distinct; } public function getDistinction() { return $this->distinction; } public function viderDistinction() { $this->distinction = null; } public function avoirLimitation() { $limitation = false; if (!is_null($this->limite_debut) && !is_null($this->limite_nbre)) { $limitation = true; } return $limitation; } public function setLimitation($limite_debut, $limite_nbre) { $this->limite_debut = $limite_debut; $this->limite_nbre = $limite_nbre; } public function getLimiteDebut() { return $this->limite_debut; } public function getLimiteNbre() { return $this->limite_nbre; } public function viderLimite() { $this->limite_debut = null; $this->limite_nbre = null; } public function addOrdre($champ, $trie = self::ORDRE_ASCENDANT) { if (!isset($this->ordre[$champ])) { if (self::ORDRE_ASCENDANT == $trie || self::ORDRE_DESCENDANT == $trie) { $this->ordre[$champ] = $trie; } else { $e = "La valeur pour le trie doit être : {self::ORDRE_ASCENDANT} ou {self::ORDRE_DESCENDANT}."; trigger_error($e, E_USER_WARNING); } } else { $e = "Le champ $champ existe déjà dans le tableau des ordres."; trigger_error($e, E_USER_WARNING); } } public function getOrdre() { $champs = array(); foreach ($this->ordre as $champ => $trie) { $champs[] = "$champ $trie"; } return implode(', ', $champs); } public function viderOrdre() { $this->ordre = null; } //+----------------------------------------------------------------------------------------------------------------+ // MÉTHODES public function envoyerRequeteConsultation($url) { $url = $this->traiterUrlParametres($url); $retour = $this->envoyerRequete($url, 'GET'); return $retour; } public function envoyerRequeteAjout($url, Array $donnees) { $retour = $this->envoyerRequete($url, 'PUT', $donnees); return $retour; } public function envoyerRequeteModif($url, Array $donnees) { $retour = $this->envoyerRequete($url, 'POST', $donnees); return $retour; } public function envoyerRequeteSuppression($url) { $retour = $this->envoyerRequete($url, 'DELETE'); return $retour; } private function envoyerRequete($url, $mode, Array $donnees = array()) { $contenu = false; if ($mode != 'GET' && $mode != 'PUT' && $mode != 'POST' && $mode != 'DELETE') { $e = "Le mode de requête '$mode' n'est pas accepté!"; trigger_error($e, E_USER_WARNING); } else { $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_REQUETE_SEPARATEUR)))); $flux = @fopen($url, 'r', false, $contexte); if (!$flux) { $this->traiterEntete($http_response_header, $url); $e = "L'ouverture de l'url '$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 $entetes = stream_get_meta_data($flux); $this->traiterEntete($entetes, $url); // Contenu actuel de $url $contenu = stream_get_contents($flux); fclose($flux); } } $this->reinitialiser(); return $contenu; } private function traiterUrlParametres($url) { $parametres = array(); if (! is_null($this->getLimiteDebut())) { $parametres[] = 'start='.$this->getLimiteDebut(); } if (! is_null($this->getLimiteNbre())) { $parametres[] = 'limit='.$this->getLimiteNbre(); } if (! is_null($this->ordre)) { $parametres[] = 'orderby='.urlencode($this->getOrdre()); } if ($this->getDistinction() != 0) { $parametres[] = 'distinct='.$this->getDistinction(); } if (count($parametres) > 0) { $url_parametres = implode('&', $parametres); $url = $url.'?'.$url_parametres; } return $url; } private function traiterEntete($entetes, $uri) { $infos = $this->analyserEntete($entetes, $uri); $this->traiterEnteteDebug($infos); $this->traiterEnteteMessage($infos); } private function analyserEntete($entetes, $uri) { $infos = array('date' => null, 'uri' => $uri, 'debugs' => null, 'messages' => null); if (isset($entetes['wrapper_data'])) { $entetes = $entetes['wrapper_data']; } foreach ($entetes as $entete) { if (preg_match('/^X-DebugJrest-Data: (.+)$/', $entete, $match)) { $infos['debugs'] = json_decode($match[1]); } if (preg_match('/^X-MessageJrest-Data: (.+)$/', $entete, $match)) { $infos['messages'] = 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 traiterEnteteDebug($entetes) { if (isset($entetes['debugs'])) { $date = $entetes['date']; $uri = $entetes['uri']; $debugs = $entetes['debugs']; foreach ($debugs as $debug) { $e = "DEBUG : $date - $uri :\n$debug"; trigger_error($e, E_USER_NOTICE); } } } private function traiterEnteteMessage($entetes) { if (isset($entetes['messages'])) { $date = $entetes['date']; $uri = $entetes['uri']; $messages = $entetes['messages']; foreach ($messages as $message) { $e = "MESSAGE : $date - $uri :\n$message"; trigger_error($e, E_USER_NOTICE); } } } private function reinitialiser() { $this->viderDistinction(); $this->viderLimite(); $this->viderOrdre(); } }