Skip to content

Commit

Permalink
Added PMA_OutputBuffering class, dropped ob.lib.php
Browse files Browse the repository at this point in the history
  • Loading branch information
roccivic committed Jun 11, 2012
1 parent a054562 commit 41cb6e2
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 136 deletions.
1 change: 0 additions & 1 deletion chk_rel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@
$response->addHTML(
PMA_getRelationsParamDiagnostic(PMA_getRelationsParam())
);
$response->response();

?>
26 changes: 23 additions & 3 deletions libraries/Footer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -226,6 +236,16 @@ private function _setHistory()
}
}

/**
*
*
* @return
*/
public function isAjax($isAjax)
{
$this->_isAjax = $isAjax;
}

/**
* Returns the PMA_Footnotes object
*
Expand All @@ -246,15 +266,15 @@ public function getDisplay()
$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;
}
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', '');
Expand All @@ -268,7 +288,7 @@ public function getDisplay()
$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)) {
Expand Down
20 changes: 11 additions & 9 deletions libraries/Header.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
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';
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -199,6 +195,16 @@ private function _addDefaultScripts()
$this->_scripts->addCode(PMA_getReloadNavigationScript(true));
}

/**
*
*
* @return
*/
public function isAjax($isAjax)
{
$this->_isAjax = $isAjax;
}

/**
* Returns the PMA_Scripts object
*
Expand Down Expand Up @@ -275,7 +281,7 @@ public function getDisplay()
$retval = '';
if (! self::$headerIsSent) {
$this->sendHttpHeaders();
if ($this->_isAjax === false) {
if (! $this->_isAjax) {
$retval .= $this->_getHtmlStart();
$retval .= $this->_getMetaTags();
$retval .= $this->_getLinkTags();
Expand Down Expand Up @@ -321,10 +327,6 @@ public function getDisplay()
*/
public function sendHttpHeaders()
{
/**
* Starts output buffering work
*/
PMA_outBufferPre();
/**
* Sends http headers
*/
Expand Down
4 changes: 0 additions & 4 deletions libraries/Menu.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,6 @@ private function _getBreadcrumbs()
} // 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
Expand Down
129 changes: 129 additions & 0 deletions libraries/OutputBuffering.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
*
*
* @package PhpMyAdmin
*/
if (! defined('PHPMYADMIN')) {
exit;
}

/**
*
*
* @package PhpMyAdmin
*/
class PMA_OutputBuffering
{
private static $_instance;
private $_mode;
private $_content;
private $_on;

private function __construct()
{
$this->_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
*/
}
}

?>
47 changes: 42 additions & 5 deletions libraries/Response.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
exit;
}

require_once 'libraries/OutputBuffering.class.php';
require_once 'libraries/Header.class.php';
require_once 'libraries/Footer.class.php';

Expand All @@ -32,17 +33,35 @@ 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
*
* @return new PMA_Response object
*/
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);
}

/**
Expand Down Expand Up @@ -83,14 +102,32 @@ private function getDisplay()
return $retval;
}

public function response()
public function simpleResponse()
{
echo $this->getDisplay();
exit;
}

public function ajaxResponse()
{
echo $this->getDisplay();
//PMA_ajaxResponse($this->getDisplay()); // FIXME
exit;
}

public static function response()
{
if (! $GLOBALS['is_ajax_request']) {
echo $this->getDisplay();
$response = self::$_instance;
$buffer = PMA_OutputBuffering::getInstance();
if (empty($response->_content)) {
$response->_content = $buffer->getContents();
}
if ($response->_isAjax) {
$response->ajaxResponse();
} else {
// FIXME
$response->simpleResponse();
}
exit;
$buffer->flush();
}
}

Expand Down
4 changes: 2 additions & 2 deletions libraries/footer.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
exit;
}

$footer = PMA_Response::getInstance()->getFooter();
$footer->display();
//$footer = PMA_Response::getInstance()->getFooter();
//$footer->display();

exit;

Expand Down
Loading

0 comments on commit 41cb6e2

Please sign in to comment.