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