Subversion Repositories eFlore/Applications.cel

Rev

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