2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Used to render the header of PMA's pages
8 declare(strict_types
=1);
12 use PhpMyAdmin\Navigation\Navigation
;
15 * Class used to output the HTTP and HTML headers
29 * PhpMyAdmin\Console instance
43 * Whether to offer the option of importing user settings
48 private $_userprefsOfferImport;
57 * The value for the id attribute for the body tag
64 * Whether to show the top menu
69 private $_menuEnabled;
71 * Whether to show the warnings
76 private $_warningsEnabled;
78 * Whether the page is in 'print view' mode
83 private $_isPrintView;
85 * Whether we are servicing an ajax request.
92 * Whether to display anything
99 * Whether the HTTP headers (and possibly some HTML)
100 * have already been sent to the browser
105 private $_headerIsSent;
108 * @var UserPreferences
110 private $userPreferences;
123 * Creates a new class instance
125 public function __construct()
127 $this->template
= new Template();
129 $this->_isEnabled
= true;
130 $this->_isAjax
= false;
133 $this->_console
= new Console();
134 $db = strlen($GLOBALS['db']) ?
$GLOBALS['db'] : '';
135 $table = strlen($GLOBALS['table']) ?
$GLOBALS['table'] : '';
136 $this->_menu
= new Menu(
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(
159 new Relation($GLOBALS['dbi']),
165 * Loads common scripts
169 private function _addDefaultScripts(): void
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
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'] : '';
234 'common_query' => Url
::getCommonRaw(),
235 'opendb_url' => Util
::getScriptNameForOption(
236 $GLOBALS['cfg']['DefaultTabDatabase'],
239 'lang' => $GLOBALS['lang'],
240 'server' => $GLOBALS['server'],
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'],
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'];
277 * Returns, as a string, a list of parameters
278 * used on the client side
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') . '';
289 $params[$key] = $key . ':"' . Sanitize
::escapeJsString($value) . '"';
292 return 'CommonParams.setAll({' . implode(',', $params) . '});';
296 * Disables the rendering of the header
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
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
340 * Setter for the ID attribute in the BODY tag
342 * @param string $id Value for the ID attribute
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
358 public function setTitle(string $title): void
360 $this->_title
= htmlspecialchars($title);
364 * Disables the display of the top menu
368 public function disableMenuAndConsole(): void
370 $this->_menuEnabled
= false;
371 $this->_console
->disable();
375 * Disables the display of the top menu
379 public function disableWarnings(): void
381 $this->_warningsEnabled
= false;
385 * Turns on 'print view' mode
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(
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 ??
'',
491 * Returns the message to be displayed at the top of
492 * the page, including the executed SQL query, if any.
496 public function getMessage(): string
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;
519 * Sends out the HTTP headers
523 public function sendHttpHeaders(): void
525 if (defined('TESTSUITE')) {
528 $map_tile_urls = ' *.tile.openstreetmap.org';
533 $GLOBALS['now'] = gmdate('D, d M Y H:i:s') . ' GMT';
534 if (! empty($GLOBALS['cfg']['CaptchaLoginPrivateKey'])
535 && ! empty($GLOBALS['cfg']['CaptchaLoginPublicKey'])
538 = ' https://apis.google.com https://www.google.com/recaptcha/'
539 . ' https://www.gstatic.com/recaptcha/ https://ssl.gstatic.com/ ';
543 /* Prevent against ClickJacking by disabling framing */
544 if (strtolower((string) $GLOBALS['cfg']['AllowThirdPartyFraming']) === 'sameorigin') {
546 'X-Frame-Options: SAMEORIGIN'
548 } elseif ($GLOBALS['cfg']['AllowThirdPartyFraming'] !== true) {
550 'X-Frame-Options: DENY'
554 'Referrer-Policy: no-referrer'
557 "Content-Security-Policy: default-src 'self' "
559 . $GLOBALS['cfg']['CSPAllow'] . ';'
560 . "script-src 'self' 'unsafe-inline' 'unsafe-eval' "
562 . $GLOBALS['cfg']['CSPAllow'] . ';'
563 . "style-src 'self' 'unsafe-inline' "
565 . $GLOBALS['cfg']['CSPAllow']
567 . "img-src 'self' data: "
568 . $GLOBALS['cfg']['CSPAllow']
572 . "object-src 'none';"
575 "X-Content-Security-Policy: default-src 'self' "
577 . $GLOBALS['cfg']['CSPAllow'] . ';'
578 . "options inline-script eval-script;"
579 . "referrer no-referrer;"
580 . "img-src 'self' data: "
581 . $GLOBALS['cfg']['CSPAllow']
585 . "object-src 'none';"
588 "X-WebKit-CSP: default-src 'self' "
590 . $GLOBALS['cfg']['CSPAllow'] . ';'
591 . "script-src 'self' "
593 . $GLOBALS['cfg']['CSPAllow']
594 . " 'unsafe-inline' 'unsafe-eval';"
595 . "referrer no-referrer;"
596 . "style-src 'self' 'unsafe-inline' "
599 . "img-src 'self' data: "
600 . $GLOBALS['cfg']['CSPAllow']
604 . "object-src 'none';"
606 // Re-enable possible disabled XSS filters
607 // see https://www.owasp.org/index.php/List_of_useful_HTTP_headers
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
615 'X-Content-Type-Options: nosniff'
617 // Adobe cross-domain-policies
618 // see https://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html
620 'X-Permitted-Cross-Domain-Policies: none'
623 // see https://developers.google.com/webmasters/control-crawl-index/docs/robots_meta_tag
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
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'];
652 $temp_title = $GLOBALS['cfg']['TitleDefault'];
654 $this->_title
= htmlspecialchars(
655 Util
::expandUserString($temp_title)
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
672 private function _addRecentTable(string $db, string $table): string
675 if ($this->_menuEnabled
676 && strlen($table) > 0
677 && $GLOBALS['cfg']['NumRecentTables'] > 0
679 $tmp_result = RecentFavoriteTable
::getInstance('recent')->add(
683 if ($tmp_result === true) {
684 $retval = RecentFavoriteTable
::getHtmlUpdateRecentTables();
686 $error = $tmp_result;
687 $retval = $error->getDisplay();
694 * Returns the phpMyAdmin version to be appended to the url to avoid caching
697 * @return string urlenocded pma version as a parameter
699 public static function getVersionParameter(): string
701 return "v=" . urlencode(PMA_VERSION
);