From 41cb6e20b0285271e91d422dfd334220e2c89e86 Mon Sep 17 00:00:00 2001 From: Rouslan Placella Date: Fri, 1 Jun 2012 17:51:38 +0100 Subject: [PATCH] Added PMA_OutputBuffering class, dropped ob.lib.php --- chk_rel.php | 1 - libraries/Footer.class.php | 26 +++++- libraries/Header.class.php | 20 +++-- libraries/Menu.class.php | 4 - libraries/OutputBuffering.class.php | 129 ++++++++++++++++++++++++++++ libraries/Response.class.php | 49 +++++++++-- libraries/footer.inc.php | 4 +- libraries/ob.lib.php | 99 --------------------- navigation.php | 7 -- querywindow.php | 6 -- 10 files changed, 208 insertions(+), 137 deletions(-) create mode 100644 libraries/OutputBuffering.class.php delete mode 100644 libraries/ob.lib.php diff --git a/chk_rel.php b/chk_rel.php index 15824cc4df..915e8b24b4 100644 --- a/chk_rel.php +++ b/chk_rel.php @@ -11,6 +11,5 @@ $response = PMA_Response::getInstance(); $response->addHTML( PMA_getRelationsParamDiagnostic(PMA_getRelationsParam()) ); -$response->response(); ?> diff --git a/libraries/Footer.class.php b/libraries/Footer.class.php index ec31fe0077..b576909898 100644 --- a/libraries/Footer.class.php +++ b/libraries/Footer.class.php @@ -34,6 +34,16 @@ class PMA_Footer */ private $_scripts; + /** + * Whether we are servicing an ajax request. + * We can't simply use $GLOBALS['is_ajax_request'] + * here since it may have not been initialised yet. + * + * @access private + * @var bool + */ + private $_isAjax; + /** * Cretes a new class instance * @@ -226,6 +236,16 @@ class PMA_Footer } } + /** + * + * + * @return + */ + public function isAjax($isAjax) + { + $this->_isAjax = $isAjax; + } + /** * Returns the PMA_Footnotes object * @@ -246,7 +266,7 @@ class PMA_Footer $retval = ''; $this->_setHistory(); - if ($GLOBALS['is_ajax_request'] != true) { + if (! $this->_isAjax) { // Link to itself to replicate windows including frameset if (! isset($GLOBALS['checked_special'])) { $GLOBALS['checked_special'] = false; @@ -254,7 +274,7 @@ class PMA_Footer if (PMA_getenv('SCRIPT_NAME') && empty($_POST) && ! $GLOBALS['checked_special'] - && ! $GLOBALS['is_ajax_request'] + && ! $this->_isAjax ) { $url_params['target'] = basename(PMA_getenv('SCRIPT_NAME')); $url = PMA_generate_common_url($url_params, 'text', ''); @@ -268,7 +288,7 @@ class PMA_Footer $retval .= $this->_getDebugMessage(); $retval .= $this->_footnotes->getDisplay(); $retval .= $this->_getErrorMessages(); - if (! $GLOBALS['is_ajax_request']) { + if (! $this->_isAjax) { $retval .= $this->_scripts->getDisplay(); // Include possible custom footers if (file_exists(CUSTOM_FOOTER_FILE)) { diff --git a/libraries/Header.class.php b/libraries/Header.class.php index 040b530da3..f5d53f8726 100644 --- a/libraries/Header.class.php +++ b/libraries/Header.class.php @@ -8,7 +8,6 @@ if (! defined('PHPMYADMIN')) { exit; } -require_once 'libraries/ob.lib.php'; require_once 'libraries/Scripts.class.php'; require_once 'libraries/RecentTable.class.php'; require_once 'libraries/Menu.class.php'; @@ -102,9 +101,6 @@ class PMA_Header public function __construct() { $this->_isAjax = false; - if (isset($_REQUEST['ajax_request']) && $_REQUEST['ajax_request'] == true) { - $this->_isAjax = true; - } $this->_bodyId = ''; $this->_title = 'phpMyAdmin'; $this->_menu = new PMA_Menu( @@ -199,6 +195,16 @@ class PMA_Header $this->_scripts->addCode(PMA_getReloadNavigationScript(true)); } + /** + * + * + * @return + */ + public function isAjax($isAjax) + { + $this->_isAjax = $isAjax; + } + /** * Returns the PMA_Scripts object * @@ -275,7 +281,7 @@ class PMA_Header $retval = ''; if (! self::$headerIsSent) { $this->sendHttpHeaders(); - if ($this->_isAjax === false) { + if (! $this->_isAjax) { $retval .= $this->_getHtmlStart(); $retval .= $this->_getMetaTags(); $retval .= $this->_getLinkTags(); @@ -321,10 +327,6 @@ class PMA_Header */ public function sendHttpHeaders() { - /** - * Starts output buffering work - */ - PMA_outBufferPre(); /** * Sends http headers */ diff --git a/libraries/Menu.class.php b/libraries/Menu.class.php index cbabbf6374..f1b192929e 100644 --- a/libraries/Menu.class.php +++ b/libraries/Menu.class.php @@ -206,10 +206,6 @@ class PMA_Menu } // end if } else { // no table selected, display database comment if present - /** - * Settings for relations stuff - */ - include_once './libraries/relation.lib.php'; $cfgRelation = PMA_getRelationsParam(); // Get additional information about tables for tooltip is done diff --git a/libraries/OutputBuffering.class.php b/libraries/OutputBuffering.class.php new file mode 100644 index 0000000000..2941c7bcd1 --- /dev/null +++ b/libraries/OutputBuffering.class.php @@ -0,0 +1,129 @@ +_mode = $this->_getMode(); + $this->_on = false; + } + + /** + * This function could be used eventually to support more modes. + * + * @return integer the output buffer mode + */ + private function _getMode() + { + $mode = 0; + if ($GLOBALS['cfg']['OBGzip'] && function_exists('ob_start')) { + if (ini_get('output_handler') == 'ob_gzhandler') { + // If a user sets the output_handler in php.ini to ob_gzhandler, then + // any right frame file in phpMyAdmin will not be handled properly by + // the browser. My fix was to check the ini file within the + // PMA_outBufferModeGet() function. + $mode = 0; + } elseif (function_exists('ob_get_level') && ob_get_level() > 0) { + // If output buffering is enabled in php.ini it's not possible to + // add the ob_gzhandler without a warning message from php 4.3.0. + // Being better safe than sorry, check for any existing output handler + // instead of just checking the 'output_buffering' setting. + $mode = 0; + } else { + $mode = 1; + } + } + // Zero (0) is no mode or in other words output buffering is OFF. + // Follow 2^0, 2^1, 2^2, 2^3 type values for the modes. + // Usefull if we ever decide to combine modes. Then a bitmask field of + // the sum of all modes will be the natural choice. + return $mode; + } + + /** + * Returns the singleton PMA_Response object + * + * @return PMA_Response object + */ + public static function getInstance() + { + if (empty(self::$_instance)) { + self::$_instance = new PMA_OutputBuffering(); + } + return self::$_instance; + } + + /** + * This function will need to run at the top of all pages if output + * output buffering is turned on. It also needs to be passed $mode from + * the PMA_outBufferModeGet() function or it will be useless. + * + */ + public function start() + { + if (! $this->_on) { + if ($this->_mode) { + ob_start('ob_gzhandler'); + } + ob_start(); + header('X-ob_mode: ' . $this->_mode); + register_shutdown_function('PMA_OutputBuffering::stop'); + $this->_on = true; + } + } + + /** + * This function will need to run at the bottom of all pages if output + * buffering is turned on. It also needs to be passed $mode from the + * PMA_outBufferModeGet() function or it will be useless. + * + */ + public static function stop() + { + $buffer = PMA_OutputBuffering::getInstance(); + if ($buffer->_on) { + $buffer->_on = false; + $buffer->_content = ob_get_contents(); + ob_end_clean(); + } + PMA_Response::response(); + } + + public function getContents() + { + return $this->_content; + } + + public function flush() + { + if (ob_get_status() && $this->_mode) { + ob_flush(); + } + /** + * previously we had here an "else flush()" but some PHP versions + * (at least PHP 5.2.11) have a bug (49816) that produces garbled + * data + */ + } +} + +?> diff --git a/libraries/Response.class.php b/libraries/Response.class.php index f7c7a2edb7..370da62cbd 100644 --- a/libraries/Response.class.php +++ b/libraries/Response.class.php @@ -9,6 +9,7 @@ if (! defined('PHPMYADMIN')) { exit; } +require_once 'libraries/OutputBuffering.class.php'; require_once 'libraries/Header.class.php'; require_once 'libraries/Footer.class.php'; @@ -32,6 +33,16 @@ class PMA_Response private $_content; private $_footer; + /** + * Whether we are servicing an ajax request. + * We can't simply use $GLOBALS['is_ajax_request'] + * here since it may have not been initialised yet. + * + * @access private + * @var bool + */ + private $_isAjax; + /** * Cretes a new class instance * @@ -39,10 +50,18 @@ class PMA_Response */ private function __construct() { + $this->_isAjax = false; + if (isset($_REQUEST['ajax_request']) && $_REQUEST['ajax_request'] == true) { + $this->_isAjax = true; + } + $buffer = PMA_OutputBuffering::getInstance(); + $buffer->start(); $this->_data = array(); $this->_header = new PMA_Header(); + $this->_header->isAjax($this->_isAjax); $this->_content = ''; $this->_footer = new PMA_Footer(); + $this->_footer->isAjax($this->_isAjax); } /** @@ -83,15 +102,33 @@ class PMA_Response return $retval; } - public function response() + public function simpleResponse() { - if (! $GLOBALS['is_ajax_request']) { - echo $this->getDisplay(); - } else { - // FIXME - } + echo $this->getDisplay(); exit; } + + public function ajaxResponse() + { + echo $this->getDisplay(); + //PMA_ajaxResponse($this->getDisplay()); // FIXME + exit; + } + + public static function response() + { + $response = self::$_instance; + $buffer = PMA_OutputBuffering::getInstance(); + if (empty($response->_content)) { + $response->_content = $buffer->getContents(); + } + if ($response->_isAjax) { + $response->ajaxResponse(); + } else { + $response->simpleResponse(); + } + $buffer->flush(); + } } ?> diff --git a/libraries/footer.inc.php b/libraries/footer.inc.php index 80c1e66a6f..1f5e448c87 100644 --- a/libraries/footer.inc.php +++ b/libraries/footer.inc.php @@ -19,8 +19,8 @@ if (! defined('PHPMYADMIN')) { exit; } -$footer = PMA_Response::getInstance()->getFooter(); -$footer->display(); +//$footer = PMA_Response::getInstance()->getFooter(); +//$footer->display(); exit; diff --git a/libraries/ob.lib.php b/libraries/ob.lib.php deleted file mode 100644 index bb1b667038..0000000000 --- a/libraries/ob.lib.php +++ /dev/null @@ -1,99 +0,0 @@ - - * http://www.jeremybrand.com/Jeremy/Brand/Jeremy_Brand.html - * - * Check for all the needed functions for output buffering - * Make some wrappers for the top and bottoms of our files. - * - * @package PhpMyAdmin - */ -if (! defined('PHPMYADMIN')) { - exit; -} - -/** - * This function be used eventually to support more modes. It is needed - * because both header and footer functions must know what each other is - * doing. - * - * @staticvar integer remember last calculated value - * @return integer the output buffer mode - */ -function PMA_outBufferModeGet() -{ - static $mode = null; - - if (null !== $mode) { - return $mode; - } - - $mode = 0; - - if ($GLOBALS['cfg']['OBGzip'] && function_exists('ob_start')) { - if (ini_get('output_handler') == 'ob_gzhandler') { - // If a user sets the output_handler in php.ini to ob_gzhandler, then - // any right frame file in phpMyAdmin will not be handled properly by - // the browser. My fix was to check the ini file within the - // PMA_outBufferModeGet() function. - $mode = 0; - } elseif (function_exists('ob_get_level') && ob_get_level() > 0) { - // If output buffering is enabled in php.ini it's not possible to - // add the ob_gzhandler without a warning message from php 4.3.0. - // Being better safe than sorry, check for any existing output handler - // instead of just checking the 'output_buffering' setting. - $mode = 0; - } else { - $mode = 1; - } - } - - // Zero (0) is no mode or in other words output buffering is OFF. - // Follow 2^0, 2^1, 2^2, 2^3 type values for the modes. - // Usefull if we ever decide to combine modes. Then a bitmask field of - // the sum of all modes will be the natural choice. - - return $mode; -} // end of the 'PMA_outBufferModeGet()' function - - -/** - * This function will need to run at the top of all pages if output - * output buffering is turned on. It also needs to be passed $mode from - * the PMA_outBufferModeGet() function or it will be useless. - * - */ -function PMA_outBufferPre() -{ - if ($mode = PMA_outBufferModeGet()) { - ob_start('ob_gzhandler'); - } - - header('X-ob_mode: ' . $mode); - - register_shutdown_function('PMA_outBufferPost'); -} // end of the 'PMA_outBufferPre()' function - - -/** - * This function will need to run at the bottom of all pages if output - * buffering is turned on. It also needs to be passed $mode from the - * PMA_outBufferModeGet() function or it will be useless. - * - */ -function PMA_outBufferPost() -{ - if (ob_get_status() && PMA_outBufferModeGet()) { - ob_flush(); - } - /** - * previously we had here an "else flush()" but some PHP versions - * (at least PHP 5.2.11) have a bug (49816) that produces garbled - * data - */ -} // end of the 'PMA_outBufferPost()' function - -?> diff --git a/navigation.php b/navigation.php index 2e752fea4c..2f788e9ecc 100644 --- a/navigation.php +++ b/navigation.php @@ -62,13 +62,6 @@ if (empty($_SESSION['debug'])) { session_write_close(); } -/** - * the output compression library - */ -require_once 'libraries/ob.lib.php'; - -PMA_outBufferPre(); - /* * selects the database if there is only one on current server */ diff --git a/querywindow.php b/querywindow.php index da9e34fd1a..65df9af248 100644 --- a/querywindow.php +++ b/querywindow.php @@ -19,12 +19,6 @@ $is_superuser = PMA_isSuperuser(); */ require_once 'libraries/sql_query_form.lib.php'; -/** - * starts output buffering if requested and supported - */ -require_once 'libraries/ob.lib.php'; -PMA_outBufferPre(); - /** * load relation params */