Subversion Repositories Applications.referentiel

Rev

Rev 36 | Rev 38 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 36 Rev 37
1
<?php
1
<?php
2
// declare(encoding='UTF-8');
2
// declare(encoding='UTF-8');
3
/**
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.
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.
5
* Elle est abstraite donc doit obligatoirement être étendue.
6
*
6
*
7
* @category		Php5
7
* @category		Php5
8
* @package 		Referentiel
8
* @package 		Referentiel
9
* @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
9
* @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
10
* @copyright	2010 Tela-Botanica
10
* @copyright	2010 Tela-Botanica
11
* @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
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
12
* @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
13
* @version		SVN: $Id$
13
* @version		SVN: $Id$
14
*/
14
*/
15
abstract class Dao {
15
abstract class Dao {
16
	const HTTP_REQUETE_SEPARATEUR = '&';
16
	const HTTP_REQUETE_SEPARATEUR = '&';
17
	protected $distinction = '0';
17
	protected $distinction = '0';
18
	protected $limite_debut = null;
18
	protected $limite_debut = null;
19
	protected $limite_nbre = null;
19
	protected $limite_nbre = null;
20
	protected $url_jrest = null;
20
	protected $url_jrest = null;
21
	
21
	
22
	public function __construct() {
22
	public function __construct() {
23
		$this->url_jrest = Config::get('url_jrest');
23
		$this->url_jrest = Config::get('url_jrest');
24
	}
24
	}
25
	
25
	
26
	//+----------------------------------------------------------------------------------------------------------------+
26
	//+----------------------------------------------------------------------------------------------------------------+
27
	// ACCESSEURS
27
	// ACCESSEURS
28
	
28
	
29
	public function avoirLimitation() {
29
	public function avoirLimitation() {
30
		$limitation = false;
30
		$limitation = false;
31
		if (!is_null($this->limite_debut) && !is_null($this->limite_nbre)) {
31
		if (!is_null($this->limite_debut) && !is_null($this->limite_nbre)) {
32
			$limitation = true;
32
			$limitation = true;
33
		}
33
		}
34
		return $limitation;
34
		return $limitation;
35
	}
35
	}
36
	
36
	
37
	public function setDistinction($distinct) {
37
	public function setDistinction($distinct) {
38
		$this->distinction = $distinct;
38
		$this->distinction = $distinct;
39
	}
39
	}
40
	
40
	
41
	public function getDistinction() {
41
	public function getDistinction() {
42
		return $this->distinction;
42
		return $this->distinction;
43
	}
43
	}
44
	
44
	
45
	public function setLimitation($limite_debut, $limite_nbre) {
45
	public function setLimitation($limite_debut, $limite_nbre) {
46
		$this->limite_debut = $limite_debut;
46
		$this->limite_debut = $limite_debut;
47
		$this->limite_nbre = $limite_nbre;
47
		$this->limite_nbre = $limite_nbre;
48
	}
48
	}
49
	
49
	
50
	public function getLimiteDebut() {
50
	public function getLimiteDebut() {
51
		return $this->limite_debut;
51
		return $this->limite_debut;
52
	}
52
	}
53
	public function getLimiteNbre() {
53
	public function getLimiteNbre() {
54
		return $this->limite_nbre;
54
		return $this->limite_nbre;
55
	}
55
	}
56
	
56
	
57
	//+----------------------------------------------------------------------------------------------------------------+
57
	//+----------------------------------------------------------------------------------------------------------------+
58
	// MÉTHODES
58
	// MÉTHODES
59
	
59
	
60
	protected function envoyerRequeteConsultation($url) {
60
	protected function envoyerRequeteConsultation($url) {
61
		$retour = $this->envoyerRequete($url, 'GET');
61
		$retour = $this->envoyerRequete($url, 'GET');
62
		return $retour;
62
		return $retour;
63
	}
63
	}
64
	
64
	
65
	protected function envoyerRequeteAjout($url, Array $donnees) {
65
	protected function envoyerRequeteAjout($url, Array $donnees) {
66
		$retour = $this->envoyerRequete($url, 'PUT', $donnees);
66
		$retour = $this->envoyerRequete($url, 'PUT', $donnees);
67
		return $retour;
67
		return $retour;
68
	}
68
	}
69
	
69
	
70
	protected function envoyerRequeteModif($url, Array $donnees) {
70
	protected function envoyerRequeteModif($url, Array $donnees) {
71
		$retour = $this->envoyerRequete($url, 'POST', $donnees);
71
		$retour = $this->envoyerRequete($url, 'POST', $donnees);
72
		return $retour;
72
		return $retour;
73
	}
73
	}
74
	
74
	
75
	protected function envoyerRequeteSuppression($url) {
75
	protected function envoyerRequeteSuppression($url) {
76
		$retour = $this->envoyerRequete($url, 'DELETE');
76
		$retour = $this->envoyerRequete($url, 'DELETE');
77
		return $retour;
77
		return $retour;
78
	}
78
	}
79
	
79
	
80
	private function envoyerRequete($url, $mode, Array $donnees = array()) {
80
	private function envoyerRequete($url, $mode, Array $donnees = array()) {
81
		$contenu = false;
81
		$contenu = false;
82
		if ($mode != 'GET' && $mode != 'PUT' && $mode != 'POST' && $mode != 'DELETE') {
82
		if ($mode != 'GET' && $mode != 'PUT' && $mode != 'POST' && $mode != 'DELETE') {
83
			$e = "Le mode de requête '$mode' n'est pas accepté!";
83
			$e = "Le mode de requête '$mode' n'est pas accepté!";
84
			trigger_error($e, E_USER_WARNING);
84
			trigger_error($e, E_USER_WARNING);
85
		} else {
85
		} else {
86
			$contexte = stream_context_create(array(
86
			$contexte = stream_context_create(array(
87
				'http' => array(
87
				'http' => array(
88
      				'method' => $mode,
88
      				'method' => $mode,
89
					'header' => "Content-type: application/x-www-form-urlencoded\r\n",
89
					'header' => "Content-type: application/x-www-form-urlencoded\r\n",
90
      				'content' => http_build_query($donnees, null, HTTP_REQUETE_SEPARATEUR))));
90
      				'content' => http_build_query($donnees, null, self::HTTP_REQUETE_SEPARATEUR))));
91
			$flux = @fopen($url, 'r', false, $contexte);
91
			$flux = @fopen($url, 'r', false, $contexte);
92
			if (!$flux) {
92
			if (!$flux) {
93
				$this->traiterEntete($http_response_header, $url);
93
				$this->traiterEntete($http_response_header, $url);
94
				$e = "L'ouverture de l'url '$url' a échoué!";
94
				$e = "L'ouverture de l'url '$url' a échoué!";
95
				trigger_error($e, E_USER_WARNING);
95
				trigger_error($e, E_USER_WARNING);
96
			} else {
96
			} else {
97
				// Informations sur les en-têtes et métadonnées du flux
97
				// Informations sur les en-têtes et métadonnées du flux
98
				$entetes = stream_get_meta_data($flux);
98
				$entetes = stream_get_meta_data($flux);
99
				$this->traiterEntete($entetes, $url);
99
				$this->traiterEntete($entetes, $url);
100
				
100
				
101
				// Contenu actuel de $url
101
				// Contenu actuel de $url
102
				$contenu = stream_get_contents($flux);
102
				$contenu = stream_get_contents($flux);
103
				
103
				
104
				fclose($flux);
104
				fclose($flux);
105
			}
105
			}
106
		}
106
		}
107
		return $contenu;
107
		return $contenu;
108
	}
108
	}
109
	
109
	
110
	private function traiterEntete($entetes, $uri) {
110
	private function traiterEntete($entetes, $uri) {
111
		$infos = $this->analyserEntete($entetes, $uri);
111
		$infos = $this->analyserEntete($entetes, $uri);
112
		$this->traiterEnteteDebug($infos);
112
		$this->traiterEnteteDebug($infos);
113
		$this->traiterEnteteMessage($infos);
113
		$this->traiterEnteteMessage($infos);
114
	}
114
	}
115
	
115
	
116
	private function analyserEntete($entetes, $uri) {
116
	private function analyserEntete($entetes, $uri) {
117
		$infos = array('date' => null, 'uri' => $uri, 'debugs' => null, 'messages' => null);
117
		$infos = array('date' => null, 'uri' => $uri, 'debugs' => null, 'messages' => null);
118
		
118
		
119
		if (isset($entetes['wrapper_data'])) {
119
		if (isset($entetes['wrapper_data'])) {
120
			$entetes = $entetes['wrapper_data'];
120
			$entetes = $entetes['wrapper_data'];
121
		}
121
		}
122
		foreach ($entetes as $entete) {
122
		foreach ($entetes as $entete) {
123
			if (preg_match('/^X-DebugJrest-Data: (.+)$/', $entete, $match)) {
123
			if (preg_match('/^X-DebugJrest-Data: (.+)$/', $entete, $match)) {
124
				$infos['debugs'] = json_decode($match[1]);
124
				$infos['debugs'] = json_decode($match[1]);
125
			}
125
			}
126
			if (preg_match('/^X-MessageJrest-Data: (.+)$/', $entete, $match)) {
126
			if (preg_match('/^X-MessageJrest-Data: (.+)$/', $entete, $match)) {
127
				$infos['messages'] = json_decode($match[1]);
127
				$infos['messages'] = json_decode($match[1]);
128
			}
128
			}
129
			if (preg_match('/^Date: .+ ([012][0-9]:[012345][0-9]:[012345][0-9]) .*$/', $entete, $match)) {
129
			if (preg_match('/^Date: .+ ([012][0-9]:[012345][0-9]:[012345][0-9]) .*$/', $entete, $match)) {
130
				$infos['date'] = $match[1];
130
				$infos['date'] = $match[1];
131
			}
131
			}
132
		}
132
		}
133
		return $infos;
133
		return $infos;
134
	}
134
	}
135
	
135
	
136
	private function traiterEnteteDebug($entetes) {
136
	private function traiterEnteteDebug($entetes) {
137
		if (isset($entetes['debugs'])) {
137
		if (isset($entetes['debugs'])) {
138
			$date = $entetes['date'];
138
			$date = $entetes['date'];
139
			$uri = $entetes['uri'];
139
			$uri = $entetes['uri'];
140
			$debugs = $entetes['debugs'];
140
			$debugs = $entetes['debugs'];
141
			foreach ($debugs as $debug) {
141
			foreach ($debugs as $debug) {
142
				Debug::printr("DEBUG : $date - $uri :\n$debug");
142
				Debug::printr("DEBUG : $date - $uri :\n$debug");
143
			}
143
			}
144
		}
144
		}
145
	}
145
	}
146
	
146
	
147
	private function traiterEnteteMessage($entetes) {
147
	private function traiterEnteteMessage($entetes) {
148
		if (isset($entetes['messages'])) {
148
		if (isset($entetes['messages'])) {
149
			$date = $entetes['date'];
149
			$date = $entetes['date'];
150
			$uri = $entetes['uri'];
150
			$uri = $entetes['uri'];
151
			$messages = $entetes['messages'];
151
			$messages = $entetes['messages'];
152
			foreach ($messages as $message) {
152
			foreach ($messages as $message) {
153
				Debug::printr("MESSAGE : $date - $uri :\n$message");
153
				Debug::printr("MESSAGE : $date - $uri :\n$message");
154
			}
154
			}
155
		}
155
		}
156
	}
156
	}
157
}
157
}