<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* function for the main export logic
*
* @package PhpMyAdmin
*/
declare(strict_types=1);
namespace PhpMyAdmin;
use PhpMyAdmin\Plugins\ExportPlugin;
use PhpMyAdmin\Plugins\SchemaPlugin;
/**
* PhpMyAdmin\Export class
*
* @package PhpMyAdmin
*/
class Export
{
/**
* @var DatabaseInterface
*/
private $dbi;
/**
* Export constructor.
* @param DatabaseInterface $dbi DatabaseInterface instance
*/
public function __construct($dbi)
{
$this->dbi = $dbi;
}
/**
* Sets a session variable upon a possible fatal error during export
*
* @return void
*/
public function shutdown(): void
{
$error = error_get_last();
if ($error != null && mb_strpos($error['message'], "execution time")) {
//set session variable to check if there was error while exporting
$_SESSION['pma_export_error'] = $error['message'];
}
}
/**
* Detect ob_gzhandler
*
* @return bool
*/
public function isGzHandlerEnabled(): bool
{
return in_array('ob_gzhandler', ob_list_handlers());
}
/**
* Detect whether gzencode is needed; it might not be needed if
* the server is already compressing by itself
*
* @return bool Whether gzencode is needed
*/
public function gzencodeNeeded(): bool
{
/*
* We should gzencode only if the function exists
* but we don't want to compress twice, therefore
* gzencode only if transparent compression is not enabled
* and gz compression was not asked via $cfg['OBGzip']
* but transparent compression does not apply when saving to server
*/
$chromeAndGreaterThan43 = PMA_USR_BROWSER_AGENT == 'CHROME'
&& PMA_USR_BROWSER_VER >= 43; // see bug #4942
return function_exists('gzencode')
&& ((! ini_get('zlib.output_compression')
&& ! $this->isGzHandlerEnabled())
|| $GLOBALS['save_on_server']
|| $chromeAndGreaterThan43);
}
/**
* Output handler for all exports, if needed buffering, it stores data into
* $dump_buffer, otherwise it prints them out.
*
* @param string $line the insert statement
*
* @return bool Whether output succeeded
*/
public function outputHandler(?string $line): bool
{
global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
// Kanji encoding convert feature
if ($GLOBALS['output_kanji_conversion']) {
$line = Encoding::kanjiStrConv(
$line,
$GLOBALS['knjenc'],
isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : ''
);
}
// If we have to buffer data, we will perform everything at once at the end
if ($GLOBALS['buffer_needed']) {
$dump_buffer .= $line;
if ($GLOBALS['onfly_compression']) {
$dump_buffer_len += strlen($line);
if ($dump_buffer_len > $GLOBALS['memory_limit']) {
if ($GLOBALS['output_charset_conversion']) {
$dump_buffer = Encoding::convertString(
'utf-8',
$GLOBALS['charset'],
$dump_buffer
);
}
if ($GLOBALS['compression'] == 'gzip'
&& $this->gzencodeNeeded()
) {
// as a gzipped file
// without the optional parameter level because it bugs
$dump_buffer = gzencode($dump_buffer);
}
if ($GLOBALS['save_on_server']) {
$write_result = @fwrite($GLOBALS['file_handle'], $dump_buffer);
// Here, use strlen rather than mb_strlen to get the length
// in bytes to compare against the number of bytes written.
if ($write_result != strlen($dump_buffer)) {
$GLOBALS['message'] = Message::error(
__('Insufficient space to save the file %s.')
);
$GLOBALS['message']->addParam($save_filename);
return false;
}
} else {
echo $dump_buffer;
}
$dump_buffer = '';
$dump_buffer_len = 0;
}
} else {
$time_now = time();
if ($time_start >= $time_now + 30) {
$time_start = $time_now;
header('X-pmaPing: Pong');
} // end if
}
} elseif ($GLOBALS['asfile']) {
if ($GLOBALS['output_charset_conversion']) {
$line = Encoding::convertString(
'utf-8'
|