1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2016 SURFnet. |
4
|
|
|
* |
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
8
|
|
|
* License, or (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU Affero General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace fkooman\RemoteStorage\Http; |
20
|
|
|
|
21
|
|
|
use fkooman\RemoteStorage\Http\Exception\HttpException; |
22
|
|
|
use fkooman\RemoteStorage\Http\Exception\InputValidationException; |
23
|
|
|
use fkooman\RemoteStorage\TplInterface; |
24
|
|
|
|
25
|
|
|
class Service |
26
|
|
|
{ |
27
|
|
|
/** @var \SURFnet\VPN\Common\TplInterface|null */ |
28
|
|
|
private $tpl; |
29
|
|
|
|
30
|
|
|
/** @var array */ |
31
|
|
|
private $routes; |
32
|
|
|
|
33
|
|
|
/** @var array */ |
34
|
|
|
private $beforeHooks; |
35
|
|
|
|
36
|
|
|
/** @var array */ |
37
|
|
|
private $afterHooks; |
38
|
|
|
|
39
|
|
|
public function __construct(TplInterface $tpl = null) |
40
|
|
|
{ |
41
|
|
|
$this->tpl = $tpl; |
|
|
|
|
42
|
|
|
$this->routes = [ |
43
|
|
|
'GET' => [], |
44
|
|
|
'POST' => [], |
45
|
|
|
]; |
46
|
|
|
$this->beforeHooks = []; |
47
|
|
|
$this->afterHooks = []; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function addBeforeHook($name, BeforeHookInterface $beforeHook) |
51
|
|
|
{ |
52
|
|
|
$this->beforeHooks[$name] = $beforeHook; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function addAfterHook($name, AfterHookInterface $afterHook) |
56
|
|
|
{ |
57
|
|
|
$this->afterHooks[$name] = $afterHook; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function addRoute($requestMethod, $pathInfo, callable $callback) |
61
|
|
|
{ |
62
|
|
|
$this->routes[$requestMethod][$pathInfo] = $callback; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function get($pathInfo, callable $callback) |
66
|
|
|
{ |
67
|
|
|
$this->addRoute('GET', $pathInfo, $callback); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function post($pathInfo, callable $callback) |
71
|
|
|
{ |
72
|
|
|
$this->addRoute('POST', $pathInfo, $callback); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function put($pathInfo, callable $callback) |
76
|
|
|
{ |
77
|
|
|
$this->addRoute('PUT', $pathInfo, $callback); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function delete($pathInfo, callable $callback) |
81
|
|
|
{ |
82
|
|
|
$this->addRoute('DELETE', $pathInfo, $callback); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function options($pathInfo, callable $callback) |
86
|
|
|
{ |
87
|
|
|
$this->addRoute('OPTIONS', $pathInfo, $callback); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function head($pathInfo, callable $callback) |
91
|
|
|
{ |
92
|
|
|
$this->addRoute('HEAD', $pathInfo, $callback); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function addModule(ServiceModuleInterface $module) |
96
|
|
|
{ |
97
|
|
|
$module->init($this); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function run(Request $request) |
101
|
|
|
{ |
102
|
|
|
try { |
103
|
|
|
// before hooks |
104
|
|
|
$hookData = []; |
105
|
|
|
foreach ($this->beforeHooks as $k => $v) { |
106
|
|
|
$hookResponse = $v->executeBefore($request, $hookData); |
107
|
|
|
// if we get back a Response object, return it immediately |
108
|
|
|
if ($hookResponse instanceof Response) { |
109
|
|
|
// run afterHooks |
110
|
|
|
return $this->runAfterHooks($request, $hookResponse); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$hookData[$k] = $hookResponse; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$requestMethod = $request->getRequestMethod(); |
117
|
|
|
$pathInfo = $request->getPathInfo(); |
118
|
|
|
|
119
|
|
|
if (!array_key_exists($requestMethod, $this->routes)) { |
120
|
|
|
throw new HttpException( |
121
|
|
|
sprintf('method "%s" not allowed', $requestMethod), |
122
|
|
|
405, |
123
|
|
|
['Allow' => implode(',', array_keys($this->routes))] |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if (!array_key_exists($pathInfo, $this->routes[$requestMethod])) { |
128
|
|
|
if (!array_key_exists('*', $this->routes[$requestMethod])) { |
129
|
|
|
throw new HttpException( |
130
|
|
|
sprintf('"%s" not found', $pathInfo), |
131
|
|
|
404 |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$pathInfo = '*'; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$response = $this->routes[$requestMethod][$pathInfo]($request, $hookData); |
139
|
|
|
|
140
|
|
|
// after hooks |
141
|
|
|
return $this->runAfterHooks($request, $response); |
142
|
|
|
} catch (InputValidationException $e) { |
143
|
|
|
// in case an InputValidationException is encountered, convert it |
144
|
|
|
// into a Bad Request HTTP response |
145
|
|
|
throw new HttpException($e->getMessage(), 400); |
146
|
|
|
} catch (HttpException $e) { |
147
|
|
|
if ($request->isBrowser()) { |
148
|
|
|
if (is_null($this->tpl)) { |
149
|
|
|
// template not available |
150
|
|
|
$response = new Response($e->getCode(), 'text/plain'); |
151
|
|
|
$response->setBody(sprintf('%d: %s', $e->getCode(), $e->getMessage())); |
152
|
|
|
} else { |
153
|
|
|
// template available |
154
|
|
|
$response = new Response($e->getCode(), 'text/html'); |
155
|
|
|
$response->setBody( |
156
|
|
|
$this->tpl->render( |
157
|
|
|
'errorPage', |
158
|
|
|
[ |
159
|
|
|
'code' => $e->getCode(), |
160
|
|
|
'message' => $e->getMessage(), |
161
|
|
|
] |
162
|
|
|
) |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
} else { |
166
|
|
|
// not a browser |
167
|
|
|
$response = new Response($e->getCode(), 'application/json'); |
168
|
|
|
$response->setBody(json_encode(['error' => $e->getMessage()])); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
foreach ($e->getResponseHeaders() as $key => $value) { |
172
|
|
|
$response->addHeader($key, $value); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// after hooks |
176
|
|
|
return $this->runAfterHooks($request, $response); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
private function runAfterHooks(Request $request, Response $response) |
181
|
|
|
{ |
182
|
|
|
foreach ($this->afterHooks as $v) { |
183
|
|
|
$response = $v->executeAfter($request, $response); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $response; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.