1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CORS processor |
4
|
|
|
* User: moyo |
5
|
|
|
* Date: 2019-01-08 |
6
|
|
|
* Time: 15:22 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Carno\Web\Policy\CORS; |
10
|
|
|
|
11
|
|
|
use Carno\HTTP\Standard\Response; |
12
|
|
|
use Carno\HTTP\Standard\ServerRequest; |
13
|
|
|
|
14
|
|
|
class Processor |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $origin = '*'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $methods = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $headers = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $exposes = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
private $credentials = false; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
private $expired = 0; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* CORS constructor. |
48
|
|
|
* @param string $origin |
49
|
|
|
*/ |
50
|
|
|
public function __construct(string $origin) |
51
|
|
|
{ |
52
|
|
|
$this->origin = $origin; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string ...$methods |
57
|
|
|
* @return static |
58
|
|
|
*/ |
59
|
|
|
public function methods(string ...$methods) : self |
60
|
|
|
{ |
61
|
|
|
$this->methods = array_map('strtoupper', $methods); |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string ...$headers |
67
|
|
|
* @return static |
68
|
|
|
*/ |
69
|
|
|
public function headers(string ...$headers) : self |
70
|
|
|
{ |
71
|
|
|
$this->headers = $headers; |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string ...$headers |
77
|
|
|
* @return static |
78
|
|
|
*/ |
79
|
|
|
public function exposes(string ...$headers) : self |
80
|
|
|
{ |
81
|
|
|
$this->exposes = $headers; |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param bool $yes |
87
|
|
|
* @return static |
88
|
|
|
*/ |
89
|
|
|
public function credentials(bool $yes) : self |
90
|
|
|
{ |
91
|
|
|
$this->credentials = $yes; |
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param int $seconds |
97
|
|
|
* @return static |
98
|
|
|
*/ |
99
|
|
|
public function expired(int $seconds) : self |
100
|
|
|
{ |
101
|
|
|
$this->expired = $seconds; |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param ServerRequest $sr |
107
|
|
|
* @param Response $respond |
108
|
|
|
* @return Response |
109
|
|
|
*/ |
110
|
|
|
final public function process(ServerRequest $sr, Response $respond) : Response |
111
|
|
|
{ |
112
|
|
|
if ($this->origin === '*' && $this->credentials) { |
113
|
|
|
$origin = $sr->getHeaderLine('Origin') ?: '*'; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$respond->withHeader('Access-Control-Allow-Origin', $origin ?? $this->origin); |
117
|
|
|
|
118
|
|
|
$this->methods && $respond->withHeader('Access-Control-Allow-Methods', $this->methods); |
|
|
|
|
119
|
|
|
$this->headers && $respond->withHeader('Access-Control-Allow-Headers', $this->headers); |
|
|
|
|
120
|
|
|
$this->exposes && $respond->withHeader('Access-Control-Expose-Headers', $this->exposes); |
|
|
|
|
121
|
|
|
|
122
|
|
|
$this->credentials && $respond->withHeader('Access-Control-Allow-Credentials', 'true'); |
123
|
|
|
$this->expired > 0 && $respond->withHeader('Access-Control-Max-Age', $this->expired); |
124
|
|
|
|
125
|
|
|
return $respond; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.