FormDisplayTemplate.php 18 KB

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