Translation update done using Pootle.
[phpmyadmin/ammaryasirr.git] / libraries / config / FormDisplay.class.php
blob37258787a2678088f6598e96a7efd3580b395f9a
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Form management class, displays and processes forms
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, 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
12 * @package phpMyAdmin
15 /**
16 * Core libraries.
18 require_once './libraries/config/FormDisplay.tpl.php';
19 require_once './libraries/config/validate.lib.php';
20 require_once './libraries/js_escape.lib.php';
22 /**
23 * Form management class, displays and processes forms
24 * @package phpMyAdmin-setup
26 class FormDisplay
28 /**
29 * Form list
30 * @var Form[]
32 private $forms = array();
34 /**
35 * Stores validation errors, indexed by paths
36 * [ Form_name ] is an array of form errors
37 * [path] is a string storing error associated with single field
38 * @var array
40 private $errors = array();
42 /**
43 * Paths changed so that they can be used as HTML ids, indexed by paths
44 * @var array
46 private $translated_paths = array();
48 /**
49 * Server paths change indexes so we define maps from current server
50 * path to the first one, indexed by work path
51 * @var array
53 private $system_paths = array();
55 /**
56 * Language strings which will be sent to PMA_messages JS variable
57 * Will be looked up in $GLOBALS: str{value} or strSetup{value}
58 * @var array
60 private $js_lang_strings = array();
62 /**
63 * Tells whether forms have been validated
64 * @var bool
66 private $is_validated = true;
68 /**
69 * Dictionary with user preferences keys
70 * @var array
72 private $userprefs_keys;
74 /**
75 * Dictionary with disallowed user preferences keys
76 * @var array
78 private $userprefs_disallow;
80 public function __construct()
82 $this->js_lang_strings = array(
83 'error_nan_p' => __('Not a positive number'),
84 'error_nan_nneg' => __('Not a non-negative number'),
85 'error_incorrect_port' => __('Not a valid port number'),
86 'error_invalid_value' => __('Incorrect value'),
87 'error_value_lte' => __('Value must be equal or lower than %s'));
88 // initialize validators
89 PMA_config_get_validators();
92 /**
93 * Registers form in form manager
95 * @param string $form_name
96 * @param array $form
97 * @param int $server_id 0 if new server, validation; >= 1 if editing a server
99 public function registerForm($form_name, array $form, $server_id = null)
101 $this->forms[$form_name] = new Form($form_name, $form, $server_id);
102 $this->is_validated = false;
103 foreach ($this->forms[$form_name]->fields as $path) {
104 $work_path = $server_id === null
105 ? $path
106 : str_replace('Servers/1/', "Servers/$server_id/", $path);
107 $this->system_paths[$work_path] = $path;
108 $this->translated_paths[$work_path] = str_replace('/', '-', $work_path);
113 * Processes forms, returns true on successful save
115 * @param bool $allow_partial_save allows for partial form saving on failed validation
116 * @param bool $check_form_submit whether check for $_POST['submit_save']
117 * @return boolean
119 public function process($allow_partial_save = true, $check_form_submit = true)
121 if ($check_form_submit && !isset($_POST['submit_save'])) {
122 return false;
125 // save forms
126 if (count($this->forms) > 0) {
127 return $this->save(array_keys($this->forms), $allow_partial_save);
129 return false;
133 * Runs validation for all registered forms
136 private function _validate()
138 if ($this->is_validated) {
139 return;
142 $cf = ConfigFile::getInstance();
143 $paths = array();
144 $values = array();
145 foreach ($this->forms as $form) {
146 /* @var $form Form */
147 $paths[] = $form->name;
148 // collect values and paths
149 foreach ($form->fields as $path) {
150 $work_path = array_search($path, $this->system_paths);
151 $values[$path] = $cf->getValue($work_path);
152 $paths[] = $path;
156 // run validation
157 $errors = PMA_config_validate($paths, $values, false);
159 // change error keys from canonical paths to work paths
160 if (is_array($errors) && count($errors) > 0) {
161 $this->errors = array();
162 foreach ($errors as $path => $error_list) {
163 $work_path = array_search($path, $this->system_paths);
164 // field error
165 if (!$work_path) {
166 // form error, fix path
167 $work_path = $path;
169 $this->errors[$work_path] = $error_list;
172 $this->is_validated = true;
176 * Outputs HTML for forms
178 * @param bool $tabbed_form
179 * @param bool $show_restore_default whether show "restore default" button besides the input field
181 public function display($tabbed_form = false, $show_restore_default = false)
183 static $js_lang_sent = false;
185 $js = array();
186 $js_default = array();
187 $tabbed_form = $tabbed_form && (count($this->forms) > 1);
188 $validators = PMA_config_get_validators();
190 display_form_top();
192 if ($tabbed_form) {
193 $tabs = array();
194 foreach ($this->forms as $form) {
195 $tabs[$form->name] = PMA_lang("Form_$form->name");
197 display_tabs_top($tabs);
200 // valdiate only when we aren't displaying a "new server" form
201 $is_new_server = false;
202 foreach ($this->forms as $form) {
203 /* @var $form Form */
204 if ($form->index === 0) {
205 $is_new_server = true;
206 break;
209 if (!$is_new_server) {
210 $this->_validate();
213 // user preferences
214 $this->_loadUserprefsInfo();
216 // display forms
217 foreach ($this->forms as $form) {
218 /* @var $form Form */
219 $form_desc = isset($GLOBALS["strConfigForm_{$form->name}_desc"])
220 ? PMA_lang("Form_{$form->name}_desc")
221 : '';
222 $form_errors = isset($this->errors[$form->name])
223 ? $this->errors[$form->name] : null;
224 display_fieldset_top(PMA_lang("Form_$form->name"),
225 $form_desc, $form_errors, array('id' => $form->name));
227 foreach ($form->fields as $field => $path) {
228 $work_path = array_search($path, $this->system_paths);
229 $translated_path = $this->translated_paths[$work_path];
230 // always true/false for user preferences display
231 // otherwise null
232 $userprefs_allow = isset($this->userprefs_keys[$path])
233 ? !isset($this->userprefs_disallow[$path])
234 : null;
235 // display input
236 $this->_displayFieldInput($form, $field, $path, $work_path,
237 $translated_path, $show_restore_default, $userprefs_allow, $js_default);
238 // register JS validators for this field
239 if (isset($validators[$path])) {
240 js_validate($translated_path, $validators[$path], $js);
243 display_fieldset_bottom();
246 if ($tabbed_form) {
247 display_tabs_bottom();
249 display_form_bottom();
251 // if not already done, send strings used for valdiation to JavaScript
252 if (!$js_lang_sent) {
253 $js_lang_sent = true;
254 $js_lang = array();
255 foreach ($this->js_lang_strings as $strName => $strValue) {
256 $js_lang[] = "'$strName': '" . PMA_jsFormat($strValue, false) . '\'';
258 $js[] = "$.extend(PMA_messages, {\n\t" . implode(",\n\t", $js_lang) . '})';
261 $js[] = "$.extend(defaultValues, {\n\t" . implode(",\n\t", $js_default) . '})';
262 display_js($js);
266 * Prepares data for input field display and outputs HTML code
268 * @param Form $form
269 * @param string $field field name as it appears in $form
270 * @param string $system_path field path, eg. Servers/1/verbose
271 * @param string $work_path work path, eg. Servers/4/verbose
272 * @param string $translated_path work path changed so that it can be used as XHTML id
273 * @param bool $show_restore_default whether show "restore default" button besides the input field
274 * @param mixed $userprefs_allow whether user preferences are enabled for this field
275 * (null - no support, true/false - enabled/disabled)
276 * @param array &$js_default array which stores JavaScript code to be displayed
278 private function _displayFieldInput(Form $form, $field, $system_path, $work_path,
279 $translated_path, $show_restore_default, $userprefs_allow, array &$js_default)
281 $name = PMA_lang_name($system_path);
282 $description = PMA_lang_name($system_path, 'desc', '');
284 $cf = ConfigFile::getInstance();
285 $value = $cf->get($work_path);
286 $value_default = $cf->getDefault($system_path);
287 $value_is_default = false;
288 if ($value === null || $value === $value_default) {
289 $value = $value_default;
290 $value_is_default = true;
293 $opts = array(
294 'doc' => $this->getDocLink($system_path),
295 'wiki' => $this->getWikiLink($system_path),
296 'show_restore_default' => $show_restore_default,
297 'userprefs_allow' => $userprefs_allow,
298 'userprefs_comment' => PMA_lang_name($system_path, 'cmt', ''));
299 if (isset($form->default[$system_path])) {
300 $opts['setvalue'] = $form->default[$system_path];
303 if (isset($this->errors[$work_path])) {
304 $opts['errors'] = $this->errors[$work_path];
306 switch ($form->getOptionType($field)) {
307 case 'string':
308 $type = 'text';
309 break;
310 case 'short_string':
311 $type = 'short_text';
312 break;
313 case 'double':
314 case 'integer':
315 $type = 'number_text';
316 break;
317 case 'boolean':
318 $type = 'checkbox';
319 break;
320 case 'select':
321 $type = 'select';
322 $opts['values'] = $form->getOptionValueList($form->fields[$field]);
323 break;
324 case 'array':
325 $type = 'list';
326 $value = (array) $value;
327 $value_default = (array) $value_default;
328 break;
329 case 'group':
330 if (substr($field, 7, 4) != 'end:') { // :group:end is changed to :group:end:{unique id} in Form class
331 display_group_header(substr($field, 7));
332 } else {
333 display_group_footer();
335 return;
336 case 'NULL':
337 trigger_error("Field $system_path has no type", E_USER_WARNING);
338 return;
341 // TrustedProxies requires changes before displaying
342 if ($system_path == 'TrustedProxies') {
343 foreach ($value as $ip => &$v) {
344 if (!preg_match('/^-\d+$/', $ip)) {
345 $v = $ip . ': ' . $v;
349 $this->_setComments($system_path, $opts);
351 // send default value to form's JS
352 $js_line = '\'' . $translated_path . '\': ';
353 switch ($type) {
354 case 'text':
355 case 'short_text':
356 case 'number_text':
357 $js_line .= '\'' . PMA_escapeJsString($value_default) . '\'';
358 break;
359 case 'checkbox':
360 $js_line .= $value_default ? 'true' : 'false';
361 break;
362 case 'select':
363 $value_default_js = is_bool($value_default)
364 ? (int) $value_default
365 : $value_default;
366 $js_line .= '[\'' . PMA_escapeJsString($value_default_js) . '\']';
367 break;
368 case 'list':
369 $js_line .= '\'' . PMA_escapeJsString(implode("\n", $value_default)) . '\'';
370 break;
372 $js_default[] = $js_line;
374 display_input($translated_path, $name, $description, $type,
375 $value, $value_is_default, $opts);
379 * Displays errors
382 public function displayErrors()
384 $this->_validate();
385 if (count($this->errors) == 0) {
386 return;
389 foreach ($this->errors as $system_path => $error_list) {
390 if (isset($this->system_paths[$system_path])) {
391 $path = $this->system_paths[$system_path];
392 $name = PMA_lang_name($path);
393 } else {
394 $name = $GLOBALS["strConfigForm_$system_path"];
396 display_errors($name, $error_list);
401 * Reverts erroneous fields to their default values
405 public function fixErrors()
407 $this->_validate();
408 if (count($this->errors) == 0) {
409 return;
412 $cf = ConfigFile::getInstance();
413 foreach (array_keys($this->errors) as $work_path) {
414 if (!isset($this->system_paths[$work_path])) {
415 continue;
417 $canonical_path = $this->system_paths[$work_path];
418 $cf->set($work_path, $cf->getDefault($canonical_path));
423 * Validates select field and casts $value to correct type
425 * @param string $value
426 * @param array $allowed
427 * @return bool
429 private function _validateSelect(&$value, array $allowed)
431 $value_cmp = is_bool($value)
432 ? (int) $value
433 : $value;
434 foreach ($allowed as $vk => $v) {
435 // equality comparison only if both values are numeric or not numeric
436 // (allows to skip 0 == 'string' equalling to true) or identity (for string-string)
437 if (($vk == $value && !(is_numeric($value_cmp) xor is_numeric($vk)))
438 || $vk === $value) {
439 // keep boolean value as boolean
440 if (!is_bool($value)) {
441 settype($value, gettype($vk));
443 return true;
446 return false;
450 * Validates and saves form data to session
452 * @param array|string $forms array of form names
453 * @param bool $allow_partial_save allows for partial form saving on failed validation
454 * @return boolean true on success (no errors and all saved)
456 public function save($forms, $allow_partial_save = true)
458 $result = true;
459 $cf = ConfigFile::getInstance();
460 $forms = (array) $forms;
462 $values = array();
463 $to_save = array();
464 $is_setup_script = defined('PMA_SETUP');
465 if ($is_setup_script) {
466 $this->_loadUserprefsInfo();
469 $this->errors = array();
470 foreach ($forms as $form) {
471 /* @var $form Form */
472 if (isset($this->forms[$form])) {
473 $form = $this->forms[$form];
474 } else {
475 continue;
477 // get current server id
478 $change_index = $form->index === 0
479 ? $cf->getServerCount() + 1
480 : false;
481 // grab POST values
482 foreach ($form->fields as $field => $system_path) {
483 $work_path = array_search($system_path, $this->system_paths);
484 $key = $this->translated_paths[$work_path];
485 $type = $form->getOptionType($field);
487 // skip groups
488 if ($type == 'group') {
489 continue;
492 // ensure the value is set
493 if (!isset($_POST[$key])) {
494 // checkboxes aren't set by browsers if they're off
495 if ($type == 'boolean') {
496 $_POST[$key] = false;
497 } else {
498 $this->errors[$form->name][] = sprintf(
499 __('Missing data for %s'),
500 '<i>' . PMA_lang_name($system_path) . '</i>');
501 $result = false;
502 continue;
506 // user preferences allow/disallow
507 if ($is_setup_script && isset($this->userprefs_keys[$system_path])) {
508 if (isset($this->userprefs_disallow[$system_path])
509 && isset($_POST[$key . '-userprefs-allow'])) {
510 unset($this->userprefs_disallow[$system_path]);
511 } else if (!isset($_POST[$key . '-userprefs-allow'])) {
512 $this->userprefs_disallow[$system_path] = true;
516 // cast variables to correct type
517 switch ($type) {
518 case 'double':
519 settype($_POST[$key], 'float');
520 break;
521 case 'boolean':
522 case 'integer':
523 if ($_POST[$key] !== '') {
524 settype($_POST[$key], $type);
526 break;
527 case 'select':
528 // special treatment for NavigationBarIconic and PropertiesIconic
529 if ($key === 'NavigationBarIconic' || $key === 'PropertiesIconic') {
530 if ($_POST[$key] !== 'both') {
531 settype($_POST[$key], 'boolean');
534 if (!$this->_validateSelect($_POST[$key], $form->getOptionValueList($system_path))) {
535 $this->errors[$work_path][] = __('Incorrect value');
536 $result = false;
537 continue;
539 break;
540 case 'string':
541 case 'short_string':
542 $_POST[$key] = trim($_POST[$key]);
543 break;
544 case 'array':
545 // eliminate empty values and ensure we have an array
546 $post_values = is_array($_POST[$key])
547 ? $_POST[$key]
548 : explode("\n", $_POST[$key]);
549 $_POST[$key] = array();
550 foreach ($post_values as $v) {
551 $v = trim($v);
552 if ($v !== '') {
553 $_POST[$key][] = $v;
556 break;
559 // now we have value with proper type
560 $values[$system_path] = $_POST[$key];
561 if ($change_index !== false) {
562 $work_path = str_replace("Servers/$form->index/",
563 "Servers/$change_index/", $work_path);
565 $to_save[$work_path] = $system_path;
569 // save forms
570 if ($allow_partial_save || empty($this->errors)) {
571 foreach ($to_save as $work_path => $path) {
572 // TrustedProxies requires changes before saving
573 if ($path == 'TrustedProxies') {
574 $proxies = array();
575 $i = 0;
576 foreach ($values[$path] as $value) {
577 $matches = array();
578 if (preg_match("/^(.+):(?:[ ]?)(\\w+)$/", $value, $matches)) {
579 // correct 'IP: HTTP header' pair
580 $ip = trim($matches[1]);
581 $proxies[$ip] = trim($matches[2]);
582 } else {
583 // save also incorrect values
584 $proxies["-$i"] = $value;
585 $i++;
588 $values[$path] = $proxies;
590 $cf->set($work_path, $values[$path], $path);
592 if ($is_setup_script) {
593 $cf->set('UserprefsDisallow', array_keys($this->userprefs_disallow));
597 // don't look for non-critical errors
598 $this->_validate();
600 return $result;
604 * Tells whether form validation failed
606 * @return boolean
608 public function hasErrors()
610 return count($this->errors) > 0;
615 * Returns link to documentation
617 * @param string $path
618 * @return string
620 public function getDocLink($path)
622 $test = substr($path, 0, 6);
623 if ($test == 'Import' || $test == 'Export') {
624 return '';
626 return 'Documentation.html#cfg_' . $this->_getOptName($path);
630 * Returns link to wiki
632 * @param string $path
633 * @return string
635 public function getWikiLink($path)
637 $opt_name = $this->_getOptName($path);
638 if (substr($opt_name, 0, 7) == 'Servers') {
639 $opt_name = substr($opt_name, 8);
640 if (strpos($opt_name, 'AllowDeny') === 0) {
641 $opt_name = str_replace('_', '_.28', $opt_name) . '.29';
644 $test = substr($path, 0, 6);
645 if ($test == 'Import') {
646 $opt_name = substr($opt_name, 7);
647 if ($opt_name == 'format') {
648 $opt_name = 'format_2';
651 if ($test == 'Export') {
652 $opt_name = substr($opt_name, 7);
654 return PMA_linkURL('http://wiki.phpmyadmin.net/pma/Config#' . $opt_name);
658 * Changes path so it can be used in URLs
660 * @param string $path
661 * @return string
663 private function _getOptName($path)
665 return str_replace(array('Servers/1/', '/'), array('Servers/', '_'), $path);
669 * Fills out {@link userprefs_keys} and {@link userprefs_disallow}
672 private function _loadUserprefsInfo()
674 if ($this->userprefs_keys === null) {
675 $this->userprefs_keys = array_flip(PMA_read_userprefs_fieldnames());
676 // read real config for user preferences display
677 $userprefs_disallow = defined('PMA_SETUP')
678 ? ConfigFile::getInstance()->get('UserprefsDisallow', array())
679 : $GLOBALS['cfg']['UserprefsDisallow'];
680 $this->userprefs_disallow = array_flip($userprefs_disallow);
685 * Sets field comments and warnings based on current environment
687 * @param string $system_path
688 * @param array $opts
690 private function _setComments($system_path, array &$opts)
692 // RecodingEngine - mark unavailable types
693 if ($system_path == 'RecodingEngine') {
694 $comment = '';
695 if (!function_exists('iconv')) {
696 $opts['values']['iconv'] .= ' (' . __('unavailable') . ')';
697 $comment = sprintf(__('"%s" requires %s extension'), 'iconv', 'iconv');
699 if (!function_exists('recode_string')) {
700 $opts['values']['recode'] .= ' (' . __('unavailable') . ')';
701 $comment .= ($comment ? ", " : '') . sprintf(__('"%s" requires %s extension'),
702 'recode', 'recode');
704 $opts['comment'] = $comment;
705 $opts['comment_warning'] = true;
707 // ZipDump, GZipDump, BZipDump - check function availability
708 if ($system_path == 'ZipDump' || $system_path == 'GZipDump' || $system_path == 'BZipDump') {
709 $comment = '';
710 $funcs = array(
711 'ZipDump' => array('zip_open', 'gzcompress'),
712 'GZipDump' => array('gzopen', 'gzencode'),
713 'BZipDump' => array('bzopen', 'bzcompress'));
714 if (!function_exists($funcs[$system_path][0])) {
715 $comment = sprintf(__('import will not work, missing function (%s)'),
716 $funcs[$system_path][0]);
718 if (!function_exists($funcs[$system_path][1])) {
719 $comment .= ($comment ? '; ' : '') . sprintf(__('export will not work, missing function (%s)'),
720 $funcs[$system_path][1]);
722 $opts['comment'] = $comment;
723 $opts['comment_warning'] = true;
725 if ($system_path == 'SQLQuery/Validate' && !$GLOBALS['cfg']['SQLValidator']['use']) {
726 $opts['comment'] = __('SQL Validator is disabled');
727 $opts['comment_warning'] = true;
729 if ($system_path == 'SQLValidator/use') {
730 if (!class_exists('SOAPClient')) {
731 @include_once 'SOAP/Client.php';
732 if (!class_exists('SOAP_Client')) {
733 $opts['comment'] = __('SOAP extension not found');
734 $opts['comment_warning'] = true;
738 if (!defined('PMA_SETUP')) {
739 if (($system_path == 'MaxDbList' || $system_path == 'MaxTableList'
740 || $system_path == 'QueryHistoryMax')) {
741 $opts['comment'] = sprintf(__('maximum %s'), $GLOBALS['cfg'][$system_path]);