-
-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathView.php
496 lines (444 loc) · 13.2 KB
/
View.php
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
<?php
class View
{
/**
* Current page content
*
* @var string
*/
private $content;
/**
* Current page title
*
* @var string
*/
public $title;
/**
* Constructor which adds certain headers
*/
public function __construct()
{
// Add security headers
header('X-XSS-Protection: 1; mode=block');
header('X-Frame-Options: DENY');
header('X-Content-Type-Options: nosniff');
header('Referrer-Policy: strict-origin-when-cross-origin');
// Add CSP header to manage
if (explode('/', path)[1] === 'manage') {
header("Content-Security-Policy: default-src 'self'; img-src 'self' data:; font-src fonts.gstatic.com; script-src 'self' 'nonce-csrf'; style-src 'self' 'unsafe-inline'; frame-ancestors 'none';");
}
}
/**
* Makes a render of a page with only an error message
*
* @param string $message The error message
* @return string
*/
public function renderErrorPage($message)
{
$this->content = '';
$this->renderTemplate('system/error');
$this->renderMessage($message);
$this->content = str_replace('{theme}', '', $this->content);
return $this->showContent();
}
/**
* Updates the message template with the given message
*
* @param string $message The message
* @return void
*/
public function renderMessage($message)
{
$content = $this->getContent();
$content = str_replace('{message}', '<div class="alert" role="alert">' . nl2br(e($message)) . '</div>', $content);
$this->content = $content;
}
/**
* Updates all the data parameters with the correct data
*
* @param string $param The param
* @param string $value The value
* @param bool $plain Text or HTML
* @return void
*/
public function renderData($param, $value, $plain = false)
{
$content = $this->getContent();
if ($plain) {
$content = str_replace('{%data ' . $param . '}', $value ?? '', $content);
} else {
$content = str_replace('{%data ' . $param . '}', e($value ?? ''), $content);
}
$this->content = $content;
}
/**
* Updates all the data parameters with the correct data with newlines
*
* @param string $param The param
* @param string $value The value
* @param bool $plain Text or HTML
* @return void
*/
public function renderDataWithLines($param, $value, $plain = false)
{
$content = $this->getContent();
$value = $plain ? ($value ?? '') : (e($value) ?? '');
if ($value === '') {
$content = preg_replace("/{%data $param.*?\n/", '', $content);
} else {
$content = str_replace('{%data ' . $param . '}', "\n" . $value, $content);
}
$this->content = $content;
}
/**
* Updates all if statements in the view template based on the given boolean
*
* @param string $condition The condition name
* @param bool $bool The condition value
* @param bool $falseCondition Whether condition is true or false
* @return void
*/
public function renderCondition($condition, $bool, $falseCondition = false)
{
$content = $this->getContent();
$search = !$falseCondition ? '/{%if ' . $condition . '}(.*?){%\/if}/s' : '/{%!if ' . $condition . '}(.*?){%\/!if}/s';
preg_match_all($search, $content, $matches);
foreach ($matches[1] as $key => $value) {
// Shows the content if the given boolean is true
if ($bool === true) {
$content = str_replace(
$matches[0][$key],
$matches[1][$key],
$content
);
// Removes the content block when false
} else {
$content = str_replace(
$matches[0][$key],
'',
$content
);
}
}
$this->content = $content;
// Render the other side of the boolean (also known as `else`)
if (!$falseCondition) {
$this->renderCondition($condition, !$bool, true);
}
}
/**
* Renders a checked checkbox if checked
*
* @param mixed $name The checkbox name
* @param mixed $checked Whether checked or not
* @return void
*/
public function renderChecked($name, $checked)
{
$content = $this->getContent();
$content = str_replace('{%checked ' . $name . '}', $checked ? 'checked' : '', $content);
$this->content = $content;
}
/**
* Renders a dataset in a foreach block, for example for in tables
*
* @param string $name The data set name
* @param array $data The data to go in
* @param bool $plain Text or HTML
* @return void
*/
public function renderDataset($name, $data, $plain = false)
{
$content = $this->getContent();
// Finds all foreach blocks in a page
preg_match_all('/{%foreach ' . $name . '}(.*?){%\/foreach}/s', $content, $blockMatches);
foreach ($blockMatches[1] as $blockKey => $blockValue) {
$template = $blockMatches[1][$blockKey];
$htmlDump = '';
// Find all parameters in the block
preg_match_all('/{' . $name . '->(.*?)}/', $template, $templateMatches);
foreach ($data as $item) {
$template = $blockMatches[1][$blockKey];
foreach ($templateMatches[1] as $templateKey => $templateValue) {
// Replace all parameters with the correct values from $data
$template = str_replace(
$templateMatches[0][$templateKey],
$plain ? $item[$templateMatches[1][$templateKey]] : e($item[$templateMatches[1][$templateKey]]),
$template
);
}
$htmlDump .= $template;
}
// Makes an complete new block with all data
$content = str_replace(
$blockMatches[0][$blockKey],
$htmlDump,
$content
);
}
$this->content = $content;
}
/**
* Renders the content of a function given
*
* @param string $view The view name
* @return void
*/
public function renderTemplate($view)
{
$content = $this->surroundBody($view);
// Search and replace template content
preg_match_all('/{(.*?)\[(.*?)]}/', $content, $matches);
foreach ($matches[1] as $key => $value) {
if (method_exists($this, $value)) {
$content = str_replace(
$matches[0][$key],
e($this->$value((string) ($matches[2][$key]))),
$content
);
}
}
$this->content = $content;
$this->renderCondition('userIsAdmin', $this->session('rank') == 7);
$this->renderCondition('isLoggedIn', $this->session('rank') > 0);
}
/**
* Renders the content of a payload
*
* @param mixed $payload The payload name
* @return void
*/
public function renderPayload($payload)
{
$content = $this->getPayload($payload);
// Search and replace payload content
preg_match_all('/{{(.*?)}}/', $content, $matches);
foreach ($matches[1] as $key => $value) {
if (method_exists($this, $value)) {
$content = str_replace(
$matches[0][$key],
e($this->$value()),
$content
);
}
}
$this->content = $content;
}
/**
* Get payload by payload name
*
* @param string $payload The payload name
* @throws Exception
* @return string
*/
public function getPayload($payload)
{
$file = __DIR__ . "/../app/views/payloads/$payload.js";
if (!is_file($file)) {
throw new Exception('Payload not found');
}
return file_get_contents($file);
}
/**
* Get alert by alert name
*
* @param string $alert The alert name
* @throws Exception
* @return string
*/
public function getAlert($alert)
{
$file = __DIR__ . "/../app/views/alerts/$alert";
if (!is_file($file)) {
throw new Exception('Alert not found');
}
return file_get_contents($file);
}
/**
* Renders the data within an alert template
*
* @param string $template The template
* @param string|object $data The data
* @return string
*/
public function renderAlertData($template, $data)
{
$content = $template;
preg_match_all('/{{(.*?)}}/', $template, $matches);
foreach ($matches[1] as $key => $value) {
if (is_object($data->{$matches[1][$key]})) {
$data->{$matches[1][$key]} = json_encode($data->{$matches[1][$key]});
}
$content = str_replace(
$matches[0][$key],
substr(!empty($data->{$matches[1][$key]}) ? $data->{$matches[1][$key]} : '', 0, 1024),
$content
);
}
return $content;
}
/**
* Surrounds the body content with the header and footer
*
* @param string $view The view name
* @return string
*/
public function surroundBody($view)
{
$content = $this->getHtml('system/header');
$content .= $this->getHtml($view);
$content .= $this->getHtml('system/footer');
$content = str_replace('{menu}', $this->getHtml('system/menu'), $content);
return $content;
}
/**
* Returns HTML block of the given view
*
* @param string $file The file name
* @return string
*/
private function getHtml($file)
{
$file = __DIR__ . "/../app/views/$file.html";
if (is_file($file)) {
return file_get_contents($file);
}
return '404';
}
/**
* Last function in controller to return all content from how its build
*
* @return string
*/
public function showContent()
{
return str_replace('{message}', '', $this->content);
}
/**
* Returns correct page title with site name
*
* @return string
*/
public function title()
{
return 'ezXSS ~ ' . $this->title;
}
/**
* Get's session data
*
* @param string $param The param
* @return string
*/
public function session($param)
{
return isset($_SESSION[$param]) ? e($_SESSION[$param]) : '';
}
/**
* Returns current content
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Returns title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Returns ezXSS version
*
* @return string
*/
public function version()
{
return e(version);
}
/**
* Returns current file name
*
* @return string
*/
public function fileName()
{
return e(ltrim(path, '/'));
}
/**
* Returns menu-active for the menu when its the current page
*
* @param string $page The page to check
* @return string
*/
public function currentPage($page)
{
$uriParts = explode('/', path);
// Check current page for reporting pages
if ((substr($page, -2) === '*0' || substr($page, -2) === '*1') && isset($uriParts[2]) && $uriParts[2] == 'reports') {
if (isset($_GET['archive']) && $_GET['archive'] == '1') {
if (substr($page, -2) === '*0') {
return 'menu-active';
}
} else {
if (substr($page, -2) === '*1') {
return 'menu-active';
}
}
}
// Check other type of pages
if (substr($page, -1) === '*' && isset($uriParts[2]) && $uriParts[2] == substr($page, 0, -1)) {
return 'menu-active';
}
if (strpos($page, '/') !== false && isset($uriParts[3]) && $uriParts[2] . '/' . $uriParts[3] == $page) {
return 'menu-active';
}
if (isset($uriParts[2]) && $uriParts[2] == $page && (!isset($uriParts[3]) || empty($uriParts[3]))) {
return 'menu-active';
}
return '';
}
/**
* Returns domain
*
* @return string
*/
public function domain()
{
return host;
}
/**
* Returns current protocol
*
* @return string
*/
public function protocol()
{
return ishttps ? 'https' : 'http';
}
/**
* Set's title
*
* @param string $title The new title
* @return void
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* Set's content type
*
* @param string $type The new content type
* @return void
*/
public function setContentType($type)
{
header('Content-Type: ' . $type . '; charset=UTF-8');
}
}