OutputBuffering.php 3.7 KB

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