OutputBuffering.class.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Output buffering wrapper
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. require_once './libraries/Util.class.php';
  12. /**
  13. * Output buffering wrapper class
  14. *
  15. * @package PhpMyAdmin
  16. */
  17. class PMA_OutputBuffering
  18. {
  19. private static $_instance;
  20. private $_mode;
  21. private $_content;
  22. private $_on;
  23. /**
  24. * Initializes class
  25. *
  26. * @return void
  27. */
  28. private function __construct()
  29. {
  30. $this->_mode = $this->_getMode();
  31. $this->_on = false;
  32. }
  33. /**
  34. * This function could be used eventually to support more modes.
  35. *
  36. * @return integer the output buffer mode
  37. */
  38. private function _getMode()
  39. {
  40. $mode = 0;
  41. if ($GLOBALS['cfg']['OBGzip'] && function_exists('ob_start')) {
  42. if (ini_get('output_handler') == 'ob_gzhandler') {
  43. // If a user sets the output_handler in php.ini to ob_gzhandler, then
  44. // any right frame file in phpMyAdmin will not be handled properly by
  45. // the browser. My fix was to check the ini file within the
  46. // PMA_outBufferModeGet() function.
  47. $mode = 0;
  48. } elseif (function_exists('ob_get_level') && ob_get_level() > 0) {
  49. // If output buffering is enabled in php.ini it's not possible to
  50. // add the ob_gzhandler without a warning message from php 4.3.0.
  51. // Being better safe than sorry, check for any existing output handler
  52. // instead of just checking the 'output_buffering' setting.
  53. $mode = 0;
  54. } else {
  55. $mode = 1;
  56. }
  57. }
  58. // Zero (0) is no mode or in other words output buffering is OFF.
  59. // Follow 2^0, 2^1, 2^2, 2^3 type values for the modes.
  60. // Usefull if we ever decide to combine modes. Then a bitmask field of
  61. // the sum of all modes will be the natural choice.
  62. return $mode;
  63. }
  64. /**
  65. * Returns the singleton PMA_OutputBuffering object
  66. *
  67. * @return PMA_OutputBuffering object
  68. */
  69. public static function getInstance()
  70. {
  71. if (empty(self::$_instance)) {
  72. self::$_instance = new PMA_OutputBuffering();
  73. }
  74. return self::$_instance;
  75. }
  76. /**
  77. * This function will need to run at the top of all pages if output
  78. * output buffering is turned on. It also needs to be passed $mode from
  79. * the PMA_outBufferModeGet() function or it will be useless.
  80. *
  81. * @return void
  82. */
  83. public function start()
  84. {
  85. if (! $this->_on) {
  86. if ($this->_mode) {
  87. ob_start('ob_gzhandler');
  88. }
  89. ob_start();
  90. if (! defined('TESTSUITE')) {
  91. header('X-ob_mode: ' . $this->_mode);
  92. }
  93. register_shutdown_function(array('PMA_OutputBuffering', 'stop'));
  94. $this->_on = true;
  95. }
  96. }
  97. /**
  98. * This function will need to run at the bottom of all pages if output
  99. * buffering is turned on. It also needs to be passed $mode from the
  100. * PMA_outBufferModeGet() function or it will be useless.
  101. *
  102. * @return void
  103. */
  104. public static function stop()
  105. {
  106. $buffer = PMA_OutputBuffering::getInstance();
  107. if ($buffer->_on) {
  108. $buffer->_on = false;
  109. $buffer->_content = ob_get_contents();
  110. ob_end_clean();
  111. }
  112. PMA_Response::response();
  113. }
  114. /**
  115. * Gets buffer content
  116. *
  117. * @return buffer content
  118. */
  119. public function getContents()
  120. {
  121. return $this->_content;
  122. }
  123. /**
  124. * Flushes output buffer
  125. *
  126. * @return void
  127. */
  128. public function flush()
  129. {
  130. if (ob_get_status() && $this->_mode) {
  131. ob_flush();
  132. }
  133. /**
  134. * previously we had here an "else flush()" but some PHP versions
  135. * (at least PHP 5.2.11) have a bug (49816) that produces garbled
  136. * data
  137. */
  138. }
  139. }
  140. ?>