|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Bluz PHP Team |
|
5
|
|
|
* @link https://github.com/bluzphp/skeleton |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Application; |
|
11
|
|
|
|
|
12
|
|
|
use Application\Auth; |
|
13
|
|
|
use Bluz\Application\Application; |
|
|
|
|
|
|
14
|
|
|
use Bluz\Auth\AuthException; |
|
|
|
|
|
|
15
|
|
|
use Bluz\Controller\Controller; |
|
|
|
|
|
|
16
|
|
|
use Bluz\Http\Exception\ForbiddenException; |
|
|
|
|
|
|
17
|
|
|
use Bluz\Http\Exception\RedirectException; |
|
|
|
|
|
|
18
|
|
|
use Bluz\Proxy\Auth as AuthProxy; |
|
|
|
|
|
|
19
|
|
|
use Bluz\Proxy\Config; |
|
|
|
|
|
|
20
|
|
|
use Bluz\Proxy\Layout; |
|
|
|
|
|
|
21
|
|
|
use Bluz\Proxy\Logger; |
|
|
|
|
|
|
22
|
|
|
use Bluz\Proxy\Messages; |
|
|
|
|
|
|
23
|
|
|
use Bluz\Proxy\Request; |
|
|
|
|
|
|
24
|
|
|
use Bluz\Proxy\Response; |
|
|
|
|
|
|
25
|
|
|
use Bluz\Proxy\Router; |
|
|
|
|
|
|
26
|
|
|
use Bluz\Proxy\Session; |
|
|
|
|
|
|
27
|
|
|
use Bluz\Proxy\Translator; |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Bootstrap |
|
31
|
|
|
* |
|
32
|
|
|
* @category Application |
|
33
|
|
|
* @package Bootstrap |
|
34
|
|
|
*/ |
|
35
|
|
|
class Bootstrap extends Application |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function initTranslator(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$translator = new \Bluz\Translator\Translator(); |
|
|
|
|
|
|
43
|
|
|
$translator->setOptions(Config::get('translator')); |
|
44
|
|
|
|
|
45
|
|
|
// supported locales |
|
46
|
|
|
// en_US, ru_RU, uk_UA |
|
47
|
|
|
$locales = [ |
|
48
|
|
|
'en' => 'en_US', |
|
49
|
|
|
'ru' => 'ru_RU', |
|
50
|
|
|
'uk' => 'uk_UA', |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
|
|
// try to check locale from cookies |
|
54
|
|
|
$locale = Request::getCookie('locale'); |
|
55
|
|
|
|
|
56
|
|
|
// try to get locale from browser |
|
57
|
|
|
if (!$locale) { |
|
58
|
|
|
$languages = Request::getAcceptLanguage(); |
|
59
|
|
|
foreach ($languages as $language => $priority) { |
|
60
|
|
|
if (array_key_exists($language, $locales)) { |
|
61
|
|
|
$locale = $language; |
|
62
|
|
|
break; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// normalize locale |
|
68
|
|
|
if (array_key_exists($locale, $locales)) { |
|
69
|
|
|
$translator->setLocale($locales[$locale]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$translator->init(); |
|
73
|
|
|
|
|
74
|
|
|
Translator::setInstance($translator); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
* |
|
80
|
|
|
* @param Controller $controller |
|
81
|
|
|
* |
|
82
|
|
|
* @return void |
|
83
|
|
|
* @throws AuthException |
|
84
|
|
|
* @throws Exception |
|
85
|
|
|
* @throws \Bluz\Db\Exception\DbException |
|
86
|
|
|
* @throws \Bluz\Db\Exception\InvalidPrimaryKeyException |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function preDispatch($controller): void |
|
89
|
|
|
{ |
|
90
|
|
|
// example of setup default title |
|
91
|
|
|
Layout::title('Bluz Skeleton'); |
|
92
|
|
|
|
|
93
|
|
|
if (!AuthProxy::getIdentity() && $controller->getModule() !== Router::getErrorModule()) { |
|
94
|
|
|
if ($token = Request::getCookie('Auth-Token')) { |
|
95
|
|
|
// try to login by token from cookies |
|
96
|
|
|
try { |
|
97
|
|
|
Auth\Provider\Cookie::authenticate($token); |
|
98
|
|
|
} catch (AuthException $e) { |
|
99
|
|
|
$this->getResponse()->setCookie('Auth-Token', '', 1, '/'); |
|
100
|
|
|
} |
|
101
|
|
|
} elseif ($token = Request::getHeader('Auth-Token')) { |
|
102
|
|
|
// try to login by token from headers |
|
103
|
|
|
Auth\Provider\Token::authenticate($token); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
parent::preDispatch($controller); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Denied access |
|
111
|
|
|
* |
|
112
|
|
|
* @param ForbiddenException $exception |
|
113
|
|
|
* |
|
114
|
|
|
* @return \Bluz\Controller\Controller|null |
|
115
|
|
|
*/ |
|
116
|
|
|
public function forbidden(ForbiddenException $exception): ?Controller |
|
117
|
|
|
{ |
|
118
|
|
|
// for AJAX and API calls (over JSON) |
|
119
|
|
|
$jsonOrApi = Request::isXmlHttpRequest() |
|
120
|
|
|
|| (Request::checkAccept([Request::TYPE_HTML, Request::TYPE_JSON]) === Request::TYPE_JSON); |
|
121
|
|
|
|
|
122
|
|
|
// for guest, for requests |
|
123
|
|
|
if (!$jsonOrApi && !AuthProxy::getIdentity()) { |
|
124
|
|
|
// save URL to session and redirect make sense if presentation is null |
|
125
|
|
|
Session::set('rollback', Request::getUri()->__toString()); |
|
126
|
|
|
// add error notice |
|
127
|
|
|
Messages::addError('You don\'t have permissions, please sign in'); |
|
128
|
|
|
// redirect to Sign In page |
|
129
|
|
|
$redirect = new RedirectException(); |
|
130
|
|
|
$redirect->setUrl(Router::getUrl('users', 'signin')); |
|
131
|
|
|
return $this->redirect($redirect); |
|
132
|
|
|
} |
|
133
|
|
|
return $this->error($exception); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Render with debug headers |
|
138
|
|
|
* |
|
139
|
|
|
* @return void |
|
140
|
|
|
*/ |
|
141
|
|
|
public function render(): void |
|
142
|
|
|
{ |
|
143
|
|
|
Logger::info('app:render'); |
|
144
|
|
|
Logger::info('app:files:' . count(get_included_files())); |
|
145
|
|
|
parent::render(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Finish it |
|
150
|
|
|
* |
|
151
|
|
|
* @return void |
|
152
|
|
|
*/ |
|
153
|
|
|
public function end(): void |
|
154
|
|
|
{ |
|
155
|
|
|
if ($errors = Logger::get('error')) { |
|
156
|
|
|
$this->sendErrors($errors); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* sendErrorBody |
|
162
|
|
|
* |
|
163
|
|
|
* @param array $errors |
|
164
|
|
|
* |
|
165
|
|
|
* @return void |
|
166
|
|
|
*/ |
|
167
|
|
|
protected function sendErrors($errors): void |
|
168
|
|
|
{ |
|
169
|
|
|
foreach ($errors as $message) { |
|
170
|
|
|
errorLog(new \ErrorException($message, 0, E_USER_ERROR)); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths