1 | <?php namespace Neomerx\Cors\Strategies; |
||
30 | class Settings implements SettingsStrategyInterface |
||
31 | { |
||
32 | use LoggerAwareTrait; |
||
33 | |||
34 | /** |
||
35 | * 'All' value for allowed origins. |
||
36 | */ |
||
37 | const VALUE_ALLOW_ORIGIN_ALL = CorsResponseHeaders::VALUE_ALLOW_ORIGIN_ALL; |
||
38 | |||
39 | /** |
||
40 | * 'All' values for allowed headers. |
||
41 | */ |
||
42 | const VALUE_ALLOW_ALL_HEADERS = '*'; |
||
43 | |||
44 | /** Settings key */ |
||
45 | const KEY_SERVER_ORIGIN = 0; |
||
46 | |||
47 | /** Settings key */ |
||
48 | const KEY_SERVER_ORIGIN_SCHEMA = 0; |
||
49 | |||
50 | /** Settings key */ |
||
51 | const KEY_SERVER_ORIGIN_HOST = 1; |
||
52 | |||
53 | /** Settings key */ |
||
54 | const KEY_SERVER_ORIGIN_PORT = 2; |
||
55 | |||
56 | /** Settings key */ |
||
57 | const KEY_ALLOWED_ORIGINS = 1; |
||
58 | |||
59 | /** Settings key */ |
||
60 | const KEY_ALLOWED_METHODS = 2; |
||
61 | |||
62 | /** Settings key */ |
||
63 | const KEY_ALLOWED_HEADERS = 3; |
||
64 | |||
65 | /** Settings key */ |
||
66 | const KEY_EXPOSED_HEADERS = 4; |
||
67 | |||
68 | /** Settings key */ |
||
69 | const KEY_IS_USING_CREDENTIALS = 5; |
||
70 | |||
71 | /** Settings key */ |
||
72 | const KEY_FLIGHT_CACHE_MAX_AGE = 6; |
||
73 | |||
74 | /** Settings key */ |
||
75 | const KEY_IS_FORCE_ADD_METHODS = 7; |
||
76 | |||
77 | /** Settings key */ |
||
78 | const KEY_IS_FORCE_ADD_HEADERS = 8; |
||
79 | |||
80 | /** Settings key */ |
||
81 | const KEY_IS_CHECK_HOST = 9; |
||
82 | |||
83 | /** |
||
84 | * @var array |
||
85 | */ |
||
86 | private $settings = [ |
||
87 | /** |
||
88 | * Array should be in parse_url() result format. |
||
89 | * |
||
90 | * @see http://php.net/manual/function.parse-url.php |
||
91 | */ |
||
92 | self::KEY_SERVER_ORIGIN => [ |
||
93 | self::KEY_SERVER_ORIGIN_SCHEMA => '', |
||
94 | self::KEY_SERVER_ORIGIN_HOST => ParsedUrlInterface::DEFAULT_PORT, |
||
95 | self::KEY_SERVER_ORIGIN_PORT => '', |
||
96 | ], |
||
97 | |||
98 | /** |
||
99 | * A list of allowed request origins (lower-cased, no trail slashes). |
||
100 | * Value `true` enables and value `null` disables origin. |
||
101 | * If all origins '*' are enabled all settings for other origins are ignored. |
||
102 | * |
||
103 | * For example, |
||
104 | * |
||
105 | * [ |
||
106 | * 'http://example.com:123' => true, |
||
107 | * 'http://evil.com' => null, |
||
108 | * self::VALUE_ALLOW_ORIGIN_ALL => null, |
||
109 | * ]; |
||
110 | */ |
||
111 | self::KEY_ALLOWED_ORIGINS => [], |
||
112 | |||
113 | /** |
||
114 | * A list of allowed request methods (case sensitive). Value `true` enables and value `null` disables method. |
||
115 | * |
||
116 | * For example, |
||
117 | * |
||
118 | * [ |
||
119 | * 'GET' => true, |
||
120 | * 'PATCH' => true, |
||
121 | * 'POST' => true, |
||
122 | * 'PUT' => null, |
||
123 | * 'DELETE' => true, |
||
124 | * ]; |
||
125 | * |
||
126 | * Security Note: you have to remember CORS is not access control system and you should not expect all |
||
127 | * cross-origin requests will have pre-flights. For so-called 'simple' methods with so-called 'simple' |
||
128 | * headers request will be made without pre-flight. Thus you can not restrict such requests with CORS |
||
129 | * and should use other means. |
||
130 | * For example method 'GET' without any headers or with only 'simple' headers will not have pre-flight |
||
131 | * request so disabling it will not restrict access to resource(s). |
||
132 | * |
||
133 | * You can read more on 'simple' methods at http://www.w3.org/TR/cors/#simple-method |
||
134 | */ |
||
135 | self::KEY_ALLOWED_METHODS => [], |
||
136 | |||
137 | /** |
||
138 | * A list of allowed request headers (lower-cased). Value `true` enables and value `null` disables header. |
||
139 | * |
||
140 | * For example, |
||
141 | * |
||
142 | * $allowedHeaders = [ |
||
143 | * 'content-type' => true, |
||
144 | * 'x-custom-request-header' => null, |
||
145 | * self::VALUE_ALLOW_ALL_HEADERS => null, |
||
146 | * ]; |
||
147 | * |
||
148 | * Security Note: you have to remember CORS is not access control system and you should not expect all |
||
149 | * cross-origin requests will have pre-flights. For so-called 'simple' methods with so-called 'simple' |
||
150 | * headers request will be made without pre-flight. Thus you can not restrict such requests with CORS |
||
151 | * and should use other means. |
||
152 | * For example method 'GET' without any headers or with only 'simple' headers will not have pre-flight |
||
153 | * request so disabling it will not restrict access to resource(s). |
||
154 | * |
||
155 | * You can read more on 'simple' headers at http://www.w3.org/TR/cors/#simple-header |
||
156 | */ |
||
157 | self::KEY_ALLOWED_HEADERS => [], |
||
158 | |||
159 | /** |
||
160 | * A list of headers (case insensitive) which will be made accessible to user agent (browser) in response. |
||
161 | * Value `true` enables and value `null` disables header. |
||
162 | * |
||
163 | * For example, |
||
164 | * |
||
165 | * [ |
||
166 | * 'Content-Type' => true, |
||
167 | * 'X-Custom-Response-Header' => true, |
||
168 | * 'X-Disabled-Header' => null, |
||
169 | * ]; |
||
170 | */ |
||
171 | self::KEY_EXPOSED_HEADERS => [], |
||
172 | |||
173 | /** |
||
174 | * If access with credentials is supported by the resource. |
||
175 | */ |
||
176 | self::KEY_IS_USING_CREDENTIALS => false, |
||
177 | |||
178 | /** |
||
179 | * Pre-flight response cache max period in seconds. |
||
180 | * |
||
181 | * @var int |
||
182 | */ |
||
183 | self::KEY_FLIGHT_CACHE_MAX_AGE => 0, |
||
184 | |||
185 | /** |
||
186 | * If allowed methods should be added to pre-flight response when |
||
187 | * 'simple' method is requested (see #6.2.9 CORS). |
||
188 | * |
||
189 | * @see http://www.w3.org/TR/cors/#resource-preflight-requests |
||
190 | */ |
||
191 | self::KEY_IS_FORCE_ADD_METHODS => false, |
||
192 | |||
193 | /** |
||
194 | * If allowed headers should be added when request headers are 'simple' and |
||
195 | * non of them is 'Content-Type' (see #6.2.10 CORS). |
||
196 | * |
||
197 | * @see http://www.w3.org/TR/cors/#resource-preflight-requests |
||
198 | * |
||
199 | * @var bool |
||
200 | */ |
||
201 | self::KEY_IS_FORCE_ADD_HEADERS => false, |
||
202 | |||
203 | /** |
||
204 | * If request 'Host' header should be checked against server's origin. |
||
205 | * |
||
206 | * @var bool |
||
207 | */ |
||
208 | self::KEY_IS_CHECK_HOST => false, |
||
209 | ]; |
||
210 | |||
211 | /** |
||
212 | * @inheritdoc |
||
213 | */ |
||
214 | 7 | public function getSettings() |
|
218 | |||
219 | /** |
||
220 | * @inheritdoc |
||
221 | */ |
||
222 | 7 | public function setSettings(array $settings) |
|
226 | |||
227 | /** |
||
228 | * @inheritdoc |
||
229 | */ |
||
230 | 11 | public function getServerOrigin() |
|
234 | |||
235 | /** |
||
236 | * @inheritdoc |
||
237 | */ |
||
238 | 18 | public function setServerOrigin($origin) |
|
244 | |||
245 | /** |
||
246 | * @inheritdoc |
||
247 | */ |
||
248 | 3 | public function isPreFlightCanBeCached(RequestInterface $request) |
|
252 | |||
253 | /** |
||
254 | * @inheritdoc |
||
255 | */ |
||
256 | 3 | public function getPreFlightCacheMaxAge(RequestInterface $request) |
|
260 | |||
261 | /** |
||
262 | * @inheritdoc |
||
263 | */ |
||
264 | 9 | public function setPreFlightCacheMaxAge($cacheMaxAge) |
|
270 | |||
271 | /** |
||
272 | * @inheritdoc |
||
273 | */ |
||
274 | 2 | public function isForceAddAllowedMethodsToPreFlightResponse() |
|
278 | |||
279 | /** |
||
280 | * @inheritdoc |
||
281 | */ |
||
282 | 9 | public function setForceAddAllowedMethodsToPreFlightResponse($forceFlag) |
|
288 | |||
289 | /** |
||
290 | * @inheritdoc |
||
291 | */ |
||
292 | 1 | public function isForceAddAllowedHeadersToPreFlightResponse() |
|
296 | |||
297 | /** |
||
298 | * @inheritdoc |
||
299 | */ |
||
300 | 9 | public function setForceAddAllowedHeadersToPreFlightResponse($forceFlag) |
|
306 | |||
307 | /** |
||
308 | * @inheritdoc |
||
309 | */ |
||
310 | 4 | public function isRequestCredentialsSupported(RequestInterface $request) |
|
314 | |||
315 | /** |
||
316 | * @inheritdoc |
||
317 | */ |
||
318 | 18 | public function setRequestCredentialsSupported($isSupported) |
|
324 | |||
325 | /** |
||
326 | * @inheritdoc |
||
327 | */ |
||
328 | 8 | public function isRequestOriginAllowed(ParsedUrlInterface $requestOrigin) |
|
341 | |||
342 | /** |
||
343 | * @inheritdoc |
||
344 | */ |
||
345 | 18 | public function setRequestAllowedOrigins(array $origins) |
|
355 | |||
356 | /** |
||
357 | * @inheritdoc |
||
358 | */ |
||
359 | 5 | public function isRequestMethodSupported($method) |
|
365 | |||
366 | /** |
||
367 | * @inheritdoc |
||
368 | */ |
||
369 | 5 | public function isRequestAllHeadersSupported($headers) |
|
391 | |||
392 | /** |
||
393 | * @inheritdoc |
||
394 | */ |
||
395 | 3 | public function getRequestAllowedMethods(RequestInterface $request, $requestMethod) |
|
399 | |||
400 | /** |
||
401 | * @inheritdoc |
||
402 | */ |
||
403 | 18 | public function setRequestAllowedMethods(array $methods) |
|
412 | |||
413 | /** |
||
414 | * @inheritdoc |
||
415 | */ |
||
416 | 3 | public function getRequestAllowedHeaders(RequestInterface $request, array $requestHeaders) |
|
420 | |||
421 | /** |
||
422 | * @inheritdoc |
||
423 | */ |
||
424 | 18 | public function setRequestAllowedHeaders(array $headers) |
|
434 | |||
435 | /** |
||
436 | * @inheritdoc |
||
437 | */ |
||
438 | 2 | public function getResponseExposedHeaders(RequestInterface $request) |
|
442 | |||
443 | /** |
||
444 | * @inheritdoc |
||
445 | */ |
||
446 | 18 | public function setResponseExposedHeaders(array $headers) |
|
452 | |||
453 | /** |
||
454 | * @inheritdoc |
||
455 | */ |
||
456 | 11 | public function isCheckHost() |
|
460 | |||
461 | /** |
||
462 | * @inheritdoc |
||
463 | */ |
||
464 | 18 | public function setCheckHost($checkFlag) |
|
470 | |||
471 | /** |
||
472 | * Select only enabled items from $list. |
||
473 | * |
||
474 | * @param array $list |
||
475 | * |
||
476 | * @return array |
||
477 | */ |
||
478 | 6 | protected function getEnabledItems(array $list) |
|
490 | } |
||
491 |