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