Translated using Weblate (Dutch)
[phpmyadmin.git] / libraries / OutputBuffering.php
blobf232cf2b141127661c5e63bdaffe1bc419d59b64
1 <?php /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3 * Output buffering wrapper
5 * @package PhpMyAdmin
6 */
7 namespace PMA\libraries;
9 /**
10 * Output buffering wrapper class
12 * @package PhpMyAdmin
14 class OutputBuffering
16 private static $_instance;
17 private $_mode;
18 private $_content;
19 private $_on;
21 /**
22 * Initializes class
24 private function __construct()
26 $this->_mode = $this->_getMode();
27 $this->_on = false;
30 /**
31 * This function could be used eventually to support more modes.
33 * @return integer the output buffer mode
35 private function _getMode()
37 $mode = 0;
38 if ($GLOBALS['cfg']['OBGzip'] && function_exists('ob_start')) {
39 if (ini_get('output_handler') == 'ob_gzhandler') {
40 // If a user sets the output_handler in php.ini to ob_gzhandler, then
41 // any right frame file in phpMyAdmin will not be handled properly by
42 // the browser. My fix was to check the ini file within the
43 // PMA_outBufferModeGet() function.
44 $mode = 0;
45 } elseif (function_exists('ob_get_level') && ob_get_level() > 0) {
46 // happens when php.ini's output_buffering is not Off
47 ob_end_clean();
48 $mode = 1;
49 } else {
50 $mode = 1;
53 // Zero (0) is no mode or in other words output buffering is OFF.
54 // Follow 2^0, 2^1, 2^2, 2^3 type values for the modes.
55 // Useful if we ever decide to combine modes. Then a bitmask field of
56 // the sum of all modes will be the natural choice.
57 return $mode;
60 /**
61 * Returns the singleton OutputBuffering object
63 * @return OutputBuffering object
65 public static function getInstance()
67 if (empty(self::$_instance)) {
68 self::$_instance = new OutputBuffering();
70 return self::$_instance;
73 /**
74 * This function will need to run at the top of all pages if output
75 * output buffering is turned on. It also needs to be passed $mode from
76 * the PMA_outBufferModeGet() function or it will be useless.
78 * @return void
80 public function start()
82 if (! $this->_on) {
83 if ($this->_mode && function_exists('ob_gzhandler')) {
84 ob_start('ob_gzhandler');
86 ob_start();
87 if (! defined('TESTSUITE')) {
88 header('X-ob_mode: ' . $this->_mode);
90 register_shutdown_function(
91 array('PMA\libraries\OutputBuffering', 'stop')
93 $this->_on = true;
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.
102 * @return void
104 public static function stop()
106 $buffer = OutputBuffering::getInstance();
107 if ($buffer->_on) {
108 $buffer->_on = false;
109 $buffer->_content = ob_get_contents();
110 ob_end_clean();
115 * Gets buffer content
117 * @return string buffer content
119 public function getContents()
121 return $this->_content;
125 * Flushes output buffer
127 * @return void
129 public function flush()
131 if (ob_get_status() && $this->_mode) {
132 ob_flush();
133 } else {
134 flush();