1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Middlewares; |
4
|
|
|
|
5
|
|
|
use App\Http\Request; |
6
|
|
|
use App\Http\Response; |
7
|
|
|
use App\Http\Headers; |
8
|
|
|
use App\Http\Interfaces\Middleware\ILayer; |
9
|
|
|
use App\Container; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* App\Middleware\Cors |
13
|
|
|
* |
14
|
|
|
* Patch initial response to accept CORS requests |
15
|
|
|
*/ |
16
|
|
|
class Cors implements ILayer |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
use \App\Middlewares\Reuse\TInit; |
20
|
|
|
|
21
|
|
|
const _SIGN = 'X-Middleware-Cors'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* peel |
25
|
|
|
* |
26
|
|
|
* @param Container $container |
27
|
|
|
* @param \Closure $next |
28
|
|
|
* @return \Closure |
29
|
|
|
*/ |
30
|
|
|
public function peel(Container $container, \Closure $next) |
31
|
|
|
{ |
32
|
|
|
$this->init($container); |
33
|
|
|
$this->process(); |
34
|
|
|
return $next($container); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* process |
39
|
|
|
* |
40
|
|
|
*/ |
41
|
|
|
private function process() |
42
|
|
|
{ |
43
|
|
|
if ($this->enabled) { |
44
|
|
|
$this->response->getHeaderManager()->add( |
45
|
|
|
self::_SIGN, |
46
|
|
|
microtime(true) |
47
|
|
|
); |
48
|
|
|
if ($this->required()) { |
49
|
|
|
$this->response |
50
|
|
|
->setCode(Response::HTTP_NOT_FOUND) |
51
|
|
|
->setContent([ |
52
|
|
|
Response::_ERROR_CODE => Response::HTTP_NOT_FOUND, |
53
|
|
|
Response::_ERROR_MSG => 'Not found' |
54
|
|
|
]) |
55
|
|
|
->getHeaderManager() |
56
|
|
|
->add(Headers::CONTENT_TYPE, 'application/json; charset=utf-8') |
57
|
|
|
->addMany([ |
58
|
|
|
Headers::HEADER_ACA_ORIGIN => '*', |
59
|
|
|
Headers::HEADER_ACA_CREDENTIALS => 'true', |
60
|
|
|
Headers::HEADER_ACA_METHODS => implode(',', [ |
61
|
|
|
Request::METHOD_GET, Request::METHOD_POST, Request::METHOD_PUT, |
62
|
|
|
Request::METHOD_DELETE, Request::METHOD_OPTIONS |
63
|
|
|
]), |
64
|
|
|
Headers::HEADER_ACA_HEADERS => implode(',', [ |
65
|
|
|
'Access-Control-Allow-Headers', 'Origin', 'Accept', |
66
|
|
|
'X-Requested-With', 'Content-Type', 'Access-Control-Request-Method', |
67
|
|
|
'Access-Control-Request-Headers', 'Access-Control-Allow-Origin', |
68
|
|
|
'Authorization' |
69
|
|
|
]) |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* required |
77
|
|
|
* |
78
|
|
|
* @return boolean |
79
|
|
|
*/ |
80
|
|
|
protected function required(): bool |
81
|
|
|
{ |
82
|
|
|
return (!$this->isExclude() |
83
|
|
|
&& $this->requestUriPrefix() === $this->prefix); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* isExclude |
88
|
|
|
* |
89
|
|
|
* @return Boolean |
90
|
|
|
*/ |
91
|
|
|
protected function isExclude(): bool |
92
|
|
|
{ |
93
|
|
|
$disallowed = $this->configParams['exclude']; |
94
|
|
|
$countEx = count($disallowed); |
95
|
|
|
for ($c = 0; $c < $countEx; ++$c) { |
96
|
|
|
$excludeUri = $this->prefix . $disallowed[$c]; |
97
|
|
|
$isExclude = ($excludeUri == $this->request->getUri()); |
98
|
|
|
if ($isExclude) { |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* uriPrefix |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
protected function requestUriPrefix(): string |
111
|
|
|
{ |
112
|
|
|
return substr($this->request->getUri(), 0, strlen($this->prefix)); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|