1795 |
jpm |
1 |
<?php
|
|
|
2 |
// declare(encoding='UTF-8');
|
|
|
3 |
/**
|
|
|
4 |
* Contexte permet d'encapsuler les super globales et de définir le contexte du web service courrant.
|
|
|
5 |
*
|
1815 |
jpm |
6 |
* @category DEL
|
|
|
7 |
* @package Services
|
|
|
8 |
* @package Bibliotheque
|
|
|
9 |
* @version 0.1
|
|
|
10 |
* @author Mathias CHOUET <mathias@tela-botanica.org>
|
|
|
11 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
12 |
* @author Aurelien PERONNET <aurelien@tela-botanica.org>
|
|
|
13 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
14 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
1795 |
jpm |
15 |
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
|
|
|
16 |
*/
|
|
|
17 |
class Contexte {
|
|
|
18 |
|
|
|
19 |
private $conteneur;
|
|
|
20 |
private $get;
|
|
|
21 |
private $getBrut;
|
|
|
22 |
private $post;
|
|
|
23 |
private $session;
|
|
|
24 |
private $cookie;
|
|
|
25 |
private $server;
|
|
|
26 |
private $urlRessource;
|
|
|
27 |
|
|
|
28 |
public function __construct($conteneur, &$server, &$get, &$post, &$session, &$cookie) {
|
|
|
29 |
$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
|
|
|
30 |
$this->server = $server;
|
|
|
31 |
$this->get = $this->nettoyerParametres($get);
|
|
|
32 |
$this->getBrut = $this->recupererParametresBruts();
|
|
|
33 |
$this->post = $post;
|
|
|
34 |
$this->session = $session;
|
|
|
35 |
$this->cookie = $cookie;
|
|
|
36 |
$this->urlRessource = $this->decouperUrlChemin();
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
public function getPhp($cle = null) {
|
|
|
40 |
return $cle === null ? $this->get : $this->get[$cle];
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
public function getQS($cle = null) {
|
|
|
44 |
return $cle === null ? $this->getBrut : $this->getBrut[$cle];
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
public function getPost($cle = null) {
|
|
|
48 |
return $cle === null ? $this->post : $this->post[$cle];
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
public function getServer($cle = null) {
|
|
|
52 |
return $cle === null ? $this->server : $this->server[$cle];
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
public function getSession($cle = null) {
|
|
|
56 |
return $cle === null ? $this->session : $this->session[$cle];
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
public function getCookie($cle = null) {
|
|
|
60 |
$retour = null;
|
|
|
61 |
if ($cle === null) {
|
|
|
62 |
$retour = $this->cookie;
|
|
|
63 |
} else if (isset($this->cookie[$cle])) {
|
|
|
64 |
$retour = $this->cookie[$cle];
|
|
|
65 |
}
|
|
|
66 |
return $retour;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
public function getRessource($position = null) {
|
|
|
70 |
return $position === null ? $this->urlRessource : $this->urlRessource[$position];
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
private function nettoyerParametres(Array $parametres) {
|
|
|
74 |
// Pas besoin d'utiliser urldecode car déjà fait par php pour les clés et valeur de $_GET
|
|
|
75 |
if (isset($parametres) && count($parametres) > 0) {
|
|
|
76 |
foreach ($parametres as $cle => $valeur) {
|
|
|
77 |
// les quotes, guillements et points-virgules ont été retirés des caractères à vérifier car
|
|
|
78 |
//ça n'a plus lieu d'être maintenant que l'on utilise protéger à peu près partout
|
|
|
79 |
$verifier = array('NULL', "\\", "\x00", "\x1a");
|
|
|
80 |
$parametres[$cle] = strip_tags(str_replace($verifier, '', $valeur));
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
return $parametres;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
private function recupererParametresBruts() {
|
|
|
87 |
$parametres_bruts = array();
|
|
|
88 |
if (isset($this->server['QUERY_STRING']) && !empty($this->server['QUERY_STRING'])) {
|
|
|
89 |
$paires = explode('&', $this->server['QUERY_STRING']);
|
|
|
90 |
foreach ($paires as $paire) {
|
|
|
91 |
$nv = explode('=', $paire);
|
|
|
92 |
$nom = urldecode($nv[0]);
|
|
|
93 |
$valeur = urldecode($nv[1]);
|
|
|
94 |
$parametres_bruts[$nom] = $valeur;
|
|
|
95 |
}
|
|
|
96 |
$parametres_bruts = $this->nettoyerParametres($parametres_bruts);
|
|
|
97 |
}
|
|
|
98 |
return $parametres_bruts;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
private function decouperUrlChemin() {
|
|
|
102 |
if (isset($this->server['REDIRECT_URL']) && $this->server['REDIRECT_URL'] != '') {
|
|
|
103 |
if (isset($this->server['REDIRECT_QUERY_STRING']) && !empty($this->server['REDIRECT_QUERY_STRING'])) {
|
|
|
104 |
$url = $this->server['REDIRECT_URL'].'?'.$this->server['REDIRECT_QUERY_STRING'];
|
|
|
105 |
} else {
|
|
|
106 |
$url = $this->server['REDIRECT_URL'];
|
|
|
107 |
}
|
|
|
108 |
} else {
|
|
|
109 |
$url = $this->server['REQUEST_URI'];
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
$tailleQueryString = strlen($this->server['QUERY_STRING']);
|
|
|
113 |
$tailleURL = ($tailleQueryString == 0) ? strlen($url) : -($tailleQueryString + 1);
|
|
|
114 |
|
|
|
115 |
$urlChaine = '';
|
|
|
116 |
if (strpos($url, $this->conteneur->getParametre('serveur.baseURL')) !== false) {
|
|
|
117 |
$urlChaine = substr($url, strlen($this->conteneur->getParametre('serveur.baseURL')), $tailleURL);
|
|
|
118 |
} else if (strpos($url, $this->conteneur->getParametre('serveur.baseAlternativeURL')) !== false) {
|
|
|
119 |
$urlChaine = substr($url, strlen($this->conteneur->getParametre('serveur.baseAlternativeURL')), $tailleURL);
|
|
|
120 |
}
|
|
|
121 |
return explode('/', $urlChaine);
|
|
|
122 |
}
|
|
|
123 |
}
|