This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Slim Framework (https://slimframework.com) |
||
4 | * |
||
5 | * @link https://github.com/slimphp/Slim |
||
6 | * @copyright Copyright (c) 2011-2017 Josh Lockhart |
||
7 | * @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License) |
||
8 | */ |
||
9 | namespace Slim; |
||
10 | |||
11 | use FastRoute\Dispatcher; |
||
12 | use Psr\Container\ContainerInterface; |
||
13 | use InvalidArgumentException; |
||
14 | use RuntimeException; |
||
15 | use Psr\Http\Message\ServerRequestInterface; |
||
16 | use FastRoute\RouteCollector; |
||
17 | use FastRoute\RouteParser; |
||
18 | use FastRoute\RouteParser\Std as StdParser; |
||
19 | use Slim\Interfaces\RouteGroupInterface; |
||
20 | use Slim\Interfaces\RouterInterface; |
||
21 | use Slim\Interfaces\RouteInterface; |
||
22 | |||
23 | /** |
||
24 | * Router |
||
25 | * |
||
26 | * This class organizes Slim application route objects. It is responsible |
||
27 | * for registering route objects, assigning names to route objects, |
||
28 | * finding routes that match the current HTTP request, and creating |
||
29 | * URLs for a named route. |
||
30 | */ |
||
31 | class Router implements RouterInterface |
||
32 | { |
||
33 | /** |
||
34 | * Container Interface |
||
35 | * |
||
36 | * @var ContainerInterface |
||
37 | */ |
||
38 | protected $container; |
||
39 | |||
40 | /** |
||
41 | * Parser |
||
42 | * |
||
43 | * @var \FastRoute\RouteParser |
||
44 | */ |
||
45 | protected $routeParser; |
||
46 | |||
47 | /** |
||
48 | * Base path used in pathFor() |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $basePath = ''; |
||
53 | |||
54 | /** |
||
55 | * Path to fast route cache file. Set to false to disable route caching |
||
56 | * |
||
57 | * @var string|False |
||
58 | */ |
||
59 | protected $cacheFile = false; |
||
60 | |||
61 | /** |
||
62 | * Routes |
||
63 | * |
||
64 | * @var Route[] |
||
65 | */ |
||
66 | protected $routes = []; |
||
67 | |||
68 | /** |
||
69 | * Route counter incrementer |
||
70 | * @var int |
||
71 | */ |
||
72 | protected $routeCounter = 0; |
||
73 | |||
74 | /** |
||
75 | * Route groups |
||
76 | * |
||
77 | * @var RouteGroup[] |
||
78 | */ |
||
79 | protected $routeGroups = []; |
||
80 | |||
81 | /** |
||
82 | * @var \FastRoute\Dispatcher |
||
83 | */ |
||
84 | protected $dispatcher; |
||
85 | |||
86 | /** |
||
87 | * Create new router |
||
88 | * |
||
89 | * @param RouteParser $parser |
||
90 | */ |
||
91 | public function __construct(RouteParser $parser = null) |
||
92 | { |
||
93 | $this->routeParser = $parser ?: new StdParser; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Set the base path used in pathFor() |
||
98 | * |
||
99 | * @param string $basePath |
||
100 | * |
||
101 | * @return self |
||
102 | */ |
||
103 | public function setBasePath($basePath) |
||
104 | { |
||
105 | if (!is_string($basePath)) { |
||
106 | throw new InvalidArgumentException('Router basePath must be a string'); |
||
107 | } |
||
108 | |||
109 | $this->basePath = $basePath; |
||
110 | |||
111 | return $this; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Set path to fast route cache file. If this is false then route caching is disabled. |
||
116 | * |
||
117 | * @param string|false $cacheFile |
||
118 | * |
||
119 | * @return self |
||
120 | */ |
||
121 | public function setCacheFile($cacheFile) |
||
122 | { |
||
123 | if (!is_string($cacheFile) && $cacheFile !== false) { |
||
124 | throw new InvalidArgumentException('Router cacheFile must be a string or false'); |
||
125 | } |
||
126 | |||
127 | $this->cacheFile = $cacheFile; |
||
128 | |||
129 | if ($cacheFile !== false && !is_writable(dirname($cacheFile))) { |
||
130 | throw new RuntimeException('Router cacheFile directory must be writable'); |
||
131 | } |
||
132 | |||
133 | |||
134 | return $this; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @param ContainerInterface $container |
||
139 | */ |
||
140 | public function setContainer(ContainerInterface $container) |
||
141 | { |
||
142 | $this->container = $container; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Add route |
||
147 | * |
||
148 | * @param string[] $methods Array of HTTP methods |
||
149 | * @param string $pattern The route pattern |
||
150 | * @param callable $handler The route callable |
||
151 | * |
||
152 | * @return RouteInterface |
||
153 | * |
||
154 | * @throws InvalidArgumentException if the route pattern isn't a string |
||
155 | */ |
||
156 | public function map($methods, $pattern, $handler) |
||
157 | { |
||
158 | if (!is_string($pattern)) { |
||
159 | throw new InvalidArgumentException('Route pattern must be a string'); |
||
160 | } |
||
161 | |||
162 | // Prepend parent group pattern(s) |
||
163 | if ($this->routeGroups) { |
||
164 | $pattern = $this->processGroups() . $pattern; |
||
165 | } |
||
166 | |||
167 | // According to RFC methods are defined in uppercase (See RFC 7231) |
||
168 | $methods = array_map("strtoupper", $methods); |
||
169 | |||
170 | // Add route |
||
171 | $route = $this->createRoute($methods, $pattern, $handler); |
||
172 | $this->routes[$route->getIdentifier()] = $route; |
||
173 | $this->routeCounter++; |
||
174 | |||
175 | return $route; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Dispatch router for HTTP request |
||
180 | * |
||
181 | * @param ServerRequestInterface $request The current HTTP request object |
||
182 | * |
||
183 | * @return array |
||
184 | * |
||
185 | * @link https://github.com/nikic/FastRoute/blob/master/src/Dispatcher.php |
||
186 | */ |
||
187 | public function dispatch(ServerRequestInterface $request) |
||
188 | { |
||
189 | $uri = '/' . ltrim($request->getUri()->getPath(), '/'); |
||
190 | |||
191 | return $this->createDispatcher()->dispatch( |
||
192 | $request->getMethod(), |
||
193 | $uri |
||
194 | ); |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Create a new Route object |
||
199 | * |
||
200 | * @param string[] $methods Array of HTTP methods |
||
201 | * @param string $pattern The route pattern |
||
202 | * @param callable $callable The route callable |
||
203 | * |
||
204 | * @return \Slim\Interfaces\RouteInterface |
||
205 | */ |
||
206 | protected function createRoute($methods, $pattern, $callable) |
||
207 | { |
||
208 | $route = new Route($methods, $pattern, $callable, $this->routeGroups, $this->routeCounter); |
||
209 | if (!empty($this->container)) { |
||
210 | $route->setContainer($this->container); |
||
211 | } |
||
212 | |||
213 | return $route; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @return \FastRoute\Dispatcher |
||
218 | */ |
||
219 | protected function createDispatcher() |
||
220 | { |
||
221 | if ($this->dispatcher) { |
||
222 | return $this->dispatcher; |
||
223 | } |
||
224 | |||
225 | $routeDefinitionCallback = function (RouteCollector $r) { |
||
226 | foreach ($this->getRoutes() as $route) { |
||
227 | $r->addRoute($route->getMethods(), $route->getPattern(), $route->getIdentifier()); |
||
228 | } |
||
229 | }; |
||
230 | |||
231 | if ($this->cacheFile) { |
||
0 ignored issues
–
show
|
|||
232 | $this->dispatcher = \FastRoute\cachedDispatcher($routeDefinitionCallback, [ |
||
233 | 'routeParser' => $this->routeParser, |
||
234 | 'cacheFile' => $this->cacheFile, |
||
235 | ]); |
||
236 | } else { |
||
237 | $this->dispatcher = \FastRoute\simpleDispatcher($routeDefinitionCallback, [ |
||
238 | 'routeParser' => $this->routeParser, |
||
239 | ]); |
||
240 | } |
||
241 | |||
242 | return $this->dispatcher; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * @param \FastRoute\Dispatcher $dispatcher |
||
247 | */ |
||
248 | public function setDispatcher(Dispatcher $dispatcher) |
||
249 | { |
||
250 | $this->dispatcher = $dispatcher; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Get route objects |
||
255 | * |
||
256 | * @return Route[] |
||
257 | */ |
||
258 | public function getRoutes() |
||
259 | { |
||
260 | return $this->routes; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Get named route object |
||
265 | * |
||
266 | * @param string $name Route name |
||
267 | * |
||
268 | * @return Route |
||
269 | * |
||
270 | * @throws RuntimeException If named route does not exist |
||
271 | */ |
||
272 | public function getNamedRoute($name) |
||
273 | { |
||
274 | foreach ($this->routes as $route) { |
||
275 | if ($name == $route->getName()) { |
||
276 | return $route; |
||
277 | } |
||
278 | } |
||
279 | throw new RuntimeException('Named route does not exist for name: ' . $name); |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Remove named route |
||
284 | * |
||
285 | * @param string $name Route name |
||
286 | * |
||
287 | * @throws RuntimeException If named route does not exist |
||
288 | */ |
||
289 | public function removeNamedRoute($name) |
||
290 | { |
||
291 | $route = $this->getNamedRoute($name); |
||
292 | |||
293 | // no exception, route exists, now remove by id |
||
294 | unset($this->routes[$route->getIdentifier()]); |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Process route groups |
||
299 | * |
||
300 | * @return string A group pattern to prefix routes with |
||
301 | */ |
||
302 | protected function processGroups() |
||
303 | { |
||
304 | $pattern = ""; |
||
305 | foreach ($this->routeGroups as $group) { |
||
306 | $pattern .= $group->getPattern(); |
||
307 | } |
||
308 | return $pattern; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Add a route group to the array |
||
313 | * |
||
314 | * @param string $pattern |
||
315 | * @param callable $callable |
||
316 | * |
||
317 | * @return RouteGroupInterface |
||
318 | */ |
||
319 | public function pushGroup($pattern, $callable) |
||
320 | { |
||
321 | $group = new RouteGroup($pattern, $callable); |
||
322 | array_push($this->routeGroups, $group); |
||
323 | return $group; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Removes the last route group from the array |
||
328 | * |
||
329 | * @return RouteGroup|bool The RouteGroup if successful, else False |
||
330 | */ |
||
331 | public function popGroup() |
||
332 | { |
||
333 | $group = array_pop($this->routeGroups); |
||
334 | return $group instanceof RouteGroup ? $group : false; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * @param $identifier |
||
339 | * @return \Slim\Interfaces\RouteInterface |
||
340 | */ |
||
341 | public function lookupRoute($identifier) |
||
342 | { |
||
343 | if (!isset($this->routes[$identifier])) { |
||
344 | throw new RuntimeException('Route not found, looks like your route cache is stale.'); |
||
345 | } |
||
346 | return $this->routes[$identifier]; |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Build the path for a named route excluding the base path |
||
351 | * |
||
352 | * @param string $name Route name |
||
353 | * @param array $data Named argument replacement data |
||
354 | * @param array $queryParams Optional query string parameters |
||
355 | * |
||
356 | * @return string |
||
357 | * |
||
358 | * @throws RuntimeException If named route does not exist |
||
359 | * @throws InvalidArgumentException If required data not provided |
||
360 | */ |
||
361 | public function relativePathFor($name, array $data = [], array $queryParams = []) |
||
362 | { |
||
363 | $route = $this->getNamedRoute($name); |
||
364 | $pattern = $route->getPattern(); |
||
365 | |||
366 | $routeDatas = $this->routeParser->parse($pattern); |
||
367 | // $routeDatas is an array of all possible routes that can be made. There is |
||
368 | // one routedata for each optional parameter plus one for no optional parameters. |
||
369 | // |
||
370 | // The most specific is last, so we look for that first. |
||
371 | $routeDatas = array_reverse($routeDatas); |
||
372 | |||
373 | $segments = []; |
||
374 | foreach ($routeDatas as $routeData) { |
||
375 | foreach ($routeData as $item) { |
||
376 | if (is_string($item)) { |
||
377 | // this segment is a static string |
||
378 | $segments[] = $item; |
||
379 | continue; |
||
380 | } |
||
381 | |||
382 | // This segment has a parameter: first element is the name |
||
383 | if (!array_key_exists($item[0], $data)) { |
||
384 | // we don't have a data element for this segment: cancel |
||
385 | // testing this routeData item, so that we can try a less |
||
386 | // specific routeData item. |
||
387 | $segments = []; |
||
388 | $segmentName = $item[0]; |
||
389 | break; |
||
390 | } |
||
391 | $segments[] = $data[$item[0]]; |
||
392 | } |
||
393 | if (!empty($segments)) { |
||
394 | // we found all the parameters for this route data, no need to check |
||
395 | // less specific ones |
||
396 | break; |
||
397 | } |
||
398 | } |
||
399 | |||
400 | if (empty($segments)) { |
||
401 | throw new InvalidArgumentException('Missing data for URL segment: ' . $segmentName); |
||
402 | } |
||
403 | $url = implode('', $segments); |
||
404 | |||
405 | if ($queryParams) { |
||
406 | $url .= '?' . http_build_query($queryParams); |
||
407 | } |
||
408 | |||
409 | return $url; |
||
410 | } |
||
411 | |||
412 | |||
413 | /** |
||
414 | * Build the path for a named route including the base path |
||
415 | * |
||
416 | * @param string $name Route name |
||
417 | * @param array $data Named argument replacement data |
||
418 | * @param array $queryParams Optional query string parameters |
||
419 | * |
||
420 | * @return string |
||
421 | * |
||
422 | * @throws RuntimeException If named route does not exist |
||
423 | * @throws InvalidArgumentException If required data not provided |
||
424 | */ |
||
425 | public function pathFor($name, array $data = [], array $queryParams = []) |
||
426 | { |
||
427 | $url = $this->relativePathFor($name, $data, $queryParams); |
||
428 | |||
429 | if ($this->basePath) { |
||
430 | $url = $this->basePath . $url; |
||
431 | } |
||
432 | |||
433 | return $url; |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * Build the path for a named route. |
||
438 | * |
||
439 | * This method is deprecated. Use pathFor() from now on. |
||
440 | * |
||
441 | * @param string $name Route name |
||
442 | * @param array $data Named argument replacement data |
||
443 | * @param array $queryParams Optional query string parameters |
||
444 | * |
||
445 | * @return string |
||
446 | * |
||
447 | * @throws RuntimeException If named route does not exist |
||
448 | * @throws InvalidArgumentException If required data not provided |
||
449 | */ |
||
450 | public function urlFor($name, array $data = [], array $queryParams = []) |
||
451 | { |
||
452 | trigger_error('urlFor() is deprecated. Use pathFor() instead.', E_USER_DEPRECATED); |
||
453 | return $this->pathFor($name, $data, $queryParams); |
||
454 | } |
||
455 | } |
||
456 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: