-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathHeader.php
More file actions
479 lines (409 loc) · 15.9 KB
/
Header.php
File metadata and controls
479 lines (409 loc) · 15.9 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
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
<?php
/**
* Used to render the header of PMA's pages
*/
declare(strict_types=1);
namespace PhpMyAdmin;
use PhpMyAdmin\Clock\Clock;
use PhpMyAdmin\Config\UserPreferences;
use PhpMyAdmin\Config\UserPreferencesHandler;
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\Console\Console;
use PhpMyAdmin\Container\ContainerBuilder;
use PhpMyAdmin\Dbal\DatabaseInterface;
use PhpMyAdmin\Html\Generator;
use PhpMyAdmin\I18n\LanguageManager;
use PhpMyAdmin\Navigation\Navigation;
use PhpMyAdmin\Theme\ThemeManager;
use Psr\Clock\ClockInterface;
use function array_merge;
use function htmlspecialchars;
use function implode;
use function ini_get;
use function json_encode;
use const JSON_HEX_TAG;
/**
* Class used to output the HTTP and HTML headers
*/
class Header
{
private Scripts|null $scripts = null;
private Menu|null $menu = null;
/**
* The page title
*/
private string $title = '';
/**
* The value for the id attribute for the body tag
*/
private string $bodyId = '';
/** Whether to show the top menu */
private bool|null $isMenuEnabled = null;
/**
* Whether to show the warnings
*/
private bool $warningsEnabled = true;
private bool $isTransformationWrapper = false;
public function __construct(
private readonly Template $template,
private readonly Console $console,
private readonly Config $config,
private readonly DatabaseInterface $dbi,
private readonly Relation $relation,
private readonly UserPreferences $userPreferences,
private readonly UserPreferencesHandler $userPreferencesHandler,
) {
}
private function isMenuEnabled(): bool
{
if ($this->isMenuEnabled !== null) {
return $this->isMenuEnabled;
}
$this->isMenuEnabled = $this->dbi->isConnected();
return $this->isMenuEnabled;
}
public function getScripts(): Scripts
{
if ($this->scripts !== null) {
return $this->scripts;
}
$this->scripts = new Scripts($this->template);
$this->scripts->addFile('runtime.js');
$this->scripts->addFile('vendor/jquery/jquery.min.js');
$this->scripts->addFile('vendor/jquery/jquery-migrate.min.js');
$this->scripts->addFile('vendor/sprintf.js');
$this->scripts->addFile('vendor/jquery/jquery-ui.min.js');
$this->scripts->addFile('vendor/bootstrap/bootstrap.js');
$this->scripts->addFile('vendor/js.cookie.min.js');
$this->scripts->addFile('vendor/jquery/jquery.validate.min.js');
$this->scripts->addFile('vendor/jquery/jquery-ui-timepicker-addon.min.js');
$this->scripts->addFile('index.php', ['route' => '/messages', 'l' => Current::$lang]);
$this->scripts->addFile('shared.js');
$this->scripts->addFile('menu_resizer.js');
$this->scripts->addFile('main.js');
$this->scripts->addCode($this->getJsParamsCode());
return $this->scripts;
}
/**
* Returns, as an array, a list of parameters
* used on the client side
*
* @return mixed[]
*/
public function getJsParams(): array
{
$pftext = $_SESSION['tmpval']['pftext'] ?? '';
$params = [
// Do not add any separator, JS code will decide
'common_query' => Url::getCommonRaw([], ''),
'opendb_url' => Url::getFromRoute($this->config->config->DefaultTabDatabase),
'lang' => Current::$lang,
'server' => Current::$server,
'table' => Current::$table,
'db' => Current::$database,
'token' => Session::getToken(),
'text_dir' => LanguageManager::$textDirection->value,
'LimitChars' => $this->config->config->limitChars,
'pftext' => $pftext,
'confirm' => $this->config->config->Confirm,
'LoginCookieValidity' => $this->config->config->LoginCookieValidity,
'session_gc_maxlifetime' => (int) ini_get('session.gc_maxlifetime'),
'logged_in' => $this->dbi->isConnected(),
'is_https' => $this->config->isHttps(),
'rootPath' => $this->config->getRootPath(),
'arg_separator' => Url::getArgSeparator(),
'version' => Version::VERSION,
];
if ($this->config->hasSelectedServer()) {
$params['auth_type'] = $this->config->selectedServer['auth_type'];
if (isset($this->config->selectedServer['user'])) {
$params['user'] = $this->config->selectedServer['user'];
}
}
return $params;
}
/**
* Returns, as a string, a list of parameters
* used on the client side
*/
public function getJsParamsCode(): string
{
$params = $this->getJsParams();
return 'window.Navigation.update(window.CommonParams.setAll(' . json_encode($params, JSON_HEX_TAG) . '));';
}
public function getMenu(): Menu
{
if ($this->menu !== null) {
return $this->menu;
}
$this->menu = new Menu(
$this->dbi,
$this->template,
$this->config,
$this->relation,
Current::$database,
Current::$table,
);
return $this->menu;
}
/**
* Setter for the ID attribute in the BODY tag
*
* @param string $id Value for the ID attribute
*/
public function setBodyId(string $id): void
{
$this->bodyId = htmlspecialchars($id);
}
/**
* Setter for the title of the page
*
* @param string $title New title
*/
public function setTitle(string $title): void
{
$this->title = htmlspecialchars($title);
}
/**
* Disables the display of the top menu
*/
public function disableMenuAndConsole(): void
{
$this->isMenuEnabled = false;
$this->console->disable();
}
/**
* Disables the display of the top menu
*/
public function disableWarnings(): void
{
$this->warningsEnabled = false;
}
/** @return array<string, mixed> */
public function getDisplay(ResponseRenderer $responseRenderer): array
{
$themeManager = ContainerBuilder::getContainer()->get(ThemeManager::class);
$theme = $themeManager->theme;
$scripts = $this->getScripts();
$userAgent = Core::getEnv('HTTP_USER_AGENT');
// The user preferences have been merged at this point
// so we can conditionally add CodeMirror, other scripts and settings
// See #20159, why we need to check for HTTP_USER_AGENT here
if ($this->config->config->CodemirrorEnable && $userAgent !== '') {
$scripts->addFile('vendor/codemirror/lib/codemirror.js');
$scripts->addFile('vendor/codemirror/mode/sql/sql.js');
$scripts->addFile('vendor/codemirror/addon/runmode/runmode.js');
$scripts->addFile('vendor/codemirror/addon/hint/show-hint.js');
$scripts->addFile('vendor/codemirror/addon/hint/sql-hint.js');
if ($this->config->config->LintEnable) {
$scripts->addFile('vendor/codemirror/addon/lint/lint.js');
$scripts->addFile('codemirror/addon/lint/sql-lint.js');
}
}
if ($this->config->config->SendErrorReports !== 'never') {
$scripts->addFile('vendor/tracekit.js');
$scripts->addFile('error_report.js');
}
if ($this->config->config->enable_drag_drop_import) {
$scripts->addFile('drag_drop_import.js');
}
if (! $this->config->config->DisableShortcutKeys) {
$scripts->addFile('shortcuts_handler.js');
}
$scripts->addCode($this->getVariablesForJavaScript());
$scripts->addCode('ConsoleEnterExecutes=' . ($this->config->config->ConsoleEnterExecutes ? 'true' : 'false'));
$scripts->addFiles($this->console->getScripts());
if ($this->isMenuEnabled() && Current::$server > 0) {
$navigation = (new Navigation($this->template, $this->relation, $this->dbi, $this->config))
->getDisplay($responseRenderer);
}
$customHeader = self::renderHeader();
// offer to load user preferences from localStorage
if (
$this->userPreferencesHandler->storageType === 'session'
&& ! isset($_SESSION['userprefs_autoload'])
) {
$loadUserPreferences = $this->userPreferences->autoloadGetHeader();
}
$menu = '';
if ($this->isMenuEnabled() && Current::$server > 0) {
$menu = $this->getMenu()->getDisplay();
}
$console = $this->console->getDisplay();
$messages = $this->getMessage();
$isLoggedIn = $this->dbi->isConnected();
$scripts->addFile('datetimepicker.js');
$scripts->addFile('validator-messages.js');
return [
'lang' => Current::$lang,
'allow_third_party_framing' => $this->config->config->AllowThirdPartyFraming,
'codemirror_enable' => $this->config->config->CodemirrorEnable,
'lint_enable' => $this->config->config->LintEnable,
'theme_path' => $theme->getPath(),
'server' => Current::$server,
'title' => $this->getPageTitle(),
'scripts' => $scripts->getDisplay(),
'body_id' => $this->bodyId,
'navigation' => $navigation ?? '',
'custom_header' => $customHeader,
'load_user_preferences' => $loadUserPreferences ?? '',
'show_hint' => $this->config->config->ShowHint,
'is_warnings_enabled' => $this->warningsEnabled,
'is_menu_enabled' => $this->isMenuEnabled(),
'is_logged_in' => $isLoggedIn,
'menu' => $menu,
'console' => $console,
'messages' => $messages,
'theme_color_mode' => $theme->getColorMode(),
'theme_color_modes' => $theme->getColorModes(),
'theme_id' => $theme->getId(),
'current_user' => $this->dbi->getCurrentUserAndHost(),
'is_mariadb' => $this->dbi->isMariaDB(),
];
}
/**
* Returns the message to be displayed at the top of
* the page, including the executed SQL query, if any.
*/
public function getMessage(): string
{
$retval = '';
$message = '';
if (Current::$message !== null) {
$message = Current::$message;
Current::$message = null;
} elseif (! empty($_REQUEST['message'])) {
$message = $_REQUEST['message'];
}
if ($message !== '') {
$retval .= Generator::getMessage($message);
}
return $retval;
}
/** @return array<string, string> */
public function getHttpHeaders(ClockInterface|null $clock = null): array
{
$headers = [
'Referrer-Policy' => 'same-origin',
'Content-Security-Policy' => $this->getCspHeader(),
/**
* Re-enable possible disabled XSS filters.
*
* @see https://developer.mozilla.org/docs/Web/HTTP/Headers/X-XSS-Protection
*/
'X-XSS-Protection' => '1; mode=block',
/**
* "nosniff", prevents Internet Explorer and Google Chrome from MIME-sniffing
* a response away from the declared content-type.
*
* @see https://developer.mozilla.org/docs/Web/HTTP/Headers/X-Content-Type-Options
*/
'X-Content-Type-Options' => 'nosniff',
/**
* Adobe cross-domain-policies.
*
* @see https://www.sentrium.co.uk/labs/application-security-101-http-headers
*/
'X-Permitted-Cross-Domain-Policies' => 'none',
/**
* Robots meta tag.
*
* @see https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag
*/
'X-Robots-Tag' => 'noindex, nofollow',
/**
* The HTTP Permissions-Policy header provides a mechanism to allow and deny
* the use of browser features in a document
* or within any <iframe> elements in the document.
*
* @see https://developer.mozilla.org/docs/Web/HTTP/Headers/Permissions-Policy
*/
'Permissions-Policy' => 'fullscreen=(self), interest-cohort=()',
];
$headers = array_merge($headers, Core::getNoCacheHeaders($clock ?? new Clock()));
/**
* A different Content-Type is set in {@see \PhpMyAdmin\Controllers\Transformation\WrapperController}.
*/
if (! $this->isTransformationWrapper) {
// Define the charset to be used
$headers['Content-Type'] = 'text/html; charset=utf-8';
}
return $headers;
}
/**
* If the page is missing the title, this function
* will set it to something reasonable
*/
public function getPageTitle(): string
{
if ($this->title === '') {
if (Current::$server > 0) {
if (Current::$table !== '') {
$tempTitle = $this->config->config->TitleTable;
} elseif (Current::$database !== '') {
$tempTitle = $this->config->config->TitleDatabase;
} elseif ($this->config->selectedServer['host'] !== '') {
$tempTitle = $this->config->config->TitleServer;
} else {
$tempTitle = $this->config->config->TitleDefault;
}
$this->title = htmlspecialchars(Util::expandUserString($this->dbi, $this->config, $tempTitle));
} else {
$this->title = 'phpMyAdmin';
}
}
return $this->title;
}
/** Get the Content-Security-Policy header */
private function getCspHeader(): string
{
$mapTileUrl = ' https://tile.openstreetmap.org';
$cspAllow = $this->config->config->CSPAllow === '' ? '' : ' ' . $this->config->config->CSPAllow;
$captchaUrl =
$this->config->config->CaptchaLoginPrivateKey === '' ||
$this->config->config->CaptchaLoginPublicKey === '' ||
$this->config->config->CaptchaApi === '' ||
$this->config->config->CaptchaRequestParam === '' ||
$this->config->config->CaptchaResponseParam === ''
? ''
: ' ' . $this->config->config->CaptchaCsp;
$csp = [
"default-src 'self'" . $captchaUrl . $cspAllow,
"img-src 'self' data:" . $captchaUrl . $cspAllow . $mapTileUrl,
"object-src 'none'",
"script-src 'self' 'unsafe-inline' 'unsafe-eval'" . $captchaUrl . $cspAllow,
"style-src 'self' 'unsafe-inline'" . $captchaUrl . $cspAllow,
];
// Prevent click-jacking by disabling inline-framing
if ($this->config->config->AllowThirdPartyFraming === 'sameorigin') {
$csp[] = "frame-ancestors 'self'";
} elseif ($this->config->config->AllowThirdPartyFraming !== true) {
$csp[] = "frame-ancestors 'none'";
}
return implode('; ', $csp) . ';';
}
private function getVariablesForJavaScript(): string
{
$maxInputVars = ini_get('max_input_vars');
$maxInputVarsValue = $maxInputVars === false || $maxInputVars === '' ? 'false' : (int) $maxInputVars;
return $this->template->render('javascript/variables', [
'first_day_of_calendar' => $this->config->config->FirstDayOfCalendar,
'max_input_vars' => $maxInputVarsValue,
]);
}
public function setIsTransformationWrapper(bool $isTransformationWrapper): void
{
$this->isTransformationWrapper = $isTransformationWrapper;
}
public function getConsole(): Console
{
return $this->console;
}
/**
* Renders user configured footer
*/
public static function renderHeader(): string
{
return Generator::renderCustom(CUSTOM_HEADER_FILE, 'pma_header');
}
}