FormDisplay.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Form management class, displays and processes forms
  5. *
  6. * Explanation of used terms:
  7. * o work_path - original field path, eg. Servers/4/verbose
  8. * o system_path - work_path modified so that it points to the first server,
  9. * eg. Servers/1/verbose
  10. * o translated_path - work_path modified for HTML field name, a path with
  11. * slashes changed to hyphens, eg. Servers-4-verbose
  12. *
  13. * @package PhpMyAdmin
  14. */
  15. declare(strict_types=1);
  16. namespace PhpMyAdmin\Config;
  17. use PhpMyAdmin\Config\Forms\User\UserFormList;
  18. use PhpMyAdmin\Sanitize;
  19. use PhpMyAdmin\Util;
  20. /**
  21. * Form management class, displays and processes forms
  22. *
  23. * @package PhpMyAdmin
  24. */
  25. class FormDisplay
  26. {
  27. /**
  28. * ConfigFile instance
  29. * @var ConfigFile
  30. */
  31. private $_configFile;
  32. /**
  33. * Form list
  34. * @var Form[]
  35. */
  36. private $_forms = [];
  37. /**
  38. * Stores validation errors, indexed by paths
  39. * [ Form_name ] is an array of form errors
  40. * [path] is a string storing error associated with single field
  41. * @var array
  42. */
  43. private $_errors = [];
  44. /**
  45. * Paths changed so that they can be used as HTML ids, indexed by paths
  46. * @var array
  47. */
  48. private $_translatedPaths = [];
  49. /**
  50. * Server paths change indexes so we define maps from current server
  51. * path to the first one, indexed by work path
  52. * @var array
  53. */
  54. private $_systemPaths = [];
  55. /**
  56. * Language strings which will be sent to Messages JS variable
  57. * Will be looked up in $GLOBALS: str{value} or strSetup{value}
  58. * @var array
  59. */
  60. private $_jsLangStrings = [];
  61. /**
  62. * Tells whether forms have been validated
  63. * @var bool
  64. */
  65. private $_isValidated = true;
  66. /**
  67. * Dictionary with user preferences keys
  68. * @var array|null
  69. */
  70. private $_userprefsKeys;
  71. /**
  72. * Dictionary with disallowed user preferences keys
  73. * @var array
  74. */
  75. private $_userprefsDisallow;
  76. /**
  77. * @var FormDisplayTemplate
  78. */
  79. private $formDisplayTemplate;
  80. /**
  81. * Constructor
  82. *
  83. * @param ConfigFile $cf Config file instance
  84. */
  85. public function __construct(ConfigFile $cf)
  86. {
  87. $this->formDisplayTemplate = new FormDisplayTemplate($GLOBALS['PMA_Config']);
  88. $this->_jsLangStrings = [
  89. 'error_nan_p' => __('Not a positive number!'),
  90. 'error_nan_nneg' => __('Not a non-negative number!'),
  91. 'error_incorrect_port' => __('Not a valid port number!'),
  92. 'error_invalid_value' => __('Incorrect value!'),
  93. 'error_value_lte' => __('Value must be less than or equal to %s!'),
  94. ];
  95. $this->_configFile = $cf;
  96. // initialize validators
  97. Validator::getValidators($this->_configFile);
  98. }
  99. /**
  100. * Returns {@link ConfigFile} associated with this instance
  101. *
  102. * @return ConfigFile
  103. */
  104. public function getConfigFile()
  105. {
  106. return $this->_configFile;
  107. }
  108. /**
  109. * Registers form in form manager
  110. *
  111. * @param string $formName Form name
  112. * @param array $form Form data
  113. * @param int $serverId 0 if new server, validation; >= 1 if editing a server
  114. *
  115. * @return void
  116. */
  117. public function registerForm($formName, array $form, $serverId = null)
  118. {
  119. $this->_forms[$formName] = new Form(
  120. $formName,
  121. $form,
  122. $this->_configFile,
  123. $serverId
  124. );
  125. $this->_isValidated = false;
  126. foreach ($this->_forms[$formName]->fields as $path) {
  127. $workPath = $serverId === null
  128. ? $path
  129. : str_replace('Servers/1/', "Servers/$serverId/", $path);
  130. $this->_systemPaths[$workPath] = $path;
  131. $this->_translatedPaths[$workPath] = str_replace('/', '-', $workPath);
  132. }
  133. }
  134. /**
  135. * Processes forms, returns true on successful save
  136. *
  137. * @param bool $allowPartialSave allows for partial form saving
  138. * on failed validation
  139. * @param bool $checkFormSubmit whether check for $_POST['submit_save']
  140. *
  141. * @return boolean whether processing was successful
  142. */
  143. public function process($allowPartialSave = true, $checkFormSubmit = true)
  144. {
  145. if ($checkFormSubmit && ! isset($_POST['submit_save'])) {
  146. return false;
  147. }
  148. // save forms
  149. if (count($this->_forms) > 0) {
  150. return $this->save(array_keys($this->_forms), $allowPartialSave);
  151. }
  152. return false;
  153. }
  154. /**
  155. * Runs validation for all registered forms
  156. *
  157. * @return void
  158. */
  159. private function _validate()
  160. {
  161. if ($this->_isValidated) {
  162. return;
  163. }
  164. $paths = [];
  165. $values = [];
  166. foreach ($this->_forms as $form) {
  167. /** @var Form $form */
  168. $paths[] = $form->name;
  169. // collect values and paths
  170. foreach ($form->fields as $path) {
  171. $workPath = array_search($path, $this->_systemPaths);
  172. $values[$path] = $this->_configFile->getValue($workPath);
  173. $paths[] = $path;
  174. }
  175. }
  176. // run validation
  177. $errors = Validator::validate(
  178. $this->_configFile,
  179. $paths,
  180. $values,
  181. false
  182. );
  183. // change error keys from canonical paths to work paths
  184. if (is_array($errors) && count($errors) > 0) {
  185. $this->_errors = [];
  186. foreach ($errors as $path => $errorList) {
  187. $workPath = array_search($path, $this->_systemPaths);
  188. // field error
  189. if (! $workPath) {
  190. // form error, fix path
  191. $workPath = $path;
  192. }
  193. $this->_errors[$workPath] = $errorList;
  194. }
  195. }
  196. $this->_isValidated = true;
  197. }
  198. /**
  199. * Outputs HTML for the forms under the menu tab
  200. *
  201. * @param bool $showRestoreDefault whether to show "restore default"
  202. * button besides the input field
  203. * @param array $jsDefault stores JavaScript code
  204. * to be displayed
  205. * @param array $js will be updated with javascript code
  206. * @param bool $showButtons whether show submit and reset button
  207. *
  208. * @return string
  209. */
  210. private function _displayForms(
  211. $showRestoreDefault,
  212. array &$jsDefault,
  213. array &$js,
  214. $showButtons
  215. ) {
  216. $htmlOutput = '';
  217. $validators = Validator::getValidators($this->_configFile);
  218. foreach ($this->_forms as $form) {
  219. /** @var Form $form */
  220. $formErrors = isset($this->_errors[$form->name])
  221. ? $this->_errors[$form->name] : null;
  222. $htmlOutput .= $this->formDisplayTemplate->displayFieldsetTop(
  223. Descriptions::get("Form_{$form->name}"),
  224. Descriptions::get("Form_{$form->name}", 'desc'),
  225. $formErrors,
  226. ['id' => $form->name]
  227. );
  228. foreach ($form->fields as $field => $path) {
  229. $workPath = array_search($path, $this->_systemPaths);
  230. $translatedPath = $this->_translatedPaths[$workPath];
  231. // always true/false for user preferences display
  232. // otherwise null
  233. $userPrefsAllow = isset($this->_userprefsKeys[$path])
  234. ? ! isset($this->_userprefsDisallow[$path])
  235. : null;
  236. // display input
  237. $htmlOutput .= $this->_displayFieldInput(
  238. $form,
  239. $field,
  240. $path,
  241. $workPath,
  242. $translatedPath,
  243. $showRestoreDefault,
  244. $userPrefsAllow,
  245. $jsDefault
  246. );
  247. // register JS validators for this field
  248. if (isset($validators[$path])) {
  249. $this->formDisplayTemplate->addJsValidate($translatedPath, $validators[$path], $js);
  250. }
  251. }
  252. $htmlOutput .= $this->formDisplayTemplate->displayFieldsetBottom($showButtons);
  253. }
  254. return $htmlOutput;
  255. }
  256. /**
  257. * Outputs HTML for forms
  258. *
  259. * @param bool $tabbedForm if true, use a form with tabs
  260. * @param bool $showRestoreDefault whether show "restore default" button
  261. * besides the input field
  262. * @param bool $showButtons whether show submit and reset button
  263. * @param string $formAction action attribute for the form
  264. * @param array|null $hiddenFields array of form hidden fields (key: field
  265. * name)
  266. *
  267. * @return string HTML for forms
  268. */
  269. public function getDisplay(
  270. $tabbedForm = false,
  271. $showRestoreDefault = false,
  272. $showButtons = true,
  273. $formAction = null,
  274. $hiddenFields = null
  275. ) {
  276. static $jsLangSent = false;
  277. $htmlOutput = '';
  278. $js = [];
  279. $jsDefault = [];
  280. $htmlOutput .= $this->formDisplayTemplate->displayFormTop($formAction, 'post', $hiddenFields);
  281. if ($tabbedForm) {
  282. $tabs = [];
  283. foreach ($this->_forms as $form) {
  284. $tabs[$form->name] = Descriptions::get("Form_$form->name");
  285. }
  286. $htmlOutput .= $this->formDisplayTemplate->displayTabsTop($tabs);
  287. }
  288. // validate only when we aren't displaying a "new server" form
  289. $isNewServer = false;
  290. foreach ($this->_forms as $form) {
  291. /** @var Form $form */
  292. if ($form->index === 0) {
  293. $isNewServer = true;
  294. break;
  295. }
  296. }
  297. if (! $isNewServer) {
  298. $this->_validate();
  299. }
  300. // user preferences
  301. $this->_loadUserprefsInfo();
  302. // display forms
  303. $htmlOutput .= $this->_displayForms(
  304. $showRestoreDefault,
  305. $jsDefault,
  306. $js,
  307. $showButtons
  308. );
  309. if ($tabbedForm) {
  310. $htmlOutput .= $this->formDisplayTemplate->displayTabsBottom();
  311. }
  312. $htmlOutput .= $this->formDisplayTemplate->displayFormBottom();
  313. // if not already done, send strings used for validation to JavaScript
  314. if (! $jsLangSent) {
  315. $jsLangSent = true;
  316. $jsLang = [];
  317. foreach ($this->_jsLangStrings as $strName => $strValue) {
  318. $jsLang[] = "'$strName': '" . Sanitize::jsFormat($strValue, false) . '\'';
  319. }
  320. $js[] = "$.extend(Messages, {\n\t"
  321. . implode(",\n\t", $jsLang) . '})';
  322. }
  323. $js[] = "$.extend(defaultValues, {\n\t"
  324. . implode(",\n\t", $jsDefault) . '})';
  325. $htmlOutput .= $this->formDisplayTemplate->displayJavascript($js);
  326. return $htmlOutput;
  327. }
  328. /**
  329. * Prepares data for input field display and outputs HTML code
  330. *
  331. * @param Form $form Form object
  332. * @param string $field field name as it appears in $form
  333. * @param string $systemPath field path, eg. Servers/1/verbose
  334. * @param string $workPath work path, eg. Servers/4/verbose
  335. * @param string $translatedPath work path changed so that it can be
  336. * used as XHTML id
  337. * @param bool $showRestoreDefault whether show "restore default" button
  338. * besides the input field
  339. * @param bool|null $userPrefsAllow whether user preferences are enabled
  340. * for this field (null - no support,
  341. * true/false - enabled/disabled)
  342. * @param array $jsDefault array which stores JavaScript code
  343. * to be displayed
  344. *
  345. * @return string|null HTML for input field
  346. */
  347. private function _displayFieldInput(
  348. Form $form,
  349. $field,
  350. $systemPath,
  351. $workPath,
  352. $translatedPath,
  353. $showRestoreDefault,
  354. $userPrefsAllow,
  355. array &$jsDefault
  356. ) {
  357. $name = Descriptions::get($systemPath);
  358. $description = Descriptions::get($systemPath, 'desc');
  359. $value = $this->_configFile->get($workPath);
  360. $valueDefault = $this->_configFile->getDefault($systemPath);
  361. $valueIsDefault = false;
  362. if ($value === null || $value === $valueDefault) {
  363. $value = $valueDefault;
  364. $valueIsDefault = true;
  365. }
  366. $opts = [
  367. 'doc' => $this->getDocLink($systemPath),
  368. 'show_restore_default' => $showRestoreDefault,
  369. 'userprefs_allow' => $userPrefsAllow,
  370. 'userprefs_comment' => Descriptions::get($systemPath, 'cmt'),
  371. ];
  372. if (isset($form->default[$systemPath])) {
  373. $opts['setvalue'] = (string) $form->default[$systemPath];
  374. }
  375. if (isset($this->_errors[$workPath])) {
  376. $opts['errors'] = $this->_errors[$workPath];
  377. }
  378. $type = '';
  379. switch ($form->getOptionType($field)) {
  380. case 'string':
  381. $type = 'text';
  382. break;
  383. case 'short_string':
  384. $type = 'short_text';
  385. break;
  386. case 'double':
  387. case 'integer':
  388. $type = 'number_text';
  389. break;
  390. case 'boolean':
  391. $type = 'checkbox';
  392. break;
  393. case 'select':
  394. $type = 'select';
  395. $opts['values'] = $form->getOptionValueList($form->fields[$field]);
  396. break;
  397. case 'array':
  398. $type = 'list';
  399. $value = (array) $value;
  400. $valueDefault = (array) $valueDefault;
  401. break;
  402. case 'group':
  403. // :group:end is changed to :group:end:{unique id} in Form class
  404. $htmlOutput = '';
  405. if (mb_substr($field, 7, 4) != 'end:') {
  406. $htmlOutput .= $this->formDisplayTemplate->displayGroupHeader(
  407. mb_substr($field, 7)
  408. );
  409. } else {
  410. $this->formDisplayTemplate->displayGroupFooter();
  411. }
  412. return $htmlOutput;
  413. case 'NULL':
  414. trigger_error("Field $systemPath has no type", E_USER_WARNING);
  415. return null;
  416. }
  417. // detect password fields
  418. if ($type === 'text'
  419. && (mb_substr($translatedPath, -9) === '-password'
  420. || mb_substr($translatedPath, -4) === 'pass'
  421. || mb_substr($translatedPath, -4) === 'Pass')
  422. ) {
  423. $type = 'password';
  424. }
  425. // TrustedProxies requires changes before displaying
  426. if ($systemPath == 'TrustedProxies') {
  427. foreach ($value as $ip => &$v) {
  428. if (! preg_match('/^-\d+$/', $ip)) {
  429. $v = $ip . ': ' . $v;
  430. }
  431. }
  432. }
  433. $this->_setComments($systemPath, $opts);
  434. // send default value to form's JS
  435. $jsLine = '\'' . $translatedPath . '\': ';
  436. switch ($type) {
  437. case 'text':
  438. case 'short_text':
  439. case 'number_text':
  440. case 'password':
  441. $jsLine .= '\'' . Sanitize::escapeJsString($valueDefault) . '\'';
  442. break;
  443. case 'checkbox':
  444. $jsLine .= $valueDefault ? 'true' : 'false';
  445. break;
  446. case 'select':
  447. $valueDefaultJs = is_bool($valueDefault)
  448. ? (int) $valueDefault
  449. : $valueDefault;
  450. $jsLine .= '[\'' . Sanitize::escapeJsString($valueDefaultJs) . '\']';
  451. break;
  452. case 'list':
  453. $jsLine .= '\'' . Sanitize::escapeJsString(implode("\n", $valueDefault))
  454. . '\'';
  455. break;
  456. }
  457. $jsDefault[] = $jsLine;
  458. return $this->formDisplayTemplate->displayInput(
  459. $translatedPath,
  460. $name,
  461. $type,
  462. $value,
  463. $description,
  464. $valueIsDefault,
  465. $opts
  466. );
  467. }
  468. /**
  469. * Displays errors
  470. *
  471. * @return string|null HTML for errors
  472. */
  473. public function displayErrors()
  474. {
  475. $this->_validate();
  476. if (count($this->_errors) === 0) {
  477. return null;
  478. }
  479. $htmlOutput = '';
  480. foreach ($this->_errors as $systemPath => $errorList) {
  481. if (isset($this->_systemPaths[$systemPath])) {
  482. $name = Descriptions::get($this->_systemPaths[$systemPath]);
  483. } else {
  484. $name = Descriptions::get('Form_' . $systemPath);
  485. }
  486. $htmlOutput .= $this->formDisplayTemplate->displayErrors($name, $errorList);
  487. }
  488. return $htmlOutput;
  489. }
  490. /**
  491. * Reverts erroneous fields to their default values
  492. *
  493. * @return void
  494. */
  495. public function fixErrors()
  496. {
  497. $this->_validate();
  498. if (count($this->_errors) === 0) {
  499. return;
  500. }
  501. $cf = $this->_configFile;
  502. foreach (array_keys($this->_errors) as $workPath) {
  503. if (! isset($this->_systemPaths[$workPath])) {
  504. continue;
  505. }
  506. $canonicalPath = $this->_systemPaths[$workPath];
  507. $cf->set($workPath, $cf->getDefault($canonicalPath));
  508. }
  509. }
  510. /**
  511. * Validates select field and casts $value to correct type
  512. *
  513. * @param string $value Current value
  514. * @param array $allowed List of allowed values
  515. *
  516. * @return bool
  517. */
  518. private function _validateSelect(&$value, array $allowed)
  519. {
  520. $valueCmp = is_bool($value)
  521. ? (int) $value
  522. : $value;
  523. foreach ($allowed as $vk => $v) {
  524. // equality comparison only if both values are numeric or not numeric
  525. // (allows to skip 0 == 'string' equalling to true)
  526. // or identity (for string-string)
  527. if (($vk == $value && ! (is_numeric($valueCmp) xor is_numeric($vk)))
  528. || $vk === $value
  529. ) {
  530. // keep boolean value as boolean
  531. if (! is_bool($value)) {
  532. settype($value, gettype($vk));
  533. }
  534. return true;
  535. }
  536. }
  537. return false;
  538. }
  539. /**
  540. * Validates and saves form data to session
  541. *
  542. * @param array|string $forms array of form names
  543. * @param bool $allowPartialSave allows for partial form saving on
  544. * failed validation
  545. *
  546. * @return boolean true on success (no errors and all saved)
  547. */
  548. public function save($forms, $allowPartialSave = true)
  549. {
  550. $result = true;
  551. $forms = (array) $forms;
  552. $values = [];
  553. $toSave = [];
  554. $isSetupScript = $GLOBALS['PMA_Config']->get('is_setup');
  555. if ($isSetupScript) {
  556. $this->_loadUserprefsInfo();
  557. }
  558. $this->_errors = [];
  559. foreach ($forms as $formName) {
  560. /** @var Form $form */
  561. if (isset($this->_forms[$formName])) {
  562. $form = $this->_forms[$formName];
  563. } else {
  564. continue;
  565. }
  566. // get current server id
  567. $changeIndex = $form->index === 0
  568. ? $this->_configFile->getServerCount() + 1
  569. : false;
  570. // grab POST values
  571. foreach ($form->fields as $field => $systemPath) {
  572. $workPath = array_search($systemPath, $this->_systemPaths);
  573. $key = $this->_translatedPaths[$workPath];
  574. $type = $form->getOptionType($field);
  575. // skip groups
  576. if ($type == 'group') {
  577. continue;
  578. }
  579. // ensure the value is set
  580. if (! isset($_POST[$key])) {
  581. // checkboxes aren't set by browsers if they're off
  582. if ($type == 'boolean') {
  583. $_POST[$key] = false;
  584. } else {
  585. $this->_errors[$form->name][] = sprintf(
  586. __('Missing data for %s'),
  587. '<i>' . Descriptions::get($systemPath) . '</i>'
  588. );
  589. $result = false;
  590. continue;
  591. }
  592. }
  593. // user preferences allow/disallow
  594. if ($isSetupScript
  595. && isset($this->_userprefsKeys[$systemPath])
  596. ) {
  597. if (isset($this->_userprefsDisallow[$systemPath])
  598. && isset($_POST[$key . '-userprefs-allow'])
  599. ) {
  600. unset($this->_userprefsDisallow[$systemPath]);
  601. } elseif (! isset($_POST[$key . '-userprefs-allow'])) {
  602. $this->_userprefsDisallow[$systemPath] = true;
  603. }
  604. }
  605. // cast variables to correct type
  606. switch ($type) {
  607. case 'double':
  608. $_POST[$key] = Util::requestString($_POST[$key]);
  609. settype($_POST[$key], 'float');
  610. break;
  611. case 'boolean':
  612. case 'integer':
  613. if ($_POST[$key] !== '') {
  614. $_POST[$key] = Util::requestString($_POST[$key]);
  615. settype($_POST[$key], $type);
  616. }
  617. break;
  618. case 'select':
  619. $successfullyValidated = $this->_validateSelect(
  620. $_POST[$key],
  621. $form->getOptionValueList($systemPath)
  622. );
  623. if (! $successfullyValidated) {
  624. $this->_errors[$workPath][] = __('Incorrect value!');
  625. $result = false;
  626. // "continue" for the $form->fields foreach-loop
  627. continue 2;
  628. }
  629. break;
  630. case 'string':
  631. case 'short_string':
  632. $_POST[$key] = Util::requestString($_POST[$key]);
  633. break;
  634. case 'array':
  635. // eliminate empty values and ensure we have an array
  636. $postValues = is_array($_POST[$key])
  637. ? $_POST[$key]
  638. : explode("\n", $_POST[$key]);
  639. $_POST[$key] = [];
  640. $this->_fillPostArrayParameters($postValues, $key);
  641. break;
  642. }
  643. // now we have value with proper type
  644. $values[$systemPath] = $_POST[$key];
  645. if ($changeIndex !== false) {
  646. $workPath = str_replace(
  647. "Servers/$form->index/",
  648. "Servers/$changeIndex/",
  649. $workPath
  650. );
  651. }
  652. $toSave[$workPath] = $systemPath;
  653. }
  654. }
  655. // save forms
  656. if (! $allowPartialSave && ! empty($this->_errors)) {
  657. // don't look for non-critical errors
  658. $this->_validate();
  659. return $result;
  660. }
  661. foreach ($toSave as $workPath => $path) {
  662. // TrustedProxies requires changes before saving
  663. if ($path == 'TrustedProxies') {
  664. $proxies = [];
  665. $i = 0;
  666. foreach ($values[$path] as $value) {
  667. $matches = [];
  668. $match = preg_match(
  669. "/^(.+):(?:[ ]?)(\\w+)$/",
  670. $value,
  671. $matches
  672. );
  673. if ($match) {
  674. // correct 'IP: HTTP header' pair
  675. $ip = trim($matches[1]);
  676. $proxies[$ip] = trim($matches[2]);
  677. } else {
  678. // save also incorrect values
  679. $proxies["-$i"] = $value;
  680. $i++;
  681. }
  682. }
  683. $values[$path] = $proxies;
  684. }
  685. $this->_configFile->set($workPath, $values[$path], $path);
  686. }
  687. if ($isSetupScript) {
  688. $this->_configFile->set(
  689. 'UserprefsDisallow',
  690. array_keys($this->_userprefsDisallow)
  691. );
  692. }
  693. // don't look for non-critical errors
  694. $this->_validate();
  695. return $result;
  696. }
  697. /**
  698. * Tells whether form validation failed
  699. *
  700. * @return boolean
  701. */
  702. public function hasErrors()
  703. {
  704. return count($this->_errors) > 0;
  705. }
  706. /**
  707. * Returns link to documentation
  708. *
  709. * @param string $path Path to documentation
  710. *
  711. * @return string
  712. */
  713. public function getDocLink($path)
  714. {
  715. $test = mb_substr($path, 0, 6);
  716. if ($test == 'Import' || $test == 'Export') {
  717. return '';
  718. }
  719. return Util::getDocuLink(
  720. 'config',
  721. 'cfg_' . $this->_getOptName($path)
  722. );
  723. }
  724. /**
  725. * Changes path so it can be used in URLs
  726. *
  727. * @param string $path Path
  728. *
  729. * @return string
  730. */
  731. private function _getOptName($path)
  732. {
  733. return str_replace(['Servers/1/', '/'], ['Servers/', '_'], $path);
  734. }
  735. /**
  736. * Fills out {@link userprefs_keys} and {@link userprefs_disallow}
  737. *
  738. * @return void
  739. */
  740. private function _loadUserprefsInfo()
  741. {
  742. if ($this->_userprefsKeys !== null) {
  743. return;
  744. }
  745. $this->_userprefsKeys = array_flip(UserFormList::getFields());
  746. // read real config for user preferences display
  747. $userPrefsDisallow = $GLOBALS['PMA_Config']->get('is_setup')
  748. ? $this->_configFile->get('UserprefsDisallow', [])
  749. : $GLOBALS['cfg']['UserprefsDisallow'];
  750. $this->_userprefsDisallow = array_flip($userPrefsDisallow ?? []);
  751. }
  752. /**
  753. * Sets field comments and warnings based on current environment
  754. *
  755. * @param string $systemPath Path to settings
  756. * @param array $opts Chosen options
  757. *
  758. * @return void
  759. */
  760. private function _setComments($systemPath, array &$opts)
  761. {
  762. // RecodingEngine - mark unavailable types
  763. if ($systemPath == 'RecodingEngine') {
  764. $comment = '';
  765. if (! function_exists('iconv')) {
  766. $opts['values']['iconv'] .= ' (' . __('unavailable') . ')';
  767. $comment = sprintf(
  768. __('"%s" requires %s extension'),
  769. 'iconv',
  770. 'iconv'
  771. );
  772. }
  773. if (! function_exists('recode_string')) {
  774. $opts['values']['recode'] .= ' (' . __('unavailable') . ')';
  775. $comment .= ($comment ? ", " : '') . sprintf(
  776. __('"%s" requires %s extension'),
  777. 'recode',
  778. 'recode'
  779. );
  780. }
  781. /* mbstring is always there thanks to polyfill */
  782. $opts['comment'] = $comment;
  783. $opts['comment_warning'] = true;
  784. }
  785. // ZipDump, GZipDump, BZipDump - check function availability
  786. if ($systemPath == 'ZipDump'
  787. || $systemPath == 'GZipDump'
  788. || $systemPath == 'BZipDump'
  789. ) {
  790. $comment = '';
  791. $funcs = [
  792. 'ZipDump' => [
  793. 'zip_open',
  794. 'gzcompress',
  795. ],
  796. 'GZipDump' => [
  797. 'gzopen',
  798. 'gzencode',
  799. ],
  800. 'BZipDump' => [
  801. 'bzopen',
  802. 'bzcompress',
  803. ],
  804. ];
  805. if (! function_exists($funcs[$systemPath][0])) {
  806. $comment = sprintf(
  807. __(
  808. 'Compressed import will not work due to missing function %s.'
  809. ),
  810. $funcs[$systemPath][0]
  811. );
  812. }
  813. if (! function_exists($funcs[$systemPath][1])) {
  814. $comment .= ($comment ? '; ' : '') . sprintf(
  815. __(
  816. 'Compressed export will not work due to missing function %s.'
  817. ),
  818. $funcs[$systemPath][1]
  819. );
  820. }
  821. $opts['comment'] = $comment;
  822. $opts['comment_warning'] = true;
  823. }
  824. if (! $GLOBALS['PMA_Config']->get('is_setup')) {
  825. if ($systemPath == 'MaxDbList' || $systemPath == 'MaxTableList'
  826. || $systemPath == 'QueryHistoryMax'
  827. ) {
  828. $opts['comment'] = sprintf(
  829. __('maximum %s'),
  830. $GLOBALS['cfg'][$systemPath]
  831. );
  832. }
  833. }
  834. }
  835. /**
  836. * Copy items of an array to $_POST variable
  837. *
  838. * @param array $postValues List of parameters
  839. * @param string $key Array key
  840. *
  841. * @return void
  842. */
  843. private function _fillPostArrayParameters(array $postValues, $key)
  844. {
  845. foreach ($postValues as $v) {
  846. $v = Util::requestString($v);
  847. if ($v !== '') {
  848. $_POST[$key][] = $v;
  849. }
  850. }
  851. }
  852. }