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