From 97449dae42a11cfa7f4571aaadeab3fefc30acf7 Mon Sep 17 00:00:00 2001 From: Rouslan Placella Date: Wed, 23 May 2012 21:22:08 +0100 Subject: [PATCH] New classes PMA_Header and PMA_Scripts --- libraries/Header.class.php | 235 ++++++++++++++++++++++++++++++++++++ libraries/Menu.class.php | 23 +++- libraries/Scripts.class.php | 134 ++++++++++++++++++++ libraries/common.inc.php | 5 + 4 files changed, 394 insertions(+), 3 deletions(-) create mode 100644 libraries/Header.class.php create mode 100644 libraries/Scripts.class.php diff --git a/libraries/Header.class.php b/libraries/Header.class.php new file mode 100644 index 0000000000..e59f1caf4d --- /dev/null +++ b/libraries/Header.class.php @@ -0,0 +1,235 @@ +_menu = PMA_Menu::getInstance(); + $this->_scripts = new PMA_Scripts(); + $this->headerIsSent = false; + // if database storage for user preferences is transient, + // offer to load exported settings from localStorage + // (detection will be done in JavaScript) + $this->_userprefs_offer_import = false; + if ($GLOBALS['PMA_Config']->get('user_preferences') == 'session' + && ! isset($_SESSION['userprefs_autoload']) + ) { + $this->_userprefs_offer_import = true; + } + } + + public static function getInstance() + { + if (empty(self::$_instance)) { + self::$_instance = new PMA_Header(); + } + return self::$_instance; + } + + public function getScripts() + { + return $this->_scripts; + } + + public function display() + { + echo $this->getDisplay(); + } + + public function getDisplay() + { + $retval = ''; + if (! $this->headerIsSent) { + $this->headerIsSent = true; + $this->sendHttpHeaders(); + if ($GLOBALS['is_ajax_request'] === false) { + $retval .= $this->_getHtmlStart(); + $retval .= $this->_getMetaTags(); + $retval .= $this->_getLinkTags(); + $retval .= $this->_getTitleTag(); + if ($this->_userprefs_offer_import) { + $this->_scripts->addFile('config.js'); + } + $retval .= $this->_scripts->getDisplay(); + $retval .= $this->_getBodyStart(); + // Include possible custom headers + if (file_exists(CUSTOM_HEADER_FILE)) { + ob_start(); + include CUSTOM_HEADER_FILE; + $retval .= ob_end_clean(); + } + // offer to load user preferences from localStorage + if ($this->_userprefs_offer_import) { + include_once './libraries/user_preferences.lib.php'; + $retval .= PMA_userprefsAutoloadGetHeader(); + } + // pass configuration for hint tooltip display + // (to be used by PMA_createqTip in js/functions.js) + if (! $GLOBALS['cfg']['ShowHint']) { + $retval .= ''; + } + $retval .= $this->_getWarnings(); + if (! defined('PMA_DISPLAY_HEADING')) { + define('PMA_DISPLAY_HEADING', 1); + } + if (PMA_DISPLAY_HEADING && $GLOBALS['server'] > 0) { + $retval .= $this->_menu->getDisplay(); + } + $retval .= $this->_addRecentTable( + $GLOBALS['db'], + $GLOBALS['table'] + ); + } + } + return $retval; + } + + public function sendHttpHeaders() + { + $this->headerIsSent = true; + /** + * Starts output buffering work + */ + PMA_outBufferPre(); + /** + * Sends http headers + */ + $GLOBALS['now'] = gmdate('D, d M Y H:i:s') . ' GMT'; + /* Prevent against ClickJacking by allowing frames only from same origin */ + if (! $GLOBALS['cfg']['AllowThirdPartyFraming']) { + header('X-Frame-Options: SAMEORIGIN'); + header("X-Content-Security-Policy: allow 'self'; options inline-script eval-script; frame-ancestors 'self'; img-src 'self' data:; script-src 'self' http://www.phpmyadmin.net"); + header("X-WebKit-CSP: allow 'self' http://www.phpmyadmin.net; options inline-script eval-script"); + } + if (! defined('IS_TRANSFORMATION_WRAPPER')) { + // Define the charset to be used + header('Content-Type: text/html; charset=utf-8'); + } + PMA_no_cache_header(); + } + + private function _getHtmlStart() + { + $lang = $GLOBALS['available_languages'][$GLOBALS['lang']][1]; + $dir = $GLOBALS['text_dir']; + + $retval = ""; + $retval .= ""; + + return $retval; + } + + private function _getMetaTags() + { + $retval = ''; + $retval .= ''; + return $retval; + } + + private function _getLinkTags() + { + $retval = ''; + $retval .= ''; + + // stylesheets + $basedir = defined('PMA_PATH_TO_BASEDIR') ? PMA_PATH_TO_BASEDIR : ''; + $common_url = PMA_generate_common_url(array('server' => $GLOBALS['server'])); + $theme_id = $GLOBALS['PMA_Config']->getThemeUniqueValue(); + $theme_path = $GLOBALS['pmaThemePath']; + + $retval .= ''; + $retval .= ''; + $retval .= ''; + + return $retval; + } + + private function _getTitleTag() + { + $retval = ""; + if (empty($GLOBALS['page_title'])) { + $retval .= 'phpMyAdmin'; + } else { + $retval .= htmlspecialchars($GLOBALS['page_title']); + } + $retval .= ""; + return $retval; + } + + private function _getBodyStart() + { + return ""; + } + + private function _getWarnings() + { + $retval = ''; + // message of "Cookies required" displayed for auth_type http or config + // note: here, the decoration won't work because without cookies, + // our standard CSS is not operational + if (empty($_COOKIE)) { + $retval .= PMA_Message::notice( + __('Cookies must be enabled past this point.') + )->getDisplay(); + } + $retval .= ""; + return $retval; + } + + /** + * Add recently used table and reload the navigation. + * + * @param string $db Database name where the table is located. + * @param string $table The table name + */ + private function _addRecentTable($db, $table) + { + $retval = ''; + if (strlen($table) && $GLOBALS['cfg']['LeftRecentTable'] > 0) { + $tmp_result = PMA_RecentTable::getInstance()->add($db, $table); + if ($tmp_result === true) { + $retval = ''; + } else { + $error = $tmp_result; + $retval = $error->getDisplay(); + } + } + return $retval; + } +} + +?> diff --git a/libraries/Menu.class.php b/libraries/Menu.class.php index 4d4f5d668f..5b234346d2 100644 --- a/libraries/Menu.class.php +++ b/libraries/Menu.class.php @@ -86,12 +86,29 @@ class PMA_Menu */ public function display() { - echo $this->_getBreadcrumbs(); - echo $this->_getMenu(); + echo $this->getDisplay(); + } + + /** + * Returns the menu and the breadcrumbs as a string + * + * @return string + */ + public function getDisplay() + { + $retval = $this->_getBreadcrumbs(); + $retval .= $this->_getMenu(); if (! empty($GLOBALS['message'])) { - echo PMA_getMessage($GLOBALS['message']); + if (isset($GLOBALS['buffer_message'])) { + $buffer_message = $GLOBALS['buffer_message']; + } + $retval .= PMA_getMessage($GLOBALS['message']); unset($GLOBALS['message']); + if (isset($buffer_message)) { + $GLOBALS['buffer_message'] = $buffer_message; + } } + return $retval; } /** diff --git a/libraries/Scripts.class.php b/libraries/Scripts.class.php new file mode 100644 index 0000000000..9e8768fb52 --- /dev/null +++ b/libraries/Scripts.class.php @@ -0,0 +1,134 @@ +_files = array(); + $this->_code = ''; + $this->_events = array(); + // Include default scripts + $this->_addDefaults(); + + } + + public function addFile($filename) + { + if (! in_array($filename, $this->_files)) { + $this->_files[] = $filename; + } + } + + public function addCode($code) + { + $this->_code .= "$code\n"; + } + + public function addEvent($event, $function) + { + $this->_events[] = array( + 'event' => $event, + 'function' => $function + ); + } + + private function _addDefaults() + { + $this->addFile('jquery/jquery-1.6.2.js'); + $this->addFile('jquery/jquery-ui-1.8.16.custom.js'); + $this->addFile('jquery/jquery.sprintf.js'); + $this->addFile('update-location.js'); + + $this->addFile('functions.js'); + $this->addFile('jquery/jquery.qtip-1.0.0-rc3.js'); + if ($GLOBALS['cfg']['CodemirrorEnable']) { + $this->addFile('codemirror/lib/codemirror.js'); + $this->addFile('codemirror/mode/mysql/mysql.js'); + } + // Cross-framing protection + if ($GLOBALS['cfg']['AllowThirdPartyFraming'] === false) { + $this->addFile('cross_framing_protection.js'); + } + // Localised strings + $params = array('lang' => $GLOBALS['lang']); + if (isset($GLOBALS['db'])) { + $params['db'] = $GLOBALS['db']; + } + $this->addFile('messages.php' . PMA_generate_common_url($params)); + // Append the theme id to this url to invalidate + // the cache on a theme change + $this->addFile( + 'get_image.js.php?theme=' + . urlencode($_SESSION['PMA_Theme']->getId()) + ); + + // generate title (unless we already have + // $GLOBALS['page_title'], from cookie auth) + if (! isset($GLOBALS['page_title'])) { + if ($GLOBALS['server'] > 0) { + if (! empty($GLOBALS['table'])) { + $temp_title = $GLOBALS['cfg']['TitleTable']; + } else if (! empty($GLOBALS['db'])) { + $temp_title = $GLOBALS['cfg']['TitleDatabase']; + } elseif (! empty($GLOBALS['cfg']['Server']['host'])) { + $temp_title = $GLOBALS['cfg']['TitleServer']; + } else { + $temp_title = $GLOBALS['cfg']['TitleDefault']; + } + $title = PMA_expandUserString($temp_title); + } + } else { + $title = $GLOBALS['page_title']; + } + if (isset($title)) { + $title = PMA_sanitize( + PMA_escapeJsString($title), + false, + true + ); + $this->addCode( + "if (typeof(parent.document) != 'undefined'" + . " && typeof(parent.document) != 'unknown'" + . " && typeof(parent.document.title) == 'string')" + . "{" + . "parent.document.title = '$title'" + . "}" + ); + } + $this->addCode(PMA_getReloadNavigationScript(true)); + } + + public function getDisplay() + { + $retval = ''; + + foreach ($this->_files as $file) { + $retval .= PMA_includeJS($file); + } + $retval .= ''; + + return $retval; + } +} diff --git a/libraries/common.inc.php b/libraries/common.inc.php index c3a0f6a2f8..6054355c06 100644 --- a/libraries/common.inc.php +++ b/libraries/common.inc.php @@ -1106,4 +1106,9 @@ if (!empty($__redirect) && in_array($__redirect, $goto_whitelist)) { include $__redirect; exit(); } + +/** + * Used to generate the header of the page + */ +include_once 'libraries/Header.class.php'; ?>