Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / Header.class.php
blob1a934c6c49e157b2ef1ed3cdd5c6328dd92c10d8
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/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';
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 object
32 private $_scripts;
33 /**
34 * PMA_Menu instance
36 * @access private
37 * @var object
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 * @return new PMA_Header object
112 public function __construct()
114 $this->_isEnabled = true;
115 $this->_isAjax = false;
116 $this->_bodyId = '';
117 $this->_title = '';
118 $db = ! empty($GLOBALS['db']) ? $GLOBALS['db'] : '';
119 $table = ! empty($GLOBALS['table']) ? $GLOBALS['table'] : '';
120 $this->_menu = new PMA_Menu(
121 $GLOBALS['server'],
122 $db,
123 $table
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
145 * @return void
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
166 // Localised strings
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());
177 } else {
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
194 * @return array
196 public function getJsParams()
198 $db = ! empty($GLOBALS['db']) ? $GLOBALS['db'] : '';
199 $table = ! empty($GLOBALS['table']) ? $GLOBALS['table'] : '';
200 return array(
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'],
209 'table' => $table,
210 'db' => $db,
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
228 * @return string
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
242 * @return void
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
255 * @return void
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()
279 return $this->_menu;
283 * Setter for the ID attribute in the BODY tag
285 * @param string $id Value for the ID attribute
287 * @return void
289 public function setBodyId($id)
291 $this->_bodyId = htmlspecialchars($id);
295 * Setter for the title of the page
297 * @param string $title New title
299 * @return void
301 public function setTitle($title)
303 $this->_title = htmlspecialchars($title);
307 * Disables the display of the top menu
309 * @return void
311 public function disableMenu()
313 $this->_menuEnabled = false;
317 * Disables the display of the top menu
319 * @return void
321 public function disableWarnings()
323 $this->_warningsEnabled = false;
327 * Turns on 'print view' mode
329 * @return void
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()
345 $retval = '';
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)) {
371 ob_start();
372 include CUSTOM_HEADER_FILE;
373 $retval .= ob_get_contents();
374 ob_end_clean();
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>';
390 $retval .= sprintf(
391 $pagetop_link,
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(
401 $GLOBALS['db'],
402 $GLOBALS['table']
406 return $retval;
410 * Returns the message to be displayed at the top of
411 * the page, including the executed SQL query, if any.
413 * @return string
415 public function getMessage()
417 $retval = '';
418 $message = '';
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;
434 return $retval;
438 * Sends out the HTTP headers
440 * @return void
442 public function sendHttpHeaders()
444 $https = $GLOBALS['PMA_Config']->isHttps();
445 $mapTilesUrls = ' *.tile.openstreetmap.org *.tile.opencyclemap.org';
448 * Sends http headers
450 $GLOBALS['now'] = gmdate('D, d M Y H:i:s') . ' GMT';
451 if (! defined('TESTSUITE')) {
452 header(
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)
459 . ";"
461 if (PMA_USR_BROWSER_AGENT == 'SAFARI'
462 && PMA_USR_BROWSER_VER < '6.0.0'
464 header(
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)
471 . ";"
473 } else {
474 header(
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)
484 . ";"
488 PMA_noCacheHeader();
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'>";
509 return $retval;
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">';
522 return $retval;
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" />';
536 // stylesheets
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" />';
545 } else {
546 $retval .= '<link rel="stylesheet" type="text/css" href="'
547 . $basedir . 'phpmyadmin.css.php'
548 . $common_url . '&amp;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" />';
554 return $retval;
558 * Returns the TITLE tag
560 * @return string the TITLE tag
562 public function getTitleTag()
564 $retval = "<title>";
565 $retval .= $this->_getPageTitle();
566 $retval .= "</title>";
567 return $retval;
571 * If the page is missing the title, this function
572 * will set it to something reasonable
574 * @return string
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'];
586 } else {
587 $temp_title = $GLOBALS['cfg']['TitleDefault'];
589 $this->_title = htmlspecialchars(
590 PMA_Util::expandUserString($temp_title)
592 } else {
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 . "'";
611 $retval .= ">";
612 return $retval;
616 * Returns some warnings to be displayed at the top of the page
618 * @return string The warnings
620 private function _getWarnings()
622 $retval = '';
623 if ($this->_warningsEnabled) {
624 $retval .= "<noscript>";
625 $retval .= PMA_message::error(
626 __("Javascript must be enabled past this point")
627 )->getDisplay();
628 $retval .= "</noscript>";
630 return $retval;
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
639 * @return string
641 private function _addRecentTable($db, $table)
643 $retval = '';
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>';
651 } else {
652 $error = $tmp_result;
653 $retval = $error->getDisplay();
656 return $retval;