Subversion Repositories eFlore/Applications.cel

Rev

Rev 713 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
470 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
*
6
* @category	php5
7
* @package 	Widget
8
* @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
9
* @copyright	2010 Tela-Botanica
10
* @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
11
* @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
12
* @version		SVN: $Id$
13
*/
14
class Dao {
698 jpm 15
	const HTTP_URL_REQUETE_SEPARATEUR = '&';
713 jpm 16
	const HTTP_URL_REQUETE_CLE_VALEUR_SEPARATEUR = '=';
698 jpm 17
	private $http_methodes = array('GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS', 'CONNECT', 'TRACE');
18
	protected $parametres = null;
19
	private $url = null;
20
	private $reponse_entetes = null;
470 jpm 21
 
22
	//+----------------------------------------------------------------------------------------------------------------+
23
	// ACCESSEURS
24
 
698 jpm 25
	public function getReponseEntetes($cle) {
26
		return $this->reponse_entetes;
470 jpm 27
	}
698 jpm 28
 
29
	public function getParametre($cle) {
30
		$valeur = (isset($this->parametres[$cle])) ? $this->parametres[$cle] : null;
31
		return $valeur;
470 jpm 32
	}
698 jpm 33
 
34
	public function ajouterParametre($cle, $valeur) {
35
		$this->parametres[$cle] = $valeur;
470 jpm 36
	}
37
 
698 jpm 38
	public function supprimerParametre($cle) {
39
		unset($this->parametres[$cle]);
470 jpm 40
	}
41
 
698 jpm 42
	public function nettoyerParametres() {
43
		$this->parametres = null;
470 jpm 44
	}
45
 
46
	//+----------------------------------------------------------------------------------------------------------------+
47
	// MÉTHODES
48
 
698 jpm 49
	public function consulter($url) {
470 jpm 50
		$retour = $this->envoyerRequete($url, 'GET');
51
		return $retour;
52
	}
53
 
698 jpm 54
	public function ajouter($url, Array $donnees) {
470 jpm 55
		$retour = $this->envoyerRequete($url, 'PUT', $donnees);
56
		return $retour;
57
	}
58
 
3289 delphine 59
	public function modifier($url, Array $donnees) {
470 jpm 60
		$retour = $this->envoyerRequete($url, 'POST', $donnees);
61
		return $retour;
62
	}
63
 
698 jpm 64
	public function supprimer($url) {
470 jpm 65
		$retour = $this->envoyerRequete($url, 'DELETE');
66
		return $retour;
67
	}
68
 
698 jpm 69
	public function envoyerRequete($url, $mode, Array $donnees = array()) {
70
		$this->url = $url;
470 jpm 71
		$contenu = false;
698 jpm 72
		if (! in_array($mode, $this->http_methodes)) {
470 jpm 73
			$e = "Le mode de requête '$mode' n'est pas accepté!";
74
			trigger_error($e, E_USER_WARNING);
75
		} else {
698 jpm 76
			if ($mode == 'GET') {
77
				$this->traiterUrlParametres();
78
			}
470 jpm 79
			$contexte = stream_context_create(array(
80
				'http' => array(
81
      				'method' => $mode,
82
					'header' => "Content-type: application/x-www-form-urlencoded\r\n",
3289 delphine 83
					'content' => http_build_query($donnees, null, self::HTTP_URL_REQUETE_SEPARATEUR))));
84
 
85
			$flux = fopen($this->url, 'r', false, $contexte);
470 jpm 86
			if (!$flux) {
698 jpm 87
				$this->reponse_entetes = $http_response_header;
713 jpm 88
				$e = "L'ouverture de l'url '{$this->url}' par la méthode HTTP '$mode' a échoué!";
470 jpm 89
				trigger_error($e, E_USER_WARNING);
90
			} else {
91
				// Informations sur les en-têtes et métadonnées du flux
698 jpm 92
				$this->reponse_entetes = stream_get_meta_data($flux);
470 jpm 93
 
94
				// Contenu actuel de $url
95
				$contenu = stream_get_contents($flux);
96
 
97
				fclose($flux);
98
			}
698 jpm 99
			$this->traiterEntete();
470 jpm 100
		}
101
		$this->reinitialiser();
102
		return $contenu;
103
	}
104
 
698 jpm 105
	private function traiterUrlParametres() {
470 jpm 106
		$parametres = array();
698 jpm 107
		if (count($this->parametres) > 0) {
108
			foreach ($this->parametres as $cle => $valeur) {
713 jpm 109
				$cle = rawurlencode($cle);
110
				$valeur = rawurlencode($valeur);
698 jpm 111
				$parametres[] = $cle.self::HTTP_URL_REQUETE_CLE_VALEUR_SEPARATEUR.$valeur;
112
			}
113
			$url_parametres = implode(self::HTTP_URL_REQUETE_SEPARATEUR, $parametres);
114
			$this->url = $this->url.'?'.$url_parametres;
470 jpm 115
		}
116
	}
117
 
698 jpm 118
	private function traiterEntete() {
119
		$infos = $this->analyserEntete();
120
		$this->traiterEnteteDebogage($infos);
470 jpm 121
	}
122
 
698 jpm 123
	private function analyserEntete() {
124
		$entetes = $this->reponse_entetes;
125
		$infos = array('date' => null, 'uri' => $this->url, 'debogages' => null);
470 jpm 126
 
127
		if (isset($entetes['wrapper_data'])) {
128
			$entetes = $entetes['wrapper_data'];
129
		}
130
		foreach ($entetes as $entete) {
698 jpm 131
			if (preg_match('/^X_REST_DEBOGAGE_MESSAGES: (.+)$/', $entete, $match)) {
132
				$infos['debogages'] = json_decode($match[1]);
470 jpm 133
			}
134
			if (preg_match('/^Date: .+ ([012][0-9]:[012345][0-9]:[012345][0-9]) .*$/', $entete, $match)) {
135
				$infos['date'] = $match[1];
136
			}
137
		}
138
		return $infos;
139
	}
140
 
698 jpm 141
	private function traiterEnteteDebogage($entetes_analyses) {
142
		if (isset($entetes['debogages'])) {
470 jpm 143
			$date = $entetes['date'];
144
			$uri = $entetes['uri'];
698 jpm 145
			$debogages = $entetes['debogages'];
146
			foreach ($debogages as $debogage) {
147
				$e = "DEBOGAGE : $date - $uri :\n$debogage";
470 jpm 148
				trigger_error($e, E_USER_NOTICE);
149
			}
150
		}
151
	}
152
 
153
	private function reinitialiser() {
698 jpm 154
		$this->nettoyerParametres();
470 jpm 155
	}
156
}