Refactor UrlParamsSetting

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
This commit is contained in:
Kamil Tekiela 2024-11-10 19:04:03 +00:00 committed by Maurício Meneghini Fauth
parent c7582c282e
commit 6add4fccf0
8 changed files with 38 additions and 37 deletions

View File

@ -3610,6 +3610,11 @@ parameters:
count: 1
path: src/Controllers/Table/ExportController.php
-
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
count: 1
path: src/Controllers/Table/ExportRowsController.php
-
message: """
#^Call to deprecated method getInstance\\(\\) of class PhpMyAdmin\\\\Config\\:
@ -7480,12 +7485,12 @@ parameters:
path: src/Http/Middleware/TokenRequestParamChecking.php
-
message: "#^Parameter \\#1 \\$page of static method PhpMyAdmin\\\\Core\\:\\:checkPageValidity\\(\\) expects string, mixed given\\.$#"
count: 2
message: "#^Cannot access offset 'back' on array\\|object\\.$#"
count: 1
path: src/Http/Middleware/UrlParamsSetting.php
-
message: "#^Static property PhpMyAdmin\\\\UrlParams\\:\\:\\$params \\(array\\<string, bool\\|int\\|string\\>\\) does not accept array\\<string, mixed\\>\\.$#"
message: "#^Cannot access offset 'goto' on array\\|object\\.$#"
count: 1
path: src/Http/Middleware/UrlParamsSetting.php

View File

@ -3140,6 +3140,9 @@
<PossiblyUnusedReturnValue>
<code><![CDATA[Response]]></code>
</PossiblyUnusedReturnValue>
<RiskyTruthyFalsyComparison>
<code><![CDATA[empty($GLOBALS['goto'])]]></code>
</RiskyTruthyFalsyComparison>
</file>
<file src="src/Controllers/Table/FindReplaceController.php">
<DeprecatedMethod>
@ -6153,19 +6156,6 @@
<code><![CDATA[(string) $_POST['token']]]></code>
</RedundantCast>
</file>
<file src="src/Http/Middleware/UrlParamsSetting.php">
<InvalidPropertyAssignmentValue>
<code><![CDATA[UrlParams::$params]]></code>
</InvalidPropertyAssignmentValue>
<PossiblyInvalidArgument>
<code><![CDATA[$_REQUEST['back']]]></code>
<code><![CDATA[$_REQUEST['goto']]]></code>
</PossiblyInvalidArgument>
<PossiblyInvalidCast>
<code><![CDATA[$_REQUEST['back']]]></code>
<code><![CDATA[$_REQUEST['goto']]]></code>
</PossiblyInvalidCast>
</file>
<file src="src/I18n/LanguageManager.php">
<DeprecatedMethod>
<code><![CDATA[Config::getInstance()]]></code>

View File

@ -26,7 +26,7 @@ final class ExportRowsController implements InvocableController
$GLOBALS['single_table'] ??= null;
$GLOBALS['where_clause'] ??= null;
if (isset($_POST['goto']) && (! isset($_POST['rows_to_delete']) || ! is_array($_POST['rows_to_delete']))) {
if (! empty($GLOBALS['goto']) && (! isset($_POST['rows_to_delete']) || ! is_array($_POST['rows_to_delete']))) {
$this->response->setRequestStatus(false);
$this->response->addJSON('message', __('No row selected.'));

View File

@ -11,6 +11,7 @@ use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Webmozart\Assert\Assert;
final class UrlParamsSetting implements MiddlewareInterface
{
@ -22,38 +23,42 @@ final class UrlParamsSetting implements MiddlewareInterface
{
UrlParams::$params = [];
$this->setGotoAndBackGlobals();
$this->setGotoAndBackGlobals($request);
return $handler->handle($request);
}
private function setGotoAndBackGlobals(): void
private function setGotoAndBackGlobals(ServerRequestInterface $request): void
{
// Holds page that should be displayed.
$GLOBALS['goto'] = '';
if (isset($_REQUEST['goto']) && Core::checkPageValidity($_REQUEST['goto'])) {
$GLOBALS['goto'] = $_REQUEST['goto'];
UrlParams::$params['goto'] = $GLOBALS['goto'];
$goto = $request->getQueryParams()['goto'] ?? $request->getParsedBody()['goto'] ?? null;
Assert::nullOrString($goto);
if ($goto !== null && Core::checkPageValidity($goto)) {
$GLOBALS['goto'] = $goto;
UrlParams::$params['goto'] = $goto;
} else {
if ($this->config->issetCookie('goto')) {
$this->config->removeCookie('goto');
}
unset($_REQUEST['goto'], $_GET['goto'], $_POST['goto']);
}
if (isset($_REQUEST['back']) && Core::checkPageValidity($_REQUEST['back'])) {
$back = $request->getQueryParams()['back'] ?? $request->getParsedBody()['back'] ?? null;
Assert::nullOrString($back);
if ($back !== null && Core::checkPageValidity($back)) {
// Returning page.
$GLOBALS['back'] = $_REQUEST['back'];
$GLOBALS['back'] = $back;
return;
}
if ($this->config->issetCookie('back')) {
$this->config->removeCookie('back');
if (! $this->config->issetCookie('back')) {
return;
}
unset($_REQUEST['back'], $_GET['back'], $_POST['back']);
$this->config->removeCookie('back');
}
}

View File

@ -49,7 +49,7 @@ class ExportRowsControllerTest extends AbstractTestCase
public function testWithoutRowsToDelete(): void
{
$_POST['goto'] = 'goto';
$GLOBALS['goto'] = 'goto';
$controller = $this->createMock(ExportController::class);
$controller->expects(self::never())->method('__invoke');
@ -67,7 +67,7 @@ class ExportRowsControllerTest extends AbstractTestCase
public function testWithRowsToDelete(): void
{
$_POST['goto'] = 'goto';
$GLOBALS['goto'] = 'goto';
$_POST['rows_to_delete'] = ['key1' => 'row1', 'key2' => 'row2'];
$controller = $this->createMock(ExportController::class);

View File

@ -77,7 +77,6 @@ class ReplaceControllerTest extends AbstractTestCase
$_POST['table'] = Current::$table;
$_POST['ajax_request'] = 'true';
$_POST['relational_display'] = 'K';
$_POST['goto'] = 'index.php?route=/sql';
$request = self::createStub(ServerRequest::class);
$request->method('getParsedBodyParam')->willReturnMap([

View File

@ -5,12 +5,12 @@ declare(strict_types=1);
namespace PhpMyAdmin\Tests\Http\Middleware;
use PhpMyAdmin\Config;
use PhpMyAdmin\Http\Factory\ServerRequestFactory;
use PhpMyAdmin\Http\Middleware\UrlParamsSetting;
use PhpMyAdmin\Tests\AbstractTestCase;
use PhpMyAdmin\UrlParams;
use PHPUnit\Framework\Attributes\CoversClass;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
#[CoversClass(UrlParamsSetting::class)]
@ -21,10 +21,10 @@ final class UrlParamsSettingTest extends AbstractTestCase
UrlParams::$params = [];
$GLOBALS['goto'] = null;
$GLOBALS['back'] = null;
$_REQUEST['goto'] = 'index.php?route=/';
$_REQUEST['back'] = 'index.php?route=/';
$request = self::createStub(ServerRequestInterface::class);
$request = ServerRequestFactory::create()->createServerRequest('POST', 'http://example.com/')
->withQueryParams(['goto' => 'index.php?route=/', 'back' => 'index.php?route=/']);
$response = self::createStub(ResponseInterface::class);
$handler = self::createMock(RequestHandlerInterface::class);
$handler->method('handle')->with($request)->willReturn($response);
@ -37,5 +37,8 @@ final class UrlParamsSettingTest extends AbstractTestCase
self::assertSame('index.php?route=/', $GLOBALS['back']);
/** @psalm-suppress TypeDoesNotContainType */
self::assertSame(['goto' => 'index.php?route=/'], UrlParams::$params);
$GLOBALS['goto'] = null;
$GLOBALS['back'] = null;
}
}

View File

@ -191,7 +191,6 @@ class SearchTest extends AbstractTestCase
{
$_POST['db'] = 'opengis';
$_POST['table'] = 'world_cities';
$_POST['back'] = 'index.php?route=/table/search';
$_POST['geom_func'] = [2 => ' '];
$_POST['customWhereClause'] = '';
$_POST['session_max_rows'] = '25';