-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseController.php
More file actions
254 lines (219 loc) · 5.66 KB
/
BaseController.php
File metadata and controls
254 lines (219 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
/**
* @author : Jakiboy
* @package : FloatPHP
* @subpackage : Kernel Component
* @version : 1.5.x
* @copyright : (c) 2018 - 2025 Jihad Sinnaour <me@jihadsinnaour.com>
* @link : https://floatphp.com
* @license : MIT
*
* This file is a part of FloatPHP Framework.
*/
declare(strict_types=1);
namespace FloatPHP\Kernel;
use FloatPHP\Classes\Http\Response;
class BaseController extends View
{
/**
* redirectIndex : [GET] /index.php
*
* @access public
* @return void
*/
public function redirectIndex() : void
{
$this->redirect();
}
/**
* Check whether user has access.
*
* @access public
* @return bool
*/
public function hasAccess() : bool
{
$ip = $this->getServerIp();
// Allow local access
if ( $this->isDebug() && $this->hasString(['127.0.0.1', '::1'], $ip) ) {
return true;
}
// Check allowed IPs
$access = false;
$allowed = $this->getAllowedAccess();
$allowed = $this->applyFilter('access-allowed-ip', $allowed);
if ( !empty($allowed) ) {
$access = $this->hasString($allowed, $ip);
} else {
// Deny access
$denied = $this->getDeniedAccess();
$denied = $this->applyFilter('access-denied-ip', $denied);
$access = !$this->hasString($denied, $ip);
}
$data = ['ip' => $ip, 'access' => $access];
$this->doAction('ip-access', $data);
return $access;
}
/**
* Add JS hook.
*
* @access protected
* @param string $js
* @param string $hook
* @return void
*/
protected function addJS(string $js, string $hook = 'add-js') : void
{
$this->addAction($hook, function () use ($js) {
$file = $this->applyFilter('view-js', 'system/js');
$this->render($file, ['js' => $js]);
});
}
/**
* Add CSS hook.
*
* @access protected
* @param string $css
* @param string $hook
* @return void
*/
protected function addCSS(string $css, string $hook = 'add-css') : void
{
$this->addAction($hook, function () use ($css) {
$file = $this->applyFilter('view-css', 'system/css');
$this->render($file, ['css' => $css]);
});
}
/**
* Verify token against request data.
*
* @access protected
* @param string $token
* @param string $action
* @param bool
*/
protected function verifyToken(?string $token = null, ?string $action = null) : bool
{
// Get token session
$session = $this->getSession('--token') ?: [];
// Get token data
$data = $session[$token] ?? [];
// Apply default data
$data = $this->mergeArray([
'action' => '',
'url' => false,
'ip' => false,
'user' => false
], $data);
// Override verification
$this->doAction('verify-token', $data);
// Verify authenticated user
if ( $this->isAuthenticated() ) {
$user = $this->getSession($this->getSessionId());
if ( $user !== $data['user'] ) {
return false;
}
}
// Verify action
if ( $action !== $data['action'] ) {
return false;
}
// Verify IP
if ( $this->getServerIp() !== $data['ip'] ) {
return false;
}
// Verify URL
if ( $this->getServer('http-referer') !== $data['url'] ) {
return false;
}
return $this->verifyHash($token, $data);
}
/**
* Verify current request.
*
* @access protected
* @param bool $force, Token validation
* @return void
*/
protected function verifyRequest(bool $force = false) : void
{
$token = (string)$this->applyFilter('request-token', '--token');
$action = (string)$this->applyFilter('request-action', '--action');
$ignore = (string)$this->applyFilter('request-ignore', '--ignore');
if ( $force ) {
if ( !$this->hasRequest($token) ) {
$msg = $this->applyFilter('invalid-signature', 'Invalid request signature');
$msg = $this->translate($msg);
$this->setResponse($msg, [], 'error', 401);
}
}
if ( $this->hasRequest($token) ) {
$action = $this->hasRequest($action) ? $this->getRequest($action) : '';
if ( !$this->verifyToken($this->getRequest($token), $action) ) {
$msg = $this->applyFilter('invalid-token', 'Invalid request token');
$msg = $this->translate($msg);
$this->setResponse($msg, [], 'error', 401);
}
}
if ( $this->hasRequest($ignore) && !empty($this->getRequest($ignore)) ) {
$msg = $this->applyFilter('invalid-data', 'Invalid request data');
$msg = $this->translate($msg);
$this->setResponse($msg, [], 'error', 401);
}
}
/**
* Sanitize current request.
*
* @access protected
* @param bool $verify, Request
* @param bool $force, Token validation
* @return array
*/
protected function sanitizeRequest(bool $verify = true, bool $force = false) : array
{
$request = $this->getRequest();
$excepts = ['PHPSESSID', 'COOKIES'];
if ( !$force ) {
$excepts = $this->mergeArray(['submit', '--token', '--action', '--ignore'], $excepts);
}
if ( $verify ) {
$this->verifyRequest($force);
}
$excepts = $this->applyFilter('sanitize-request', $excepts);
foreach ($excepts as $except) {
if ( isset($request[$except]) ) {
unset($request[$except]);
}
}
return $request ?: [];
}
/**
* Set HTTP response (Translated).
*
* @access protected
* @param string $msg
* @param mixed $content
* @param string $status
* @param int $code
* @return void
*/
protected function setResponse(string $msg = '', $content = [], string $status = 'success', int $code = 200) : void
{
$msg = $this->translate($msg);
Response::set($msg, $content, $status, $code);
}
/**
* Set HTTP response.
*
* @access protected
* @param string $msg
* @param mixed $content
* @param string $status
* @param int $code
* @return void
*/
protected function setHttpResponse(string $msg = '', $content = [], string $status = 'success', int $code = 200) : void
{
Response::set($msg, $content, $status, $code);
}
}