Create Util::getUploadSizeInBytes()

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
This commit is contained in:
Kamil Tekiela 2025-01-02 22:16:57 +00:00
parent 2fd793f53a
commit 1af9a6ca13
10 changed files with 35 additions and 78 deletions

View File

@ -606,13 +606,13 @@ parameters:
-
message: '#^Only booleans are allowed in a negated boolean, string\|false given\.$#'
identifier: booleanNot.exprNotBoolean
count: 2
count: 1
path: src/Config.php
-
message: '#^Only booleans are allowed in an if condition, string\|false given\.$#'
identifier: if.condNotBoolean
count: 2
count: 1
path: src/Config.php
-
@ -2082,12 +2082,6 @@ parameters:
count: 1
path: src/Controllers/Database/ImportController.php
-
message: '#^Cannot cast mixed to int\.$#'
identifier: cast.int
count: 1
path: src/Controllers/Database/ImportController.php
-
message: '#^Parameter \#2 \$db of static method PhpMyAdmin\\Database\\MultiTableQuery\:\:displayResults\(\) expects string, mixed given\.$#'
identifier: argument.type
@ -3054,15 +3048,6 @@ parameters:
count: 1
path: src/Controllers/Preferences/MainPanelController.php
-
message: '''
#^Call to deprecated method getInstance\(\) of class PhpMyAdmin\\Config\:
Use dependency injection instead\.$#
'''
identifier: staticMethod.deprecated
count: 1
path: src/Controllers/Preferences/ManageController.php
-
message: '#^Loose comparison via "\!\=" is not allowed\.$#'
identifier: notEqual.notAllowed
@ -3288,12 +3273,6 @@ parameters:
count: 1
path: src/Controllers/Server/ImportController.php
-
message: '#^Cannot cast mixed to int\.$#'
identifier: cast.int
count: 1
path: src/Controllers/Server/ImportController.php
-
message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#'
identifier: empty.notAllowed
@ -3954,12 +3933,6 @@ parameters:
count: 1
path: src/Controllers/Table/ImportController.php
-
message: '#^Cannot cast mixed to int\.$#'
identifier: cast.int
count: 1
path: src/Controllers/Table/ImportController.php
-
message: '#^Binary operation "\+\=" between int and mixed results in an error\.$#'
identifier: assignOp.invalid
@ -8994,12 +8967,6 @@ parameters:
count: 1
path: src/InsertEdit.php
-
message: '#^Cannot cast mixed to int\.$#'
identifier: cast.int
count: 1
path: src/InsertEdit.php
-
message: '#^Cannot cast mixed to string\.$#'
identifier: cast.string
@ -19419,7 +19386,7 @@ parameters:
Use dependency injection instead\.$#
'''
identifier: staticMethod.deprecated
count: 34
count: 33
path: tests/unit/InsertEditTest.php
-

View File

@ -227,10 +227,8 @@
<code><![CDATA[$this->settings]]></code>
</PropertyTypeCoercion>
<RiskyTruthyFalsyComparison>
<code><![CDATA[! $fileSize]]></code>
<code><![CDATA[! ini_get('file_uploads')]]></code>
<code><![CDATA[$cookieName]]></code>
<code><![CDATA[$postSize]]></code>
<code><![CDATA[empty($dir)]]></code>
<code><![CDATA[empty($path)]]></code>
<code><![CDATA[ini_get('zlib.output_compression')]]></code>
@ -1780,9 +1778,6 @@
</PossiblyUnusedMethod>
</file>
<file src="src/Controllers/Preferences/ManageController.php">
<DeprecatedMethod>
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
<MixedArgument>
<code><![CDATA[$configuration['ThemeDefault']]]></code>
<code><![CDATA[$configuration['ThemeDefault']]]></code>
@ -11866,7 +11861,6 @@
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
</DeprecatedMethod>

View File

@ -40,7 +40,6 @@ use function is_string;
use function is_writable;
use function mb_strtolower;
use function md5;
use function min;
use function mkdir;
use function ob_end_clean;
use function ob_start;
@ -162,7 +161,6 @@ class Config
{
$this->checkGd2();
$this->checkUpload();
$this->checkUploadSize();
$this->checkOutputCompression();
}
@ -579,30 +577,6 @@ class Config
$this->set('enable_upload', false);
}
/**
* Maximum upload size as limited by PHP
* Used with permission from Moodle (https://moodle.org/) by Martin Dougiamas
*
* this section generates max_upload_size in bytes
*/
public function checkUploadSize(): void
{
$fileSize = ini_get('upload_max_filesize');
if (! $fileSize) {
$fileSize = '5M';
}
$size = Core::getRealSize($fileSize);
$postSize = ini_get('post_max_size');
if ($postSize) {
$size = min($size, Core::getRealSize($postSize));
}
$this->set('max_upload_size', $size);
}
/**
* Checks if protocol is https
*

View File

@ -97,7 +97,7 @@ final class ImportController implements InvocableController
$options = Plugins::getOptions('Import', $importList);
$skipQueriesDefault = $this->getSkipQueries($request->getParam('skip_queries'));
$isAllowInterruptChecked = Plugins::checkboxCheck('Import', 'allow_interrupt');
$maxUploadSize = (int) $config->get('max_upload_size');
$maxUploadSize = Util::getUploadSizeInBytes();
$this->response->render('database/import/index', [
'page_settings_error_html' => $pageSettingsErrorHtml,

View File

@ -246,7 +246,7 @@ final class ManageController implements InvocableController
$this->response->render('preferences/manage/main', [
'error' => $error instanceof Message ? $error->getDisplay() : '',
'max_upload_size' => Config::getInstance()->get('max_upload_size'),
'max_upload_size' => Util::getUploadSizeInBytes(),
'exists_setup_and_not_exists_config' => @file_exists(ROOT_PATH . 'setup/index.php')
&& ! @file_exists(CONFIG_FILE),
]);

View File

@ -79,7 +79,7 @@ final class ImportController implements InvocableController
$options = Plugins::getOptions('Import', $importList);
$skipQueriesDefault = $this->getSkipQueries($request->getParam('skip_queries'));
$isAllowInterruptChecked = Plugins::checkboxCheck('Import', 'allow_interrupt');
$maxUploadSize = (int) $config->get('max_upload_size');
$maxUploadSize = Util::getUploadSizeInBytes();
$this->response->render('server/import/index', [
'page_settings_error_html' => $pageSettingsErrorHtml,

View File

@ -127,7 +127,7 @@ final class ImportController implements InvocableController
$options = Plugins::getOptions('Import', $importList);
$skipQueriesDefault = $this->getSkipQueries($request->getParam('skip_queries'));
$isAllowInterruptChecked = Plugins::checkboxCheck('Import', 'allow_interrupt');
$maxUploadSize = (int) $config->get('max_upload_size');
$maxUploadSize = Util::getUploadSizeInBytes();
$this->response->render('table/import/index', [
'page_settings_error_html' => $pageSettingsErrorHtml,

View File

@ -490,7 +490,7 @@ class InsertEdit
'longblob' => 4294967296,// yeah, really
};
$thisFieldMaxSize = (int) $this->config->get('max_upload_size'); // from PHP max
$thisFieldMaxSize = Util::getUploadSizeInBytes();
return Util::getFormattedMaximumUploadSize(min($thisFieldMaxSize, $maxFieldSize)) . "\n";
}

View File

@ -38,6 +38,7 @@ use function htmlspecialchars;
use function htmlspecialchars_decode;
use function implode;
use function in_array;
use function ini_get;
use function is_array;
use function is_numeric;
use function is_object;
@ -50,6 +51,7 @@ use function mb_strpos;
use function mb_strrpos;
use function mb_strtolower;
use function mb_substr;
use function min;
use function number_format;
use function ord;
use function parse_url;
@ -1989,4 +1991,26 @@ class Util
return $_SESSION['tmpval']['table_limit_offset'];
}
/**
* Maximum upload size as limited by PHP
* Used with permission from Moodle (https://moodle.org/) by Martin Dougiamas
*/
public static function getUploadSizeInBytes(): int
{
$fileSize = ini_get('upload_max_filesize');
if ($fileSize === '' || $fileSize === false) {
$fileSize = '5M';
}
$size = Core::getRealSize($fileSize);
$postSize = ini_get('post_max_size');
if ($postSize !== '' && $postSize !== false) {
$size = min($size, Core::getRealSize($postSize));
}
return $size;
}
}

View File

@ -655,8 +655,6 @@ class InsertEditTest extends AbstractTestCase
*/
public function testGetMaxUploadSize(): void
{
$config = Config::getInstance();
$config->set('max_upload_size', 257);
$type = 'tinyblob';
$result = $this->callFunction(
$this->insertEdit,
@ -668,8 +666,8 @@ class InsertEditTest extends AbstractTestCase
self::assertSame("(Max: 256B)\n", $result);
// case 2
$config->set('max_upload_size', 250);
$type = 'tinyblob';
// this should stub Util::getUploadSizeInBytes() but it's not possible
$type = 'blob';
$result = $this->callFunction(
$this->insertEdit,
InsertEdit::class,
@ -677,7 +675,7 @@ class InsertEditTest extends AbstractTestCase
[$type],
);
self::assertSame("(Max: 250B)\n", $result);
self::assertSame("(Max: 64KiB)\n", $result);
}
/**