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 |
|
1819 |
jpm |
28 |
private $mapping = array('getPhp' => 'get',
|
|
|
29 |
'getQS' => 'getBrut',
|
|
|
30 |
'getPost' => 'post',
|
|
|
31 |
'getServer' => 'server',
|
|
|
32 |
'getSession' => 'session',
|
|
|
33 |
'getCookie' => 'cookie',
|
1826 |
jpm |
34 |
'getRessource' => 'urlRessource',
|
|
|
35 |
'setCookie' => 'cookie');
|
1819 |
jpm |
36 |
|
1795 |
jpm |
37 |
public function __construct($conteneur, &$server, &$get, &$post, &$session, &$cookie) {
|
|
|
38 |
$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
|
|
|
39 |
$this->server = $server;
|
|
|
40 |
$this->get = $this->nettoyerParametres($get);
|
|
|
41 |
$this->getBrut = $this->recupererParametresBruts();
|
|
|
42 |
$this->post = $post;
|
|
|
43 |
$this->session = $session;
|
|
|
44 |
$this->cookie = $cookie;
|
|
|
45 |
$this->urlRessource = $this->decouperUrlChemin();
|
|
|
46 |
}
|
|
|
47 |
|
1819 |
jpm |
48 |
public function __call($nom, $arguments) {
|
|
|
49 |
if (!isset($this->mapping[$nom])) {
|
|
|
50 |
$msg = "La méthode $nom n'existe pas dans l'objet {get_class()}";
|
|
|
51 |
throw new Exception($msg, RestServeur::HTTP_CODE_ERREUR);
|
|
|
52 |
}
|
|
|
53 |
$attributNom = $this->mapping[$nom];
|
|
|
54 |
$data = $this->$attributNom;
|
1795 |
jpm |
55 |
|
1826 |
jpm |
56 |
if (substr($nom, 0, 3) == 'get') {
|
|
|
57 |
$cle = isset($arguments[0]) ? $arguments[0] : null;
|
|
|
58 |
return $this->getGenerique($data, $cle);
|
|
|
59 |
} else if (substr($nom, 0, 3) == 'set') {
|
|
|
60 |
$cle = isset($arguments[0]) ? $arguments[0] : null;
|
|
|
61 |
$valeur = isset($arguments[1]) ? $arguments[1] : null;
|
|
|
62 |
return $this->setGenerique($data, $cle, $valeur);
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
private function getGenerique($data, $cle) {
|
1795 |
jpm |
67 |
$retour = null;
|
|
|
68 |
if ($cle === null) {
|
1819 |
jpm |
69 |
$retour = $data;
|
|
|
70 |
} else if (isset($data[$cle])) {
|
|
|
71 |
$retour = $data[$cle];
|
1795 |
jpm |
72 |
}
|
|
|
73 |
return $retour;
|
|
|
74 |
}
|
|
|
75 |
|
1826 |
jpm |
76 |
private function setGenerique($data, $cle, $valeur) {
|
|
|
77 |
if ($valeur === null) {
|
|
|
78 |
unset($data[$cle]);
|
|
|
79 |
} else {
|
|
|
80 |
$data[$cle] = $valeur;
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
|
1795 |
jpm |
84 |
private function nettoyerParametres(Array $parametres) {
|
|
|
85 |
// Pas besoin d'utiliser urldecode car déjà fait par php pour les clés et valeur de $_GET
|
|
|
86 |
if (isset($parametres) && count($parametres) > 0) {
|
|
|
87 |
foreach ($parametres as $cle => $valeur) {
|
|
|
88 |
// les quotes, guillements et points-virgules ont été retirés des caractères à vérifier car
|
|
|
89 |
//ça n'a plus lieu d'être maintenant que l'on utilise protéger à peu près partout
|
|
|
90 |
$verifier = array('NULL', "\\", "\x00", "\x1a");
|
|
|
91 |
$parametres[$cle] = strip_tags(str_replace($verifier, '', $valeur));
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
return $parametres;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
private function recupererParametresBruts() {
|
|
|
98 |
$parametres_bruts = array();
|
|
|
99 |
if (isset($this->server['QUERY_STRING']) && !empty($this->server['QUERY_STRING'])) {
|
|
|
100 |
$paires = explode('&', $this->server['QUERY_STRING']);
|
|
|
101 |
foreach ($paires as $paire) {
|
1922 |
jpm |
102 |
if ($paire != '' && substr_count($paire, '=') == 1) {
|
|
|
103 |
$nv = explode('=', $paire);
|
|
|
104 |
$nom = urldecode($nv[0]);
|
|
|
105 |
$valeur = urldecode($nv[1]);
|
|
|
106 |
$parametres_bruts[$nom] = $valeur;
|
|
|
107 |
}
|
1795 |
jpm |
108 |
}
|
|
|
109 |
$parametres_bruts = $this->nettoyerParametres($parametres_bruts);
|
|
|
110 |
}
|
|
|
111 |
return $parametres_bruts;
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
private function decouperUrlChemin() {
|
|
|
115 |
if (isset($this->server['REDIRECT_URL']) && $this->server['REDIRECT_URL'] != '') {
|
|
|
116 |
if (isset($this->server['REDIRECT_QUERY_STRING']) && !empty($this->server['REDIRECT_QUERY_STRING'])) {
|
|
|
117 |
$url = $this->server['REDIRECT_URL'].'?'.$this->server['REDIRECT_QUERY_STRING'];
|
|
|
118 |
} else {
|
|
|
119 |
$url = $this->server['REDIRECT_URL'];
|
|
|
120 |
}
|
|
|
121 |
} else {
|
|
|
122 |
$url = $this->server['REQUEST_URI'];
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
$tailleQueryString = strlen($this->server['QUERY_STRING']);
|
|
|
126 |
$tailleURL = ($tailleQueryString == 0) ? strlen($url) : -($tailleQueryString + 1);
|
|
|
127 |
|
|
|
128 |
$urlChaine = '';
|
|
|
129 |
if (strpos($url, $this->conteneur->getParametre('serveur.baseURL')) !== false) {
|
|
|
130 |
$urlChaine = substr($url, strlen($this->conteneur->getParametre('serveur.baseURL')), $tailleURL);
|
|
|
131 |
} else if (strpos($url, $this->conteneur->getParametre('serveur.baseAlternativeURL')) !== false) {
|
|
|
132 |
$urlChaine = substr($url, strlen($this->conteneur->getParametre('serveur.baseAlternativeURL')), $tailleURL);
|
|
|
133 |
}
|
|
|
134 |
return explode('/', $urlChaine);
|
|
|
135 |
}
|
|
|
136 |
}
|