Subversion Repositories eFlore/Applications.cel

Rev

Rev 470 | Rev 713 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 470 Rev 698
Line 10... Line 10...
10
* @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
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
11
* @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
12
* @version		SVN: $Id$
12
* @version		SVN: $Id$
13
*/
13
*/
14
class Dao {
14
class Dao {
15
	const ORDRE_ASCENDANT = 'ASC';
-
 
16
	const ORDRE_DESCENDANT = 'DESC';
-
 
17
	const HTTP_REQUETE_SEPARATEUR = '&';
15
	const HTTP_URL_REQUETE_SEPARATEUR = '&';
18
	protected $distinction = '0';
16
	private $http_methodes = array('GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS', 'CONNECT', 'TRACE');
19
	protected $limite_debut = null;
17
	protected $parametres = null;
20
	protected $limite_nbre = null;
18
	private $url = null;
21
	protected $ordre = null;
19
	private $reponse_entetes = null;
Line 22... Line 20...
22
	
20
	
23
	//+----------------------------------------------------------------------------------------------------------------+
21
	//+----------------------------------------------------------------------------------------------------------------+
Line 24... Line -...
24
	// ACCESSEURS
-
 
25
	
-
 
26
	public function setDistinction($distinct) {
-
 
27
		$this->distinction = $distinct;
22
	// ACCESSEURS
28
	}
23
	
29
	public function getDistinction() {
-
 
30
		return $this->distinction;
-
 
31
	}
-
 
32
	public function viderDistinction() {
24
	public function getReponseEntetes($cle) {
Line 33... Line 25...
33
		$this->distinction = null;
25
		return $this->reponse_entetes;
34
	}
-
 
35
	
26
	}
36
	public function avoirLimitation() {
-
 
37
		$limitation = false;
-
 
38
		if (!is_null($this->limite_debut) && !is_null($this->limite_nbre)) {
27
	
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() {
28
	public function getParametre($cle) {
Line 54... Line 29...
54
		$this->limite_debut = null;
29
		$valeur = (isset($this->parametres[$cle])) ? $this->parametres[$cle] : null;
55
		$this->limite_nbre = null;
-
 
56
	}
-
 
57
	
30
		return $valeur;
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 {
31
	}
-
 
32
	
67
			$e = "Le champ $champ existe déjà dans le tableau des ordres.";
33
	public function ajouterParametre($cle, $valeur) {
68
			trigger_error($e, E_USER_WARNING);
-
 
69
		}
34
		$this->parametres[$cle] = $valeur;
70
	}
-
 
71
	public function getOrdre() {
-
 
72
		$champs = array();
-
 
73
		foreach ($this->ordre as $champ => $trie) {
35
	}
-
 
36
	
74
			$champs[] = "$champ $trie";
37
	public function supprimerParametre($cle) {
75
		}
38
		unset($this->parametres[$cle]);
76
		return implode(', ', $champs);
39
	}
Line 77... Line 40...
77
	}
40
	
78
	public function viderOrdre() {
41
	public function nettoyerParametres() {
Line 79... Line 42...
79
		$this->ordre = null;
42
		$this->parametres = null;
80
	}
-
 
81
	
43
	}
82
	//+----------------------------------------------------------------------------------------------------------------+
44
	
83
	// MÉTHODES
45
	//+----------------------------------------------------------------------------------------------------------------+
Line 84... Line 46...
84
	
46
	// MÉTHODES
85
	public function envoyerRequeteConsultation($url) {
47
	
86
		$url = $this->traiterUrlParametres($url);
48
	public function consulter($url) {
87
		$retour = $this->envoyerRequete($url, 'GET');
49
		$retour = $this->envoyerRequete($url, 'GET');
Line 88... Line 50...
88
		return $retour;
50
		return $retour;
89
	}
51
	}
90
	
52
	
91
	public function envoyerRequeteAjout($url, Array $donnees) {
53
	public function ajouter($url, Array $donnees) {
Line 92... Line 54...
92
		$retour = $this->envoyerRequete($url, 'PUT', $donnees);
54
		$retour = $this->envoyerRequete($url, 'PUT', $donnees);
93
		return $retour;
55
		return $retour;
94
	}
56
	}
95
	
57
	
Line 96... Line 58...
96
	public function envoyerRequeteModif($url, Array $donnees) {
58
	public function modifier($url, Array $donnees) {
-
 
59
		$retour = $this->envoyerRequete($url, 'POST', $donnees);
97
		$retour = $this->envoyerRequete($url, 'POST', $donnees);
60
		return $retour;
98
		return $retour;
61
	}
99
	}
62
	
100
	
63
	public function supprimer($url) {
101
	public function envoyerRequeteSuppression($url) {
64
		$retour = $this->envoyerRequete($url, 'DELETE');
-
 
65
		return $retour;
-
 
66
	}
-
 
67
	
102
		$retour = $this->envoyerRequete($url, 'DELETE');
68
	public function envoyerRequete($url, $mode, Array $donnees = array()) {
103
		return $retour;
69
		$this->url = $url;
104
	}
70
		$contenu = false;
105
	
71
		if (! in_array($mode, $this->http_methodes)) {
106
	private function envoyerRequete($url, $mode, Array $donnees = array()) {
72
			$e = "Le mode de requête '$mode' n'est pas accepté!";
107
		$contenu = false;
73
			trigger_error($e, E_USER_WARNING);
108
		if ($mode != 'GET' && $mode != 'PUT' && $mode != 'POST' && $mode != 'DELETE') {
74
		} else {
109
			$e = "Le mode de requête '$mode' n'est pas accepté!";
75
			if ($mode == 'GET') {
110
			trigger_error($e, E_USER_WARNING);
76
				$this->traiterUrlParametres();
111
		} else {
77
			}
112
			$contexte = stream_context_create(array(
78
			$contexte = stream_context_create(array(
113
				'http' => array(
79
				'http' => array(
114
      				'method' => $mode,
80
      				'method' => $mode,
115
					'header' => "Content-type: application/x-www-form-urlencoded\r\n",
-
 
Line 116... Line 81...
116
      				'content' => http_build_query($donnees, null, self::HTTP_REQUETE_SEPARATEUR))));
81
					'header' => "Content-type: application/x-www-form-urlencoded\r\n",
117
			$flux = @fopen($url, 'r', false, $contexte);
82
      				'content' => http_build_query($donnees, null, self::HTTP_URL_REQUETE_SEPARATEUR))));
Line 118... Line 83...
118
			if (!$flux) {
83
			$flux = @fopen($url, 'r', false, $contexte);
119
				$this->traiterEntete($http_response_header, $url);
84
			if (!$flux) {
-
 
85
				$this->reponse_entetes = $http_response_header;
120
				$e = "L'ouverture de l'url '$url' par la méthode HTTP '$mode' a échoué!";
86
				$e = "L'ouverture de l'url '$url' par la méthode HTTP '$mode' a échoué!";
121
				trigger_error($e, E_USER_WARNING);
87
				trigger_error($e, E_USER_WARNING);
122
			} else {
88
			} else {
123
				// Informations sur les en-têtes et métadonnées du flux
89
				// Informations sur les en-têtes et métadonnées du flux
Line 124... Line 90...
124
				$entetes = stream_get_meta_data($flux);
90
				$this->reponse_entetes = stream_get_meta_data($flux);
125
				$this->traiterEntete($entetes, $url);
91
				
126
				
-
 
127
				// Contenu actuel de $url
-
 
128
				$contenu = stream_get_contents($flux);
-
 
129
				
-
 
130
				fclose($flux);
-
 
131
			}
-
 
132
		}
92
				// Contenu actuel de $url
133
		$this->reinitialiser();
-
 
134
		return $contenu;
-
 
135
	}
93
				$contenu = stream_get_contents($flux);
136
	
94
				
137
	private function traiterUrlParametres($url) {
95
				fclose($flux);
138
		$parametres = array();
-
 
139
		if (! is_null($this->getLimiteDebut())) {
96
			}
140
			$parametres[] = 'start='.$this->getLimiteDebut();
97
			$this->traiterEntete();
141
		}
98
		}
142
		if (! is_null($this->getLimiteNbre())) {
-
 
143
			$parametres[] = 'limit='.$this->getLimiteNbre();
99
		$this->reinitialiser();
Line 144... Line 100...
144
		}
100
		return $contenu;
145
		if (! is_null($this->ordre)) {
101
	}
146
			$parametres[] = 'orderby='.urlencode($this->getOrdre());
-
 
147
		}
102
	
148
		if ($this->getDistinction() != 0) {
103
	private function traiterUrlParametres() {
Line 149... Line 104...
149
			$parametres[] = 'distinct='.$this->getDistinction();
104
		$parametres = array();
-
 
105
		if (count($this->parametres) > 0) {
150
		}
106
			foreach ($this->parametres as $cle => $valeur) {
Line 151... Line 107...
151
		if (count($parametres) > 0) {
107
				$parametres[] = $cle.self::HTTP_URL_REQUETE_CLE_VALEUR_SEPARATEUR.$valeur;
152
			$url_parametres = implode('&', $parametres);
108
			}
153
			$url = $url.'?'.$url_parametres;
109
			$url_parametres = implode(self::HTTP_URL_REQUETE_SEPARATEUR, $parametres);
154
		}
110
			$this->url = $this->url.'?'.$url_parametres;
155
		return $url;
111
		}
156
	}
-
 
157
	
-
 
158
	private function traiterEntete($entetes, $uri) {
-
 
159
		$infos = $this->analyserEntete($entetes, $uri);
112
	}
160
		$this->traiterEnteteDebug($infos);
113
	
161
		$this->traiterEnteteMessage($infos);
114
	private function traiterEntete() {
162
	}
115
		$infos = $this->analyserEntete();
163
	
116
		$this->traiterEnteteDebogage($infos);
164
	private function analyserEntete($entetes, $uri) {
117
	}
165
		$infos = array('date' => null, 'uri' => $uri, 'debugs' => null, 'messages' => null);
118
	
166
		
119
	private function analyserEntete() {
Line 167... Line -...
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
			}
120
		$entetes = $this->reponse_entetes; 
180
		}
121
		$infos = array('date' => null, 'uri' => $this->url, 'debogages' => null);
181
		return $infos;
122
		
182
	}
123
		if (isset($entetes['wrapper_data'])) {
183
	
124
			$entetes = $entetes['wrapper_data'];
184
	private function traiterEnteteDebug($entetes) {
125
		}
185
		if (isset($entetes['debugs'])) {
126
		foreach ($entetes as $entete) {
186
			$date = $entetes['date'];
127
			if (preg_match('/^X_REST_DEBOGAGE_MESSAGES: (.+)$/', $entete, $match)) {
187
			$uri = $entetes['uri'];
128
				$infos['debogages'] = json_decode($match[1]);
188
			$debugs = $entetes['debugs'];
129
			}
189
			foreach ($debugs as $debug) {
130
			if (preg_match('/^Date: .+ ([012][0-9]:[012345][0-9]:[012345][0-9]) .*$/', $entete, $match)) {
Line 190... Line 131...
190
				$e = "DEBUG : $date - $uri :\n$debug";
131
				$infos['date'] = $match[1];
191
				trigger_error($e, E_USER_NOTICE);
-
 
192
			}
132
			}
193
		}
-
 
194
	}
133
		}
195
	
134
		return $infos;
196
	private function traiterEnteteMessage($entetes) {
135
	}