1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
include_once __ROOT_DIR . '/libs/php-jwt/src/BeforeValidException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/ExpiredException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/SignatureInvalidException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/JWT.php';
use \Firebase\JWT\JWT;
class Request
{
protected $controllerName;
protected $uriParameters;
protected $data;
protected static $_instance;
public static function getCurrentRequest()
{
if (is_null(self::$_instance)) {
self::$_instance = new Request();
}
return self::$_instance;
}
public function __construct()
{
$this->initBaseURI();
$this->initControllerAndParametersFromURI();
$this->initData();
}
// intialise baseURI
// e.g. http://eden.imt-lille-douai.fr/~luc.fabresse/api.php => __BASE_URI = /~luc.fabresse
// e.g. http://localhost/CDAW/api.php => __BASE_URI = /CDAW
protected function initBaseURI()
{
$this->baseURI = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
}
// intialise controllerName et uriParameters
// controllerName contient chaîne 'default' ou le nom du controleur s'il est présent dans l'URI (la requête)
// uriParameters contient un tableau vide ou un tableau contenant les paramètres passés dans l'URI (la requête)
// e.g. http://eden.imt-lille-douai.fr/~luc.fabresse/api.php
// => controllerName == 'default'
// uriParameters == []
// e.g. http://eden.imt-lille-douai.fr/~luc.fabresse/api.php/user/1
// => controllerName == 'user'
// uriParameters == [ 1 ]
protected function initControllerAndParametersFromURI()
{
$prefix = $_SERVER['SCRIPT_NAME'];
$uriParameters = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$i=0;
while ($i<strlen($prefix) && $i<strlen($uriParameters)) {
if ($prefix[$i]===$uriParameters[$i]) {
$i++;
}
}
$uriParameters = substr($uriParameters, $i);
$uriParameters = trim($uriParameters, '/');
$uriSegments = explode('/', $uriParameters);
$this->controllerName = array_shift($uriSegments) ?: "default";
$this->uriParameters = $uriSegments;
}
// ==============
// Public API
// ==============
// retourne le name du controleur qui doit traiter la requête courante
public function getControllerName()
{
return $this->controllerName;
}
public function getUriParams()
{
return $this->uriParameters;
}
public function initData()
{
if ($this->getHttpMethod() === 'PUT' || $this->getHttpMethod() === 'POST') {
$jsondata=file_get_contents("php://input");
$this->data = json_decode($jsondata, true);
}
}
// retourne la méthode HTTP utilisée dans la requête courante
public function getHttpMethod()
{
return $_SERVER["REQUEST_METHOD"];
}
public function getData()
{
return $this->data;
}
// Return JWT token (string) in Authorization header or throw an exception
public function getJwtToken()
{
// Field names are case-insensitive : https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html
$headers = array_change_key_case(getallheaders());
if (!isset($headers['authorization'])) {
throw new Exception("Missing Authorization field");
}
$autorization = $headers['authorization'];
$arr = explode(" ", $autorization);
if (count($arr)<2) {
throw new Exception("Missing JWT token");
}
$jwt_token = $arr[1];
return $jwt_token;
}
// Return array with decodedJWT or error message if decoding fails
public function verifyJwtToken()
{
try {
$jwt_token = $this->getJwtToken();
$decodedJWT = JWT::decode($jwt_token, JWT_BACKEND_KEY, array('HS256'));
$arrayResult = [
"message" => "Valid token.",
"decodedJWT" => $decodedJWT
];
} catch (Exception $e) {
header('WWW-Authenticate: Bearer realm="'.JWT_ISSUER.'"');
$arrayResult = [
"message" => "Access denied.",
"error" => $e->getMessage()
];
}
return $arrayResult;
}
public function getIpAddr()
{
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
}