Exclude test/doctum-config.php from Scrutinizer analysis
[phpmyadmin.git] / libraries / classes / OutputBuffering.php
blob77149760249962f2cc1a2ba4ece79315f991aa2f
1 <?php /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3 * Output buffering wrapper
5 * @package PhpMyAdmin
6 */
7 declare(strict_types=1);
9 namespace PhpMyAdmin;
11 /**
12 * Output buffering wrapper class
14 * @package PhpMyAdmin
16 class OutputBuffering
18 private static $_instance;
19 private $_mode;
20 private $_content;
21 private $_on;
23 /**
24 * Initializes class
26 private function __construct()
28 $this->_mode = $this->_getMode();
29 $this->_on = false;
32 /**
33 * This function could be used eventually to support more modes.
35 * @return integer the output buffer mode
37 private function _getMode()
39 $mode = 0;
40 if ($GLOBALS['cfg']['OBGzip'] && function_exists('ob_start')) {
41 if (ini_get('output_handler') == 'ob_gzhandler') {
42 // If a user sets the output_handler in php.ini to ob_gzhandler, then
43 // any right frame file in phpMyAdmin will not be handled properly by
44 // the browser. My fix was to check the ini file within the
45 // PMA_outBufferModeGet() function.
46 $mode = 0;
47 } elseif (function_exists('ob_get_level') && ob_get_level() > 0) {
48 // happens when php.ini's output_buffering is not Off
49 ob_end_clean();
50 $mode = 1;
51 } else {
52 $mode = 1;
55 // Zero (0) is no mode or in other words output buffering is OFF.
56 // Follow 2^0, 2^1, 2^2, 2^3 type values for the modes.
57 // Useful if we ever decide to combine modes. Then a bitmask field of
58 // the sum of all modes will be the natural choice.
59 return $mode;
62 /**
63 * Returns the singleton OutputBuffering object
65 * @return OutputBuffering object
67 public static function getInstance()
69 if (empty(self::$_instance)) {
70 self::$_instance = new OutputBuffering();
72 return self::$_instance;
75 /**
76 * This function will need to run at the top of all pages if output
77 * output buffering is turned on. It also needs to be passed $mode from
78 * the PMA_outBufferModeGet() function or it will be useless.
80 * @return void
82 public function start()
84 if (! $this->_on) {
85 if ($this->_mode && function_exists('ob_gzhandler')) {
86 ob_start('ob_gzhandler');
88 ob_start();
89 if (! defined('TESTSUITE')) {
90 header('X-ob_mode: ' . $this->_mode);
92 register_shutdown_function(
94 OutputBuffering::class,
95 'stop',
98 $this->_on = true;
103 * This function will need to run at the bottom of all pages if output
104 * buffering is turned on. It also needs to be passed $mode from the
105 * PMA_outBufferModeGet() function or it will be useless.
107 * @return void
109 public static function stop()
111 $buffer = OutputBuffering::getInstance();
112 if ($buffer->_on) {
113 $buffer->_on = false;
114 $buffer->_content = ob_get_contents();
115 if (ob_get_length() > 0) {
116 ob_end_clean();
122 * Gets buffer content
124 * @return string buffer content
126 public function getContents()
128 return $this->_content;
132 * Flushes output buffer
134 * @return void
136 public function flush()
138 if (ob_get_status() && $this->_mode) {
139 ob_flush();
140 } else {
141 flush();