phpmyadmin/libraries/classes/Controllers/Preferences/SqlController.php
Maurício Meneghini Fauth 77957a69a5
Add trailing comma for multi-line functions
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
2023-02-24 20:48:46 -03:00

104 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Preferences;
use PhpMyAdmin\Config;
use PhpMyAdmin\Config\ConfigFile;
use PhpMyAdmin\Config\Forms\User\SqlForm;
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\Controllers\AbstractController;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Template;
use PhpMyAdmin\TwoFactor;
use PhpMyAdmin\Url;
use PhpMyAdmin\UserPreferences;
use function define;
use function ltrim;
class SqlController extends AbstractController
{
public function __construct(
ResponseRenderer $response,
Template $template,
private UserPreferences $userPreferences,
private Relation $relation,
private Config $config,
) {
parent::__construct($response, $template);
}
public function __invoke(ServerRequest $request): void
{
$GLOBALS['cf'] ??= null;
$GLOBALS['error'] ??= null;
$GLOBALS['tabHash'] ??= null;
$GLOBALS['hash'] ??= null;
$GLOBALS['server'] ??= null;
$GLOBALS['cf'] = new ConfigFile($this->config->baseSettings);
$this->userPreferences->pageInit($GLOBALS['cf']);
$formDisplay = new SqlForm($GLOBALS['cf'], 1);
if ($request->hasBodyParam('revert')) {
// revert erroneous fields to their default values
$formDisplay->fixErrors();
$this->redirect('/preferences/sql');
return;
}
$GLOBALS['error'] = null;
if ($formDisplay->process(false) && ! $formDisplay->hasErrors()) {
// Load 2FA settings
$twoFactor = new TwoFactor($GLOBALS['cfg']['Server']['user']);
// save settings
$result = $this->userPreferences->save($GLOBALS['cf']->getConfigArray());
// save back the 2FA setting only
$twoFactor->save();
if ($result === true) {
// reload config
$this->config->loadUserPreferences();
$GLOBALS['tabHash'] = $request->getParsedBodyParam('tab_hash');
$GLOBALS['hash'] = ltrim($GLOBALS['tabHash'], '#');
$this->userPreferences->redirect('index.php?route=/preferences/sql', null, $GLOBALS['hash']);
return;
}
$GLOBALS['error'] = $result;
}
$relationParameters = $this->relation->getRelationParameters();
$this->render('preferences/header', [
'route' => $request->getRoute(),
'is_saved' => $request->hasQueryParam('saved'),
'has_config_storage' => $relationParameters->userPreferencesFeature !== null,
]);
$formErrors = $formDisplay->displayErrors();
$this->render('preferences/forms/main', [
'error' => $GLOBALS['error'] ? $GLOBALS['error']->getDisplay() : '',
'has_errors' => $formDisplay->hasErrors(),
'errors' => $formErrors,
'form' => $formDisplay->getDisplay(
true,
Url::getFromRoute('/preferences/sql'),
['server' => $GLOBALS['server']],
),
]);
if ($this->response->isAjax()) {
$this->response->addJSON('disableNaviSettings', true);
} else {
define('PMA_DISABLE_NAVI_SETTINGS', true);
}
}
}