1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mouf\Mvc\Splash\Filters; |
4
|
|
|
|
5
|
|
|
use Mouf\Mvc\Splash\Utils\SplashException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Filter that requires the use of HTTPS (if enabled in the conf) |
9
|
|
|
* By passing @RequireHttps("force"), an Exception is thrown if the action is called in HTTP. |
10
|
|
|
* By passing @RequireHttps("no"), no test is performed. |
11
|
|
|
* By passing @RequireHttps("redirect"), the call is redirected to HTTPS. Does only work with GET requests. |
12
|
|
|
* |
13
|
|
|
* @Annotation |
14
|
|
|
*/ |
15
|
|
|
class RequireHttpsAnnotation |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The value passed to the filter. |
19
|
|
|
*/ |
20
|
|
|
protected $value; |
21
|
|
|
|
22
|
|
|
public function __construct(array $values) |
23
|
|
|
{ |
24
|
|
|
$value = $values['value']; |
25
|
|
|
if ($value === 'force') { |
26
|
|
|
$this->value = 'force'; |
27
|
|
|
} elseif (strpos($value, 'no') !== false) { |
28
|
|
|
$this->value = 'no'; |
29
|
|
|
} elseif (strpos($value, 'redirect') !== false) { |
30
|
|
|
$this->value = 'redirect'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if ($this->value === null) { |
34
|
|
|
throw new SplashException('You need to specify a value (either "force", "no" or "redirect") to the @RequireHttpsAnnotation.'); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
View Code Duplication |
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next, ContainerInterface $container) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$uri = $request->getUri(); |
41
|
|
|
$scheme = $uri->getScheme(); |
42
|
|
|
if ($scheme === 'http') { |
43
|
|
|
if ($request->getMethod() !== 'GET') { |
44
|
|
|
throw new SplashException('Only GET HTTP methods can be redirected to HTTPS'); |
45
|
|
|
} |
46
|
|
|
$uri = $uri->withScheme('https'); |
47
|
|
|
return new RedirectResponse($uri); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $next($request, $response); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.