-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.php
More file actions
220 lines (175 loc) · 6.03 KB
/
Copy pathRequest.php
File metadata and controls
220 lines (175 loc) · 6.03 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/routing/blob/master/LICENSE
*/
namespace Quid\Routing;
use Quid\Base\Html;
use Quid\Main;
// request
// extended class with methods to route an HTTP request
class Request extends Main\Request
{
// config
protected static array $config = [
'navigation'=>'Quid-Navigation'
];
// construct
// construit un objet request
// permet de mettre un objet route en argument
final public function __construct($value=null,?array $attr=null)
{
if($value instanceof Route)
$value = static::fromRoute($value);
parent::__construct($value,$attr);
}
// isAjaxNavigation
// retourne vrai si la requête vient de la navigation history api
final public function isAjaxNavigation():bool
{
return $this->isAjax() && $this->isHeader($this->getAttr('navigation'));
}
// isAjaxNotNavigation
// retourne vrai si la requête est ajax mais pas la navigation via history api
final public function isAjaxNotNavigation():bool
{
return $this->isAjax() && !$this->isHeader($this->getAttr('navigation'));
}
// isPathApi
// retourne vrai si la requête semble être un api
final public function isPathApi():bool
{
return str_starts_with($this->path(),'/api/') && strlen($this->path()) > 5;
}
// manageRedirect
// vérifie la requête et manage les redirections possibles
// certaines errors vont générer un code http 400 plutôt que 404 (bad request)
// retourne un tableau avec les clés type, code et location
// gère externalPost, redirection, requestUnsafe et requestInvalid
final public function manageRedirect(?Redirection $redirection=null):array
{
$return = ['type'=>null,'code'=>null,'location'=>null];
$isAjax = $this->isAjax();
$isSafe = $this->isPathSafe();
$isExternalPost = $this->isExternalPost();
$schemeHost = $this->schemeHost();
$redirect = $this->redirect();
$hasExtension = $this->hasExtension();
$argumentNotCli = $this->isPathArgumentNotCli();
$isApi = $this->isPathApi();
// ajouté récemment pour pouvoir éviter la redirection de language si c'est un api
if($isApi)
$requestInvalid = false;
else
$requestInvalid = (!empty($redirect) || $argumentNotCli);
// externalPost
if($isExternalPost === true)
{
$return['type'] = 'externalPost';
$return['code'] = 400;
}
else
{
// redirection
if(!empty($redirection))
{
$to = $redirection->get($this);
if(!empty($to))
{
$return['code'] = 301;
$return['location'] = $to;
}
}
// requestUnsafe
if(empty($return['location']) && $isSafe === false)
{
$return['type'] = 'requestUnsafe';
if($isAjax === true)
$return['code'] = 400;
else
{
if($this->absolute() !== $schemeHost && !$hasExtension)
$return['location'] = $schemeHost;
else
$return['code'] = 400;
}
}
// requestInvalid
if(empty($return['location']) && $requestInvalid === true)
{
$redirect = $redirect ?: $schemeHost;
$return['type'] = 'requestInvalid';
if($isAjax === true)
$return['code'] = 400;
else
$return['location'] = $redirect;
}
if($return['location'] !== null && $return['code'] === null)
$return['code'] = 302;
}
return $return;
}
// match
// retourne un tableau avec toutes les routes qui matchs avec la requête
final public function match(Routes $routes,bool $fallback=false,bool $debug=false):?array
{
return $routes->match($this,$fallback,$debug);
}
// route
// retourne la première route qui match avec la requête
final public function route(Routes $routes,$after=null,bool $fallback=false,bool $debug=false):?Route
{
return $routes->route($this,$after,$fallback,$debug);
}
// fromRoute
// retourne un tableau de départ request à partir d'un objet route
final public static function fromRoute(Route $route):array
{
$return = [];
$return['uri'] = $route->uriAbsolute();
$return['ajax'] = $route::isAjax() ?? false;
if($route::isMethod('post'))
{
$session = $route::session();
$return['method'] = 'post';
$postMatch = $route::getConfig('match/post');
$post = [];
if($route::hasMatch('csrf'))
{
$name = $session->getCsrfName();
$post[$name] = $session->csrf();
}
if($route::hasMatch('genuine'))
{
$name = Html::getGenuineName();
$post[$name] = '';
$name = Html::getGenuineName(2);
$post[$name] = 1;
}
if($route::hasMatch('captcha'))
{
$name = $session->getCaptchaName();
$post[$name] = $session->captcha();
}
if(is_array($postMatch) && !empty($postMatch))
{
foreach ($postMatch as $key => $value)
{
if(is_string($key))
$post[$key] = $value;
elseif(is_string($value))
$post[$value] = '';
}
}
$return['post'] = $post;
}
else
$return['method'] = 'get';
return $return;
}
}
// init
Request::__init();
?>