Subversion Repositories eFlore/Applications.cel

Rev

Go to most recent revision | Details | 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
 
698 jpm 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",
698 jpm 83
      				'content' => http_build_query($donnees, null, self::HTTP_URL_REQUETE_SEPARATEUR))));
713 jpm 84
			$flux = @fopen($this->url, 'r', false, $contexte);
470 jpm 85
			if (!$flux) {
698 jpm 86
				$this->reponse_entetes = $http_response_header;
713 jpm 87
				$e = "L'ouverture de l'url '{$this->url}' par la méthode HTTP '$mode' a échoué!";
470 jpm 88
				trigger_error($e, E_USER_WARNING);
89
			} else {
90
				// Informations sur les en-têtes et métadonnées du flux
698 jpm 91
				$this->reponse_entetes = stream_get_meta_data($flux);
470 jpm 92
 
93
				// Contenu actuel de $url
94
				$contenu = stream_get_contents($flux);
95
 
96
				fclose($flux);
97
			}
698 jpm 98
			$this->traiterEntete();
470 jpm 99
		}
100
		$this->reinitialiser();
101
		return $contenu;
102
	}
103
 
698 jpm 104
	private function traiterUrlParametres() {
470 jpm 105
		$parametres = array();
698 jpm 106
		if (count($this->parametres) > 0) {
107
			foreach ($this->parametres as $cle => $valeur) {
713 jpm 108
				$cle = rawurlencode($cle);
109
				$valeur = rawurlencode($valeur);
698 jpm 110
				$parametres[] = $cle.self::HTTP_URL_REQUETE_CLE_VALEUR_SEPARATEUR.$valeur;
111
			}
112
			$url_parametres = implode(self::HTTP_URL_REQUETE_SEPARATEUR, $parametres);
113
			$this->url = $this->url.'?'.$url_parametres;
470 jpm 114
		}
115
	}
116
 
698 jpm 117
	private function traiterEntete() {
118
		$infos = $this->analyserEntete();
119
		$this->traiterEnteteDebogage($infos);
470 jpm 120
	}
121
 
698 jpm 122
	private function analyserEntete() {
123
		$entetes = $this->reponse_entetes;
124
		$infos = array('date' => null, 'uri' => $this->url, 'debogages' => null);
470 jpm 125
 
126
		if (isset($entetes['wrapper_data'])) {
127
			$entetes = $entetes['wrapper_data'];
128
		}
129
		foreach ($entetes as $entete) {
698 jpm 130
			if (preg_match('/^X_REST_DEBOGAGE_MESSAGES: (.+)$/', $entete, $match)) {
131
				$infos['debogages'] = json_decode($match[1]);
470 jpm 132
			}
133
			if (preg_match('/^Date: .+ ([012][0-9]:[012345][0-9]:[012345][0-9]) .*$/', $entete, $match)) {
134
				$infos['date'] = $match[1];
135
			}
136
		}
137
		return $infos;
138
	}
139
 
698 jpm 140
	private function traiterEnteteDebogage($entetes_analyses) {
141
		if (isset($entetes['debogages'])) {
470 jpm 142
			$date = $entetes['date'];
143
			$uri = $entetes['uri'];
698 jpm 144
			$debogages = $entetes['debogages'];
145
			foreach ($debogages as $debogage) {
146
				$e = "DEBOGAGE : $date - $uri :\n$debogage";
470 jpm 147
				trigger_error($e, E_USER_NOTICE);
148
			}
149
		}
150
	}
151
 
152
	private function reinitialiser() {
698 jpm 153
		$this->nettoyerParametres();
470 jpm 154
	}
155
}