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)
479 if (PMA_USR_BROWSER_AGENT
== 'SAFARI'
480 && PMA_USR_BROWSER_VER
< '6.0.0'
483 "X-WebKit-CSP: allow 'self' "
484 . 'https://www.google.com '
485 . $GLOBALS['cfg']['CSPAllow'] . ';'
486 . "options inline-script eval-script;"
487 . "img-src 'self' data: "
488 . $GLOBALS['cfg']['CSPAllow']
489 . ($https ?
"" : $mapTilesUrls)
494 "X-WebKit-CSP: default-src 'self' "
495 . 'https://www.google.com '
496 . $GLOBALS['cfg']['CSPAllow'] . ';'
497 . "script-src 'self' "
498 . 'https://www.google.com '
499 . $GLOBALS['cfg']['CSPAllow']
500 . " 'unsafe-inline' 'unsafe-eval';"
501 . "style-src 'self' 'unsafe-inline' "
502 . 'https://www.google.com '
504 . "img-src 'self' data: "
505 . $GLOBALS['cfg']['CSPAllow']
506 . ($https ?
"" : $mapTilesUrls)
512 if (! defined('IS_TRANSFORMATION_WRAPPER') && ! defined('TESTSUITE')) {
513 // Define the charset to be used
514 header('Content-Type: text/html; charset=utf-8');
516 $this->_headerIsSent
= true;
520 * Returns the DOCTYPE and the start HTML tag
522 * @return string DOCTYPE and HTML tags
524 private function _getHtmlStart()
526 $lang = $GLOBALS['available_languages'][$GLOBALS['lang']][1];
527 $dir = $GLOBALS['text_dir'];
529 $retval = "<!DOCTYPE HTML>";
530 $retval .= "<html lang='$lang' dir='$dir' class='";
531 $retval .= strtolower(PMA_USR_BROWSER_AGENT
) . " ";
532 $retval .= strtolower(PMA_USR_BROWSER_AGENT
)
533 . intval(PMA_USR_BROWSER_VER
) . "'>";
539 * Returns the META tags
541 * @return string the META tags
543 private function _getMetaTags()
545 $retval = '<meta charset="utf-8" />';
546 $retval .= '<meta name="robots" content="noindex,nofollow" />';
547 $retval .= '<meta http-equiv="X-UA-Compatible" content="IE=Edge">';
548 if (! $GLOBALS['cfg']['AllowThirdPartyFraming']) {
549 $retval .= '<style>html{display: none;}</style>';
555 * Returns the LINK tags for the favicon and the stylesheets
557 * @return string the LINK tags
559 private function _getLinkTags()
561 $retval = '<link rel="icon" href="favicon.ico" '
562 . 'type="image/x-icon" />'
563 . '<link rel="shortcut icon" href="favicon.ico" '
564 . 'type="image/x-icon" />';
566 $basedir = defined('PMA_PATH_TO_BASEDIR') ? PMA_PATH_TO_BASEDIR
: '';
567 $common_url = PMA_URL_getCommon(array('server' => $GLOBALS['server']));
568 $theme_id = $GLOBALS['PMA_Config']->getThemeUniqueValue();
569 $theme_path = $GLOBALS['pmaThemePath'];
571 if ($this->_isPrintView
) {
572 $retval .= '<link rel="stylesheet" type="text/css" href="'
573 . $basedir . 'print.css" />';
575 $retval .= '<link rel="stylesheet" type="text/css" href="'
576 . $basedir . 'phpmyadmin.css.php'
577 . $common_url . '&nocache='
578 . $theme_id . $GLOBALS['text_dir'] . '" />';
579 $retval .= '<link rel="stylesheet" type="text/css" href="'
580 . $theme_path . '/jquery/jquery-ui-1.9.2.custom.css" />';
587 * Returns the TITLE tag
589 * @return string the TITLE tag
591 public function getTitleTag()
594 $retval .= $this->_getPageTitle();
595 $retval .= "</title>";
600 * If the page is missing the title, this function
601 * will set it to something reasonable
605 private function _getPageTitle()
607 if (empty($this->_title
)) {
608 if ($GLOBALS['server'] > 0) {
609 if (! empty($GLOBALS['table'])) {
610 $temp_title = $GLOBALS['cfg']['TitleTable'];
611 } else if (! empty($GLOBALS['db'])) {
612 $temp_title = $GLOBALS['cfg']['TitleDatabase'];
613 } elseif (! empty($GLOBALS['cfg']['Server']['host'])) {
614 $temp_title = $GLOBALS['cfg']['TitleServer'];
616 $temp_title = $GLOBALS['cfg']['TitleDefault'];
618 $this->_title
= htmlspecialchars(
619 PMA_Util
::expandUserString($temp_title)
622 $this->_title
= 'phpMyAdmin';
625 return $this->_title
;
629 * Returns the close tag to the HEAD
630 * and the start tag for the BODY
632 * @return string HEAD and BODY tags
634 private function _getBodyStart()
636 $retval = "</head><body";
637 if (! empty($this->_bodyId
)) {
638 $retval .= " id='" . $this->_bodyId
. "'";
645 * Returns some warnings to be displayed at the top of the page
647 * @return string The warnings
649 private function _getWarnings()
652 if ($this->_warningsEnabled
) {
653 $retval .= "<noscript>";
654 $retval .= PMA_message
::error(
655 __("Javascript must be enabled past this point")
657 $retval .= "</noscript>";
663 * Add recently used table and reload the navigation.
665 * @param string $db Database name where the table is located.
666 * @param string $table The table name
670 private function _addRecentTable($db, $table)
673 if ($this->_menuEnabled
675 && $GLOBALS['cfg']['NumRecentTables'] > 0
677 $tmp_result = PMA_RecentTable
::getInstance()->add($db, $table);
678 if ($tmp_result === true) {
679 $params = array('ajax_request' => true, 'recent_table' => true);
680 $url = 'index.php' . PMA_URL_getCommon($params);
681 $retval = '<a class="hide" id="update_recent_tables"';
682 $retval .= ' href="' . $url . '"></a>';
684 $error = $tmp_result;
685 $retval = $error->getDisplay();