Skip to content

Commit 1af9a6c

Browse files
committed
Create Util::getUploadSizeInBytes()
Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
1 parent 2fd793f commit 1af9a6c

10 files changed

Lines changed: 35 additions & 78 deletions

File tree

phpstan-baseline.neon

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -606,13 +606,13 @@ parameters:
606606
-
607607
message: '#^Only booleans are allowed in a negated boolean, string\|false given\.$#'
608608
identifier: booleanNot.exprNotBoolean
609-
count: 2
609+
count: 1
610610
path: src/Config.php
611611

612612
-
613613
message: '#^Only booleans are allowed in an if condition, string\|false given\.$#'
614614
identifier: if.condNotBoolean
615-
count: 2
615+
count: 1
616616
path: src/Config.php
617617

618618
-
@@ -2082,12 +2082,6 @@ parameters:
20822082
count: 1
20832083
path: src/Controllers/Database/ImportController.php
20842084

2085-
-
2086-
message: '#^Cannot cast mixed to int\.$#'
2087-
identifier: cast.int
2088-
count: 1
2089-
path: src/Controllers/Database/ImportController.php
2090-
20912085
-
20922086
message: '#^Parameter \#2 \$db of static method PhpMyAdmin\\Database\\MultiTableQuery\:\:displayResults\(\) expects string, mixed given\.$#'
20932087
identifier: argument.type
@@ -3054,15 +3048,6 @@ parameters:
30543048
count: 1
30553049
path: src/Controllers/Preferences/MainPanelController.php
30563050

3057-
-
3058-
message: '''
3059-
#^Call to deprecated method getInstance\(\) of class PhpMyAdmin\\Config\:
3060-
Use dependency injection instead\.$#
3061-
'''
3062-
identifier: staticMethod.deprecated
3063-
count: 1
3064-
path: src/Controllers/Preferences/ManageController.php
3065-
30663051
-
30673052
message: '#^Loose comparison via "\!\=" is not allowed\.$#'
30683053
identifier: notEqual.notAllowed
@@ -3288,12 +3273,6 @@ parameters:
32883273
count: 1
32893274
path: src/Controllers/Server/ImportController.php
32903275

3291-
-
3292-
message: '#^Cannot cast mixed to int\.$#'
3293-
identifier: cast.int
3294-
count: 1
3295-
path: src/Controllers/Server/ImportController.php
3296-
32973276
-
32983277
message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#'
32993278
identifier: empty.notAllowed
@@ -3954,12 +3933,6 @@ parameters:
39543933
count: 1
39553934
path: src/Controllers/Table/ImportController.php
39563935

3957-
-
3958-
message: '#^Cannot cast mixed to int\.$#'
3959-
identifier: cast.int
3960-
count: 1
3961-
path: src/Controllers/Table/ImportController.php
3962-
39633936
-
39643937
message: '#^Binary operation "\+\=" between int and mixed results in an error\.$#'
39653938
identifier: assignOp.invalid
@@ -8994,12 +8967,6 @@ parameters:
89948967
count: 1
89958968
path: src/InsertEdit.php
89968969

