Translated using Weblate (Estonian)
[phpmyadmin.git] / libraries / classes / Header.php
blob2ba9337b666078ce929be86f11e50a7f09b3c899
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Used to render the header of PMA's pages
6 * @package PhpMyAdmin
7 */
8 declare(strict_types=1);
10 namespace PhpMyAdmin;
12 use PhpMyAdmin\Navigation\Navigation;
14 /**
15 * Class used to output the HTTP and HTML headers
17 * @package PhpMyAdmin
19 class Header
21 /**
22 * Scripts instance
24 * @access private
25 * @var Scripts
27 private $_scripts;
28 /**
29 * PhpMyAdmin\Console instance
31 * @access private
32 * @var Console
34 private $_console;
35 /**
36 * Menu instance
38 * @access private
39 * @var Menu
41 private $_menu;
42 /**
43 * Whether to offer the option of importing user settings
45 * @access private
46 * @var bool
48 private $_userprefsOfferImport;
49 /**
50 * The page title
52 * @access private
53 * @var string
55 private $_title;
56 /**
57 * The value for the id attribute for the body tag
59 * @access private
60 * @var string
62 private $_bodyId;
63 /**
64 * Whether to show the top menu
66 * @access private
67 * @var bool
69 private $_menuEnabled;
70 /**
71 * Whether to show the warnings
73 * @access private
74 * @var bool
76 private $_warningsEnabled;
77 /**
78 * Whether the page is in 'print view' mode
80 * @access private
81 * @var bool
83 private $_isPrintView;
84 /**
85 * Whether we are servicing an ajax request.
87 * @access private
88 * @var bool
90 private $_isAjax;
91 /**
92 * Whether to display anything
94 * @access private
95 * @var bool
97 private $_isEnabled;
98 /**
99 * Whether the HTTP headers (and possibly some HTML)
100 * have already been sent to the browser
102 * @access private
103 * @var bool
105 private $_headerIsSent;
108 * @var UserPreferences
110 private $userPreferences;
113 * @var Template
115 private $template;
118 * @var Navigation
120 private $navigation;
123 * Creates a new class instance
125 public function __construct()
127 $this->template = new Template();
129 $this->_isEnabled = true;
130 $this->_isAjax = false;
131 $this->_bodyId = '';
132 $this->_title = '';
133 $this->_console = new Console();
134 $db = strlen($GLOBALS['db']) ? $GLOBALS['db'] : '';
135 $table = strlen($GLOBALS['table']) ? $GLOBALS['table'] : '';
136 $this->_menu = new Menu(
137 $db,
138 $table
140 $this->_menuEnabled = true;
141 $this->_warningsEnabled = true;
142 $this->_isPrintView = false;
143 $this->_scripts = new Scripts();
144 $this->_addDefaultScripts();
145 $this->_headerIsSent = false;
146 // if database storage for user preferences is transient,
147 // offer to load exported settings from localStorage
148 // (detection will be done in JavaScript)
149 $this->_userprefsOfferImport = false;
150 if ($GLOBALS['PMA_Config']->get('user_preferences') == 'session'
151 && ! isset($_SESSION['userprefs_autoload'])
153 $this->_userprefsOfferImport = true;
156 $this->userPreferences = new UserPreferences();
157 $this->navigation = new Navigation(
158 $this->template,
159 new Relation($GLOBALS['dbi']),
160 $GLOBALS['dbi']
165 * Loads common scripts
167 * @return void
169 private function _addDefaultScripts(): void
171 // Localised strings
172 $this->_scripts->addFile('vendor/jquery/jquery.min.js');
173 $this->_scripts->addFile('vendor/jquery/jquery-migrate.js');
174 $this->_scripts->addFile('whitelist.php');
175 $this->_scripts->addFile('vendor/sprintf.js');
176 $this->_scripts->addFile('ajax.js');
177 $this->_scripts->addFile('keyhandler.js');
178 $this->_scripts->addFile('vendor/bootstrap/bootstrap.bundle.min.js');
179 $this->_scripts->addFile('vendor/jquery/jquery-ui.min.js');
180 $this->_scripts->addFile('vendor/js.cookie.js');
181 $this->_scripts->addFile('vendor/jquery/jquery.mousewheel.js');
182 $this->_scripts->addFile('vendor/jquery/jquery.event.drag-2.2.js');
183 $this->_scripts->addFile('vendor/jquery/jquery.validate.js');
184 $this->_scripts->addFile('vendor/jquery/jquery-ui-timepicker-addon.js');
185 $this->_scripts->addFile('vendor/jquery/jquery.ba-hashchange-1.3.js');
186 $this->_scripts->addFile('vendor/jquery/jquery.debounce-1.0.5.js');
187 $this->_scripts->addFile('menu_resizer.js');
189 // Cross-framing protection
190 if ($GLOBALS['cfg']['AllowThirdPartyFraming'] === false) {
191 $this->_scripts->addFile('cross_framing_protection.js');
194 $this->_scripts->addFile('rte.js');
195 if ($GLOBALS['cfg']['SendErrorReports'] !== 'never') {
196 $this->_scripts->addFile('vendor/tracekit.js');
197 $this->_scripts->addFile('error_report.js');
200 // Here would not be a good place to add CodeMirror because
201 // the user preferences have not been merged at this point
203 $this->_scripts->addFile('messages.php', ['l' => $GLOBALS['lang']]);
204 $this->_scripts->addFile('config.js');
205 $this->_scripts->addFile('doclinks.js');
206 $this->_scripts->addFile('functions.js');
207 $this->_scripts->addFile('navigation.js');
208 $this->_scripts->addFile('indexes.js');
209 $this->_scripts->addFile('common.js');
210 $this->_scripts->addFile('page_settings.js');
211 if ($GLOBALS['cfg']['enable_drag_drop_import'] === true) {
212 $this->_scripts->addFile('drag_drop_import.js');
214 if (! $GLOBALS['PMA_Config']->get('DisableShortcutKeys')) {
215 $this->_scripts->addFile('shortcuts_handler.js');
217 $this->_scripts->addCode($this->getJsParamsCode());
221 * Returns, as an array, a list of parameters
222 * used on the client side
224 * @return array
226 public function getJsParams(): array
228 $db = strlen($GLOBALS['db']) ? $GLOBALS['db'] : '';
229 $table = strlen($GLOBALS['table']) ? $GLOBALS['table'] : '';
230 $pftext = isset($_SESSION['tmpval']['pftext'])
231 ? $_SESSION['tmpval']['pftext'] : '';
233 $params = [
234 'common_query' => Url::getCommonRaw(),
235 'opendb_url' => Util::getScriptNameForOption(
236 $GLOBALS['cfg']['DefaultTabDatabase'],
237 'database'
239 'lang' => $GLOBALS['lang'],
240 'server' => $GLOBALS['server'],
241 'table' => $table,
242 'db' => $db,
243 'token' => $_SESSION[' PMA_token '],
244 'text_dir' => $GLOBALS['text_dir'],
245 'show_databases_navigation_as_tree' => $GLOBALS['cfg']['ShowDatabasesNavigationAsTree'],
246 'pma_text_default_tab' => Util::getTitleForTarget(
247 $GLOBALS['cfg']['DefaultTabTable']
249 'pma_text_left_default_tab' => Util::getTitleForTarget(
250 $GLOBALS['cfg']['NavigationTreeDefaultTabTable']
252 'pma_text_left_default_tab2' => Util::getTitleForTarget(
253 $GLOBALS['cfg']['NavigationTreeDefaultTabTable2']
255 'LimitChars' => $GLOBALS['cfg']['LimitChars'],
256 'pftext' => $pftext,
257 'confirm' => $GLOBALS['cfg']['Confirm'],
258 'LoginCookieValidity' => $GLOBALS['cfg']['LoginCookieValidity'],
259 'session_gc_maxlifetime' => (int) ini_get('session.gc_maxlifetime'),
260 'logged_in' => isset($GLOBALS['dbi']) ? $GLOBALS['dbi']->isUserType('logged') : false,
261 'is_https' => $GLOBALS['PMA_Config']->isHttps(),
262 'rootPath' => $GLOBALS['PMA_Config']->getRootPath(),
263 'arg_separator' => Url::getArgSeparator(),
264 'PMA_VERSION' => PMA_VERSION,
266 if (isset($GLOBALS['cfg']['Server'], $GLOBALS['cfg']['Server']['auth_type'])) {
267 $params['auth_type'] = $GLOBALS['cfg']['Server']['auth_type'];
268 if (isset($GLOBALS['cfg']['Server']['user'])) {
269 $params['user'] = $GLOBALS['cfg']['Server']['user'];
273 return $params;
277 * Returns, as a string, a list of parameters
278 * used on the client side
280 * @return string
282 public function getJsParamsCode(): string
284 $params = $this->getJsParams();
285 foreach ($params as $key => $value) {
286 if (is_bool($value)) {
287 $params[$key] = $key . ':' . ($value ? 'true' : 'false') . '';
288 } else {
289 $params[$key] = $key . ':"' . Sanitize::escapeJsString($value) . '"';
292 return 'CommonParams.setAll({' . implode(',', $params) . '});';
296 * Disables the rendering of the header
298 * @return void
300 public function disable(): void
302 $this->_isEnabled = false;
306 * Set the ajax flag to indicate whether
307 * we are servicing an ajax request
309 * @param bool $isAjax Whether we are servicing an ajax request
311 * @return void
313 public function setAjax(bool $isAjax): void
315 $this->_isAjax = $isAjax;
316 $this->_console->setAjax($isAjax);
320 * Returns the Scripts object
322 * @return Scripts object
324 public function getScripts(): Scripts
326 return $this->_scripts;
330 * Returns the Menu object
332 * @return Menu object
334 public function getMenu(): Menu
336 return $this->_menu;
340 * Setter for the ID attribute in the BODY tag
342 * @param string $id Value for the ID attribute
344 * @return void
346 public function setBodyId(string $id): void
348 $this->_bodyId = htmlspecialchars($id);
352 * Setter for the title of the page
354 * @param string $title New title
356 * @return void
358 public function setTitle(string $title): void
360 $this->_title = htmlspecialchars($title);
364 * Disables the display of the top menu
366 * @return void
368 public function disableMenuAndConsole(): void
370 $this->_menuEnabled = false;
371 $this->_console->disable();
375 * Disables the display of the top menu
377 * @return void
379 public function disableWarnings(): void
381 $this->_warningsEnabled = false;
385 * Turns on 'print view' mode
387 * @return void
389 public function enablePrintView(): void
391 $this->disableMenuAndConsole();
392 $this->setTitle(__('Print view') . ' - phpMyAdmin ' . PMA_VERSION);
393 $this->_isPrintView = true;
397 * Generates the header
399 * @return string The header
401 public function getDisplay(): string
403 if (! $this->_headerIsSent) {
404 if (! $this->_isAjax && $this->_isEnabled) {
405 $this->sendHttpHeaders();
407 $baseDir = defined('PMA_PATH_TO_BASEDIR') ? PMA_PATH_TO_BASEDIR : '';
408 $uniqueValue = $GLOBALS['PMA_Config']->getThemeUniqueValue();
409 $themePath = $GLOBALS['pmaThemePath'];
410 $version = self::getVersionParameter();
412 // The user preferences have been merged at this point
413 // so we can conditionally add CodeMirror
414 if ($GLOBALS['cfg']['CodemirrorEnable']) {
415 $this->_scripts->addFile('vendor/codemirror/lib/codemirror.js');
416 $this->_scripts->addFile('vendor/codemirror/mode/sql/sql.js');
417 $this->_scripts->addFile('vendor/codemirror/addon/runmode/runmode.js');
418 $this->_scripts->addFile('vendor/codemirror/addon/hint/show-hint.js');
419 $this->_scripts->addFile('vendor/codemirror/addon/hint/sql-hint.js');
420 if ($GLOBALS['cfg']['LintEnable']) {
421 $this->_scripts->addFile('vendor/codemirror/addon/lint/lint.js');
422 $this->_scripts->addFile(
423 'codemirror/addon/lint/sql-lint.js'
427 $this->_scripts->addCode(
428 'ConsoleEnterExecutes='
429 . ($GLOBALS['cfg']['ConsoleEnterExecutes'] ? 'true' : 'false')
431 $this->_scripts->addFiles($this->_console->getScripts());
432 if ($this->_userprefsOfferImport) {
433 $this->_scripts->addFile('config.js');
436 if ($this->_menuEnabled && $GLOBALS['server'] > 0) {
437 $navigation = $this->navigation->getDisplay();
440 $customHeader = Config::renderHeader();
442 // offer to load user preferences from localStorage
443 if ($this->_userprefsOfferImport) {
444 $loadUserPreferences = $this->userPreferences->autoloadGetHeader();
447 if ($this->_menuEnabled && $GLOBALS['server'] > 0) {
448 $menu = $this->_menu->getDisplay();
450 $console = $this->_console->getDisplay();
451 $messages = $this->getMessage();
453 if ($this->_isEnabled && empty($_REQUEST['recent_table'])) {
454 $recentTable = $this->_addRecentTable(
455 $GLOBALS['db'],
456 $GLOBALS['table']
459 return $this->template->render('header', [
460 'is_ajax' => $this->_isAjax,
461 'is_enabled' => $this->_isEnabled,
462 'lang' => $GLOBALS['lang'],
463 'allow_third_party_framing' => $GLOBALS['cfg']['AllowThirdPartyFraming'],
464 'is_print_view' => $this->_isPrintView,
465 'base_dir' => $baseDir ?? '',
466 'unique_value' => $uniqueValue ?? '',
467 'theme_path' => $themePath ?? '',
468 'version' => $version ?? '',
469 'text_dir' => $GLOBALS['text_dir'],
470 'server' => $GLOBALS['server'] ?? null,
471 'title' => $this->getPageTitle(),
472 'scripts' => $this->_scripts->getDisplay(),
473 'body_id' => $this->_bodyId,
474 'navigation' => $navigation ?? '',
475 'custom_header' => $customHeader ?? '',
476 'load_user_preferences' => $loadUserPreferences ?? '',
477 'show_hint' => $GLOBALS['cfg']['ShowHint'],
478 'is_warnings_enabled' => $this->_warningsEnabled,
479 'is_menu_enabled' => $this->_menuEnabled,
480 'menu' => $menu ?? '',
481 'console' => $console ?? '',
482 'messages' => $messages ?? '',
483 'has_recent_table' => empty($_REQUEST['recent_table']),
484 'recent_table' => $recentTable ?? '',
487 return '';
491 * Returns the message to be displayed at the top of
492 * the page, including the executed SQL query, if any.
494 * @return string
496 public function getMessage(): string
498 $retval = '';
499 $message = '';
500 if (! empty($GLOBALS['message'])) {
501 $message = $GLOBALS['message'];
502 unset($GLOBALS['message']);
503 } elseif (! empty($_REQUEST['message'])) {
504 $message = $_REQUEST['message'];
506 if (! empty($message)) {
507 if (isset($GLOBALS['buffer_message'])) {
508 $buffer_message = $GLOBALS['buffer_message'];
510 $retval .= Util::getMessage($message);
511 if (isset($buffer_message)) {
512 $GLOBALS['buffer_message'] = $buffer_message;
515 return $retval;
519 * Sends out the HTTP headers
521 * @return void
523 public function sendHttpHeaders(): void
525 if (defined('TESTSUITE')) {
526 return;
528 $map_tile_urls = ' *.tile.openstreetmap.org';
531 * Sends http headers
533 $GLOBALS['now'] = gmdate('D, d M Y H:i:s') . ' GMT';
534 if (! empty($GLOBALS['cfg']['CaptchaLoginPrivateKey'])
535 && ! empty($GLOBALS['cfg']['CaptchaLoginPublicKey'])
537 $captcha_url
538 = ' https://apis.google.com https://www.google.com/recaptcha/'
539 . ' https://www.gstatic.com/recaptcha/ https://ssl.gstatic.com/ ';
540 } else {
541 $captcha_url = '';
543 /* Prevent against ClickJacking by disabling framing */
544 if (strtolower((string) $GLOBALS['cfg']['AllowThirdPartyFraming']) === 'sameorigin') {
545 header(
546 'X-Frame-Options: SAMEORIGIN'
548 } elseif ($GLOBALS['cfg']['AllowThirdPartyFraming'] !== true) {
549 header(
550 'X-Frame-Options: DENY'
553 header(
554 'Referrer-Policy: no-referrer'
556 header(
557 "Content-Security-Policy: default-src 'self' "
558 . $captcha_url
559 . $GLOBALS['cfg']['CSPAllow'] . ';'
560 . "script-src 'self' 'unsafe-inline' 'unsafe-eval' "
561 . $captcha_url
562 . $GLOBALS['cfg']['CSPAllow'] . ';'
563 . "style-src 'self' 'unsafe-inline' "
564 . $captcha_url
565 . $GLOBALS['cfg']['CSPAllow']
566 . ";"
567 . "img-src 'self' data: "
568 . $GLOBALS['cfg']['CSPAllow']
569 . $map_tile_urls
570 . $captcha_url
571 . ";"
572 . "object-src 'none';"
574 header(
575 "X-Content-Security-Policy: default-src 'self' "
576 . $captcha_url
577 . $GLOBALS['cfg']['CSPAllow'] . ';'
578 . "options inline-script eval-script;"
579 . "referrer no-referrer;"
580 . "img-src 'self' data: "
581 . $GLOBALS['cfg']['CSPAllow']
582 . $map_tile_urls
583 . $captcha_url
584 . ";"
585 . "object-src 'none';"
587 header(
588 "X-WebKit-CSP: default-src 'self' "
589 . $captcha_url
590 . $GLOBALS['cfg']['CSPAllow'] . ';'
591 . "script-src 'self' "
592 . $captcha_url
593 . $GLOBALS['cfg']['CSPAllow']
594 . " 'unsafe-inline' 'unsafe-eval';"
595 . "referrer no-referrer;"
596 . "style-src 'self' 'unsafe-inline' "
597 . $captcha_url
598 . ';'
599 . "img-src 'self' data: "
600 . $GLOBALS['cfg']['CSPAllow']
601 . $map_tile_urls
602 . $captcha_url
603 . ";"
604 . "object-src 'none';"
606 // Re-enable possible disabled XSS filters
607 // see https://www.owasp.org/index.php/List_of_useful_HTTP_headers
608 header(
609 'X-XSS-Protection: 1; mode=block'
611 // "nosniff", prevents Internet Explorer and Google Chrome from MIME-sniffing
612 // a response away from the declared content-type
613 // see https://www.owasp.org/index.php/List_of_useful_HTTP_headers
614 header(
615 'X-Content-Type-Options: nosniff'
617 // Adobe cross-domain-policies
618 // see https://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html
619 header(
620 'X-Permitted-Cross-Domain-Policies: none'
622 // Robots meta tag
623 // see https://developers.google.com/webmasters/control-crawl-index/docs/robots_meta_tag
624 header(
625 'X-Robots-Tag: noindex, nofollow'
627 Core::noCacheHeader();
628 if (! defined('IS_TRANSFORMATION_WRAPPER')) {
629 // Define the charset to be used
630 header('Content-Type: text/html; charset=utf-8');
632 $this->_headerIsSent = true;
636 * If the page is missing the title, this function
637 * will set it to something reasonable
639 * @return string
641 public function getPageTitle(): string
643 if (strlen($this->_title) == 0) {
644 if ($GLOBALS['server'] > 0) {
645 if (strlen($GLOBALS['table'])) {
646 $temp_title = $GLOBALS['cfg']['TitleTable'];
647 } elseif (strlen($GLOBALS['db'])) {
648 $temp_title = $GLOBALS['cfg']['TitleDatabase'];
649 } elseif (strlen($GLOBALS['cfg']['Server']['host'])) {
650 $temp_title = $GLOBALS['cfg']['TitleServer'];
651 } else {
652 $temp_title = $GLOBALS['cfg']['TitleDefault'];
654 $this->_title = htmlspecialchars(
655 Util::expandUserString($temp_title)
657 } else {
658 $this->_title = 'phpMyAdmin';
661 return $this->_title;
665 * Add recently used table and reload the navigation.
667 * @param string $db Database name where the table is located.
668 * @param string $table The table name
670 * @return string
672 private function _addRecentTable(string $db, string $table): string
674 $retval = '';
675 if ($this->_menuEnabled
676 && strlen($table) > 0
677 && $GLOBALS['cfg']['NumRecentTables'] > 0
679 $tmp_result = RecentFavoriteTable::getInstance('recent')->add(
680 $db,
681 $table
683 if ($tmp_result === true) {
684 $retval = RecentFavoriteTable::getHtmlUpdateRecentTables();
685 } else {
686 $error = $tmp_result;
687 $retval = $error->getDisplay();
690 return $retval;
694 * Returns the phpMyAdmin version to be appended to the url to avoid caching
695 * between versions
697 * @return string urlenocded pma version as a parameter
699 public static function getVersionParameter(): string
701 return "v=" . urlencode(PMA_VERSION);