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