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/RecentTable.class.php';
14 require_once 'libraries/Menu.class.php';
15 require_once 'libraries/navigation/Navigation.class.php';
16 require_once 'libraries/url_generating.lib.php';
20 * Class used to output the HTTP and HTML headers
27 * PMA_Scripts instance
41 * Whether to offer the option of importing user settings
46 private $_userprefsOfferImport;
55 * The value for the id attribute for the body tag
62 * Whether to show the top menu
67 private $_menuEnabled;
69 * Whether to show the warnings
74 private $_warningsEnabled;
76 * Whether the page is in 'print view' mode
81 private $_isPrintView;
83 * Whether we are servicing an ajax request.
84 * We can't simply use $GLOBALS['is_ajax_request']
85 * here since it may have not been initialised yet.
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 * Creates a new class instance
110 * @return new PMA_Header object
112 public function __construct()
114 $this->_isEnabled
= true;
115 $this->_isAjax
= false;
118 $db = ! empty($GLOBALS['db']) ?
$GLOBALS['db'] : '';
119 $table = ! empty($GLOBALS['table']) ?
$GLOBALS['table'] : '';
120 $this->_menu
= new PMA_Menu(
125 $this->_menuEnabled
= true;
126 $this->_warningsEnabled
= true;
127 $this->_isPrintView
= false;
128 $this->_scripts
= new PMA_Scripts();
129 $this->_addDefaultScripts();
130 $this->_headerIsSent
= false;
131 // if database storage for user preferences is transient,
132 // offer to load exported settings from localStorage
133 // (detection will be done in JavaScript)
134 $this->_userprefsOfferImport
= false;
135 if ($GLOBALS['PMA_Config']->get('user_preferences') == 'session'
136 && ! isset($_SESSION['userprefs_autoload'])
138 $this->_userprefsOfferImport
= true;
143 * Loads common scripts
147 private function _addDefaultScripts()
149 $this->_scripts
->addFile('jquery/jquery-1.8.3.min.js');
150 $this->_scripts
->addFile('ajax.js');
151 $this->_scripts
->addFile('keyhandler.js');
152 $this->_scripts
->addFile('jquery/jquery-ui-1.9.2.custom.min.js');
153 $this->_scripts
->addFile('jquery/jquery.sprintf.js');
154 $this->_scripts
->addFile('jquery/jquery.cookie.js');
155 $this->_scripts
->addFile('jquery/jquery.mousewheel.js');
156 $this->_scripts
->addFile('jquery/jquery.event.drag-2.2.js');
157 $this->_scripts
->addFile('jquery/jquery-ui-timepicker-addon.js');
158 $this->_scripts
->addFile('jquery/jquery.ba-hashchange-1.3.js');
159 $this->_scripts
->addFile('jquery/jquery.debounce-1.0.5.js');
160 $this->_scripts
->addFile('jquery/jquery.menuResizer-1.0.js');
162 // Cross-framing protection
163 if ($GLOBALS['cfg']['AllowThirdPartyFraming'] === false) {
164 $this->_scripts
->addFile('cross_framing_protection.js');
167 $this->_scripts
->addFile('rte.js');
168 if ($GLOBALS['cfg']['ErrorReporting'] == true) {
169 $this->_scripts
->addFile('tracekit/tracekit.js');
170 $this->_scripts
->addFile('error_report.js');
173 // Here would not be a good place to add CodeMirror because
174 // the user preferences have not been merged at this point
177 $params = array('lang' => $GLOBALS['lang']);
178 if (isset($GLOBALS['db'])) {
179 $params['db'] = $GLOBALS['db'];
181 $this->_scripts
->addFile('messages.php' . PMA_URL_getCommon($params));
182 // Append the theme id to this url to invalidate
183 // the cache on a theme change. Though this might be
184 // unavailable for fatal errors.
185 if (isset($_SESSION['PMA_Theme'])) {
186 $theme_id = urlencode($_SESSION['PMA_Theme']->getId());
188 $theme_id = 'default';
190 $this->_scripts
->addFile(
191 'get_image.js.php?theme=' . $theme_id
193 $this->_scripts
->addFile('doclinks.js');
194 $this->_scripts
->addFile('functions.js');
195 $this->_scripts
->addFile('navigation.js');
196 $this->_scripts
->addFile('indexes.js');
197 $this->_scripts
->addFile('common.js');
198 $this->_scripts
->addCode($this->getJsParamsCode());
202 * Returns, as an array, a list of parameters
203 * used on the client side
207 public function getJsParams()
209 $db = ! empty($GLOBALS['db']) ?
$GLOBALS['db'] : '';
210 $table = ! empty($GLOBALS['table']) ?
$GLOBALS['table'] : '';
212 'common_query' => PMA_URL_getCommon('', '', '&'),
213 'opendb_url' => $GLOBALS['cfg']['DefaultTabDatabase'],
214 'safari_browser' => PMA_USR_BROWSER_AGENT
== 'SAFARI' ?
1 : 0,
215 'querywindow_height' => $GLOBALS['cfg']['QueryWindowHeight'],
216 'querywindow_width' => $GLOBALS['cfg']['QueryWindowWidth'],
217 'collation_connection' => $GLOBALS['collation_connection'],
218 'lang' => $GLOBALS['lang'],
219 'server' => $GLOBALS['server'],
222 'token' => $_SESSION[' PMA_token '],
223 'text_dir' => $GLOBALS['text_dir'],
224 'pma_absolute_uri' => $GLOBALS['cfg']['PmaAbsoluteUri'],
225 'pma_text_default_tab' => PMA_Util
::getTitleForTarget(
226 $GLOBALS['cfg']['DefaultTabTable']
228 'pma_text_left_default_tab' => PMA_Util
::getTitleForTarget(
229 $GLOBALS['cfg']['NavigationTreeDefaultTabTable']
231 'confirm' => $GLOBALS['cfg']['Confirm']
236 * Returns, as a string, a list of parameters
237 * used on the client side
241 public function getJsParamsCode()
243 $params = $this->getJsParams();
244 foreach ($params as $key => $value) {
245 $params[$key] = $key . ':"' . PMA_escapeJsString($value) . '"';
247 return 'PMA_commonParams.setAll({' . implode(',', $params) . '});';
251 * Disables the rendering of the header
255 public function disable()
257 $this->_isEnabled
= false;
261 * Set the ajax flag to indicate whether
262 * we are sevicing an ajax request
264 * @param bool $isAjax Whether we are sevicing an ajax request
268 public function setAjax($isAjax)
270 $this->_isAjax
= ($isAjax == true);
274 * Returns the PMA_Scripts object
276 * @return PMA_Scripts object
278 public function getScripts()
280 return $this->_scripts
;
284 * Returns the PMA_Menu object
286 * @return PMA_Menu object
288 public function getMenu()
294 * Setter for the ID attribute in the BODY tag
296 * @param string $id Value for the ID attribute
300 public function setBodyId($id)
302 $this->_bodyId
= htmlspecialchars($id);
306 * Setter for the title of the page
308 * @param string $title New title
312 public function setTitle($title)
314 $this->_title
= htmlspecialchars($title);
318 * Disables the display of the top menu
322 public function disableMenu()
324 $this->_menuEnabled
= false;
328 * Disables the display of the top menu
332 public function disableWarnings()
334 $this->_warningsEnabled
= false;
338 * Turns on 'print view' mode
342 public function enablePrintView()
344 $this->disableMenu();
345 $this->setTitle(__('Print view') . ' - phpMyAdmin ' . PMA_VERSION
);
346 $this->_isPrintView
= true;
350 * Generates the header
352 * @return string The header
354 public function getDisplay()
357 if (! $this->_headerIsSent
) {
358 if (! $this->_isAjax
&& $this->_isEnabled
) {
359 $this->sendHttpHeaders();
360 $retval .= $this->_getHtmlStart();
361 $retval .= $this->_getMetaTags();
362 $retval .= $this->_getLinkTags();
363 $retval .= $this->getTitleTag();
365 // The user preferences have been merged at this point
366 // so we can conditionally add CodeMirror
367 if ($GLOBALS['cfg']['CodemirrorEnable']) {
368 $this->_scripts
->addFile('codemirror/lib/codemirror.js');
369 $this->_scripts
->addFile('codemirror/mode/sql/sql.js');
370 $this->_scripts
->addFile('codemirror/addon/runmode/runmode.js');
372 if ($this->_userprefsOfferImport
) {
373 $this->_scripts
->addFile('config.js');
375 $retval .= $this->_scripts
->getDisplay();
376 $retval .= $this->_getBodyStart();
377 if ($this->_menuEnabled
&& $GLOBALS['server'] > 0) {
378 $nav = new PMA_Navigation();
379 $retval .= $nav->getDisplay();
381 // Include possible custom headers
382 if (file_exists(CUSTOM_HEADER_FILE
)) {
384 include CUSTOM_HEADER_FILE
;
385 $retval .= ob_get_contents();
388 // offer to load user preferences from localStorage
389 if ($this->_userprefsOfferImport
) {
390 include_once './libraries/user_preferences.lib.php';
391 $retval .= PMA_userprefsAutoloadGetHeader();
393 // pass configuration for hint tooltip display
394 // (to be used by PMA_tooltip() in js/functions.js)
395 if (! $GLOBALS['cfg']['ShowHint']) {
396 $retval .= '<span id="no_hint" class="hide"></span>';
398 $retval .= $this->_getWarnings();
399 if ($this->_menuEnabled
&& $GLOBALS['server'] > 0) {
400 $retval .= $this->_menu
->getDisplay();
402 '<a id="goto_pagetop" href="#" title="%s">%s</a>',
403 __('Click on the bar to scroll to top of page'),
404 PMA_Util
::getImage('s_top.png')
407 $retval .= '<div id="page_content">';
408 $retval .= $this->getMessage();
410 if ($this->_isEnabled
&& empty($_REQUEST['recent_table'])) {
411 $retval .= $this->_addRecentTable(
421 * Returns the message to be displayed at the top of
422 * the page, including the executed SQL query, if any.
426 public function getMessage()
430 if (! empty($GLOBALS['message'])) {
431 $message = $GLOBALS['message'];
432 unset($GLOBALS['message']);
433 } else if (! empty($_REQUEST['message'])) {
434 $message = $_REQUEST['message'];
436 if (! empty($message)) {
437 if (isset($GLOBALS['buffer_message'])) {
438 $buffer_message = $GLOBALS['buffer_message'];
440 $retval .= PMA_Util
::getMessage($message);
441 if (isset($buffer_message)) {
442 $GLOBALS['buffer_message'] = $buffer_message;
449 * Sends out the HTTP headers
453 public function sendHttpHeaders()
455 $https = $GLOBALS['PMA_Config']->isHttps();
456 $mapTilesUrls = ' *.tile.openstreetmap.org *.tile.opencyclemap.org';
461 $GLOBALS['now'] = gmdate('D, d M Y H:i:s') . ' GMT';
462 if (! defined('TESTSUITE')) {
463 /* Prevent against ClickJacking by disabling framing */
464 if (! $GLOBALS['cfg']['AllowThirdPartyFraming']) {
466 'X-Frame-Options: DENY'
470 "X-Content-Security-Policy: default-src 'self' "
471 . 'https://www.google.com '
472 . $GLOBALS['cfg']['CSPAllow'] . ';'
473 . "options inline-script eval-script;"
474 . "img-src 'self' data: "
475 . $GLOBALS['cfg']['CSPAllow']
476 . ($https ?
"" : $mapTilesUrls)
478 . " https://www.google.com"
481 if (PMA_USR_BROWSER_AGENT
== 'SAFARI'
482 && PMA_USR_BROWSER_VER
< '6.0.0'
485 "X-WebKit-CSP: allow 'self' "
486 . 'https://www.google.com '
487 . $GLOBALS['cfg']['CSPAllow'] . ';'
488 . "options inline-script eval-script;"
489 . "img-src 'self' data: "
490 . $GLOBALS['cfg']['CSPAllow']
491 . ($https ?
"" : $mapTilesUrls)
493 . " https://www.google.com"
498 "X-WebKit-CSP: default-src 'self' "
499 . 'https://www.google.com '
500 . $GLOBALS['cfg']['CSPAllow'] . ';'
501 . "script-src 'self' "
502 . 'https://www.google.com '
503 . $GLOBALS['cfg']['CSPAllow']
504 . " 'unsafe-inline' 'unsafe-eval';"
505 . "style-src 'self' 'unsafe-inline' "
506 . 'https://www.google.com '
508 . "img-src 'self' data: "
509 . $GLOBALS['cfg']['CSPAllow']
510 . ($https ?
"" : $mapTilesUrls)
512 . " https://www.google.com"
518 if (! defined('IS_TRANSFORMATION_WRAPPER') && ! defined('TESTSUITE')) {
519 // Define the charset to be used
520 header('Content-Type: text/html; charset=utf-8');
522 $this->_headerIsSent
= true;
526 * Returns the DOCTYPE and the start HTML tag
528 * @return string DOCTYPE and HTML tags
530 private function _getHtmlStart()
532 $lang = $GLOBALS['available_languages'][$GLOBALS['lang']][1];
533 $dir = $GLOBALS['text_dir'];
535 $retval = "<!DOCTYPE HTML>";
536 $retval .= "<html lang='$lang' dir='$dir' class='";
537 $retval .= strtolower(PMA_USR_BROWSER_AGENT
) . " ";
538 $retval .= strtolower(PMA_USR_BROWSER_AGENT
)
539 . intval(PMA_USR_BROWSER_VER
) . "'>";
545 * Returns the META tags
547 * @return string the META tags
549 private function _getMetaTags()
551 $retval = '<meta charset="utf-8" />';
552 $retval .= '<meta name="robots" content="noindex,nofollow" />';
553 $retval .= '<meta http-equiv="X-UA-Compatible" content="IE=Edge">';
554 if (! $GLOBALS['cfg']['AllowThirdPartyFraming']) {
555 $retval .= '<style>html{display: none;}</style>';
561 * Returns the LINK tags for the favicon and the stylesheets
563 * @return string the LINK tags
565 private function _getLinkTags()
567 $retval = '<link rel="icon" href="favicon.ico" '
568 . 'type="image/x-icon" />'
569 . '<link rel="shortcut icon" href="favicon.ico" '
570 . 'type="image/x-icon" />';
572 $basedir = defined('PMA_PATH_TO_BASEDIR') ? PMA_PATH_TO_BASEDIR
: '';
573 $common_url = PMA_URL_getCommon(array('server' => $GLOBALS['server']));
574 $theme_id = $GLOBALS['PMA_Config']->getThemeUniqueValue();
575 $theme_path = $GLOBALS['pmaThemePath'];
577 if ($this->_isPrintView
) {
578 $retval .= '<link rel="stylesheet" type="text/css" href="'
579 . $basedir . 'print.css" />';
581 $retval .= '<link rel="stylesheet" type="text/css" href="'
582 . $basedir . 'phpmyadmin.css.php'
583 . $common_url . '&nocache='
584 . $theme_id . $GLOBALS['text_dir'] . '" />';
585 $retval .= '<link rel="stylesheet" type="text/css" href="'
586 . $theme_path . '/jquery/jquery-ui-1.9.2.custom.css" />';
593 * Returns the TITLE tag
595 * @return string the TITLE tag
597 public function getTitleTag()
600 $retval .= $this->_getPageTitle();
601 $retval .= "</title>";
606 * If the page is missing the title, this function
607 * will set it to something reasonable
611 private function _getPageTitle()
613 if (empty($this->_title
)) {
614 if ($GLOBALS['server'] > 0) {
615 if (! empty($GLOBALS['table'])) {
616 $temp_title = $GLOBALS['cfg']['TitleTable'];
617 } else if (! empty($GLOBALS['db'])) {
618 $temp_title = $GLOBALS['cfg']['TitleDatabase'];
619 } elseif (! empty($GLOBALS['cfg']['Server']['host'])) {
620 $temp_title = $GLOBALS['cfg']['TitleServer'];
622 $temp_title = $GLOBALS['cfg']['TitleDefault'];
624 $this->_title
= htmlspecialchars(
625 PMA_Util
::expandUserString($temp_title)
628 $this->_title
= 'phpMyAdmin';
631 return $this->_title
;
635 * Returns the close tag to the HEAD
636 * and the start tag for the BODY
638 * @return string HEAD and BODY tags
640 private function _getBodyStart()
642 $retval = "</head><body";
643 if (! empty($this->_bodyId
)) {
644 $retval .= " id='" . $this->_bodyId
. "'";
651 * Returns some warnings to be displayed at the top of the page
653 * @return string The warnings
655 private function _getWarnings()
658 if ($this->_warningsEnabled
) {
659 $retval .= "<noscript>";
660 $retval .= PMA_message
::error(
661 __("Javascript must be enabled past this point")
663 $retval .= "</noscript>";
669 * Add recently used table and reload the navigation.
671 * @param string $db Database name where the table is located.
672 * @param string $table The table name
676 private function _addRecentTable($db, $table)
679 if ($this->_menuEnabled
681 && $GLOBALS['cfg']['NumRecentTables'] > 0
683 $tmp_result = PMA_RecentTable
::getInstance()->add($db, $table);
684 if ($tmp_result === true) {
685 $params = array('ajax_request' => true, 'recent_table' => true);
686 $url = 'index.php' . PMA_URL_getCommon($params);
687 $retval = '<a class="hide" id="update_recent_tables"';
688 $retval .= ' href="' . $url . '"></a>';
690 $error = $tmp_result;
691 $retval = $error->getDisplay();