From bd2ef2b8eeac35ed8654cc0f8320123bf4dec7dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Mon, 30 Aug 2021 17:45:28 -0300 Subject: [PATCH] Get post params from ServerRequest class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- .../BrowseForeignersController.php | 50 +++++++++++-------- .../Controllers/CheckRelationsController.php | 20 ++++---- .../classes/Controllers/ColumnController.php | 12 +++-- .../classes/Controllers/ConfigController.php | 21 +++++--- .../Controllers/ErrorReportController.php | 50 +++++++++---------- phpstan-baseline.neon | 5 -- psalm-baseline.xml | 47 ++--------------- 7 files changed, 90 insertions(+), 115 deletions(-) diff --git a/libraries/classes/Controllers/BrowseForeignersController.php b/libraries/classes/Controllers/BrowseForeignersController.php index f694404753..275a71572d 100644 --- a/libraries/classes/Controllers/BrowseForeignersController.php +++ b/libraries/classes/Controllers/BrowseForeignersController.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace PhpMyAdmin\Controllers; use PhpMyAdmin\BrowseForeigners; +use PhpMyAdmin\Http\ServerRequest; use PhpMyAdmin\Relation; use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\Template; @@ -32,19 +33,24 @@ class BrowseForeignersController extends AbstractController $this->relation = $relation; } - public function __invoke(): void + public function __invoke(ServerRequest $request): void { - $params = [ - 'db' => $_POST['db'] ?? null, - 'table' => $_POST['table'] ?? null, - 'field' => $_POST['field'] ?? null, - 'fieldkey' => $_POST['fieldkey'] ?? null, - 'data' => $_POST['data'] ?? null, - 'foreign_showAll' => $_POST['foreign_showAll'] ?? null, - 'foreign_filter' => $_POST['foreign_filter'] ?? null, - ]; + /** @var string|null $database */ + $database = $request->getParsedBodyParam('db'); + /** @var string|null $table */ + $table = $request->getParsedBodyParam('table'); + /** @var string|null $field */ + $field = $request->getParsedBodyParam('field'); + /** @var string $fieldKey */ + $fieldKey = $request->getParsedBodyParam('fieldkey', ''); + /** @var string $data */ + $data = $request->getParsedBodyParam('data', ''); + /** @var string|null $foreignShowAll */ + $foreignShowAll = $request->getParsedBodyParam('foreign_showAll'); + /** @var string $foreignFilter */ + $foreignFilter = $request->getParsedBodyParam('foreign_filter', ''); - if (! isset($params['db'], $params['table'], $params['field'])) { + if (! isset($database, $table, $field)) { return; } @@ -54,28 +60,28 @@ class BrowseForeignersController extends AbstractController $header->setBodyId('body_browse_foreigners'); $foreigners = $this->relation->getForeigners( - $params['db'], - $params['table'] + $database, + $table ); $foreignLimit = $this->browseForeigners->getForeignLimit( - $params['foreign_showAll'] + $foreignShowAll ); $foreignData = $this->relation->getForeignData( $foreigners, - $params['field'], + $field, true, - $params['foreign_filter'] ?? '', - $foreignLimit ?? null, + $foreignFilter, + $foreignLimit ?? '', true ); $this->response->addHTML($this->browseForeigners->getHtmlForRelationalFieldSelection( - $params['db'], - $params['table'], - $params['field'], + $database, + $table, + $field, $foreignData, - $params['fieldkey'] ?? '', - $params['data'] ?? '' + $fieldKey, + $data )); } } diff --git a/libraries/classes/Controllers/CheckRelationsController.php b/libraries/classes/Controllers/CheckRelationsController.php index 776973a310..9f93ec13ca 100644 --- a/libraries/classes/Controllers/CheckRelationsController.php +++ b/libraries/classes/Controllers/CheckRelationsController.php @@ -7,6 +7,7 @@ declare(strict_types=1); namespace PhpMyAdmin\Controllers; +use PhpMyAdmin\Http\ServerRequest; use PhpMyAdmin\Relation; use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\Template; @@ -25,31 +26,32 @@ class CheckRelationsController extends AbstractController $this->relation = $relation; } - public function __invoke(): void + public function __invoke(ServerRequest $request): void { global $db; - $params = [ - 'create_pmadb' => $_POST['create_pmadb'] ?? null, - 'fixall_pmadb' => $_POST['fixall_pmadb'] ?? null, - 'fix_pmadb' => $_POST['fix_pmadb'] ?? null, - ]; + /** @var string|null $createPmaDb */ + $createPmaDb = $request->getParsedBodyParam('create_pmadb'); + /** @var string|null $fixAllPmaDb */ + $fixAllPmaDb = $request->getParsedBodyParam('fixall_pmadb'); + /** @var string|null $fixPmaDb */ + $fixPmaDb = $request->getParsedBodyParam('fix_pmadb'); $cfgStorageDbName = $this->relation->getConfigurationStorageDbName(); // If request for creating the pmadb - if (isset($params['create_pmadb']) && $this->relation->createPmaDatabase($cfgStorageDbName)) { + if (isset($createPmaDb) && $this->relation->createPmaDatabase($cfgStorageDbName)) { $this->relation->fixPmaTables($cfgStorageDbName); } // If request for creating all PMA tables. - if (isset($params['fixall_pmadb'])) { + if (isset($fixAllPmaDb)) { $this->relation->fixPmaTables($db); } $cfgRelation = $this->relation->getRelationsParam(); // If request for creating missing PMA tables. - if (isset($params['fix_pmadb'])) { + if (isset($fixPmaDb)) { $this->relation->fixPmaTables($cfgRelation['db']); } diff --git a/libraries/classes/Controllers/ColumnController.php b/libraries/classes/Controllers/ColumnController.php index 177f42740d..4beae8d011 100644 --- a/libraries/classes/Controllers/ColumnController.php +++ b/libraries/classes/Controllers/ColumnController.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace PhpMyAdmin\Controllers; use PhpMyAdmin\DatabaseInterface; +use PhpMyAdmin\Http\ServerRequest; use PhpMyAdmin\Message; use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\Template; @@ -24,15 +25,20 @@ final class ColumnController extends AbstractController $this->dbi = $dbi; } - public function __invoke(): void + public function __invoke(ServerRequest $request): void { - if (! isset($_POST['db'], $_POST['table'])) { + /** @var string|null $db */ + $db = $request->getParsedBodyParam('db'); + /** @var string|null $table */ + $table = $request->getParsedBodyParam('table'); + + if (! isset($db, $table)) { $this->response->setRequestStatus(false); $this->response->addJSON(['message' => Message::error()]); return; } - $this->response->addJSON(['columns' => $this->dbi->getColumnNames($_POST['db'], $_POST['table'])]); + $this->response->addJSON(['columns' => $this->dbi->getColumnNames($db, $table)]); } } diff --git a/libraries/classes/Controllers/ConfigController.php b/libraries/classes/Controllers/ConfigController.php index 714b1eed18..63c8f3d7bd 100644 --- a/libraries/classes/Controllers/ConfigController.php +++ b/libraries/classes/Controllers/ConfigController.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace PhpMyAdmin\Controllers; use PhpMyAdmin\Config; +use PhpMyAdmin\Http\ServerRequest; use PhpMyAdmin\Message; use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\Template; @@ -25,28 +26,36 @@ final class ConfigController extends AbstractController $this->config = $config; } - public function get(): void + public function get(ServerRequest $request): void { - if (! isset($_POST['key'])) { + /** @var string|null $key */ + $key = $request->getParsedBodyParam('key'); + + if (! isset($key)) { $this->response->setRequestStatus(false); $this->response->addJSON(['message' => Message::error()]); return; } - $this->response->addJSON(['value' => $this->config->get($_POST['key'])]); + $this->response->addJSON(['value' => $this->config->get($key)]); } - public function set(): void + public function set(ServerRequest $request): void { - if (! isset($_POST['key'], $_POST['value'])) { + /** @var string|null $key */ + $key = $request->getParsedBodyParam('key'); + /** @var string|null $value */ + $value = $request->getParsedBodyParam('value'); + + if (! isset($key, $value)) { $this->response->setRequestStatus(false); $this->response->addJSON(['message' => Message::error()]); return; } - $result = $this->config->setUserValue(null, $_POST['key'], json_decode($_POST['value'])); + $result = $this->config->setUserValue(null, $key, json_decode($value)); if ($result === true) { return; diff --git a/libraries/classes/Controllers/ErrorReportController.php b/libraries/classes/Controllers/ErrorReportController.php index 3c2273e8e4..fbb88e5724 100644 --- a/libraries/classes/Controllers/ErrorReportController.php +++ b/libraries/classes/Controllers/ErrorReportController.php @@ -9,6 +9,7 @@ namespace PhpMyAdmin\Controllers; use PhpMyAdmin\ErrorHandler; use PhpMyAdmin\ErrorReport; +use PhpMyAdmin\Http\ServerRequest; use PhpMyAdmin\Message; use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\Template; @@ -46,23 +47,27 @@ class ErrorReportController extends AbstractController $this->errorHandler = $errorHandler; } - public function __invoke(): void + public function __invoke(ServerRequest $request): void { global $cfg; - if ( - ! isset($_POST['exception_type']) - || ! in_array($_POST['exception_type'], ['js', 'php']) - ) { + /** @var string $exceptionType */ + $exceptionType = $request->getParsedBodyParam('exception_type', ''); + /** @var string|null $sendErrorReport */ + $sendErrorReport = $request->getParsedBodyParam('send_error_report'); + /** @var string|null $automatic */ + $automatic = $request->getParsedBodyParam('automatic'); + /** @var string|null $alwaysSend */ + $alwaysSend = $request->getParsedBodyParam('always_send'); + /** @var string|null $getSettings */ + $getSettings = $request->getParsedBodyParam('get_settings'); + + if (! in_array($exceptionType, ['js', 'php'])) { return; } - if ( - isset($_POST['send_error_report']) - && ($_POST['send_error_report'] == true - || $_POST['send_error_report'] == '1') - ) { - if ($_POST['exception_type'] === 'php') { + if ($sendErrorReport) { + if ($exceptionType === 'php') { /** * Prevent infinite error submission. * Happens in case error submissions fails. @@ -85,7 +90,7 @@ class ErrorReportController extends AbstractController } } - $reportData = $this->errorReport->getData($_POST['exception_type']); + $reportData = $this->errorReport->getData($exceptionType); // report if and only if there were 'actual' errors. if (count($reportData) > 0) { $server_response = $this->errorReport->send($reportData); @@ -99,11 +104,7 @@ class ErrorReportController extends AbstractController /* Message to show to the user */ if ($success) { - if ( - (isset($_POST['automatic']) - && $_POST['automatic'] === 'true') - || $cfg['SendErrorReports'] === 'always' - ) { + if ($automatic === 'true' || $cfg['SendErrorReports'] === 'always') { $msg = __( 'An error has been detected and an error report has been ' . 'automatically submitted based on your settings.' @@ -134,35 +135,32 @@ class ErrorReportController extends AbstractController /* Add message to response */ if ($this->response->isAjax()) { - if ($_POST['exception_type'] === 'js') { + if ($exceptionType === 'js') { $this->response->addJSON('message', $msg); } else { $this->response->addJSON('errSubmitMsg', $msg); } - } elseif ($_POST['exception_type'] === 'php') { + } elseif ($exceptionType === 'php') { $jsCode = 'Functions.ajaxShowMessage(\'\', false);'; $this->response->getFooter()->getScripts()->addCode($jsCode); } - if ($_POST['exception_type'] === 'php') { + if ($exceptionType === 'php') { // clear previous errors & save new ones. $this->errorHandler->savePreviousErrors(); } /* Persist always send settings */ - if ( - isset($_POST['always_send']) - && $_POST['always_send'] === 'true' - ) { + if ($alwaysSend === 'true') { $userPreferences = new UserPreferences(); $userPreferences->persistOption('SendErrorReports', 'always', 'ask'); } } - } elseif (! empty($_POST['get_settings'])) { + } elseif ($getSettings) { $this->response->addJSON('report_setting', $cfg['SendErrorReports']); - } elseif ($_POST['exception_type'] === 'js') { + } elseif ($exceptionType === 'js') { $this->response->addJSON('report_modal', $this->errorReport->getEmptyModal()); $this->response->addHTML($this->errorReport->getForm()); } else { diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 15fc285329..7933755c1d 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -80,11 +80,6 @@ parameters: count: 1 path: libraries/classes/Config/Validator.php - - - message: "#^Parameter \\#5 \\$foreign_limit of method PhpMyAdmin\\\\Relation\\:\\:getForeignData\\(\\) expects string, string\\|null given\\.$#" - count: 1 - path: libraries/classes/Controllers/BrowseForeignersController.php - - message: "#^Argument of an invalid type array\\|null supplied for foreach, only iterables are supported\\.$#" count: 1 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index ea4d01cff2..66d019b099 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1083,23 +1083,6 @@ $params['message'] - - - $params['data'] ?? '' - $params['db'] - $params['db'] - $params['field'] - $params['field'] - $params['fieldkey'] ?? '' - $params['foreign_filter'] ?? '' - $params['foreign_showAll'] - $params['table'] - $params['table'] - - - $foreignLimit ?? null - - readgzfile @@ -1110,18 +1093,9 @@ $db - - - $_POST['db'] - $_POST['table'] - - - - $_POST['key'] - $_POST['key'] - $_POST['value'] - json_decode($_POST['value']) + + json_decode($value) @@ -1861,9 +1835,6 @@ - - $_POST['exception_type'] - $decoded_response['success'] @@ -14130,22 +14101,10 @@ - + $_GET['db'] $_GET['table'] - $controllerName - - $action - $controllerName - - - $controller - [$controllerName, $action] - - - $action -