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