phpmyadmin/libraries/classes/OutputBuffering.php
Kamil Tekiela 0bdcaba2d2
Add native property types (#18143)
* Add native property types

Includes TypeHints.UnionTypeHintFormat

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Set some default values for properties

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Format and promote properties

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Remove redundant asserts

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Redundant cast

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* $tmanager->theme is never null

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Redundant variable

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Fix empty on $statementInfo bool

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Redundant casts

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Redundant issets

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* getPacked() returns nullable string

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Redundant if

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* $this->content can be null

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Simplify ThemeManager::getInstance()

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Use isset for checking if property is initialized

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Use nullable instead of uninitialized property

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* password is no longer nullable

I can't verify that none of the globals ever tried to set it to null, but the variable should never be nullable.

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Update baselines

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* $same_wide_width param can be float or int

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Update Message.php

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Cast Sub_part to int

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Fix one line doc comments

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

---------

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
2023-02-26 23:30:41 -03:00

142 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpMyAdmin;
use function defined;
use function flush;
use function function_exists;
use function header;
use function ini_get;
use function ob_end_clean;
use function ob_flush;
use function ob_get_contents;
use function ob_get_length;
use function ob_get_level;
use function ob_get_status;
use function ob_start;
use function sprintf;
/**
* Output buffering wrapper class
*/
class OutputBuffering
{
private int $mode;
private string $content = '';
private bool $on = false;
public function __construct()
{
$this->mode = $this->getMode();
}
/**
* This function could be used eventually to support more modes.
*
* @return int the output buffer mode
*/
private function getMode(): int
{
if (! defined('TESTSUITE') && $GLOBALS['cfg']['OBGzip']) {
if (ini_get('output_handler') === 'ob_gzhandler') {
// If a user sets the output_handler in php.ini to ob_gzhandler, then
// any right frame file in phpMyAdmin will not be handled properly by
// the browser. My fix was to check the ini file within the
// PMA_outBufferModeGet() function.
return 0;
}
if (ob_get_level() > 0) {
// happens when php.ini's output_buffering is not Off
ob_end_clean();
return 1;
}
return 1;
}
// Zero (0) is no mode or in other words output buffering is OFF.
// Follow 2^0, 2^1, 2^2, 2^3 type values for the modes.
// Useful if we ever decide to combine modes. Then a bitmask field of
// the sum of all modes will be the natural choice.
return 0;
}
/**
* This function will need to run at the top of all pages if output
* output buffering is turned on. It also needs to be passed $mode from
* the PMA_outBufferModeGet() function or it will be useless.
*/
public function start(): void
{
if (defined('TESTSUITE') || $this->on) {
return;
}
if ($this->mode && function_exists('ob_gzhandler')) {
ob_start('ob_gzhandler');
}
ob_start();
$this->sendHeader('X-ob_mode', (string) $this->mode);
$this->on = true;
}
private function sendHeader(string $name, string $value): void
{
if (defined('TESTSUITE')) {
return;
}
header(sprintf('%s: %s', $name, $value));
}
/**
* This function will need to run at the bottom of all pages if output
* buffering is turned on. It also needs to be passed $mode from the
* PMA_outBufferModeGet() function or it will be useless.
*/
public function stop(): void
{
if (! $this->on) {
return;
}
$this->on = false;
$this->content = (string) ob_get_contents();
if (ob_get_length() <= 0) {
return;
}
ob_end_clean();
}
/**
* Gets buffer content
*
* @return string buffer content
*/
public function getContents(): string
{
return $this->content;
}
/**
* Flushes output buffer
*/
public function flush(): void
{
if (ob_get_status() && $this->mode) {
ob_flush();
} else {
flush();
}
}
}