| 144 | jpm | 1 | <?php
 | 
        
           |  |  | 2 | // declare(encoding='UTF-8');
 | 
        
           |  |  | 3 | /**
 | 
        
           |  |  | 4 | * 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.
 | 
        
           |  |  | 5 | * Elle est abstraite donc doit obligatoirement être étendue.
 | 
        
           |  |  | 6 | *
 | 
        
           |  |  | 7 | * @category		Php5
 | 
        
           |  |  | 8 | * @package 		Referentiel
 | 
        
           |  |  | 9 | * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
 | 
        
           |  |  | 10 | * @copyright	2010 Tela-Botanica
 | 
        
           |  |  | 11 | * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
 | 
        
           |  |  | 12 | * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
 | 
        
           |  |  | 13 | * @version		SVN: $Id$
 | 
        
           |  |  | 14 | */
 | 
        
           |  |  | 15 | abstract class Dao {
 | 
        
           |  |  | 16 | 	const ORDRE_ASCENDANT = 'ASC';
 | 
        
           |  |  | 17 | 	const ORDRE_DESCENDANT = 'DESC';
 | 
        
           |  |  | 18 | 	const HTTP_REQUETE_SEPARATEUR = '&';
 | 
        
           |  |  | 19 | 	protected $distinction = '0';
 | 
        
           |  |  | 20 | 	protected $limite_debut = null;
 | 
        
           |  |  | 21 | 	protected $limite_nbre = null;
 | 
        
           |  |  | 22 | 	protected $ordre = null;
 | 
        
           |  |  | 23 | 	protected $url_jrest = null;
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | 	public function __construct() {
 | 
        
           |  |  | 26 | 		$this->url_jrest = Config::get('url_jrest');
 | 
        
           |  |  | 27 | 	}
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | 	//+----------------------------------------------------------------------------------------------------------------+
 | 
        
           |  |  | 30 | 	// ACCESSEURS
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 | 	public function setDistinction($distinct) {
 | 
        
           |  |  | 33 | 		$this->distinction = $distinct;
 | 
        
           |  |  | 34 | 	}
 | 
        
           |  |  | 35 | 	public function getDistinction() {
 | 
        
           |  |  | 36 | 		return $this->distinction;
 | 
        
           |  |  | 37 | 	}
 | 
        
           |  |  | 38 | 	public function viderDistinction() {
 | 
        
           |  |  | 39 | 		$this->distinction = null;
 | 
        
           |  |  | 40 | 	}
 | 
        
           |  |  | 41 |   | 
        
           |  |  | 42 | 	public function avoirLimitation() {
 | 
        
           |  |  | 43 | 		$limitation = false;
 | 
        
           |  |  | 44 | 		if (!is_null($this->limite_debut) && !is_null($this->limite_nbre)) {
 | 
        
           |  |  | 45 | 			$limitation = true;
 | 
        
           |  |  | 46 | 		}
 | 
        
           |  |  | 47 | 		return $limitation;
 | 
        
           |  |  | 48 | 	}
 | 
        
           |  |  | 49 | 	public function setLimitation($limite_debut, $limite_nbre) {
 | 
        
           |  |  | 50 | 		$this->limite_debut = $limite_debut;
 | 
        
           |  |  | 51 | 		$this->limite_nbre = $limite_nbre;
 | 
        
           |  |  | 52 | 	}
 | 
        
           |  |  | 53 | 	public function getLimiteDebut() {
 | 
        
           |  |  | 54 | 		return $this->limite_debut;
 | 
        
           |  |  | 55 | 	}
 | 
        
           |  |  | 56 | 	public function getLimiteNbre() {
 | 
        
           |  |  | 57 | 		return $this->limite_nbre;
 | 
        
           |  |  | 58 | 	}
 | 
        
           |  |  | 59 | 	public function viderLimite() {
 | 
        
           |  |  | 60 | 		$this->limite_debut = null;
 | 
        
           |  |  | 61 | 		$this->limite_nbre = null;
 | 
        
           |  |  | 62 | 	}
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 | 	public function addOrdre($champ, $trie = self::ORDRE_ASCENDANT) {
 | 
        
           |  |  | 65 | 		if (!isset($this->ordre[$champ])) {
 | 
        
           |  |  | 66 | 			if (self::ORDRE_ASCENDANT == $trie || self::ORDRE_DESCENDANT == $trie) {
 | 
        
           |  |  | 67 | 				$this->ordre[$champ] = $trie;
 | 
        
           |  |  | 68 | 			} else {
 | 
        
           |  |  | 69 | 				$e = "La valeur pour le trie doit être : {self::ORDRE_ASCENDANT} ou {self::ORDRE_DESCENDANT}.";
 | 
        
           |  |  | 70 | 				trigger_error($e, E_USER_WARNING);
 | 
        
           |  |  | 71 | 			}
 | 
        
           |  |  | 72 | 		} else {
 | 
        
           |  |  | 73 | 			$e = "Le champ $champ existe déjà dans le tableau des ordres.";
 | 
        
           |  |  | 74 | 			trigger_error($e, E_USER_WARNING);
 | 
        
           |  |  | 75 | 		}
 | 
        
           |  |  | 76 | 	}
 | 
        
           |  |  | 77 | 	public function getOrdre() {
 | 
        
           |  |  | 78 | 		$champs = array();
 | 
        
           |  |  | 79 | 		foreach ($this->ordre as $champ => $trie) {
 | 
        
           |  |  | 80 | 			$champs[] = "$champ $trie";
 | 
        
           |  |  | 81 | 		}
 | 
        
           |  |  | 82 | 		return implode(', ', $champs);
 | 
        
           |  |  | 83 | 	}
 | 
        
           |  |  | 84 | 	public function viderOrdre() {
 | 
        
           |  |  | 85 | 		$this->ordre = null;
 | 
        
           |  |  | 86 | 	}
 | 
        
           |  |  | 87 |   | 
        
           |  |  | 88 | 	//+----------------------------------------------------------------------------------------------------------------+
 | 
        
           |  |  | 89 | 	// MÉTHODES
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 | 	protected function envoyerRequeteConsultation($url) {
 | 
        
           |  |  | 92 | 		$url = $this->traiterUrlParametres($url);
 | 
        
           |  |  | 93 | 		$retour = $this->envoyerRequete($url, 'GET');
 | 
        
           |  |  | 94 | 		return $retour;
 | 
        
           |  |  | 95 | 	}
 | 
        
           |  |  | 96 |   | 
        
           |  |  | 97 | 	protected function envoyerRequeteAjout($url, Array $donnees) {
 | 
        
           |  |  | 98 | 		$retour = $this->envoyerRequete($url, 'PUT', $donnees);
 | 
        
           |  |  | 99 | 		return $retour;
 | 
        
           |  |  | 100 | 	}
 | 
        
           |  |  | 101 |   | 
        
           |  |  | 102 | 	protected function envoyerRequeteModif($url, Array $donnees) {
 | 
        
           |  |  | 103 | 		$retour = $this->envoyerRequete($url, 'POST', $donnees);
 | 
        
           |  |  | 104 | 		return $retour;
 | 
        
           |  |  | 105 | 	}
 | 
        
           |  |  | 106 |   | 
        
           |  |  | 107 | 	protected function envoyerRequeteSuppression($url) {
 | 
        
           |  |  | 108 | 		$retour = $this->envoyerRequete($url, 'DELETE');
 | 
        
           |  |  | 109 | 		return $retour;
 | 
        
           |  |  | 110 | 	}
 | 
        
           |  |  | 111 |   | 
        
           |  |  | 112 | 	private function envoyerRequete($url, $mode, Array $donnees = array()) {
 | 
        
           |  |  | 113 | 		$contenu = false;
 | 
        
           |  |  | 114 | 		if ($mode != 'GET' && $mode != 'PUT' && $mode != 'POST' && $mode != 'DELETE') {
 | 
        
           |  |  | 115 | 			$e = "Le mode de requête '$mode' n'est pas accepté!";
 | 
        
           |  |  | 116 | 			trigger_error($e, E_USER_WARNING);
 | 
        
           |  |  | 117 | 		} else {
 | 
        
           |  |  | 118 | 			$contexte = stream_context_create(array(
 | 
        
           |  |  | 119 | 				'http' => array(
 | 
        
           |  |  | 120 |       				'method' => $mode,
 | 
        
           |  |  | 121 | 					'header' => "Content-type: application/x-www-form-urlencoded\r\n",
 | 
        
           |  |  | 122 |       				'content' => http_build_query($donnees, null, self::HTTP_REQUETE_SEPARATEUR))));
 | 
        
           |  |  | 123 | 			$flux = @fopen($url, 'r', false, $contexte);
 | 
        
           |  |  | 124 | 			if (!$flux) {
 | 
        
           |  |  | 125 | 				$this->traiterEntete($http_response_header, $url);
 | 
        
           | 146 | jpm | 126 | 				$e = "L'ouverture de l'url '$url' par la méthode HTTP '$mode' a échoué!";
 | 
        
           | 144 | jpm | 127 | 				trigger_error($e, E_USER_WARNING);
 | 
        
           |  |  | 128 | 			} else {
 | 
        
           |  |  | 129 | 				// Informations sur les en-têtes et métadonnées du flux
 | 
        
           |  |  | 130 | 				$entetes = stream_get_meta_data($flux);
 | 
        
           |  |  | 131 | 				$this->traiterEntete($entetes, $url);
 | 
        
           |  |  | 132 |   | 
        
           |  |  | 133 | 				// Contenu actuel de $url
 | 
        
           |  |  | 134 | 				$contenu = stream_get_contents($flux);
 | 
        
           |  |  | 135 |   | 
        
           |  |  | 136 | 				fclose($flux);
 | 
        
           |  |  | 137 | 			}
 | 
        
           |  |  | 138 | 		}
 | 
        
           |  |  | 139 | 		$this->reinitialiser();
 | 
        
           |  |  | 140 | 		return $contenu;
 | 
        
           |  |  | 141 | 	}
 | 
        
           |  |  | 142 |   | 
        
           |  |  | 143 | 	private function traiterUrlParametres($url) {
 | 
        
           |  |  | 144 | 		$parametres = array();
 | 
        
           |  |  | 145 | 		if (! is_null($this->getLimiteDebut())) {
 | 
        
           |  |  | 146 | 			$parametres[] = 'start='.$this->getLimiteDebut();
 | 
        
           |  |  | 147 | 		}
 | 
        
           |  |  | 148 | 		if (! is_null($this->getLimiteNbre())) {
 | 
        
           |  |  | 149 | 			$parametres[] = 'limit='.$this->getLimiteNbre();
 | 
        
           |  |  | 150 | 		}
 | 
        
           |  |  | 151 | 		if (! is_null($this->ordre)) {
 | 
        
           |  |  | 152 | 			$parametres[] = 'orderby='.urlencode($this->getOrdre());
 | 
        
           |  |  | 153 | 		}
 | 
        
           |  |  | 154 | 		if ($this->getDistinction() != 0) {
 | 
        
           |  |  | 155 | 			$parametres[] = 'distinct='.$this->getDistinction();
 | 
        
           |  |  | 156 | 		}
 | 
        
           |  |  | 157 | 		if (count($parametres) > 0) {
 | 
        
           |  |  | 158 | 			$url_parametres = implode('&', $parametres);
 | 
        
           |  |  | 159 | 			$url = $url.'?'.$url_parametres;
 | 
        
           |  |  | 160 | 		}
 | 
        
           |  |  | 161 | 		return $url;
 | 
        
           |  |  | 162 | 	}
 | 
        
           |  |  | 163 |   | 
        
           |  |  | 164 | 	private function traiterEntete($entetes, $uri) {
 | 
        
           |  |  | 165 | 		$infos = $this->analyserEntete($entetes, $uri);
 | 
        
           |  |  | 166 | 		$this->traiterEnteteDebug($infos);
 | 
        
           |  |  | 167 | 		$this->traiterEnteteMessage($infos);
 | 
        
           |  |  | 168 | 	}
 | 
        
           |  |  | 169 |   | 
        
           |  |  | 170 | 	private function analyserEntete($entetes, $uri) {
 | 
        
           |  |  | 171 | 		$infos = array('date' => null, 'uri' => $uri, 'debugs' => null, 'messages' => null);
 | 
        
           |  |  | 172 |   | 
        
           |  |  | 173 | 		if (isset($entetes['wrapper_data'])) {
 | 
        
           |  |  | 174 | 			$entetes = $entetes['wrapper_data'];
 | 
        
           |  |  | 175 | 		}
 | 
        
           |  |  | 176 | 		foreach ($entetes as $entete) {
 | 
        
           |  |  | 177 | 			if (preg_match('/^X-DebugJrest-Data: (.+)$/', $entete, $match)) {
 | 
        
           |  |  | 178 | 				$infos['debugs'] = json_decode($match[1]);
 | 
        
           |  |  | 179 | 			}
 | 
        
           |  |  | 180 | 			if (preg_match('/^X-MessageJrest-Data: (.+)$/', $entete, $match)) {
 | 
        
           |  |  | 181 | 				$infos['messages'] = json_decode($match[1]);
 | 
        
           |  |  | 182 | 			}
 | 
        
           |  |  | 183 | 			if (preg_match('/^Date: .+ ([012][0-9]:[012345][0-9]:[012345][0-9]) .*$/', $entete, $match)) {
 | 
        
           |  |  | 184 | 				$infos['date'] = $match[1];
 | 
        
           |  |  | 185 | 			}
 | 
        
           |  |  | 186 | 		}
 | 
        
           |  |  | 187 | 		return $infos;
 | 
        
           |  |  | 188 | 	}
 | 
        
           |  |  | 189 |   | 
        
           |  |  | 190 | 	private function traiterEnteteDebug($entetes) {
 | 
        
           |  |  | 191 | 		if (isset($entetes['debugs'])) {
 | 
        
           |  |  | 192 | 			$date = $entetes['date'];
 | 
        
           |  |  | 193 | 			$uri = $entetes['uri'];
 | 
        
           |  |  | 194 | 			$debugs = $entetes['debugs'];
 | 
        
           |  |  | 195 | 			foreach ($debugs as $debug) {
 | 
        
           |  |  | 196 | 				Debug::printr("DEBUG : $date - $uri :\n$debug");
 | 
        
           |  |  | 197 | 			}
 | 
        
           |  |  | 198 | 		}
 | 
        
           |  |  | 199 | 	}
 | 
        
           |  |  | 200 |   | 
        
           |  |  | 201 | 	private function traiterEnteteMessage($entetes) {
 | 
        
           |  |  | 202 | 		if (isset($entetes['messages'])) {
 | 
        
           |  |  | 203 | 			$date = $entetes['date'];
 | 
        
           |  |  | 204 | 			$uri = $entetes['uri'];
 | 
        
           |  |  | 205 | 			$messages = $entetes['messages'];
 | 
        
           |  |  | 206 | 			foreach ($messages as $message) {
 | 
        
           |  |  | 207 | 				Debug::printr("MESSAGE : $date - $uri :\n$message");
 | 
        
           |  |  | 208 | 			}
 | 
        
           |  |  | 209 | 		}
 | 
        
           |  |  | 210 | 	}
 | 
        
           |  |  | 211 |   | 
        
           |  |  | 212 | 	private function reinitialiser() {
 | 
        
           |  |  | 213 | 		$this->viderDistinction();
 | 
        
           |  |  | 214 | 		$this->viderLimite();
 | 
        
           |  |  | 215 | 		$this->viderOrdre();
 | 
        
           |  |  | 216 | 	}
 | 
        
           |  |  | 217 | }
 |