Line 16... |
Line 16... |
16 |
const HTTP_URL_REQUETE_CLE_VALEUR_SEPARATEUR = '=';
|
16 |
const HTTP_URL_REQUETE_CLE_VALEUR_SEPARATEUR = '=';
|
17 |
private $http_methodes = array('GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS', 'CONNECT', 'TRACE');
|
17 |
private $http_methodes = array('GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS', 'CONNECT', 'TRACE');
|
18 |
protected $parametres = null;
|
18 |
protected $parametres = null;
|
19 |
private $url = null;
|
19 |
private $url = null;
|
20 |
private $reponse_entetes = null;
|
20 |
private $reponse_entetes = null;
|
- |
|
21 |
private $tracer_ip_source = null;
|
- |
|
22 |
private $requete_headers = array();
|
21 |
|
23 |
|
- |
|
24 |
public function __construct() {
|
- |
|
25 |
//verifie si paramètre tracer_ip_source est sur TRUE dans config.ini
|
- |
|
26 |
if (Config::existe('tracer_ip_source')) {
|
- |
|
27 |
//lire la config
|
- |
|
28 |
$valeur = Config::get('tracer_ip_source');
|
- |
|
29 |
//si oui, active X-Forwarded-For
|
- |
|
30 |
$this->setTracerIPSource($valeur);
|
- |
|
31 |
}
|
- |
|
32 |
//pas de else, si pas dans la config $tracer_ip_source sera NULL/FALSE
|
- |
|
33 |
}
|
22 |
//+------------------------------------------------------------------------------------------------------+
|
34 |
//+------------------------------------------------------------------------------------------------------+
|
23 |
// ACCESSEURS
|
35 |
// ACCESSEURS
|
Line 24... |
Line 36... |
24 |
|
36 |
|
25 |
public function getReponseEntetes() {
|
37 |
public function getReponseEntetes() {
|
Line 40... |
Line 52... |
40 |
}
|
52 |
}
|
Line 41... |
Line 53... |
41 |
|
53 |
|
42 |
public function nettoyerParametres() {
|
54 |
public function nettoyerParametres() {
|
43 |
$this->parametres = null;
|
55 |
$this->parametres = null;
|
44 |
}
|
56 |
}
|
- |
|
57 |
|
- |
|
58 |
public function setTracerIPSource($valeur) {
|
- |
|
59 |
$this->tracer_ip_source = $valeur;
|
- |
|
60 |
}
|
45 |
|
61 |
|
46 |
//+------------------------------------------------------------------------------------------------------+
|
62 |
//+------------------------------------------------------------------------------------------------------+
|
Line 47... |
Line 63... |
47 |
// MÉTHODES
|
63 |
// MÉTHODES
|
48 |
|
64 |
|
Line 72... |
Line 88... |
72 |
// nous n'activons le wrapper que pour GET pour l'instant
|
88 |
// nous n'activons le wrapper que pour GET pour l'instant
|
73 |
// car l'utilisation de curl pour les autres modes pourrait
|
89 |
// car l'utilisation de curl pour les autres modes pourrait
|
74 |
// vraisemblablement induire des comportements différents. (test-suite needed)
|
90 |
// vraisemblablement induire des comportements différents. (test-suite needed)
|
75 |
$contenu = $this->envoyerCurlRequete($url, $mode, $donnees);
|
91 |
$contenu = $this->envoyerCurlRequete($url, $mode, $donnees);
|
76 |
} else {
|
92 |
} else {
|
77 |
$contenu = $this->envoyerStreamRequete($url, $mode, $donnees);
|
93 |
$contenu = $this->envoyerStreamRequete($url, $mode, $donnees);
|
78 |
}
|
94 |
}
|
79 |
return $contenu;
|
95 |
return $contenu;
|
80 |
}
|
96 |
}
|
Line 81... |
Line 97... |
81 |
|
97 |
|
82 |
private function envoyerStreamRequete($url, $mode, Array $donnees = array()) {
|
98 |
private function envoyerStreamRequete($url, $mode, Array $donnees = array()) {
|
83 |
$this->url = $url;
|
99 |
$this->url = $url;
|
84 |
$contenu = false;
|
100 |
$contenu = false;
|
85 |
if (! in_array($mode, $this->http_methodes)) {
|
101 |
if (! in_array($mode, $this->http_methodes)) {
|
86 |
$e = "Le mode de requête '$mode' n'est pas accepté!";
|
102 |
$e = "Le mode de requête '$mode' n'est pas accepté!";
|
87 |
trigger_error($e, E_USER_WARNING);
|
103 |
trigger_error($e, E_USER_WARNING);
|
88 |
} else {
|
104 |
} else {
|
89 |
if ($mode == 'GET') {
|
105 |
if ($mode == 'GET') {
|
90 |
$this->traiterUrlParametres();
|
106 |
$this->traiterUrlParametres();
|
91 |
}
|
107 |
}
|
92 |
$content = http_build_query($donnees, null, self::HTTP_URL_REQUETE_SEPARATEUR);
|
108 |
$content = http_build_query($donnees, null, self::HTTP_URL_REQUETE_SEPARATEUR);
|
- |
|
109 |
$this->requete_headers['http'] = array();
|
- |
|
110 |
$this->requete_headers['http']['method'] = $mode;
|
93 |
$contexte = stream_context_create(array(
|
111 |
$this->requete_headers['http']['header'] = "Content-type: application/x-www-form-urlencoded\r\n";
|
- |
|
112 |
if ($this->tracer_ip_source) {
|
- |
|
113 |
// ajoute X-Forwarded-For au header
|
94 |
'http' => array(
|
114 |
$this->requete_headers['http']['header'] .= "X-Forwarded-For: {$_SERVER['REMOTE_ADDR']}, {$_SERVER['SERVER_ADDR']}\r\n";
|
- |
|
115 |
}
|
- |
|
116 |
$this->requete_headers['http']['header'] .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']} ApiTela/";
|
95 |
'method' => $mode,
|
117 |
$this->requete_headers['http']['header'] .= Framework::getInfoAppli('nom');
|
96 |
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
|
118 |
$this->requete_headers['http']['header'] .= "\r\n";
|
- |
|
119 |
$this->requete_headers['http']['content'] = $content;
|
97 |
'content' => $content)));
|
120 |
$contexte = stream_context_create($this->requete_headers);
|
98 |
$flux = @fopen($this->url, 'r', false, $contexte);
|
121 |
$flux = @fopen($this->url, 'r', false, $contexte);
|
99 |
if (!$flux) {
|
122 |
if (!$flux) {
|
100 |
$entetesFmt = print_r($this->analyserEntete(), true);
|
123 |
$entetesFmt = print_r($this->analyserEntete(), true);
|
101 |
$e = "Echec requête '$mode' : {$this->url}\n".
|
124 |
$e = "Echec requête '$mode' : {$this->url}\n".
|
102 |
"Paramètres requête : $content \n".
|
125 |
"Paramètres requête : $content \n".
|