FormDisplayTemplate.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <?php
  2. /**
  3. * Form templates
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Config;
  7. use PhpMyAdmin\Config;
  8. use PhpMyAdmin\Html\Generator;
  9. use PhpMyAdmin\Sanitize;
  10. use PhpMyAdmin\Template;
  11. use function array_flip;
  12. use function array_merge;
  13. use function array_shift;
  14. use function defined;
  15. use function htmlspecialchars;
  16. use function htmlspecialchars_decode;
  17. use function implode;
  18. use function is_bool;
  19. use function mb_strtolower;
  20. use function sprintf;
  21. use function is_string;
  22. /**
  23. * PhpMyAdmin\Config\FormDisplayTemplate class
  24. */
  25. class FormDisplayTemplate
  26. {
  27. /** @var int */
  28. public $group;
  29. /** @var Config */
  30. protected $config;
  31. /** @var Template */
  32. public $template;
  33. /**
  34. * @param Config $config Config instance
  35. */
  36. public function __construct(Config $config)
  37. {
  38. $this->config = $config;
  39. $this->template = new Template();
  40. }
  41. /**
  42. * Displays top part of the form
  43. *
  44. * @param string $action default: $_SERVER['REQUEST_URI']
  45. * @param string $method 'post' or 'get'
  46. * @param array|null $hiddenFields array of form hidden fields (key: field name)
  47. */
  48. public function displayFormTop(
  49. $action = null,
  50. $method = 'post',
  51. $hiddenFields = null
  52. ): string {
  53. static $hasCheckPageRefresh = false;
  54. if ($action === null) {
  55. $action = $_SERVER['REQUEST_URI'];
  56. }
  57. if ($method !== 'post') {
  58. $method = 'get';
  59. }
  60. /**
  61. * We do validation on page refresh when browser remembers field values,
  62. * add a field with known value which will be used for checks.
  63. */
  64. if (! $hasCheckPageRefresh) {
  65. $hasCheckPageRefresh = true;
  66. }
  67. return $this->template->render('config/form_display/form_top', [
  68. 'method' => $method,
  69. 'action' => $action,
  70. 'has_check_page_refresh' => $hasCheckPageRefresh,
  71. 'hidden_fields' => (array) $hiddenFields,
  72. ]);
  73. }
  74. /**
  75. * Displays form tabs which are given by an array indexed by fieldset id
  76. * ({@link self::displayFieldsetTop}), with values being tab titles.
  77. *
  78. * @param array $tabs tab names
  79. */
  80. public function displayTabsTop(array $tabs): string
  81. {
  82. return $this->template->render('config/form_display/tabs_top', ['tabs' => $tabs]);
  83. }
  84. /**
  85. * Displays top part of a fieldset
  86. *
  87. * @param string $title title of fieldset
  88. * @param string $description description shown on top of fieldset
  89. * @param array|null $errors error messages to display
  90. * @param array $attributes optional extra attributes of fieldset
  91. */
  92. public function displayFieldsetTop(
  93. $title = '',
  94. $description = '',
  95. $errors = null,
  96. array $attributes = []
  97. ): string {
  98. $this->group = 0;
  99. $attributes = array_merge(['class' => 'optbox'], $attributes);
  100. return $this->template->render('config/form_display/fieldset_top', [
  101. 'attributes' => $attributes,
  102. 'title' => $title,
  103. 'description' => $description,
  104. 'errors' => $errors,
  105. ]);
  106. }
  107. /**
  108. * Displays input field
  109. *
  110. * $opts keys:
  111. * o doc - (string) documentation link
  112. * o errors - error array
  113. * o setvalue - (string) shows button allowing to set predefined value
  114. * o show_restore_default - (boolean) whether show "restore default" button
  115. * o userprefs_allow - whether user preferences are enabled for this field
  116. * (null - no support, true/false - enabled/disabled)
  117. * o userprefs_comment - (string) field comment
  118. * o values - key - value pairs for <select> fields
  119. * o values_escaped - (boolean) tells whether values array is already escaped
  120. * (defaults to false)
  121. * o values_disabled - (array)list of disabled values (keys from values)
  122. * o comment - (string) tooltip comment
  123. * o comment_warning - (bool) whether this comments warns about something
  124. *
  125. * @param string $path config option path
  126. * @param string $name config option name
  127. * @param string $type type of config option
  128. * @param mixed $value current value
  129. * @param string $description verbose description
  130. * @param bool $valueIsDefault whether value is default
  131. * @param array|null $opts see above description
  132. */
  133. public function displayInput(
  134. $path,
  135. $name,
  136. $type,
  137. $value,
  138. $description = '',
  139. $valueIsDefault = true,
  140. $opts = null
  141. ): string {
  142. static $icons; // An array of IMG tags used further below in the function
  143. if (defined('TESTSUITE')) {
  144. $icons = null;
  145. }
  146. $isSetupScript = $this->config->get('is_setup');
  147. if ($icons === null) { // if the static variables have not been initialised
  148. $icons = [];
  149. // Icon definitions:
  150. // The same indexes will be used in the $icons array.
  151. // The first element contains the filename and the second
  152. // element is used for the "alt" and "title" attributes.
  153. $iconInit = [
  154. 'edit' => [
  155. 'b_edit',
  156. '',
  157. ],
  158. 'help' => [
  159. 'b_help',
  160. __('Documentation'),
  161. ],
  162. 'reload' => [
  163. 's_reload',
  164. '',
  165. ],
  166. 'tblops' => [
  167. 'b_tblops',
  168. '',
  169. ],
  170. ];
  171. if ($isSetupScript) {
  172. // When called from the setup script, we don't have access to the
  173. // sprite-aware getImage() function because the PMA_theme class
  174. // has not been loaded, so we generate the img tags manually.
  175. foreach ($iconInit as $k => $v) {
  176. $title = '';
  177. if (! empty($v[1])) {
  178. $title = ' title="' . $v[1] . '"';
  179. }
  180. $icons[$k] = sprintf(
  181. '<img alt="%s" src="%s"%s>',
  182. $v[1],
  183. '../themes/pmahomme/img/' . $v[0] . '.png',
  184. $title
  185. );
  186. }
  187. } else {
  188. // In this case we just use getImage() because it's available
  189. foreach ($iconInit as $k => $v) {
  190. $icons[$k] = Generator::getImage(
  191. $v[0],
  192. $v[1]
  193. );
  194. }
  195. }
  196. }
  197. $hasErrors = isset($opts['errors']) && ! empty($opts['errors']);
  198. $optionIsDisabled = ! $isSetupScript && isset($opts['userprefs_allow'])
  199. && ! $opts['userprefs_allow'];
  200. $nameId = 'name="' . htmlspecialchars($path) . '" id="'
  201. . htmlspecialchars($path) . '"';
  202. $fieldClass = $type === 'checkbox' ? 'checkbox' : '';
  203. if (! $valueIsDefault) {
  204. $fieldClass .= ($fieldClass == '' ? '' : ' ')
  205. . ($hasErrors ? 'custom field-error' : 'custom');
  206. }
  207. $fieldClass = $fieldClass ? ' class="' . $fieldClass . '"' : '';
  208. $trClass = $this->group > 0
  209. ? 'group-field group-field-' . $this->group
  210. : '';
  211. if (isset($opts['setvalue']) && $opts['setvalue'] === ':group') {
  212. unset($opts['setvalue']);
  213. $this->group++;
  214. $trClass = 'group-header-field group-header-' . $this->group;
  215. }
  216. if ($optionIsDisabled) {
  217. $trClass .= ($trClass ? ' ' : '') . 'disabled-field';
  218. }
  219. $trClass = $trClass ? ' class="' . $trClass . '"' : '';
  220. $htmlOutput = '<tr' . $trClass . '>';
  221. $htmlOutput .= '<th>';
  222. $htmlOutput .= '<label for="' . htmlspecialchars($path) . '">' . htmlspecialchars_decode($name)
  223. . '</label>';
  224. if (! empty($opts['doc'])) {
  225. $htmlOutput .= '<span class="doc">';
  226. $htmlOutput .= '<a href="' . $opts['doc']
  227. . '" target="documentation">' . $icons['help'] . '</a>';
  228. $htmlOutput .= "\n";
  229. $htmlOutput .= '</span>';
  230. }
  231. if ($optionIsDisabled) {
  232. $htmlOutput .= '<span class="disabled-notice" title="';
  233. $htmlOutput .= __(
  234. 'This setting is disabled, it will not be applied to your configuration.'
  235. );
  236. $htmlOutput .= '">' . __('Disabled') . '</span>';
  237. }
  238. if (! empty($description)) {
  239. $htmlOutput .= '<small>' . $description . '</small>';
  240. }
  241. $htmlOutput .= '</th>';
  242. $htmlOutput .= '<td>';
  243. switch ($type) {
  244. case 'text':
  245. $htmlOutput .= '<input type="text" class="w-75" ' . $nameId . $fieldClass
  246. . ' value="' . htmlspecialchars($value) . '">';
  247. break;
  248. case 'password':
  249. $htmlOutput .= '<input type="password" class="w-75" ' . $nameId . $fieldClass
  250. . ' value="' . htmlspecialchars($value) . '">';
  251. break;
  252. case 'short_text':
  253. // As seen in the reporting server (#15042) we sometimes receive
  254. // an array here. No clue about its origin nor content, so let's avoid
  255. // a notice on htmlspecialchars().
  256. if (is_string($value)) {
  257. $htmlOutput .= '<input type="text" size="25" ' . $nameId
  258. . $fieldClass . ' value="' . htmlspecialchars($value)
  259. . '">';
  260. }
  261. break;
  262. case 'number_text':
  263. $htmlOutput .= '<input type="number" ' . $nameId . $fieldClass
  264. . ' value="' . htmlspecialchars((string) $value) . '">';
  265. break;
  266. case 'checkbox':
  267. $htmlOutput .= '<span' . $fieldClass . '><input type="checkbox" ' . $nameId
  268. . ($value ? ' checked="checked"' : '') . '></span>';
  269. break;
  270. case 'select':
  271. $htmlOutput .= '<select class="w-75" ' . $nameId . $fieldClass . '>';
  272. $escape = ! (isset($opts['values_escaped']) && $opts['values_escaped']);
  273. $valuesDisabled = isset($opts['values_disabled'])
  274. ? array_flip($opts['values_disabled']) : [];
  275. foreach ($opts['values'] as $optValueKey => $optValue) {
  276. // set names for boolean values
  277. if (is_bool($optValue)) {
  278. $optValue = mb_strtolower(
  279. $optValue ? __('Yes') : __('No')
  280. );
  281. }
  282. // escape if necessary
  283. if ($escape) {
  284. $display = htmlspecialchars((string) $optValue);
  285. $displayValue = htmlspecialchars((string) $optValueKey);
  286. } else {
  287. $display = $optValue;
  288. $displayValue = $optValueKey;
  289. }
  290. // compare with selected value
  291. // boolean values are cast to integers when used as array keys
  292. $selected = is_bool($value)
  293. ? (int) $value === $optValueKey
  294. : $optValueKey === $value;
  295. $htmlOutput .= '<option value="' . $displayValue . '"';
  296. if ($selected) {
  297. $htmlOutput .= ' selected="selected"';
  298. }
  299. if (isset($valuesDisabled[$optValueKey])) {
  300. $htmlOutput .= ' disabled="disabled"';
  301. }
  302. $htmlOutput .= '>' . $display . '</option>';
  303. }
  304. $htmlOutput .= '</select>';
  305. break;
  306. case 'list':
  307. $val = $value;
  308. if (isset($val['wrapper_params'])) {
  309. unset($val['wrapper_params']);
  310. }
  311. $htmlOutput .= '<textarea cols="35" rows="5" ' . $nameId . $fieldClass
  312. . '>' . htmlspecialchars(implode("\n", $val)) . '</textarea>';
  313. break;
  314. }
  315. if ($isSetupScript
  316. && isset($opts['userprefs_comment'])
  317. && $opts['userprefs_comment']
  318. ) {
  319. $htmlOutput .= '<a class="userprefs-comment" title="'
  320. . htmlspecialchars($opts['userprefs_comment']) . '">'
  321. . $icons['tblops'] . '</a>';
  322. }
  323. if (isset($opts['setvalue']) && $opts['setvalue']) {
  324. $htmlOutput .= '<a class="set-value hide" href="#'
  325. . htmlspecialchars($path . '=' . $opts['setvalue']) . '" title="'
  326. . sprintf(__('Set value: %s'), htmlspecialchars($opts['setvalue']))
  327. . '">' . $icons['edit'] . '</a>';
  328. }
  329. if (isset($opts['show_restore_default']) && $opts['show_restore_default']) {
  330. $htmlOutput .= '<a class="restore-default hide" href="#' . $path . '" title="'
  331. . __('Restore default value') . '">' . $icons['reload'] . '</a>';
  332. }
  333. // this must match with displayErrors() in scripts/config.js
  334. if ($hasErrors) {
  335. $htmlOutput .= "\n <dl class=\"inline_errors\">";
  336. foreach ($opts['errors'] as $error) {
  337. $htmlOutput .= '<dd>' . htmlspecialchars($error) . '</dd>';
  338. }
  339. $htmlOutput .= '</dl>';
  340. }
  341. $htmlOutput .= '</td>';
  342. if ($isSetupScript && isset($opts['userprefs_allow'])) {
  343. $htmlOutput .= '<td class="userprefs-allow" title="' .
  344. __('Allow users to customize this value') . '">';
  345. $htmlOutput .= '<input type="checkbox" name="' . $path
  346. . '-userprefs-allow" ';
  347. if ($opts['userprefs_allow']) {
  348. $htmlOutput .= 'checked="checked"';
  349. }
  350. $htmlOutput .= '>';
  351. $htmlOutput .= '</td>';
  352. } elseif ($isSetupScript) {
  353. $htmlOutput .= '<td>&nbsp;</td>';
  354. }
  355. $htmlOutput .= '</tr>';
  356. return $htmlOutput;
  357. }
  358. /**
  359. * Display group header
  360. *
  361. * @param string $headerText Text of header
  362. */
  363. public function displayGroupHeader(string $headerText): string
  364. {
  365. $this->group++;
  366. if ($headerText === '') {
  367. return '';
  368. }
  369. $colspan = $this->config->get('is_setup') ? 3 : 2;
  370. return $this->template->render('config/form_display/group_header', [
  371. 'group' => $this->group,
  372. 'colspan' => $colspan,
  373. 'header_text' => $headerText,
  374. ]);
  375. }
  376. /**
  377. * Display group footer
  378. */
  379. public function displayGroupFooter(): void
  380. {
  381. $this->group--;
  382. }
  383. /**
  384. * Displays bottom part of a fieldset
  385. *
  386. * @param bool $showButtons Whether show submit and reset button
  387. */
  388. public function displayFieldsetBottom(bool $showButtons = true): string
  389. {
  390. return $this->template->render('config/form_display/fieldset_bottom', [
  391. 'show_buttons' => $showButtons,
  392. 'is_setup' => $this->config->get('is_setup'),
  393. ]);
  394. }
  395. /**
  396. * Closes form tabs
  397. */
  398. public function displayTabsBottom(): string
  399. {
  400. return $this->template->render('config/form_display/tabs_bottom');
  401. }
  402. /**
  403. * Displays bottom part of the form
  404. */
  405. public function displayFormBottom(): string
  406. {
  407. return $this->template->render('config/form_display/form_bottom');
  408. }
  409. /**
  410. * Appends JS validation code to $js_array
  411. *
  412. * @param string $fieldId ID of field to validate
  413. * @param string|array $validators validators callback
  414. * @param array $jsArray will be updated with javascript code
  415. */
  416. public function addJsValidate($fieldId, $validators, array &$jsArray): void
  417. {
  418. foreach ((array) $validators as $validator) {
  419. $validator = (array) $validator;
  420. $vName = array_shift($validator);
  421. $vArgs = [];
  422. foreach ($validator as $arg) {
  423. $vArgs[] = Sanitize::escapeJsString($arg);
  424. }
  425. $vArgs = $vArgs ? ", ['" . implode("', '", $vArgs) . "']" : '';
  426. $jsArray[] = "registerFieldValidator('" . $fieldId . "', '" . $vName . "', true" . $vArgs . ')';
  427. }
  428. }
  429. /**
  430. * Displays JavaScript code
  431. *
  432. * @param array $jsArray lines of javascript code
  433. */
  434. public function displayJavascript(array $jsArray): string
  435. {
  436. if (empty($jsArray)) {
  437. return '';
  438. }
  439. return $this->template->render('javascript/display', ['js_array' => $jsArray]);
  440. }
  441. /**
  442. * Displays error list
  443. *
  444. * @param string $name Name of item with errors
  445. * @param array $errorList List of errors to show
  446. *
  447. * @return string HTML for errors
  448. */
  449. public function displayErrors($name, array $errorList): string
  450. {
  451. return $this->template->render('config/form_display/errors', [
  452. 'name' => $name,
  453. 'error_list' => $errorList,
  454. ]);
  455. }
  456. }