|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* This file is part of Aura for PHP. |
|
5
|
|
|
* |
|
6
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace Aura\Router\Rule; |
|
10
|
|
|
|
|
11
|
|
|
use Aura\Router\Route; |
|
12
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* |
|
16
|
|
|
* A rule for HTTPS/SSL/TLS. |
|
17
|
|
|
* |
|
18
|
|
|
* @package Aura.Router |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
class Secure implements RuleInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* |
|
25
|
|
|
* Checks that the Route `$secure` matches the corresponding server values. |
|
26
|
|
|
* |
|
27
|
|
|
* @param ServerRequestInterface $request The HTTP request. |
|
28
|
|
|
* |
|
29
|
|
|
* @param Route $route The route. |
|
30
|
|
|
* |
|
31
|
|
|
* @return bool True on success, false on failure. |
|
32
|
|
|
* |
|
33
|
|
|
*/ |
|
34
|
8 |
|
public function __invoke(ServerRequestInterface $request, Route $route) |
|
35
|
|
|
{ |
|
36
|
8 |
|
if ($route->secure === null) { |
|
37
|
5 |
|
return true; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
3 |
|
$server = $request->getServerParams(); |
|
41
|
3 |
|
$secure = $this->https($server) || $this->port443($server) || $this->forwarded($server); |
|
42
|
3 |
|
return $route->secure == $secure; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* |
|
47
|
|
|
* Is HTTPS on? |
|
48
|
|
|
* |
|
49
|
|
|
* @param array $server The server params. |
|
50
|
|
|
* |
|
51
|
|
|
* @return bool |
|
52
|
|
|
* |
|
53
|
|
|
*/ |
|
54
|
3 |
|
protected function https($server) |
|
55
|
|
|
{ |
|
56
|
3 |
|
return isset($server['HTTPS']) |
|
57
|
3 |
|
&& $server['HTTPS'] == 'on'; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* |
|
63
|
|
|
* Is the request on port 443? |
|
64
|
|
|
* |
|
65
|
|
|
* @param array $server The server params. |
|
66
|
|
|
* |
|
67
|
|
|
* @return bool |
|
68
|
|
|
* |
|
69
|
|
|
*/ |
|
70
|
3 |
|
protected function port443($server) |
|
71
|
|
|
{ |
|
72
|
3 |
|
return isset($server['SERVER_PORT']) |
|
73
|
3 |
|
&& $server['SERVER_PORT'] == 443; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* |
|
78
|
|
|
* Is X-Forwarded-Proto https? |
|
79
|
|
|
* |
|
80
|
|
|
* @param array $server The server params. |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool |
|
83
|
|
|
* |
|
84
|
|
|
*/ |
|
85
|
3 |
|
protected function forwarded($server) |
|
86
|
|
|
{ |
|
87
|
3 |
|
return isset($server['HTTP_X_FORWARDED_PROTO']) |
|
88
|
3 |
|
&& $server['HTTP_X_FORWARDED_PROTO'] == 'https'; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|