Translated using Weblate (Hebrew)
[phpmyadmin.git] / src / Config / FormDisplay.php
blob5e9f9b5a54469887bcc8abaa0d326f151729bc75
1 <?php
2 /**
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;
21 use PhpMyAdmin\Util;
23 use function __;
24 use function array_flip;
25 use function array_keys;
26 use function array_search;
27 use function explode;
28 use function function_exists;
29 use function gettype;
30 use function implode;
31 use function is_array;
32 use function is_bool;
33 use function is_numeric;
34 use function mb_substr;
35 use function preg_match;
36 use function settype;
37 use function sprintf;
38 use function str_ends_with;
39 use function str_replace;
40 use function str_starts_with;
41 use function trigger_error;
42 use function trim;
44 use const E_USER_WARNING;
46 /**
47 * Form management class, displays and processes forms
49 class FormDisplay
51 /**
52 * Form list
54 * @var array<string, Form>
56 private array $forms = [];
58 /**
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
63 * @var mixed[]
65 private array $errors = [];
67 /**
68 * Paths changed so that they can be used as HTML ids, indexed by paths
70 * @var mixed[]
72 private array $translatedPaths = [];
74 /**
75 * Server paths change indexes so we define maps from current server
76 * path to the first one, indexed by work path
78 * @var mixed[]
80 private array $systemPaths = [];
82 /**
83 * Tells whether forms have been validated
85 private bool $isValidated = true;
87 /**
88 * Dictionary with user preferences keys
90 private array|null $userprefsKeys = null;
92 /**
93 * Dictionary with disallowed user preferences keys
95 * @var mixed[]
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
134 ? $path
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'])) {
151 return false;
154 // save forms
155 if ($this->forms !== []) {
156 return $this->save(array_keys($this->forms), $allowPartialSave);
159 return false;
163 * Runs validation for all registered forms
165 private function validate(): void
167 if ($this->isValidated) {
168 return;
171 $paths = [];
172 $values = [];
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);
179 $paths[] = $path;
183 // run validation
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 !== []) {
188 $this->errors = [];
189 foreach ($errors as $path => $errorList) {
190 $workPath = array_search($path, $this->systemPaths);
191 // field error
192 if (! $workPath) {
193 // form error, fix path
194 $workPath = $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
210 * name)
212 * @return string HTML for forms
214 public function getDisplay(
215 bool $showButtons = true,
216 string|null $formAction = null,
217 array|null $hiddenFields = null,
218 ): string {
219 $fieldValidators = [];
220 $defaultValues = [];
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;
230 $tabs = [];
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) {
239 $isNewServer = true;
240 break;
244 if (! $isNewServer) {
245 $this->validate();
248 // user preferences
249 $this->loadUserprefsInfo();
251 $validators = Validator::getValidators($this->configFile);
252 $forms = [];
254 foreach ($this->forms as $key => $form) {
255 $this->formDisplayTemplate->group = 0;
256 $forms[$key] = [
257 'name' => $form->name,
258 'descriptions' => [
259 'name' => Descriptions::get('Form_' . $form->name),
260 'desc' => Descriptions::get('Form_' . $form->name, 'desc'),
262 'errors' => $this->errors[$form->name] ?? null,
263 'fields_html' => '',
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
270 // otherwise null
271 $userPrefsAllow = isset($this->userprefsKeys[$path])
272 ? ! isset($this->userprefsDisallow[$path])
273 : null;
274 // display input
275 $forms[$key]['fields_html'] .= $this->displayFieldInput(
276 $form,
277 $field,
278 $path,
279 $workPath,
280 $translatedPath,
281 $userPrefsAllow,
282 $defaultValues,
284 // register JS validators for this field
285 if (! isset($validators[$path])) {
286 continue;
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,
297 'tabs' => $tabs,
298 'forms' => $forms,
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
312 * used as XHTML id
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
318 * to be displayed
320 * @return string|null HTML for input field
322 private function displayFieldInput(
323 Form $form,
324 string $field,
325 string $systemPath,
326 string $workPath,
327 string $translatedPath,
328 bool|null $userPrefsAllow,
329 array &$jsDefault,
330 ): string|null {
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;
342 $opts = [
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];
356 $type = '';
357 switch ($form->getOptionType($field)) {
358 case 'string':
359 $type = 'text';
360 break;
361 case 'short_string':
362 $type = 'short_text';
363 break;
364 case 'double':
365 case 'integer':
366 $type = 'number_text';
367 break;
368 case 'boolean':
369 $type = 'checkbox';
370 break;
371 case 'select':
372 $type = 'select';
373 $opts['values'] = $form->getOptionValueList($form->fields[$field]);
374 break;
375 case 'array':
376 $type = 'list';
377 $value = (array) $value;
378 $valueDefault = (array) $valueDefault;
379 break;
380 case 'group':
381 // :group:end is changed to :group:end:{unique id} in Form class
382 $htmlOutput = '';
383 if (mb_substr($field, 7, 4) !== 'end:') {
384 $htmlOutput .= $this->formDisplayTemplate->displayGroupHeader(
385 mb_substr($field, 7),
387 } else {
388 $this->formDisplayTemplate->displayGroupFooter();
391 return $htmlOutput;
393 case 'NULL':
394 trigger_error('Field ' . $systemPath . ' has no type', E_USER_WARNING);
396 return null;
399 // detect password fields
400 if (
401 $type === 'text'
402 && (str_ends_with($translatedPath, '-password')
403 || str_ends_with($translatedPath, 'pass')
404 || str_ends_with($translatedPath, 'Pass'))
406 $type = 'password';
409 // TrustedProxies requires changes before displaying
410 if ($systemPath === 'TrustedProxies') {
411 foreach ($value as $ip => &$v) {
412 if (preg_match('/^-\d+$/', $ip) === 1) {
413 continue;
416 $v = $ip . ': ' . $v;
420 $this->setComments($systemPath, $opts);
422 // send default value to form's JS
423 $jsLine = '';
424 switch ($type) {
425 case 'text':
426 case 'short_text':
427 case 'number_text':
428 case 'password':
429 $jsLine = (string) $valueDefault;
430 break;
431 case 'checkbox':
432 $jsLine = (bool) $valueDefault;
433 break;
434 case 'select':
435 $valueDefaultJs = is_bool($valueDefault)
436 ? (int) $valueDefault
437 : $valueDefault;
438 $jsLine = (array) $valueDefaultJs;
439 break;
440 case 'list':
441 $val = $valueDefault;
442 if (isset($val['wrapper_params'])) {
443 unset($val['wrapper_params']);
446 $jsLine = implode("\n", $val);
447 break;
450 $jsDefault[$translatedPath] = $jsLine;
452 return $this->formDisplayTemplate->displayInput(
453 $translatedPath,
454 $name,
455 $type,
456 $value,
457 $description,
458 $valueIsDefault,
459 $opts,
464 * Displays errors
466 * @return string HTML for errors
468 public function displayErrors(): string
470 $this->validate();
472 $htmlOutput = '';
474 foreach ($this->errors as $systemPath => $errorList) {
475 if (isset($this->systemPaths[$systemPath])) {
476 $name = Descriptions::get($this->systemPaths[$systemPath]);
477 } else {
478 $name = Descriptions::get('Form_' . $systemPath);
481 $htmlOutput .= $this->formDisplayTemplate->displayErrors($name, $errorList);
484 return $htmlOutput;
488 * Reverts erroneous fields to their default values
490 public function fixErrors(): void
492 $this->validate();
493 if ($this->errors === []) {
494 return;
497 foreach (array_keys($this->errors) as $workPath) {
498 if (! isset($this->systemPaths[$workPath])) {
499 continue;
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)
516 ? (int) $value
517 : $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) {
523 continue;
526 // keep boolean value as boolean
527 if (! is_bool($value)) {
528 // phpcs:ignore Generic.PHP.ForbiddenFunctions
529 settype($value, gettype($vk));
532 return true;
535 return false;
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
546 $result = true;
547 $values = [];
548 $toSave = [];
549 $isSetupScript = Config::getInstance()->get('is_setup');
550 if ($isSetupScript) {
551 $this->loadUserprefsInfo();
554 $this->errors = [];
555 foreach ($forms as $formName) {
556 if (! isset($this->forms[$formName])) {
557 continue;
560 $form = $this->forms[$formName];
561 // get current server id
562 $changeIndex = $form->index === 0
563 ? $this->configFile->getServerCount() + 1
564 : false;
565 // grab POST values
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);
571 // skip groups
572 if ($type === 'group') {
573 continue;
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>',
584 $result = false;
585 continue;
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
601 switch ($type) {
602 case 'double':
603 $_POST[$key] = Util::requestString($_POST[$key]);
604 $_POST[$key] = (float) $_POST[$key];
605 break;
606 case 'boolean':
607 case 'integer':
608 if ($_POST[$key] !== '') {
609 $_POST[$key] = Util::requestString($_POST[$key]);
610 // phpcs:ignore Generic.PHP.ForbiddenFunctions
611 settype($_POST[$key], $type);
614 break;
615 case 'select':
616 $successfullyValidated = $this->validateSelect(
617 $_POST[$key],
618 $form->getOptionValueList($systemPath),
620 if (! $successfullyValidated) {
621 $this->errors[$workPath][] = __('Incorrect value!');
622 $result = false;
623 // "continue" for the $form->fields foreach-loop
624 continue 2;
627 break;
628 case 'string':
629 case 'short_string':
630 $_POST[$key] = Util::requestString($_POST[$key]);
631 break;
632 case 'array':
633 // eliminate empty values and ensure we have an array
634 $postValues = is_array($_POST[$key])
635 ? $_POST[$key]
636 : explode("\n", $_POST[$key]);
637 $_POST[$key] = [];
638 $this->fillPostArrayParameters($postValues, $key);
639 break;
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 . '/',
648 $workPath,
652 $toSave[$workPath] = $systemPath;
656 // save forms
657 if (! $allowPartialSave && $this->errors !== []) {
658 // don't look for non-critical errors
659 $this->validate();
661 return $result;
664 foreach ($toSave as $workPath => $path) {
665 // TrustedProxies requires changes before saving
666 if ($path === 'TrustedProxies') {
667 $proxies = [];
668 $i = 0;
669 foreach ($values[$path] as $value) {
670 $matches = [];
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]);
675 } else {
676 // save also incorrect values
677 $proxies['-' . $i] = $value;
678 $i++;
682 $values[$path] = $proxies;
685 $this->configFile->set($workPath, $values[$path], $path);
688 if ($isSetupScript) {
689 $this->configFile->set(
690 'UserprefsDisallow',
691 array_keys($this->userprefsDisallow),
695 // don't look for non-critical errors
696 $this->validate();
698 return $result;
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')) {
717 return '';
720 return MySQLDocumentation::getDocumentationLink(
721 'config',
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) {
743 return;
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') {
765 $comment = '';
766 if (! function_exists('iconv')) {
767 $opts['values']['iconv'] .= ' (' . __('unavailable') . ')';
768 $comment = sprintf(
769 __('"%s" requires %s extension'),
770 'iconv',
771 'iconv',
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') {
782 $comment = '';
783 $funcs = [
784 'ZipDump' => ['zip_open', 'gzcompress'],
785 'GZipDump' => ['gzopen', 'gzencode'],
786 'BZipDump' => ['bzopen', 'bzcompress'],
788 if (! function_exists($funcs[$systemPath][0])) {
789 $comment = sprintf(
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')) {
812 return;
815 if ($systemPath !== 'MaxDbList' && $systemPath !== 'MaxTableList' && $systemPath !== 'QueryHistoryMax') {
816 return;
819 $opts['comment'] = sprintf(
820 __('maximum %s'),
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);
835 if ($v === '') {
836 continue;
839 $_POST[$key][] = $v;