added japanese language
[openemr.git] / phpmyadmin / libraries / Header.class.php
blobbc1e4a6f1329c70c17d5b501155ef8bff331ca03
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Used to render the header of PMA's pages
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
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/navigation/Navigation.class.php';
16 require_once 'libraries/url_generating.lib.php';
19 /**
20 * Class used to output the HTTP and HTML headers
22 * @package PhpMyAdmin
24 class PMA_Header
26 /**
27 * PMA_Scripts instance
29 * @access private
30 * @var PMA_Scripts
32 private $_scripts;
33 /**
34 * PMA_Menu instance
36 * @access private
37 * @var PMA_Menu
39 private $_menu;
40 /**
41 * Whether to offer the option of importing user settings
43 * @access private
44 * @var bool
46 private $_userprefsOfferImport;
47 /**
48 * The page title
50 * @access private
51 * @var string
53 private $_title;
54 /**
55 * The value for the id attribute for the body tag
57 * @access private
58 * @var string
60 private $_bodyId;
61 /**
62 * Whether to show the top menu
64 * @access private
65 * @var bool
67 private $_menuEnabled;
68 /**
69 * Whether to show the warnings
71 * @access private
72 * @var bool
74 private $_warningsEnabled;
75 /**
76 * Whether the page is in 'print view' mode
78 * @access private
79 * @var bool
81 private $_isPrintView;
82 /**
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.
87 * @access private
88 * @var bool
90 private $_isAjax;
91 /**
92 * Whether to display anything
94 * @access private
95 * @var bool
97 private $_isEnabled;
98 /**
99 * Whether the HTTP headers (and possibly some HTML)
100 * have already been sent to the browser
102 * @access private
103 * @var bool
105 private $_headerIsSent;
108 * Creates a new class instance
110 public function __construct()
112 $this->_isEnabled = true;
113 $this->_isAjax = false;
114 $this->_bodyId = '';
115 $this->_title = '';
116 $db = ! empty($GLOBALS['db']) ? $GLOBALS['db'] : '';
117 $table = ! empty($GLOBALS['table']) ? $GLOBALS['table'] : '';
118 $this->_menu = new PMA_Menu(
119 $GLOBALS['server'],
120 $db,
121 $table
123 $this->_menuEnabled = true;
124 $this->_warningsEnabled = true;
125 $this->_isPrintView = false;
126 $this->_scripts = new PMA_Scripts();
127 $this->_addDefaultScripts();
128 $this->_headerIsSent = false;
129 // if database storage for user preferences is transient,
130 // offer to load exported settings from localStorage
131 // (detection will be done in JavaScript)
132 $this->_userprefsOfferImport = false;
133 if ($GLOBALS['PMA_Config']->get('user_preferences') == 'session'
134 && ! isset($_SESSION['userprefs_autoload'])
136 $this->_userprefsOfferImport = true;
141 * Loads common scripts
143 * @return void
145 private function _addDefaultScripts()
147 $this->_scripts->addFile('jquery/jquery-1.8.3.min.js');
148 $this->_scripts->addFile('ajax.js');
149 $this->_scripts->addFile('keyhandler.js');
150 $this->_scripts->addFile('jquery/jquery-ui-1.9.2.custom.min.js');
151 $this->_scripts->addFile('jquery/jquery.sprintf.js');
152 $this->_scripts->addFile('jquery/jquery.cookie.js');
153 $this->_scripts->addFile('jquery/jquery.mousewheel.js');
154 $this->_scripts->addFile('jquery/jquery.event.drag-2.2.js');
155 $this->_scripts->addFile('jquery/jquery-ui-timepicker-addon.js');
156 $this->_scripts->addFile('jquery/jquery.ba-hashchange-1.3.js');
157 $this->_scripts->addFile('jquery/jquery.debounce-1.0.5.js');
158 $this->_scripts->addFile('jquery/jquery.menuResizer-1.0.js');
160 // Cross-framing protection
161 if ($GLOBALS['cfg']['AllowThirdPartyFraming'] === false) {
162 $this->_scripts->addFile('cross_framing_protection.js');
165 $this->_scripts->addFile('rte.js');
166 if ($GLOBALS['cfg']['SendErrorReports'] !== 'never') {
167 $this->_scripts->addFile('tracekit/tracekit.js');
168 $this->_scripts->addFile('error_report.js');
171 // Here would not be a good place to add CodeMirror because
172 // the user preferences have not been merged at this point
174 // Localised strings
175 $params = array('lang' => $GLOBALS['lang']);
176 if (isset($GLOBALS['db'])) {
177 $params['db'] = $GLOBALS['db'];
179 $this->_scripts->addFile('messages.php' . PMA_URL_getCommon($params));
180 // Append the theme id to this url to invalidate
181 // the cache on a theme change. Though this might be
182 // unavailable for fatal errors.
183 if (isset($_SESSION['PMA_Theme'])) {
184 $theme_id = urlencode($_SESSION['PMA_Theme']->getId());
185 } else {
186 $theme_id = 'default';
188 $this->_scripts->addFile(
189 'get_image.js.php?theme=' . $theme_id
191 $this->_scripts->addFile('doclinks.js');
192 $this->_scripts->addFile('functions.js');
193 $this->_scripts->addFile('navigation.js');
194 $this->_scripts->addFile('indexes.js');
195 $this->_scripts->addFile('common.js');
196 $this->_scripts->addCode($this->getJsParamsCode());
200 * Returns, as an array, a list of parameters
201 * used on the client side
203 * @return array
205 public function getJsParams()
207 $db = ! empty($GLOBALS['db']) ? $GLOBALS['db'] : '';
208 $table = ! empty($GLOBALS['table']) ? $GLOBALS['table'] : '';
209 return array(
210 'common_query' => PMA_URL_getCommon('', '', '&'),
211 'opendb_url' => $GLOBALS['cfg']['DefaultTabDatabase'],
212 'safari_browser' => PMA_USR_BROWSER_AGENT == 'SAFARI' ? 1 : 0,
213 'querywindow_height' => $GLOBALS['cfg']['QueryWindowHeight'],
214 'querywindow_width' => $GLOBALS['cfg']['QueryWindowWidth'],
215 'collation_connection' => $GLOBALS['collation_connection'],
216 'lang' => $GLOBALS['lang'],
217 'server' => $GLOBALS['server'],
218 'table' => $table,
219 'db' => $db,
220 'token' => $_SESSION[' PMA_token '],
221 'text_dir' => $GLOBALS['text_dir'],
222 'pma_absolute_uri' => $GLOBALS['cfg']['PmaAbsoluteUri'],
223 'pma_text_default_tab' => PMA_Util::getTitleForTarget(
224 $GLOBALS['cfg']['DefaultTabTable']
226 'pma_text_left_default_tab' => PMA_Util::getTitleForTarget(
227 $GLOBALS['cfg']['NavigationTreeDefaultTabTable']
229 'confirm' => $GLOBALS['cfg']['Confirm']
234 * Returns, as a string, a list of parameters
235 * used on the client side
237 * @return string
239 public function getJsParamsCode()
241 $params = $this->getJsParams();
242 foreach ($params as $key => $value) {
243 $params[$key] = $key . ':"' . PMA_escapeJsString($value) . '"';
245 return 'PMA_commonParams.setAll({' . implode(',', $params) . '});';
249 * Disables the rendering of the header
251 * @return void
253 public function disable()
255 $this->_isEnabled = false;
259 * Set the ajax flag to indicate whether
260 * we are sevicing an ajax request
262 * @param bool $isAjax Whether we are sevicing an ajax request
264 * @return void
266 public function setAjax($isAjax)
268 $this->_isAjax = ($isAjax == true);
272 * Returns the PMA_Scripts object
274 * @return PMA_Scripts object
276 public function getScripts()
278 return $this->_scripts;
282 * Returns the PMA_Menu object
284 * @return PMA_Menu object
286 public function getMenu()
288 return $this->_menu;
292 * Setter for the ID attribute in the BODY tag
294 * @param string $id Value for the ID attribute
296 * @return void
298 public function setBodyId($id)
300 $this->_bodyId = htmlspecialchars($id);
304 * Setter for the title of the page
306 * @param string $title New title
308 * @return void
310 public function setTitle($title)
312 $this->_title = htmlspecialchars($title);
316 * Disables the display of the top menu
318 * @return void
320 public function disableMenu()
322 $this->_menuEnabled = false;
326 * Disables the display of the top menu
328 * @return void
330 public function disableWarnings()
332 $this->_warningsEnabled = false;
336 * Turns on 'print view' mode
338 * @return void
340 public function enablePrintView()
342 $this->disableMenu();
343 $this->setTitle(__('Print view') . ' - phpMyAdmin ' . PMA_VERSION);
344 $this->_isPrintView = true;
348 * Generates the header
350 * @return string The header
352 public function getDisplay()
354 $retval = '';
355 if (! $this->_headerIsSent) {
356 if (! $this->_isAjax && $this->_isEnabled) {
357 $this->sendHttpHeaders();
358 $retval .= $this->_getHtmlStart();
359 $retval .= $this->_getMetaTags();
360 $retval .= $this->_getLinkTags();
361 $retval .= $this->getTitleTag();
363 // The user preferences have been merged at this point
364 // so we can conditionally add CodeMirror
365 if ($GLOBALS['cfg']['CodemirrorEnable']) {
366 $this->_scripts->addFile('codemirror/lib/codemirror.js');
367 $this->_scripts->addFile('codemirror/mode/sql/sql.js');
368 $this->_scripts->addFile('codemirror/addon/runmode/runmode.js');
370 if ($this->_userprefsOfferImport) {
371 $this->_scripts->addFile('config.js');
373 $retval .= $this->_scripts->getDisplay();
374 $retval .= $this->_getBodyStart();
375 if ($this->_menuEnabled && $GLOBALS['server'] > 0) {
376 $nav = new PMA_Navigation();
377 $retval .= $nav->getDisplay();
379 // Include possible custom headers
380 if (file_exists(CUSTOM_HEADER_FILE)) {
381 $retval .= '<div id="pma_header">';
382 ob_start();
383 include CUSTOM_HEADER_FILE;
384 $retval .= ob_get_contents();
385 ob_end_clean();
386 $retval .= '</div>';
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();
401 $retval .= sprintf(
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(
412 $GLOBALS['db'],
413 $GLOBALS['table']
417 return $retval;
421 * Returns the message to be displayed at the top of
422 * the page, including the executed SQL query, if any.
424 * @return string
426 public function getMessage()
428 $retval = '';
429 $message = '';
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;
445 return $retval;
449 * Sends out the HTTP headers
451 * @return void
453 public function sendHttpHeaders()
455 $https = $GLOBALS['PMA_Config']->isHttps();
456 $mapTilesUrls = ' *.tile.openstreetmap.org *.tile.opencyclemap.org';
459 * Sends http headers
461 $GLOBALS['now'] = gmdate('D, d M Y H:i:s') . ' GMT';
462 if (! defined('TESTSUITE')) {
463 $use_captcha = (
464 !empty($GLOBALS['cfg']['CaptchaLoginPrivateKey'])
465 && !empty($GLOBALS['cfg']['CaptchaLoginPublicKey'])
467 /* Prevent against ClickJacking by disabling framing */
468 if (! $GLOBALS['cfg']['AllowThirdPartyFraming']) {
469 header(
470 'X-Frame-Options: DENY'
473 header(
474 "Content-Security-Policy: default-src 'self' "
475 . ($use_captcha ? 'https://www.google.com ' : ' ')
476 . $GLOBALS['cfg']['CSPAllow'] . ';'
477 . "script-src 'self' 'unsafe-inline' 'unsafe-eval' "
478 . ($use_captcha ? 'https://www.google.com ' : ' ')
479 . $GLOBALS['cfg']['CSPAllow'] . ';'
480 . ";"
481 . "style-src 'self' 'unsafe-inline' "
482 . ($use_captcha ? 'https://www.google.com ' : ' ')
483 . $GLOBALS['cfg']['CSPAllow']
484 . ";"
485 . "img-src 'self' data: "
486 . $GLOBALS['cfg']['CSPAllow']
487 . ($https ? "" : $mapTilesUrls)
488 // for reCAPTCHA
489 . ($use_captcha ? ' https://www.google.com' : ' ')
490 . ";"
492 header(
493 "X-Content-Security-Policy: default-src 'self' "
494 . ($use_captcha ? 'https://www.google.com ' : ' ')
495 . $GLOBALS['cfg']['CSPAllow'] . ';'
496 . "options inline-script eval-script;"
497 . "img-src 'self' data: "
498 . $GLOBALS['cfg']['CSPAllow']
499 . ($https ? "" : $mapTilesUrls)
500 // for reCAPTCHA
501 . ($use_captcha ? ' https://www.google.com' : ' ')
502 . ";"
504 if (PMA_USR_BROWSER_AGENT == 'SAFARI'
505 && PMA_USR_BROWSER_VER < '6.0.0'
507 header(
508 "X-WebKit-CSP: allow 'self' "
509 . ($use_captcha ? 'https://www.google.com ' : ' ')
510 . $GLOBALS['cfg']['CSPAllow'] . ';'
511 . "options inline-script eval-script;"
512 . "img-src 'self' data: "
513 . $GLOBALS['cfg']['CSPAllow']
514 . ($https ? "" : $mapTilesUrls)
515 // for reCAPTCHA
516 . ($use_captcha ? ' https://www.google.com' : ' ')
517 . ";"
519 } else {
520 header(
521 "X-WebKit-CSP: default-src 'self' "
522 . ($use_captcha ? 'https://www.google.com ' : ' ')
523 . $GLOBALS['cfg']['CSPAllow'] . ';'
524 . "script-src 'self' "
525 . ($use_captcha ? 'https://www.google.com ' : ' ')
526 . $GLOBALS['cfg']['CSPAllow']
527 . " 'unsafe-inline' 'unsafe-eval';"
528 . "style-src 'self' 'unsafe-inline' "
529 . ($use_captcha ? 'https://www.google.com ' : ' ')
530 . ';'
531 . "img-src 'self' data: "
532 . $GLOBALS['cfg']['CSPAllow']
533 . ($https ? "" : $mapTilesUrls)
534 // for reCAPTCHA
535 . ($use_captcha ? ' https://www.google.com' : ' ')
536 . ";"
540 PMA_noCacheHeader();
541 if (! defined('IS_TRANSFORMATION_WRAPPER') && ! defined('TESTSUITE')) {
542 // Define the charset to be used
543 header('Content-Type: text/html; charset=utf-8');
545 $this->_headerIsSent = true;
549 * Returns the DOCTYPE and the start HTML tag
551 * @return string DOCTYPE and HTML tags
553 private function _getHtmlStart()
555 $lang = $GLOBALS['available_languages'][$GLOBALS['lang']][1];
556 $dir = $GLOBALS['text_dir'];
558 $retval = "<!DOCTYPE HTML>";
559 $retval .= "<html lang='$lang' dir='$dir' class='";
560 $retval .= strtolower(PMA_USR_BROWSER_AGENT) . " ";
561 $retval .= strtolower(PMA_USR_BROWSER_AGENT)
562 . intval(PMA_USR_BROWSER_VER) . "'>";
564 return $retval;
568 * Returns the META tags
570 * @return string the META tags
572 private function _getMetaTags()
574 $retval = '<meta charset="utf-8" />';
575 $retval .= '<meta name="robots" content="noindex,nofollow" />';
576 $retval .= '<meta http-equiv="X-UA-Compatible" content="IE=Edge">';
577 if (! $GLOBALS['cfg']['AllowThirdPartyFraming']) {
578 $retval .= '<style id="cfs-style">html{display: none;}</style>';
580 return $retval;
584 * Returns the LINK tags for the favicon and the stylesheets
586 * @return string the LINK tags
588 private function _getLinkTags()
590 $retval = '<link rel="icon" href="favicon.ico" '
591 . 'type="image/x-icon" />'
592 . '<link rel="shortcut icon" href="favicon.ico" '
593 . 'type="image/x-icon" />';
594 // stylesheets
595 $basedir = defined('PMA_PATH_TO_BASEDIR') ? PMA_PATH_TO_BASEDIR : '';
596 $common_url = PMA_URL_getCommon(array('server' => $GLOBALS['server']));
597 $theme_id = $GLOBALS['PMA_Config']->getThemeUniqueValue();
598 $theme_path = $GLOBALS['pmaThemePath'];
600 if ($this->_isPrintView) {
601 $retval .= '<link rel="stylesheet" type="text/css" href="'
602 . $basedir . 'print.css" />';
603 } else {
604 $retval .= '<link rel="stylesheet" type="text/css" href="'
605 . $basedir . 'phpmyadmin.css.php'
606 . $common_url . '&amp;nocache='
607 . $theme_id . $GLOBALS['text_dir'] . '" />';
608 $retval .= '<link rel="stylesheet" type="text/css" href="'
609 . $theme_path . '/jquery/jquery-ui-1.9.2.custom.css" />';
612 return $retval;
616 * Returns the TITLE tag
618 * @return string the TITLE tag
620 public function getTitleTag()
622 $retval = "<title>";
623 $retval .= $this->_getPageTitle();
624 $retval .= "</title>";
625 return $retval;
629 * If the page is missing the title, this function
630 * will set it to something reasonable
632 * @return string
634 private function _getPageTitle()
636 if (empty($this->_title)) {
637 if ($GLOBALS['server'] > 0) {
638 if (! empty($GLOBALS['table'])) {
639 $temp_title = $GLOBALS['cfg']['TitleTable'];
640 } else if (! empty($GLOBALS['db'])) {
641 $temp_title = $GLOBALS['cfg']['TitleDatabase'];
642 } elseif (! empty($GLOBALS['cfg']['Server']['host'])) {
643 $temp_title = $GLOBALS['cfg']['TitleServer'];
644 } else {
645 $temp_title = $GLOBALS['cfg']['TitleDefault'];
647 $this->_title = htmlspecialchars(
648 PMA_Util::expandUserString($temp_title)
650 } else {
651 $this->_title = 'phpMyAdmin';
654 return $this->_title;
658 * Returns the close tag to the HEAD
659 * and the start tag for the BODY
661 * @return string HEAD and BODY tags
663 private function _getBodyStart()
665 $retval = "</head><body";
666 if (! empty($this->_bodyId)) {
667 $retval .= " id='" . $this->_bodyId . "'";
669 $retval .= ">";
670 return $retval;
674 * Returns some warnings to be displayed at the top of the page
676 * @return string The warnings
678 private function _getWarnings()
680 $retval = '';
681 if ($this->_warningsEnabled) {
682 $retval .= "<noscript>";
683 $retval .= PMA_message::error(
684 __("Javascript must be enabled past this point!")
685 )->getDisplay();
686 $retval .= "</noscript>";
688 return $retval;
692 * Add recently used table and reload the navigation.
694 * @param string $db Database name where the table is located.
695 * @param string $table The table name
697 * @return string
699 private function _addRecentTable($db, $table)
701 $retval = '';
702 if ($this->_menuEnabled
703 && strlen($table)
704 && $GLOBALS['cfg']['NumRecentTables'] > 0
706 $tmp_result = PMA_RecentFavoriteTable::getInstance('recent')->add($db, $table);
707 if ($tmp_result === true) {
708 $params = array('ajax_request' => true, 'recent_table' => true);
709 $url = 'index.php' . PMA_URL_getCommon($params);
710 $retval = '<a class="hide" id="update_recent_tables"';
711 $retval .= ' href="' . $url . '"></a>';
712 } else {
713 $error = $tmp_result;
714 $retval = $error->getDisplay();
717 return $retval;