|
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 _PREFLIGHT = 'preflight'; |
|
22
|
|
|
const _SIGN = 'X-Middleware-Cors'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* peel |
|
26
|
|
|
* |
|
27
|
|
|
* @param Container $container |
|
28
|
|
|
* @param \Closure $next |
|
29
|
|
|
* @return \Closure |
|
30
|
|
|
*/ |
|
31
|
8 |
|
public function peel(Container $container, \Closure $next) |
|
32
|
|
|
{ |
|
33
|
8 |
|
$this->init($container); |
|
34
|
8 |
|
$this->process(); |
|
35
|
8 |
|
return $next($container); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* process |
|
40
|
|
|
* |
|
41
|
|
|
*/ |
|
42
|
2 |
|
private function process() |
|
43
|
|
|
{ |
|
44
|
2 |
|
if ($this->enabled) { |
|
45
|
2 |
|
$this->response->getHeaderManager()->add( |
|
46
|
2 |
|
self::_SIGN, |
|
47
|
2 |
|
microtime(true) |
|
48
|
|
|
); |
|
49
|
2 |
|
if ($this->required()) { |
|
50
|
1 |
|
if (Request::METHOD_OPTIONS == $this->request->getMethod()) { |
|
51
|
|
|
$pureCa = preg_replace('/\?.*/', '', $this->caUri()) . '/'; |
|
52
|
|
|
$caFrags = explode('/', $pureCa); |
|
53
|
|
|
$controller = $caFrags[0]; |
|
54
|
|
|
$this->kernel->setAction([$controller, self::_PREFLIGHT]); |
|
55
|
|
|
} |
|
56
|
1 |
|
$this->response |
|
57
|
1 |
|
->setCode(Response::HTTP_NOT_FOUND) |
|
58
|
1 |
|
->setContent([ |
|
59
|
1 |
|
Response::_ERROR_CODE => Response::HTTP_NOT_FOUND, |
|
60
|
|
|
Response::_ERROR_MSG => 'Not found' |
|
61
|
|
|
]) |
|
62
|
1 |
|
->getHeaderManager() |
|
63
|
1 |
|
->add(Headers::CONTENT_TYPE, 'application/json; charset=utf-8') |
|
64
|
1 |
|
->addMany([ |
|
65
|
1 |
|
Headers::HEADER_ACA_ORIGIN => '*', |
|
66
|
1 |
|
Headers::HEADER_ACA_CREDENTIALS => 'true', |
|
67
|
1 |
|
Headers::HEADER_ACA_METHODS => implode(',', [ |
|
68
|
1 |
|
Request::METHOD_GET, Request::METHOD_POST, Request::METHOD_PUT, |
|
69
|
|
|
Request::METHOD_DELETE, Request::METHOD_OPTIONS |
|
70
|
|
|
]), |
|
71
|
1 |
|
Headers::HEADER_ACA_HEADERS => implode(',', [ |
|
72
|
1 |
|
'Access-Control-Allow-Headers', 'Origin', 'Accept', |
|
73
|
|
|
'X-Requested-With', 'Content-Type', 'Access-Control-Request-Method', |
|
74
|
|
|
'Access-Control-Request-Headers', 'Access-Control-Allow-Origin', |
|
75
|
|
|
'Authorization' |
|
76
|
|
|
]) |
|
77
|
|
|
]); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* required |
|
84
|
|
|
* |
|
85
|
|
|
* @return boolean |
|
86
|
|
|
*/ |
|
87
|
1 |
|
protected function required(): bool |
|
88
|
|
|
{ |
|
89
|
1 |
|
return (!$this->isExclude() |
|
90
|
1 |
|
&& $this->requestUriPrefix() === $this->prefix); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* isExclude |
|
95
|
|
|
* |
|
96
|
|
|
* @return Boolean |
|
97
|
|
|
*/ |
|
98
|
1 |
|
protected function isExclude(): bool |
|
99
|
|
|
{ |
|
100
|
1 |
|
$disallowed = $this->configParams['exclude']; |
|
101
|
1 |
|
$countEx = count($disallowed); |
|
102
|
1 |
|
for ($c = 0; $c < $countEx; ++$c) { |
|
103
|
1 |
|
$excludeUri = $this->prefix . $disallowed[$c]; |
|
104
|
1 |
|
$isExclude = ($excludeUri == $this->request->getUri()); |
|
105
|
1 |
|
if ($isExclude) { |
|
106
|
1 |
|
return true; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
1 |
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* return controller action from uri |
|
114
|
|
|
* |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
1 |
|
protected function caUri():string |
|
118
|
|
|
{ |
|
119
|
1 |
|
return str_replace($this->prefix, '', $this->request->getUri()); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* uriPrefix |
|
124
|
|
|
* |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
1 |
|
protected function requestUriPrefix(): string |
|
128
|
|
|
{ |
|
129
|
1 |
|
return substr($this->request->getUri(), 0, strlen($this->prefix)); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|