3 * Form management class, displays and processes forms
5 * Explanation of used terms:
6 * o work_path - original field path, eg. Servers/4/verbose
7 * o system_path - work_path modified so that it points to the first server,
8 * eg. Servers/1/verbose
9 * o translated_path - work_path modified for HTML field name, a path with
10 * slashes changed to hyphens, eg. Servers-4-verbose
13 declare(strict_types
=1);
15 namespace PhpMyAdmin\Config
;
17 use PhpMyAdmin\Config
;
18 use PhpMyAdmin\Config\Forms\User\UserFormList
;
19 use PhpMyAdmin\Html\MySQLDocumentation
;
20 use PhpMyAdmin\Sanitize
;
24 use function array_flip
;
25 use function array_keys
;
26 use function array_search
;
28 use function function_exists
;
31 use function is_array
;
33 use function is_numeric
;
34 use function mb_substr
;
35 use function preg_match
;
38 use function str_ends_with
;
39 use function str_replace
;
40 use function str_starts_with
;
41 use function trigger_error
;
44 use const E_USER_WARNING
;
47 * Form management class, displays and processes forms
54 * @var array<string, Form>
56 private array $forms = [];
59 * Stores validation errors, indexed by paths
60 * [ Form_name ] is an array of form errors
61 * [path] is a string storing error associated with single field
65 private array $errors = [];
68 * Paths changed so that they can be used as HTML ids, indexed by paths
72 private array $translatedPaths = [];
75 * Server paths change indexes so we define maps from current server
76 * path to the first one, indexed by work path
80 private array $systemPaths = [];
83 * Tells whether forms have been validated
85 private bool $isValidated = true;
88 * Dictionary with user preferences keys
90 private array|
null $userprefsKeys = null;
93 * Dictionary with disallowed user preferences keys
97 private array $userprefsDisallow = [];
99 private FormDisplayTemplate
$formDisplayTemplate;
101 private bool $isSetupScript;
103 private static bool $hasCheckPageRefresh = false;
105 public function __construct(private ConfigFile
$configFile)
107 $this->formDisplayTemplate
= new FormDisplayTemplate(Config
::getInstance());
108 $this->isSetupScript
= Sanitize
::isSetup();
109 // initialize validators
110 Validator
::getValidators($this->configFile
);
114 * Returns {@link ConfigFile} associated with this instance
116 public function getConfigFile(): ConfigFile
118 return $this->configFile
;
122 * Registers form in form manager
124 * @param string $formName Form name
125 * @param mixed[] $form Form data
126 * @param int|null $serverId 0 if new server, validation; >= 1 if editing a server
128 public function registerForm(string $formName, array $form, int|
null $serverId = null): void
130 $this->forms
[$formName] = new Form($formName, $form, $this->configFile
, $serverId);
131 $this->isValidated
= false;
132 foreach ($this->forms
[$formName]->fields
as $path) {
133 $workPath = $serverId === null
135 : str_replace('Servers/1/', 'Servers/' . $serverId . '/', $path);
136 $this->systemPaths
[$workPath] = $path;
137 $this->translatedPaths
[$workPath] = str_replace('/', '-', $workPath);
142 * Processes forms, returns true on successful save
144 * @param bool $allowPartialSave allows for partial form saving
145 * on failed validation
146 * @param bool $checkFormSubmit whether check for $_POST['submit_save']
148 public function process(bool $allowPartialSave = true, bool $checkFormSubmit = true): bool
150 if ($checkFormSubmit && ! isset($_POST['submit_save'])) {
155 if ($this->forms
!== []) {
156 return $this->save(array_keys($this->forms
), $allowPartialSave);
163 * Runs validation for all registered forms
165 private function validate(): void
167 if ($this->isValidated
) {
173 foreach ($this->forms
as $form) {
174 $paths[] = $form->name
;
175 // collect values and paths
176 foreach ($form->fields
as $path) {
177 $workPath = array_search($path, $this->systemPaths
);
178 $values[$path] = $this->configFile
->getValue($workPath);
184 $errors = Validator
::validate($this->configFile
, $paths, $values, false);
186 // change error keys from canonical paths to work paths
187 if (is_array($errors) && $errors !== []) {
189 foreach ($errors as $path => $errorList) {
190 $workPath = array_search($path, $this->systemPaths
);
193 // form error, fix path
197 $this->errors
[$workPath] = $errorList;
201 $this->isValidated
= true;
205 * Outputs HTML for forms
207 * @param bool $showButtons whether show submit and reset button
208 * @param string|null $formAction action attribute for the form
209 * @param mixed[]|null $hiddenFields array of form hidden fields (key: field
212 * @return string HTML for forms
214 public function getDisplay(
215 bool $showButtons = true,
216 string|
null $formAction = null,
217 array|
null $hiddenFields = null,
219 $fieldValidators = [];
223 * We do validation on page refresh when browser remembers field values,
224 * add a field with known value which will be used for checks.
226 if (! self
::$hasCheckPageRefresh) {
227 self
::$hasCheckPageRefresh = true;
231 foreach ($this->forms
as $form) {
232 $tabs[$form->name
] = Descriptions
::get('Form_' . $form->name
);
235 // validate only when we aren't displaying a "new server" form
236 $isNewServer = false;
237 foreach ($this->forms
as $form) {
238 if ($form->index
=== 0) {
244 if (! $isNewServer) {
249 $this->loadUserprefsInfo();
251 $validators = Validator
::getValidators($this->configFile
);
254 foreach ($this->forms
as $key => $form) {
255 $this->formDisplayTemplate
->group
= 0;
257 'name' => $form->name
,
259 'name' => Descriptions
::get('Form_' . $form->name
),
260 'desc' => Descriptions
::get('Form_' . $form->name
, 'desc'),
262 'errors' => $this->errors
[$form->name
] ??
null,
266 foreach ($form->fields
as $field => $path) {
267 $workPath = array_search($path, $this->systemPaths
);
268 $translatedPath = $this->translatedPaths
[$workPath];
269 // always true/false for user preferences display
271 $userPrefsAllow = isset($this->userprefsKeys
[$path])
272 ?
! isset($this->userprefsDisallow
[$path])
275 $forms[$key]['fields_html'] .= $this->displayFieldInput(
284 // register JS validators for this field
285 if (! isset($validators[$path])) {
289 $this->formDisplayTemplate
->addJsValidate($translatedPath, $validators[$path], $fieldValidators);
293 return $this->formDisplayTemplate
->display([
294 'action' => $formAction,
295 'has_check_page_refresh' => self
::$hasCheckPageRefresh,
296 'hidden_fields' => (array) $hiddenFields,
299 'show_buttons' => $showButtons,
300 'default_values' => $defaultValues,
301 'field_validators' => $fieldValidators,
306 * Prepares data for input field display and outputs HTML code
308 * @param string $field field name as it appears in $form
309 * @param string $systemPath field path, eg. Servers/1/verbose
310 * @param string $workPath work path, eg. Servers/4/verbose
311 * @param string $translatedPath work path changed so that it can be
313 * besides the input field
314 * @param bool|null $userPrefsAllow whether user preferences are enabled
315 * for this field (null - no support,
316 * true/false - enabled/disabled)
317 * @param mixed[] $jsDefault array which stores JavaScript code
320 * @return string|null HTML for input field
322 private function displayFieldInput(
327 string $translatedPath,
328 bool|
null $userPrefsAllow,
331 $name = Descriptions
::get($systemPath);
332 $description = Descriptions
::get($systemPath, 'desc');
334 $value = $this->configFile
->get($workPath);
335 $valueDefault = $this->configFile
->getDefault($systemPath);
336 $valueIsDefault = false;
337 if ($value === null ||
$value === $valueDefault) {
338 $value = $valueDefault;
339 $valueIsDefault = true;
343 'doc' => $this->getDocLink($systemPath),
344 'show_restore_default' => true,
345 'userprefs_allow' => $userPrefsAllow,
346 'userprefs_comment' => Descriptions
::get($systemPath, 'cmt'),
348 if (isset($form->default[$systemPath])) {
349 $opts['setvalue'] = (string) $form->default[$systemPath];
352 if (isset($this->errors
[$workPath])) {
353 $opts['errors'] = $this->errors
[$workPath];
357 switch ($form->getOptionType($field)) {
362 $type = 'short_text';
366 $type = 'number_text';
373 $opts['values'] = $form->getOptionValueList($form->fields
[$field]);
377 $value = (array) $value;
378 $valueDefault = (array) $valueDefault;
381 // :group:end is changed to :group:end:{unique id} in Form class
383 if (mb_substr($field, 7, 4) !== 'end:') {
384 $htmlOutput .= $this->formDisplayTemplate
->displayGroupHeader(
385 mb_substr($field, 7),
388 $this->formDisplayTemplate
->displayGroupFooter();
394 trigger_error('Field ' . $systemPath . ' has no type', E_USER_WARNING
);
399 // detect password fields
402 && (str_ends_with($translatedPath, '-password')
403 ||
str_ends_with($translatedPath, 'pass')
404 ||
str_ends_with($translatedPath, 'Pass'))
409 // TrustedProxies requires changes before displaying
410 if ($systemPath === 'TrustedProxies') {
411 foreach ($value as $ip => &$v) {
412 if (preg_match('/^-\d+$/', $ip) === 1) {
416 $v = $ip . ': ' . $v;
420 $this->setComments($systemPath, $opts);
422 // send default value to form's JS
429 $jsLine = (string) $valueDefault;
432 $jsLine = (bool) $valueDefault;
435 $valueDefaultJs = is_bool($valueDefault)
436 ?
(int) $valueDefault
438 $jsLine = (array) $valueDefaultJs;
441 $val = $valueDefault;
442 if (isset($val['wrapper_params'])) {
443 unset($val['wrapper_params']);
446 $jsLine = implode("\n", $val);
450 $jsDefault[$translatedPath] = $jsLine;
452 return $this->formDisplayTemplate
->displayInput(
466 * @return string HTML for errors
468 public function displayErrors(): string
474 foreach ($this->errors
as $systemPath => $errorList) {
475 if (isset($this->systemPaths
[$systemPath])) {
476 $name = Descriptions
::get($this->systemPaths
[$systemPath]);
478 $name = Descriptions
::get('Form_' . $systemPath);
481 $htmlOutput .= $this->formDisplayTemplate
->displayErrors($name, $errorList);
488 * Reverts erroneous fields to their default values
490 public function fixErrors(): void
493 if ($this->errors
=== []) {
497 foreach (array_keys($this->errors
) as $workPath) {
498 if (! isset($this->systemPaths
[$workPath])) {
502 $canonicalPath = $this->systemPaths
[$workPath];
503 $this->configFile
->set($workPath, $this->configFile
->getDefault($canonicalPath));
508 * Validates select field and casts $value to correct type
510 * @param string|bool $value Current value
511 * @param mixed[] $allowed List of allowed values
513 private function validateSelect(string|
bool &$value, array $allowed): bool
515 $valueCmp = is_bool($value)
518 foreach (array_keys($allowed) as $vk) {
519 // equality comparison only if both values are numeric or not numeric
520 // (allows to skip 0 == 'string' equalling to true)
521 // or identity (for string-string)
522 if (! ($vk == $value && ! (is_numeric($valueCmp) xor is_numeric($vk))) && $vk !== $value) {
526 // keep boolean value as boolean
527 if (! is_bool($value)) {
528 // phpcs:ignore Generic.PHP.ForbiddenFunctions
529 settype($value, gettype($vk));
539 * Validates and saves form data to session
541 * @param string[] $forms List of form names.
542 * @param bool $allowPartialSave Allows for partial form saving on failed validation.
544 public function save(array $forms, bool $allowPartialSave = true): bool
549 $isSetupScript = Config
::getInstance()->get('is_setup');
550 if ($isSetupScript) {
551 $this->loadUserprefsInfo();
555 foreach ($forms as $formName) {
556 if (! isset($this->forms
[$formName])) {
560 $form = $this->forms
[$formName];
561 // get current server id
562 $changeIndex = $form->index
=== 0
563 ?
$this->configFile
->getServerCount() +
1
566 foreach ($form->fields
as $field => $systemPath) {
567 $workPath = array_search($systemPath, $this->systemPaths
);
568 $key = $this->translatedPaths
[$workPath];
569 $type = (string) $form->getOptionType($field);
572 if ($type === 'group') {
576 // ensure the value is set
577 if (! isset($_POST[$key])) {
578 // checkboxes aren't set by browsers if they're off
579 if ($type !== 'boolean') {
580 $this->errors
[$form->name
][] = sprintf(
581 __('Missing data for %s'),
582 '<i>' . Descriptions
::get($systemPath) . '</i>',
588 $_POST[$key] = false;
591 // user preferences allow/disallow
592 if ($isSetupScript && isset($this->userprefsKeys
[$systemPath])) {
593 if (isset($this->userprefsDisallow
[$systemPath], $_POST[$key . '-userprefs-allow'])) {
594 unset($this->userprefsDisallow
[$systemPath]);
595 } elseif (! isset($_POST[$key . '-userprefs-allow'])) {
596 $this->userprefsDisallow
[$systemPath] = true;
600 // cast variables to correct type
603 $_POST[$key] = Util
::requestString($_POST[$key]);
604 $_POST[$key] = (float) $_POST[$key];
608 if ($_POST[$key] !== '') {
609 $_POST[$key] = Util
::requestString($_POST[$key]);
610 // phpcs:ignore Generic.PHP.ForbiddenFunctions
611 settype($_POST[$key], $type);
616 $successfullyValidated = $this->validateSelect(
618 $form->getOptionValueList($systemPath),
620 if (! $successfullyValidated) {
621 $this->errors
[$workPath][] = __('Incorrect value!');
623 // "continue" for the $form->fields foreach-loop
630 $_POST[$key] = Util
::requestString($_POST[$key]);
633 // eliminate empty values and ensure we have an array
634 $postValues = is_array($_POST[$key])
636 : explode("\n", $_POST[$key]);
638 $this->fillPostArrayParameters($postValues, $key);
642 // now we have value with proper type
643 $values[$systemPath] = $_POST[$key];
644 if ($changeIndex !== false) {
645 $workPath = str_replace(
646 'Servers/' . $form->index
. '/',
647 'Servers/' . $changeIndex . '/',
652 $toSave[$workPath] = $systemPath;
657 if (! $allowPartialSave && $this->errors
!== []) {
658 // don't look for non-critical errors
664 foreach ($toSave as $workPath => $path) {
665 // TrustedProxies requires changes before saving
666 if ($path === 'TrustedProxies') {
669 foreach ($values[$path] as $value) {
671 if (preg_match('/^(.+):(?:[ ]?)(\\w+)$/', $value, $matches) === 1) {
672 // correct 'IP: HTTP header' pair
673 $ip = trim($matches[1]);
674 $proxies[$ip] = trim($matches[2]);
676 // save also incorrect values
677 $proxies['-' . $i] = $value;
682 $values[$path] = $proxies;
685 $this->configFile
->set($workPath, $values[$path], $path);
688 if ($isSetupScript) {
689 $this->configFile
->set(
691 array_keys($this->userprefsDisallow
),
695 // don't look for non-critical errors
702 * Tells whether form validation failed
704 public function hasErrors(): bool
706 return $this->errors
!== [];
710 * Returns link to documentation
712 * @param string $path Path to documentation
714 public function getDocLink(string $path): string
716 if (str_starts_with($path, 'Import') ||
str_starts_with($path, 'Export')) {
720 return MySQLDocumentation
::getDocumentationLink(
722 'cfg_' . $this->getOptName($path),
723 $this->isSetupScript ?
'../' : './',
728 * Changes path so it can be used in URLs
730 * @param string $path Path
732 private function getOptName(string $path): string
734 return str_replace(['Servers/1/', '/'], ['Servers/', '_'], $path);
738 * Fills out {@link userprefs_keys} and {@link userprefs_disallow}
740 private function loadUserprefsInfo(): void
742 if ($this->userprefsKeys
!== null) {
746 $this->userprefsKeys
= array_flip(UserFormList
::getFields());
747 // read real config for user preferences display
748 $config = Config
::getInstance();
749 $userPrefsDisallow = $config->get('is_setup')
750 ?
$this->configFile
->get('UserprefsDisallow', [])
751 : $config->settings
['UserprefsDisallow'];
752 $this->userprefsDisallow
= array_flip($userPrefsDisallow ??
[]);
756 * Sets field comments and warnings based on current environment
758 * @param string $systemPath Path to settings
759 * @param mixed[] $opts Chosen options
761 private function setComments(string $systemPath, array &$opts): void
763 // RecodingEngine - mark unavailable types
764 if ($systemPath === 'RecodingEngine') {
766 if (! function_exists('iconv')) {
767 $opts['values']['iconv'] .= ' (' . __('unavailable') . ')';
769 __('"%s" requires %s extension'),
775 /* mbstring is always there thanks to polyfill */
776 $opts['comment'] = $comment;
777 $opts['comment_warning'] = true;
780 // ZipDump, GZipDump, BZipDump - check function availability
781 if ($systemPath === 'ZipDump' ||
$systemPath === 'GZipDump' ||
$systemPath === 'BZipDump') {
784 'ZipDump' => ['zip_open', 'gzcompress'],
785 'GZipDump' => ['gzopen', 'gzencode'],
786 'BZipDump' => ['bzopen', 'bzcompress'],
788 if (! function_exists($funcs[$systemPath][0])) {
791 'Compressed import will not work due to missing function %s.',
793 $funcs[$systemPath][0],
797 if (! function_exists($funcs[$systemPath][1])) {
798 $comment .= ($comment !== '' ?
'; ' : '') . sprintf(
800 'Compressed export will not work due to missing function %s.',
802 $funcs[$systemPath][1],
806 $opts['comment'] = $comment;
807 $opts['comment_warning'] = true;
810 $config = Config
::getInstance();
811 if ($config->get('is_setup')) {
815 if ($systemPath !== 'MaxDbList' && $systemPath !== 'MaxTableList' && $systemPath !== 'QueryHistoryMax') {
819 $opts['comment'] = sprintf(
821 $config->settings
[$systemPath],
826 * Copy items of an array to $_POST variable
828 * @param mixed[] $postValues List of parameters
829 * @param string $key Array key
831 private function fillPostArrayParameters(array $postValues, string $key): void
833 foreach ($postValues as $v) {
834 $v = Util
::requestString($v);