OutputBuffering.class.php 3.8 KB

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