1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psr7Middlewares\Middleware; |
4
|
|
|
|
5
|
|
|
use Psr7Middlewares\Utils; |
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Middleware to redirect to https protocol. |
11
|
|
|
*/ |
12
|
|
|
class Https |
13
|
|
|
{ |
14
|
|
|
use Utils\RedirectTrait; |
15
|
|
|
|
16
|
|
|
const HEADER = 'Strict-Transport-Security'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param int One year by default |
20
|
|
|
*/ |
21
|
|
|
private $maxAge = 31536000; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param bool Whether include subdomains |
25
|
|
|
*/ |
26
|
|
|
private $includeSubdomains = false; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param bool Whether check the headers "HTTP_X_FORWARDED_PROTO: https" or "HTTP_X_FORWARDED_PORT: 443" |
30
|
|
|
*/ |
31
|
|
|
private $checkHttpsForward = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set basic config. |
35
|
|
|
*/ |
36
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
$this->redirect(301); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Configure the max-age HSTS in seconds. |
43
|
|
|
* |
44
|
|
|
* @param int $maxAge |
45
|
|
|
* |
46
|
|
|
* @return self |
47
|
|
|
*/ |
48
|
|
|
public function maxAge($maxAge) |
49
|
|
|
{ |
50
|
|
|
$this->maxAge = $maxAge; |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Configure the includeSubDomains HSTS directive. |
57
|
|
|
* |
58
|
|
|
* @param bool $includeSubdomains |
59
|
|
|
* |
60
|
|
|
* @return self |
61
|
|
|
*/ |
62
|
|
|
public function includeSubdomains($includeSubdomains = true) |
63
|
|
|
{ |
64
|
|
|
$this->includeSubdomains = $includeSubdomains; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Configure whether check the following headers before redirect: |
71
|
|
|
* HTTP_X_FORWARDED_PROTO: https |
72
|
|
|
* HTTP_X_FORWARDED_PORT: 443 |
73
|
|
|
* |
74
|
|
|
* @param bool $checkHttpsForward |
75
|
|
|
* |
76
|
|
|
* @return self |
77
|
|
|
*/ |
78
|
|
|
public function checkHttpsForward($checkHttpsForward = true) |
79
|
|
|
{ |
80
|
|
|
$this->checkHttpsForward = $checkHttpsForward; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Execute the middleware. |
87
|
|
|
* |
88
|
|
|
* @param ServerRequestInterface $request |
89
|
|
|
* @param ResponseInterface $response |
90
|
|
|
* @param callable $next |
91
|
|
|
* |
92
|
|
|
* @return ResponseInterface |
93
|
|
|
*/ |
94
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
95
|
|
|
{ |
96
|
|
|
$uri = $request->getUri(); |
97
|
|
|
|
98
|
|
|
if (strtolower($uri->getScheme()) !== 'https') { |
99
|
|
|
$uri = $uri->withScheme('https')->withPort(443); |
100
|
|
|
|
101
|
|
|
if ($this->redirectStatus !== false && (!$this->checkHttpsForward || ($request->getHeaderLine('HTTP_X_FORWARDED_PROTO') !== 'https' && $request->getHeaderLine('HTTP_X_FORWARDED_PORT') !== '443'))) { |
102
|
|
|
return $this->getRedirectResponse($request, $uri, $response); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$request = $request->withUri($uri); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (!empty($this->maxAge)) { |
109
|
|
|
$response = $response->withHeader(self::HEADER, sprintf('max-age=%d%s', $this->maxAge, $this->includeSubdomains ? ';includeSubDomains' : '')); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$response = $next($request, $response); |
113
|
|
|
|
114
|
|
|
if (Utils\Helpers::isRedirect($response)) { |
115
|
|
|
return $response->withHeader('Location', str_replace('http://', 'https://', $response->getHeaderLine('Location'))); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $response; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|