8997-
-
8998-
message: '#^Cannot cast mixed to int\.$#'
8999-
identifier: cast.int
9000-
count: 1
9001-
path: src/InsertEdit.php
9002-
90038970
-
90048971
message: '#^Cannot cast mixed to string\.$#'
90058972
identifier: cast.string
@@ -19419,7 +19386,7 @@ parameters:
1941919386
Use dependency injection instead\.$#
1942019387
'''
1942119388
identifier: staticMethod.deprecated
19422-
count: 34
19389+
count: 33
1942319390
path: tests/unit/InsertEditTest.php
1942419391

1942519392
-

psalm-baseline.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,8 @@
227227
<code><![CDATA[$this->settings]]></code>
228228
</PropertyTypeCoercion>
229229
<RiskyTruthyFalsyComparison>
230-
<code><![CDATA[! $fileSize]]></code>
231230
<code><![CDATA[! ini_get('file_uploads')]]></code>
232231
<code><![CDATA[$cookieName]]></code>
233-
<code><![CDATA[$postSize]]></code>
234232
<code><![CDATA[empty($dir)]]></code>
235233
<code><![CDATA[empty($path)]]></code>
236234
<code><![CDATA[ini_get('zlib.output_compression')]]></code>
@@ -1780,9 +1778,6 @@
17801778
</PossiblyUnusedMethod>
17811779
</file>
17821780
<file src="src/Controllers/Preferences/ManageController.php">
1783-
<DeprecatedMethod>
1784-
<code><![CDATA[Config::getInstance()]]></code>
1785-
</DeprecatedMethod>
17861781
<MixedArgument>
17871782
<code><![CDATA[$configuration['ThemeDefault']]]></code>
17881783
<code><![CDATA[$configuration['ThemeDefault']]]></code>
@@ -11866,7 +11861,6 @@
1186611861
<code><![CDATA[Config::getInstance()]]></code>
1186711862
<code><![CDATA[Config::getInstance()]]></code>
1186811863
<code><![CDATA[Config::getInstance()]]></code>
11869-
<code><![CDATA[Config::getInstance()]]></code>
1187011864
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
1187111865
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
1187211866
</DeprecatedMethod>

src/Config.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
use function is_writable;
4141
use function mb_strtolower;
4242
use function md5;
43-
use function min;
4443
use function mkdir;
4544
use function ob_end_clean;
4645
use function ob_start;
@@ -162,7 +161,6 @@ public function checkSystem(): void
162161
{
163162
$this->checkGd2();
164163
$this->checkUpload();
165-
$this->checkUploadSize();
166164
$this->checkOutputCompression();
167165
}
168166

@@ -579,30 +577,6 @@ public function checkUpload(): void
579577
$this->set('enable_upload', false);
580578
}
581579

582-
/**
583-
* Maximum upload size as limited by PHP
584-
* Used with permission from Moodle (https://moodle.org/) by Martin Dougiamas
585-
*
586-
* this section generates max_upload_size in bytes
587-
*/
588-
public function checkUploadSize(): void
589-
{
590-
$fileSize = ini_get('upload_max_filesize');
591-
592-
if (! $fileSize) {
593-
$fileSize = '5M';
594-
}
595-
596-
$size = Core::getRealSize($fileSize);
597-
$postSize = ini_get('post_max_size');
598-
599-
if ($postSize) {
600-
$size = min($size, Core::getRealSize($postSize));
601-
}
602-
603-
$this->set('max_upload_size', $size);
604-
}
605-
606580
/**
607581
* Checks if protocol is https
608582
*

src/Controllers/Database/ImportController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function __invoke(ServerRequest $request): Response
9797
$options = Plugins::getOptions('Import', $importList);
9898
$skipQueriesDefault = $this->getSkipQueries($request->getParam('skip_queries'));
9999
$isAllowInterruptChecked = Plugins::checkboxCheck('Import', 'allow_interrupt');
100-
$maxUploadSize = (int) $config->get('max_upload_size');
100+
$maxUploadSize = Util::getUploadSizeInBytes();
101101

102102
$this->response->render('database/import/index', [
103103
'page_settings_error_html' => $pageSettingsErrorHtml,

src/Controllers/Preferences/ManageController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public function __invoke(ServerRequest $request): Response
246246

247247
$this->response->render('preferences/manage/main', [
248248
'error' => $error instanceof Message ? $error->getDisplay() : '',
249-
'max_upload_size' => Config::getInstance()->get('max_upload_size'),
249+
'max_upload_size' => Util::getUploadSizeInBytes(),
250250
'exists_setup_and_not_exists_config' => @file_exists(ROOT_PATH . 'setup/index.php')
251251
&& ! @file_exists(CONFIG_FILE),
252252
]);

src/Controllers/Server/ImportController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function __invoke(ServerRequest $request): Response
7979
$options = Plugins::getOptions('Import', $importList);
8080
$skipQueriesDefault = $this->getSkipQueries($request->getParam('skip_queries'));
8181
$isAllowInterruptChecked = Plugins::checkboxCheck('Import', 'allow_interrupt');
82-
$maxUploadSize = (int) $config->get('max_upload_size');
82+
$maxUploadSize = Util::getUploadSizeInBytes();
8383

8484
$this->response->render('server/import/index', [
8585
'page_settings_error_html' => $pageSettingsErrorHtml,

src/Controllers/Table/ImportController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function __invoke(ServerRequest $request): Response
127127
$options = Plugins::getOptions('Import', $importList);
128128
$skipQueriesDefault = $this->getSkipQueries($request->getParam('skip_queries'));
129129
$isAllowInterruptChecked = Plugins::checkboxCheck('Import', 'allow_interrupt');
130-
$maxUploadSize = (int) $config->get('max_upload_size');
130+
$maxUploadSize = Util::getUploadSizeInBytes();
131131

132132
$this->response->render('table/import/index', [
133133
'page_settings_error_html' => $pageSettingsErrorHtml,

src/InsertEdit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ private function getMaxUploadSize(string $type): string
490490
'longblob' => 4294967296,// yeah, really
491491
};
492492

493-
$thisFieldMaxSize = (int) $this->config->get('max_upload_size'); // from PHP max
493+
$thisFieldMaxSize = Util::getUploadSizeInBytes();
494494

495495
return Util::getFormattedMaximumUploadSize(min($thisFieldMaxSize, $maxFieldSize)) . "\n";
496496
}

src/Util.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use function htmlspecialchars_decode;
3939
use function implode;
4040
use function in_array;
41+
use function ini_get;
4142
use function is_array;
4243
use function is_numeric;
4344
use function is_object;
@@ -50,6 +51,7 @@
5051
use function mb_strrpos;
5152
use function mb_strtolower;
5253
use function mb_substr;
54+
use function min;
5355
use function number_format;
5456
use function ord;
5557
use function parse_url;
@@ -1989,4 +1991,26 @@ public static function getTableListPosition(ServerRequest $request, string $db):
19891991

19901992
return $_SESSION['tmpval']['table_limit_offset'];
19911993
}
1994+
1995+
/**
1996+
* Maximum upload size as limited by PHP
1997+
* Used with permission from Moodle (https://moodle.org/) by Martin Dougiamas
1998+
*/
1999+
public static function getUploadSizeInBytes(): int
2000+
{
2001+
$fileSize = ini_get('upload_max_filesize');
2002+
2003+
if ($fileSize === '' || $fileSize === false) {
2004+
$fileSize = '5M';
2005+
}
2006+
2007+
$size = Core::getRealSize($fileSize);
2008+
$postSize = ini_get('post_max_size');
2009+
2010+
if ($postSize !== '' && $postSize !== false) {
2011+
$size = min($size, Core::getRealSize($postSize));
2012+
}
2013+
2014+
return $size;
2015+
}
19922016
}

tests/unit/InsertEditTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,6 @@ public function testGetHTMLinput(): void
655655
*/
656656
public function testGetMaxUploadSize(): void
657657
{
658-
$config = Config::getInstance();
659-
$config->set('max_upload_size', 257);
660658
$type = 'tinyblob';
661659
$result = $this->callFunction(
662660
$this->insertEdit,
@@ -668,16 +666,16 @@ public function testGetMaxUploadSize(): void
668666
self::assertSame("(Max: 256B)\n", $result);
669667

670668
// case 2
671-
$config->set('max_upload_size', 250);
672-
$type = 'tinyblob';
669+
// this should stub Util::getUploadSizeInBytes() but it's not possible
670+
$type = 'blob';
673671
$result = $this->callFunction(
674672
$this->insertEdit,
675673
InsertEdit::class,
676674
'getMaxUploadSize',
677675
[$type],
678676
);
679677

680-
self::assertSame("(Max: 250B)\n", $result);
678+
self::assertSame("(Max: 64KiB)\n", $result);
681679
}
682680

683681
/**

0 commit comments

Comments
 (0)