1
1
<?php
2
- /**
2
+ /*
3
3
* sysPass
4
4
*
5
- * @author nuxsmin
6
- * @link https://syspass.org
7
- * @copyright 2012-2019 , Rubén Domínguez nuxsmin@$syspass.org
5
+ * @author nuxsmin
6
+ * @link https://syspass.org
7
+ * @copyright 2012-2022 , Rubén Domínguez nuxsmin@$syspass.org
8
8
*
9
9
* This file is part of sysPass.
10
10
*
19
19
* GNU General Public License for more details.
20
20
*
21
21
* You should have received a copy of the GNU General Public License
22
- * along with sysPass. If not, see <http://www.gnu.org/licenses/>.
22
+ * along with sysPass. If not, see <http://www.gnu.org/licenses/>.
23
23
*/
24
24
25
25
namespace SP ;
@@ -91,10 +91,6 @@ final class Bootstrap
91
91
* @var Klein
92
92
*/
93
93
private $ router ;
94
- /**
95
- * @var Language
96
- */
97
- private $ language ;
98
94
/**
99
95
* @var Request
100
96
*/
@@ -127,7 +123,6 @@ private final function __construct(Container $container)
127
123
$ this ->configData = $ this ->config ->getConfigData ();
128
124
$ this ->router = $ container ->get (Klein::class);
129
125
$ this ->request = $ container ->get (Request::class);
130
- $ this ->language = $ container ->get (Language::class);
131
126
132
127
$ this ->initRouter ();
133
128
}
@@ -161,22 +156,31 @@ function ($request, $response, $service) use ($oops) {
161
156
list ($ controller , $ action ) = explode ('/ ' , $ apiRequest ->getMethod ());
162
157
163
158
$ controllerClass = 'SP \\Modules \\' . ucfirst (APP_MODULE ) . '\\Controllers \\' . ucfirst ($ controller ) . 'Controller ' ;
164
- $ method = $ action . 'Action ' ;
159
+ $ method = $ action. 'Action ' ;
165
160
166
161
if (!method_exists ($ controllerClass , $ method )) {
167
- logger ($ controllerClass . ':: ' . $ method );
162
+ logger ($ controllerClass. ':: ' . $ method );
168
163
169
164
/** @var Response $response */
170
165
$ response ->headers ()->set ('Content-type ' , 'application/json; charset=utf-8 ' );
171
- return $ response ->body (JsonRpcResponse::getResponseError ($ oops , JsonRpcResponse::METHOD_NOT_FOUND , $ apiRequest ->getId ()));
166
+
167
+ return $ response ->body (
168
+ JsonRpcResponse::getResponseError (
169
+ $ oops ,
170
+ JsonRpcResponse::METHOD_NOT_FOUND ,
171
+ $ apiRequest ->getId ()
172
+ )
173
+ );
172
174
}
173
175
176
+ $ this ->setCors ($ response );
177
+
174
178
$ this ->initializeCommon ();
175
179
176
180
self ::$ container ->get (InitApi::class)
177
181
->initialize ($ controller );
178
182
179
- logger ('Routing call: ' . $ controllerClass . ':: ' . $ method );
183
+ logger ('Routing call: ' . $ controllerClass. ':: ' . $ method );
180
184
181
185
return call_user_func ([new $ controllerClass (self ::$ container , $ method , $ apiRequest ), $ method ]);
182
186
} catch (\Exception $ e ) {
@@ -216,14 +220,17 @@ function ($request, $response, $service) use ($oops) {
216
220
$ this ->initializePluginClasses ();
217
221
218
222
if (!method_exists ($ controllerClass , $ methodName )) {
219
- logger ($ controllerClass . ':: ' . $ methodName );
223
+ logger ($ controllerClass. ':: ' . $ methodName );
220
224
221
225
/** @var Response $response */
222
226
$ response ->code (404 );
223
227
224
228
throw new RuntimeException ($ oops );
225
229
}
226
230
231
+ $ this ->setCors ($ response );
232
+ $ this ->setXFrame ($ response );
233
+
227
234
$ this ->initializeCommon ();
228
235
229
236
switch (APP_MODULE ) {
@@ -291,7 +298,7 @@ protected function initializeCommon()
291
298
if (!self ::$ checkPhpVersion ) {
292
299
throw new InitializationException (
293
300
sprintf (__ ('Required PHP version >= %s <= %s ' ), '7.3 ' , '7.4 ' ),
294
- InitializationException ::ERROR ,
301
+ Core \ Exceptions \SPException ::ERROR ,
295
302
__u ('Please update the PHP version to run sysPass ' )
296
303
);
297
304
}
@@ -473,4 +480,23 @@ public static function run(Container $container, $module = APP_MODULE)
473
480
throw new InitializationException ('Unknown module ' );
474
481
}
475
482
}
483
+
484
+ protected function setCors (Response $ response ): void
485
+ {
486
+ $ response ->header (
487
+ 'Access-Control-Allow-Origin ' ,
488
+ $ this ->configData ->getApplicationUrl () ?? $ this ->request ->getHttpHost ()
489
+ );
490
+ $ response ->header (
491
+ 'Access-Control-Allow-Headers ' ,
492
+ 'X-Requested-With, Content-Type, Accept, Origin, Authorization '
493
+ );
494
+ $ response ->header ('Access-Control-Allow-Methods ' , 'GET, POST, OPTIONS ' );
495
+ }
496
+
497
+ protected function setXFrame (Response $ response ): void
498
+ {
499
+ $ response ->header ('X-FRAME-OPTIONS ' , 'DENY ' );
500
+ $ response ->header ('Content-Security-Policy ' , 'frame-ancestors \'none \'' );
501
+ }
476
502
}
0 commit comments