|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Vperyod HTTPS Redirector |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* Copyright (C) 2016 Jake Johns |
|
8
|
|
|
* |
|
9
|
|
|
* This software may be modified and distributed under the terms |
|
10
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
11
|
|
|
* |
|
12
|
|
|
* @category Middleware |
|
13
|
|
|
* @package Vperyod\HttpsRedirect |
|
14
|
|
|
* @author Jake Johns <[email protected]> |
|
15
|
|
|
* @copyright 2016 Jake Johns |
|
16
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
|
17
|
|
|
* @link http://github.com/vperyod/vperyod.https-redirect |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
namespace Vperyod\HttpsRedirect; |
|
22
|
|
|
|
|
23
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
24
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* HttpsRedirect |
|
28
|
|
|
* |
|
29
|
|
|
* @category Middleware |
|
30
|
|
|
* @package Vperyod\HttpsRedirect |
|
31
|
|
|
* @author Jake Johns <[email protected]> |
|
32
|
|
|
* @license http://jnj.mit-license.org/ MIT License |
|
33
|
|
|
* @link http://github.com/vperyod/vperyod.https-redirect |
|
34
|
|
|
*/ |
|
35
|
|
|
class ForceHttps |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Redirect to HTTPS schema if necessary |
|
40
|
|
|
* |
|
41
|
|
|
* @param Request $request PSR7 Request |
|
42
|
|
|
* @param Response $response PSR7 Response |
|
43
|
|
|
* @param callable $next next middleware |
|
44
|
|
|
* |
|
45
|
|
|
* @return Response |
|
46
|
|
|
* |
|
47
|
|
|
* @access public |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public function __invoke(Request $request, Response $response, callable $next) |
|
50
|
|
|
{ |
|
51
|
|
|
|
|
52
|
2 |
|
if (! $this->isHttps($request)) { |
|
53
|
1 |
|
return $this->redirect($request, $response); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
return $next($request, $response); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Is request Https? |
|
61
|
|
|
* |
|
62
|
|
|
* @param Request $request PSR7 Request |
|
63
|
|
|
* |
|
64
|
|
|
* @return bool |
|
65
|
|
|
* |
|
66
|
|
|
* @access protected |
|
67
|
|
|
*/ |
|
68
|
2 |
|
protected function isHttps(Request $request) |
|
69
|
|
|
{ |
|
70
|
2 |
|
$uri = $request->getUri(); |
|
71
|
2 |
|
return strtolower($uri->getScheme()) == 'https'; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Redirect to HTTPS |
|
76
|
|
|
* |
|
77
|
|
|
* @param Request $request PSR7 Request |
|
78
|
|
|
* @param Response $response PSR7 Response |
|
79
|
|
|
* |
|
80
|
|
|
* @return Response |
|
81
|
|
|
* |
|
82
|
|
|
* @access protected |
|
83
|
|
|
*/ |
|
84
|
1 |
|
protected function redirect(Request $request, Response $response) |
|
85
|
|
|
{ |
|
86
|
1 |
|
$uri = $request->getUri() |
|
87
|
1 |
|
->withScheme('https') |
|
88
|
1 |
|
->withPort(443); |
|
89
|
|
|
|
|
90
|
|
|
return $response |
|
91
|
1 |
|
->withStatus(301) |
|
92
|
1 |
|
->withHeader('Location', (string) $uri); |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|