2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Used to render the header of PMA's pages
8 if (! defined('PHPMYADMIN')) {
12 require_once 'libraries/Scripts.class.php';
13 require_once 'libraries/RecentFavoriteTable.class.php';
14 require_once 'libraries/Menu.class.php';
15 require_once 'libraries/Console.class.php';
16 require_once 'libraries/navigation/Navigation.class.php';
17 require_once 'libraries/url_generating.lib.php';
21 * Class used to output the HTTP and HTML headers
28 * PMA_Scripts instance
35 * PMA_Console instance
49 * Whether to offer the option of importing user settings
54 private $_userprefsOfferImport;
63 * The value for the id attribute for the body tag
70 * Whether to show the top menu
75 private $_menuEnabled;
77 * Whether to show the warnings
82 private $_warningsEnabled;
84 * Whether the page is in 'print view' mode
89 private $_isPrintView;
91 * Whether we are servicing an ajax request.
92 * We can't simply use $GLOBALS['is_ajax_request']
93 * here since it may have not been initialised yet.
100 * Whether to display anything
107 * Whether the HTTP headers (and possibly some HTML)
108 * have already been sent to the browser
113 private $_headerIsSent;
116 * Creates a new class instance
118 public function __construct()
120 $this->_isEnabled
= true;
121 $this->_isAjax
= false;
124 $this->_console
= new PMA_Console();
125 $db = ! empty($GLOBALS['db']) ?
$GLOBALS['db'] : '';
126 $table = ! empty($GLOBALS['table']) ?
$GLOBALS['table'] : '';
127 $this->_menu
= new PMA_Menu(
132 $this->_menuEnabled
= true;
133 $this->_warningsEnabled
= true;
134 $this->_isPrintView
= false;
135 $this->_scripts
= new PMA_Scripts();
136 $this->_addDefaultScripts();
137 $this->_headerIsSent
= false;
138 // if database storage for user preferences is transient,
139 // offer to load exported settings from localStorage
140 // (detection will be done in JavaScript)
141 $this->_userprefsOfferImport
= false;
142 if ($GLOBALS['PMA_Config']->get('user_preferences') == 'session'
143 && ! isset($_SESSION['userprefs_autoload'])
145 $this->_userprefsOfferImport
= true;
150 * Loads common scripts
154 private function _addDefaultScripts()
157 $params = array('lang' => $GLOBALS['lang']);
158 if (isset($GLOBALS['db'])) {
159 $params['db'] = $GLOBALS['db'];
161 $this->_scripts
->addFile('jquery/jquery-1.11.1.min.js');
162 $this->_scripts
->addFile(
163 'whitelist.php' . PMA_URL_getCommon($params), false, true
165 $this->_scripts
->addFile('sprintf.js');
166 $this->_scripts
->addFile('ajax.js');
167 $this->_scripts
->addFile('keyhandler.js');
168 $this->_scripts
->addFile('jquery/jquery-ui-1.11.2.min.js');
169 $this->_scripts
->addFile('jquery/jquery.cookie.js');
170 $this->_scripts
->addFile('jquery/jquery.mousewheel.js');
171 $this->_scripts
->addFile('jquery/jquery.event.drag-2.2.js');
172 $this->_scripts
->addFile('jquery/jquery-ui-timepicker-addon.js');
173 $this->_scripts
->addFile('jquery/jquery.ba-hashchange-1.3.js');
174 $this->_scripts
->addFile('jquery/jquery.debounce-1.0.5.js');
175 $this->_scripts
->addFile('menu-resizer.js');
177 // Cross-framing protection
178 if ($GLOBALS['cfg']['AllowThirdPartyFraming'] === false) {
179 $this->_scripts
->addFile('cross_framing_protection.js');
182 $this->_scripts
->addFile('rte.js');
183 if ($GLOBALS['cfg']['SendErrorReports'] !== 'never') {
184 $this->_scripts
->addFile('tracekit/tracekit.js');
185 $this->_scripts
->addFile('error_report.js');
188 // Here would not be a good place to add CodeMirror because
189 // the user preferences have not been merged at this point
191 $this->_scripts
->addFile('messages.php' . PMA_URL_getCommon($params));
192 // Append the theme id to this url to invalidate
193 // the cache on a theme change. Though this might be
194 // unavailable for fatal errors.
195 if (isset($_SESSION['PMA_Theme'])) {
196 $theme_id = urlencode($_SESSION['PMA_Theme']->getId());
198 $theme_id = 'default';
200 $this->_scripts
->addFile(
201 'get_image.js.php?theme=' . $theme_id
203 $this->_scripts
->addFile('doclinks.js');
204 $this->_scripts
->addFile('functions.js');
205 $this->_scripts
->addFile('navigation.js');
206 $this->_scripts
->addFile('indexes.js');
207 $this->_scripts
->addFile('common.js');
208 $this->_scripts
->addFile('config.js');
209 $this->_scripts
->addFile('page_settings.js');
210 $this->_scripts
->addCode($this->getJsParamsCode());
214 * Returns, as an array, a list of parameters
215 * used on the client side
219 public function getJsParams()
221 $db = ! empty($GLOBALS['db']) ?
$GLOBALS['db'] : '';
222 $table = ! empty($GLOBALS['table']) ?
$GLOBALS['table'] : '';
223 $pftext = ! empty($_SESSION['tmpval']['pftext'])
224 ?
$_SESSION['tmpval']['pftext'] : '';
226 // not sure when this happens, but it happens
227 if (! isset($GLOBALS['collation_connection'])) {
228 $GLOBALS['collation_connection'] = 'utf8_general_ci';
232 'common_query' => PMA_URL_getCommon(array(), 'text'),
233 'opendb_url' => PMA_Util
::getScriptNameForOption(
234 $GLOBALS['cfg']['DefaultTabDatabase'], 'database'
236 'safari_browser' => PMA_USR_BROWSER_AGENT
== 'SAFARI' ?
1 : 0,
237 'collation_connection' => $GLOBALS['collation_connection'],
238 'lang' => $GLOBALS['lang'],
239 'server' => $GLOBALS['server'],
242 'token' => $_SESSION[' PMA_token '],
243 'text_dir' => $GLOBALS['text_dir'],
244 'show_databases_navigation_as_tree'=> $GLOBALS['cfg']['ShowDatabasesNavigationAsTree'],
245 'pma_absolute_uri' => $GLOBALS['cfg']['PmaAbsoluteUri'],
246 'pma_text_default_tab' => PMA_Util
::getTitleForTarget(
247 $GLOBALS['cfg']['DefaultTabTable']
249 'pma_text_left_default_tab' => PMA_Util
::getTitleForTarget(
250 $GLOBALS['cfg']['NavigationTreeDefaultTabTable']
252 'pma_text_left_default_tab2' => PMA_Util
::getTitleForTarget(
253 $GLOBALS['cfg']['NavigationTreeDefaultTabTable2']
255 'LimitChars' => $GLOBALS['cfg']['LimitChars'],
257 'confirm' => $GLOBALS['cfg']['Confirm'],
258 'LoginCookieValidity' => $GLOBALS['cfg']['LoginCookieValidity'],
259 'logged_in' => isset($GLOBALS['userlink']) ?
true : false,
260 'PMA_VERSION' => PMA_VERSION
262 if (isset($GLOBALS['cfg']['Server'])
263 && isset($GLOBALS['cfg']['Server']['auth_type'])
265 $params['auth_type'] = $GLOBALS['cfg']['Server']['auth_type'];
272 * Returns, as a string, a list of parameters
273 * used on the client side
277 public function getJsParamsCode()
279 $params = $this->getJsParams();
280 foreach ($params as $key => $value) {
281 $params[$key] = $key . ':"' . PMA_escapeJsString($value) . '"';
283 return 'PMA_commonParams.setAll({' . implode(',', $params) . '});';
287 * Disables the rendering of the header
291 public function disable()
293 $this->_isEnabled
= false;
297 * Set the ajax flag to indicate whether
298 * we are servicing an ajax request
300 * @param bool $isAjax Whether we are servicing an ajax request
304 public function setAjax($isAjax)
306 $this->_isAjax
= !!$isAjax;
307 $this->_console
->setAjax($isAjax);
311 * Returns the PMA_Scripts object
313 * @return PMA_Scripts object
315 public function getScripts()
317 return $this->_scripts
;
321 * Returns the PMA_Menu object
323 * @return PMA_Menu object
325 public function getMenu()
331 * Setter for the ID attribute in the BODY tag
333 * @param string $id Value for the ID attribute
337 public function setBodyId($id)
339 $this->_bodyId
= htmlspecialchars($id);
343 * Setter for the title of the page
345 * @param string $title New title
349 public function setTitle($title)
351 $this->_title
= htmlspecialchars($title);
355 * Disables the display of the top menu
359 public function disableMenuAndConsole()
361 $this->_menuEnabled
= false;
362 $this->_console
->disable();
366 * Disables the display of the top menu
370 public function disableWarnings()
372 $this->_warningsEnabled
= false;
376 * Turns on 'print view' mode
380 public function enablePrintView()
382 $this->disableMenuAndConsole();
383 $this->setTitle(__('Print view') . ' - phpMyAdmin ' . PMA_VERSION
);
384 $this->_isPrintView
= true;
388 * Generates the header
390 * @return string The header
392 public function getDisplay()
395 if (! $this->_headerIsSent
) {
396 if (! $this->_isAjax
&& $this->_isEnabled
) {
397 $this->sendHttpHeaders();
398 $retval .= $this->_getHtmlStart();
399 $retval .= $this->_getMetaTags();
400 $retval .= $this->_getLinkTags();
401 $retval .= $this->getTitleTag();
403 // The user preferences have been merged at this point
404 // so we can conditionally add CodeMirror
405 if ($GLOBALS['cfg']['CodemirrorEnable']) {
406 $this->_scripts
->addFile('codemirror/lib/codemirror.js');
407 $this->_scripts
->addFile('codemirror/mode/sql/sql.js');
408 $this->_scripts
->addFile('codemirror/addon/runmode/runmode.js');
409 $this->_scripts
->addFile('codemirror/addon/hint/show-hint.js');
410 $this->_scripts
->addFile('codemirror/addon/hint/sql-hint.js');
411 if ($GLOBALS['cfg']['LintEnable']) {
412 $this->_scripts
->addFile('codemirror/addon/lint/lint.js');
413 $this->_scripts
->addFile(
414 'codemirror/addon/lint/sql-lint.js'
418 $this->_scripts
->addCode(
419 'ConsoleEnterExecutes='
420 . ($GLOBALS['cfg']['ConsoleEnterExecutes'] ?
'true' : 'false')
422 $this->_scripts
->addFiles($this->_console
->getScripts());
423 if ($this->_userprefsOfferImport
) {
424 $this->_scripts
->addFile('config.js');
426 $retval .= $this->_scripts
->getDisplay();
427 $retval .= '<noscript>';
428 $retval .= '<style>html{display:block}</style>';
429 $retval .= '</noscript>';
430 $retval .= $this->_getBodyStart();
431 if ($this->_menuEnabled
&& $GLOBALS['server'] > 0) {
432 $nav = new PMA_Navigation();
433 $retval .= $nav->getDisplay();
435 // Include possible custom headers
436 if (file_exists(CUSTOM_HEADER_FILE
)) {
437 $retval .= '<div id="pma_header">';
439 include CUSTOM_HEADER_FILE
;
440 $retval .= ob_get_contents();
444 // offer to load user preferences from localStorage
445 if ($this->_userprefsOfferImport
) {
446 include_once './libraries/user_preferences.lib.php';
447 $retval .= PMA_userprefsAutoloadGetHeader();
449 // pass configuration for hint tooltip display
450 // (to be used by PMA_tooltip() in js/functions.js)
451 if (! $GLOBALS['cfg']['ShowHint']) {
452 $retval .= '<span id="no_hint" class="hide"></span>';
454 $retval .= $this->_getWarnings();
455 if ($this->_menuEnabled
&& $GLOBALS['server'] > 0) {
456 $retval .= $this->_menu
->getDisplay();
457 $retval .= '<span id="page_nav_icons">';
458 $retval .= '<span id="lock_page_icon"></span>';
459 $retval .= '<span id="page_settings_icon">'
460 . PMA_Util
::getImage(
462 __('Page-related settings')
466 '<a id="goto_pagetop" href="#">%s</a>',
469 __('Click on the bar to scroll to top of page')
472 $retval .= '</span>';
474 $retval .= $this->_console
->getDisplay();
475 $retval .= '<div id="page_content">';
476 $retval .= $this->getMessage();
478 if ($this->_isEnabled
&& empty($_REQUEST['recent_table'])) {
479 $retval .= $this->_addRecentTable(
489 * Returns the message to be displayed at the top of
490 * the page, including the executed SQL query, if any.
494 public function getMessage()
498 if (! empty($GLOBALS['message'])) {
499 $message = $GLOBALS['message'];
500 unset($GLOBALS['message']);
501 } else if (! empty($_REQUEST['message'])) {
502 $message = $_REQUEST['message'];
504 if (! empty($message)) {
505 if (isset($GLOBALS['buffer_message'])) {
506 $buffer_message = $GLOBALS['buffer_message'];
508 $retval .= PMA_Util
::getMessage($message);
509 if (isset($buffer_message)) {
510 $GLOBALS['buffer_message'] = $buffer_message;
517 * Sends out the HTTP headers
521 public function sendHttpHeaders()
523 if (defined('TESTSUITE') && ! defined('PMA_TEST_HEADERS')) {
526 if ($GLOBALS['PMA_Config']->isHttps()) {
529 $map_tile_urls = ' *.tile.openstreetmap.org *.tile.opencyclemap.org';
535 $GLOBALS['now'] = gmdate('D, d M Y H:i:s') . ' GMT';
536 if (!empty($GLOBALS['cfg']['CaptchaLoginPrivateKey'])
537 && !empty($GLOBALS['cfg']['CaptchaLoginPublicKey'])
539 $captcha_url = ' https://apis.google.com https://www.google.com/recaptcha/'
540 . ' https://www.gstatic.com/recaptcha/ https://ssl.gstatic.com/ ';
544 /* Prevent against ClickJacking by disabling framing */
545 if (! $GLOBALS['cfg']['AllowThirdPartyFraming']) {
547 'X-Frame-Options: DENY'
551 "Content-Security-Policy: default-src 'self' "
553 . $GLOBALS['cfg']['CSPAllow'] . ';'
554 . "script-src 'self' 'unsafe-inline' 'unsafe-eval' "
556 . $GLOBALS['cfg']['CSPAllow'] . ';'
558 . "style-src 'self' 'unsafe-inline' "
560 . $GLOBALS['cfg']['CSPAllow']
562 . "img-src 'self' data: "
563 . $GLOBALS['cfg']['CSPAllow']
569 "X-Content-Security-Policy: default-src 'self' "
571 . $GLOBALS['cfg']['CSPAllow'] . ';'
572 . "options inline-script eval-script;"
573 . "img-src 'self' data: "
574 . $GLOBALS['cfg']['CSPAllow']
580 "X-WebKit-CSP: default-src 'self' "
582 . $GLOBALS['cfg']['CSPAllow'] . ';'
583 . "script-src 'self' "
585 . $GLOBALS['cfg']['CSPAllow']
586 . " 'unsafe-inline' 'unsafe-eval';"
587 . "style-src 'self' 'unsafe-inline' "
590 . "img-src 'self' data: "
591 . $GLOBALS['cfg']['CSPAllow']
597 if (! defined('IS_TRANSFORMATION_WRAPPER')) {
598 // Define the charset to be used
599 header('Content-Type: text/html; charset=utf-8');
601 $this->_headerIsSent
= true;
605 * Returns the DOCTYPE and the start HTML tag
607 * @return string DOCTYPE and HTML tags
609 private function _getHtmlStart()
611 $lang = $GLOBALS['available_languages'][$GLOBALS['lang']][1];
612 $dir = $GLOBALS['text_dir'];
614 $retval = "<!DOCTYPE HTML>";
615 $retval .= "<html lang='$lang' dir='$dir' class='";
616 $retval .= /*overload*/mb_strtolower(PMA_USR_BROWSER_AGENT
) . " ";
617 $retval .= /*overload*/mb_strtolower(PMA_USR_BROWSER_AGENT
)
618 . intval(PMA_USR_BROWSER_VER
) . "'>";
625 * Returns the META tags
627 * @return string the META tags
629 private function _getMetaTags()
631 $retval = '<meta charset="utf-8" />';
632 $retval .= '<meta name="robots" content="noindex,nofollow" />';
633 $retval .= '<meta http-equiv="X-UA-Compatible" content="IE=Edge">';
634 if (! $GLOBALS['cfg']['AllowThirdPartyFraming']) {
635 $retval .= '<style id="cfs-style">html{display: none;}</style>';
641 * Returns the LINK tags for the favicon and the stylesheets
643 * @return string the LINK tags
645 private function _getLinkTags()
647 $retval = '<link rel="icon" href="favicon.ico" '
648 . 'type="image/x-icon" />'
649 . '<link rel="shortcut icon" href="favicon.ico" '
650 . 'type="image/x-icon" />';
652 $basedir = defined('PMA_PATH_TO_BASEDIR') ? PMA_PATH_TO_BASEDIR
: '';
653 $theme_id = $GLOBALS['PMA_Config']->getThemeUniqueValue();
654 $theme_path = $GLOBALS['pmaThemePath'];
655 $v = self
::getVersionParameter();
657 if ($this->_isPrintView
) {
658 $retval .= '<link rel="stylesheet" type="text/css" href="'
659 . $basedir . 'print.css?' . $v . '" />';
661 // load jQuery's CSS prior to our theme's CSS, to let the theme
662 // override jQuery's CSS
663 $retval .= '<link rel="stylesheet" type="text/css" href="'
664 . $theme_path . '/jquery/jquery-ui-1.11.2.css" />';
665 $retval .= '<link rel="stylesheet" type="text/css" href="'
666 . $basedir . 'js/codemirror/lib/codemirror.css?' . $v . '" />';
667 $retval .= '<link rel="stylesheet" type="text/css" href="'
668 . $basedir . 'js/codemirror/addon/hint/show-hint.css?' . $v . '" />';
669 $retval .= '<link rel="stylesheet" type="text/css" href="'
670 . $basedir . 'js/codemirror/addon/lint/lint.css?' . $v . '" />';
671 $retval .= '<link rel="stylesheet" type="text/css" href="'
672 . $basedir . 'phpmyadmin.css.php?'
673 . 'nocache=' . $theme_id . $GLOBALS['text_dir'] . '" />';
674 // load Print view's CSS last, so that it overrides all other CSS while 'printing'
675 $retval .= '<link rel="stylesheet" type="text/css" href="'
676 . $theme_path . '/css/printview.css?' . $v . '" />';
683 * Returns the TITLE tag
685 * @return string the TITLE tag
687 public function getTitleTag()
690 $retval .= $this->_getPageTitle();
691 $retval .= "</title>";
696 * If the page is missing the title, this function
697 * will set it to something reasonable
701 private function _getPageTitle()
703 if (empty($this->_title
)) {
704 if ($GLOBALS['server'] > 0) {
705 if (! empty($GLOBALS['table'])) {
706 $temp_title = $GLOBALS['cfg']['TitleTable'];
707 } else if (! empty($GLOBALS['db'])) {
708 $temp_title = $GLOBALS['cfg']['TitleDatabase'];
709 } elseif (! empty($GLOBALS['cfg']['Server']['host'])) {
710 $temp_title = $GLOBALS['cfg']['TitleServer'];
712 $temp_title = $GLOBALS['cfg']['TitleDefault'];
714 $this->_title
= htmlspecialchars(
715 PMA_Util
::expandUserString($temp_title)
718 $this->_title
= 'phpMyAdmin';
721 return $this->_title
;
725 * Returns the close tag to the HEAD
726 * and the start tag for the BODY
728 * @return string HEAD and BODY tags
730 private function _getBodyStart()
732 $retval = "</head><body";
733 if (! empty($this->_bodyId
)) {
734 $retval .= " id='" . $this->_bodyId
. "'";
741 * Returns some warnings to be displayed at the top of the page
743 * @return string The warnings
745 private function _getWarnings()
748 if ($this->_warningsEnabled
) {
749 $retval .= "<noscript>";
750 $retval .= PMA_message
::error(
751 __("Javascript must be enabled past this point!")
753 $retval .= "</noscript>";
759 * Add recently used table and reload the navigation.
761 * @param string $db Database name where the table is located.
762 * @param string $table The table name
766 private function _addRecentTable($db, $table)
769 if ($this->_menuEnabled
770 && /*overload*/mb_strlen($table)
771 && $GLOBALS['cfg']['NumRecentTables'] > 0
773 $tmp_result = PMA_RecentFavoriteTable
::getInstance('recent')
775 if ($tmp_result === true) {
776 $retval = PMA_RecentFavoriteTable
::getHtmlUpdateRecentTables();
778 $error = $tmp_result;
779 $retval = $error->getDisplay();
786 * Returns the phpMyAdmin version to be appended to the url to avoid caching
789 * @return string urlenocded pma version as a parameter
791 public static function getVersionParameter()
793 return "v=" . urlencode(PMA_VERSION
);