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.js');
150 $this->_scripts
->addFile('ajax.js');
151 $this->_scripts
->addFile('keyhandler.js');
152 $this->_scripts
->addFile('jquery/jquery-ui-1.9.2.custom.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');
161 $this->_scripts
->addFile('rte.js');
163 // Here would not be a good place to add CodeMirror because
164 // the user preferences have not been merged at this point
167 $params = array('lang' => $GLOBALS['lang']);
168 if (isset($GLOBALS['db'])) {
169 $params['db'] = $GLOBALS['db'];
171 $this->_scripts
->addFile('messages.php' . PMA_generate_common_url($params));
172 // Append the theme id to this url to invalidate
173 // the cache on a theme change. Though this might be
174 // unavailable for fatal errors.
175 if (isset($_SESSION['PMA_Theme'])) {
176 $theme_id = urlencode($_SESSION['PMA_Theme']->getId());
178 $theme_id = 'default';
180 $this->_scripts
->addFile(
181 'get_image.js.php?theme=' . $theme_id
183 $this->_scripts
->addFile('functions.js');
184 $this->_scripts
->addFile('navigation.js');
185 $this->_scripts
->addFile('indexes.js');
186 $this->_scripts
->addFile('common.js');
187 $this->_scripts
->addCode($this->getJsParamsCode());
191 * Returns, as an array, a list of parameters
192 * used on the client side
196 public function getJsParams()
198 $db = ! empty($GLOBALS['db']) ?
$GLOBALS['db'] : '';
199 $table = ! empty($GLOBALS['table']) ?
$GLOBALS['table'] : '';
201 'common_query' => PMA_generate_common_url('', '', '&'),
202 'opendb_url' => $GLOBALS['cfg']['DefaultTabDatabase'],
203 'safari_browser' => PMA_USR_BROWSER_AGENT
== 'SAFARI' ?
1 : 0,
204 'querywindow_height' => $GLOBALS['cfg']['QueryWindowHeight'],
205 'querywindow_width' => $GLOBALS['cfg']['QueryWindowWidth'],
206 'collation_connection' => $GLOBALS['collation_connection'],
207 'lang' => $GLOBALS['lang'],
208 'server' => $GLOBALS['server'],
211 'token' => $_SESSION[' PMA_token '],
212 'text_dir' => $GLOBALS['text_dir'],
213 'pma_absolute_uri' => $GLOBALS['cfg']['PmaAbsoluteUri'],
214 'pma_text_default_tab' => PMA_Util
::getTitleForTarget(
215 $GLOBALS['cfg']['DefaultTabTable']
217 'pma_text_left_default_tab' => PMA_Util
::getTitleForTarget(
218 $GLOBALS['cfg']['NavigationTreeDefaultTabTable']
220 'confirm' => $GLOBALS['cfg']['Confirm']
225 * Returns, as a string, a list of parameters
226 * used on the client side
230 public function getJsParamsCode()
232 $params = $this->getJsParams();
233 foreach ($params as $key => $value) {
234 $params[$key] = $key . ':"' . PMA_escapeJsString($value) . '"';
236 return 'PMA_commonParams.setAll({' . implode(',', $params) . '});';
240 * Disables the rendering of the header
244 public function disable()
246 $this->_isEnabled
= false;
250 * Set the ajax flag to indicate whether
251 * we are sevicing an ajax request
253 * @param bool $isAjax Whether we are sevicing an ajax request
257 public function setAjax($isAjax)
259 $this->_isAjax
= ($isAjax == true);
263 * Returns the PMA_Scripts object
265 * @return PMA_Scripts object
267 public function getScripts()
269 return $this->_scripts
;
273 * Returns the PMA_Menu object
275 * @return PMA_Menu object
277 public function getMenu()
283 * Setter for the ID attribute in the BODY tag
285 * @param string $id Value for the ID attribute
289 public function setBodyId($id)
291 $this->_bodyId
= htmlspecialchars($id);
295 * Setter for the title of the page
297 * @param string $title New title
301 public function setTitle($title)
303 $this->_title
= htmlspecialchars($title);
307 * Disables the display of the top menu
311 public function disableMenu()
313 $this->_menuEnabled
= false;
317 * Disables the display of the top menu
321 public function disableWarnings()
323 $this->_warningsEnabled
= false;
327 * Turns on 'print view' mode
331 public function enablePrintView()
333 $this->disableMenu();
334 $this->setTitle(__('Print view') . ' - phpMyAdmin ' . PMA_VERSION
);
335 $this->_isPrintView
= true;
339 * Generates the header
341 * @return string The header
343 public function getDisplay()
346 if (! $this->_headerIsSent
) {
347 if (! $this->_isAjax
&& $this->_isEnabled
) {
348 $this->sendHttpHeaders();
349 $retval .= $this->_getHtmlStart();
350 $retval .= $this->_getMetaTags();
351 $retval .= $this->_getLinkTags();
352 $retval .= $this->getTitleTag();
354 // The user preferences have been merged at this point
355 // so we can conditionally add CodeMirror
356 if ($GLOBALS['cfg']['CodemirrorEnable']) {
357 $this->_scripts
->addFile('codemirror/lib/codemirror.js');
358 $this->_scripts
->addFile('codemirror/mode/mysql/mysql.js');
360 if ($this->_userprefsOfferImport
) {
361 $this->_scripts
->addFile('config.js');
363 $retval .= $this->_scripts
->getDisplay();
364 $retval .= $this->_getBodyStart();
365 if ($this->_menuEnabled
&& $GLOBALS['server'] > 0) {
366 $nav = new PMA_Navigation();
367 $retval .= $nav->getDisplay();
369 // Include possible custom headers
370 if (file_exists(CUSTOM_HEADER_FILE
)) {
372 include CUSTOM_HEADER_FILE
;
373 $retval .= ob_get_contents();
376 // offer to load user preferences from localStorage
377 if ($this->_userprefsOfferImport
) {
378 include_once './libraries/user_preferences.lib.php';
379 $retval .= PMA_userprefsAutoloadGetHeader();
381 // pass configuration for hint tooltip display
382 // (to be used by PMA_tooltip() in js/functions.js)
383 if (! $GLOBALS['cfg']['ShowHint']) {
384 $retval .= '<span id="no_hint" class="hide"></span>';
386 $retval .= $this->_getWarnings();
387 if ($this->_menuEnabled
&& $GLOBALS['server'] > 0) {
388 $retval .= $this->_menu
->getDisplay();
389 $pagetop_link = '<a id="goto_pagetop" href="#" title="%s">%s</a>';
392 __('Click on the bar to scroll to top of page'),
393 PMA_Util
::getImage('s_top.png')
396 $retval .= '<div id="page_content">';
397 $retval .= $this->getMessage();
399 if ($this->_isEnabled
&& empty($_REQUEST['recent_table'])) {
400 $retval .= $this->_addRecentTable(
410 * Returns the message to be displayed at the top of
411 * the page, including the executed SQL query, if any.
415 public function getMessage()
419 if (! empty($GLOBALS['message'])) {
420 $message = $GLOBALS['message'];
421 unset($GLOBALS['message']);
422 } else if (! empty($_REQUEST['message'])) {
423 $message = $_REQUEST['message'];
425 if (! empty($message)) {
426 if (isset($GLOBALS['buffer_message'])) {
427 $buffer_message = $GLOBALS['buffer_message'];
429 $retval .= PMA_Util
::getMessage($message);
430 if (isset($buffer_message)) {
431 $GLOBALS['buffer_message'] = $buffer_message;
438 * Sends out the HTTP headers
442 public function sendHttpHeaders()
444 $https = $GLOBALS['PMA_Config']->isHttps();
445 $mapTilesUrls = ' *.tile.openstreetmap.org *.tile.opencyclemap.org';
450 $GLOBALS['now'] = gmdate('D, d M Y H:i:s') . ' GMT';
451 if (! defined('TESTSUITE')) {
453 "X-Content-Security-Policy: default-src 'self' "
454 . $GLOBALS['cfg']['CSPAllow'] . ';'
455 . "options inline-script eval-script;"
456 . "img-src 'self' data: "
457 . $GLOBALS['cfg']['CSPAllow']
458 . ($https ?
"" : $mapTilesUrls)
461 if (PMA_USR_BROWSER_AGENT
== 'SAFARI'
462 && PMA_USR_BROWSER_VER
< '6.0.0'
465 "X-WebKit-CSP: allow 'self' "
466 . $GLOBALS['cfg']['CSPAllow'] . ';'
467 . "options inline-script eval-script;"
468 . "img-src 'self' data: "
469 . $GLOBALS['cfg']['CSPAllow']
470 . ($https ?
"" : $mapTilesUrls)
475 "X-WebKit-CSP: default-src 'self' "
476 . $GLOBALS['cfg']['CSPAllow'] . ';'
477 . "script-src 'self' "
478 . $GLOBALS['cfg']['CSPAllow']
479 . " 'unsafe-inline' 'unsafe-eval';"
480 . "style-src 'self' 'unsafe-inline';"
481 . "img-src 'self' data: "
482 . $GLOBALS['cfg']['CSPAllow']
483 . ($https ?
"" : $mapTilesUrls)
489 if (! defined('IS_TRANSFORMATION_WRAPPER') && ! defined('TESTSUITE')) {
490 // Define the charset to be used
491 header('Content-Type: text/html; charset=utf-8');
493 $this->_headerIsSent
= true;
497 * Returns the DOCTYPE and the start HTML tag
499 * @return string DOCTYPE and HTML tags
501 private function _getHtmlStart()
503 $lang = $GLOBALS['available_languages'][$GLOBALS['lang']][1];
504 $dir = $GLOBALS['text_dir'];
506 $retval = "<!DOCTYPE HTML>";
507 $retval .= "<html lang='$lang' dir='$dir'>";
513 * Returns the META tags
515 * @return string the META tags
517 private function _getMetaTags()
519 $retval = '<meta charset="utf-8" />';
520 $retval .= '<meta name="robots" content="noindex,nofollow" />';
521 $retval .= '<meta http-equiv="X-UA-Compatible" content="IE=Edge">';
526 * Returns the LINK tags for the favicon and the stylesheets
528 * @return string the LINK tags
530 private function _getLinkTags()
532 $retval = '<link rel="icon" href="favicon.ico" '
533 . 'type="image/x-icon" />'
534 . '<link rel="shortcut icon" href="favicon.ico" '
535 . 'type="image/x-icon" />';
537 $basedir = defined('PMA_PATH_TO_BASEDIR') ? PMA_PATH_TO_BASEDIR
: '';
538 $common_url = PMA_generate_common_url(array('server' => $GLOBALS['server']));
539 $theme_id = $GLOBALS['PMA_Config']->getThemeUniqueValue();
540 $theme_path = $GLOBALS['pmaThemePath'];
542 if ($this->_isPrintView
) {
543 $retval .= '<link rel="stylesheet" type="text/css" href="'
544 . $basedir . 'print.css" />';
546 $retval .= '<link rel="stylesheet" type="text/css" href="'
547 . $basedir . 'phpmyadmin.css.php'
548 . $common_url . '&nocache='
549 . $theme_id . $GLOBALS['text_dir'] . '" />';
550 $retval .= '<link rel="stylesheet" type="text/css" href="'
551 . $theme_path . '/jquery/jquery-ui-1.9.2.custom.css" />';
558 * Returns the TITLE tag
560 * @return string the TITLE tag
562 public function getTitleTag()
565 $retval .= $this->_getPageTitle();
566 $retval .= "</title>";
571 * If the page is missing the title, this function
572 * will set it to something reasonable
576 private function _getPageTitle()
578 if (empty($this->_title
)) {
579 if ($GLOBALS['server'] > 0) {
580 if (! empty($GLOBALS['table'])) {
581 $temp_title = $GLOBALS['cfg']['TitleTable'];
582 } else if (! empty($GLOBALS['db'])) {
583 $temp_title = $GLOBALS['cfg']['TitleDatabase'];
584 } elseif (! empty($GLOBALS['cfg']['Server']['host'])) {
585 $temp_title = $GLOBALS['cfg']['TitleServer'];
587 $temp_title = $GLOBALS['cfg']['TitleDefault'];
589 $this->_title
= htmlspecialchars(
590 PMA_Util
::expandUserString($temp_title)
593 $this->_title
= 'phpMyAdmin';
596 return $this->_title
;
600 * Returns the close tag to the HEAD
601 * and the start tag for the BODY
603 * @return string HEAD and BODY tags
605 private function _getBodyStart()
607 $retval = "</head><body";
608 if (! empty($this->_bodyId
)) {
609 $retval .= " id='" . $this->_bodyId
. "'";
616 * Returns some warnings to be displayed at the top of the page
618 * @return string The warnings
620 private function _getWarnings()
623 if ($this->_warningsEnabled
) {
624 $retval .= "<noscript>";
625 $retval .= PMA_message
::error(
626 __("Javascript must be enabled past this point")
628 $retval .= "</noscript>";
634 * Add recently used table and reload the navigation.
636 * @param string $db Database name where the table is located.
637 * @param string $table The table name
641 private function _addRecentTable($db, $table)
644 if ($this->_menuEnabled
&& strlen($table) && $GLOBALS['cfg']['NumRecentTables'] > 0) {
645 $tmp_result = PMA_RecentTable
::getInstance()->add($db, $table);
646 if ($tmp_result === true) {
647 $params = array('ajax_request' => true, 'recent_table' => true);
648 $url = 'index.php' . PMA_generate_common_url($params);
649 $retval = '<a class="hide" id="update_recent_tables"';
650 $retval .= ' href="' . $url . '"></a>';
652 $error = $tmp_result;
653 $retval = $error->getDisplay();