2 /* vim: set expandtab sw=4 ts=4 sts=4: */
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
18 require_once './libraries/config/FormDisplay.tpl.php';
19 require_once './libraries/config/validate.lib.php';
20 require_once './libraries/js_escape.lib.php';
23 * Form management class, displays and processes forms
24 * @package phpMyAdmin-setup
32 private $forms = array();
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
40 private $errors = array();
43 * Paths changed so that they can be used as HTML ids, indexed by paths
46 private $translated_paths = array();
49 * Server paths change indexes so we define maps from current server
50 * path to the first one, indexed by work path
53 private $system_paths = array();
56 * Language strings which will be sent to PMA_messages JS variable
57 * Will be looked up in $GLOBALS: str{value} or strSetup{value}
60 private $js_lang_strings = array();
63 * Tells whether forms have been validated
66 private $is_validated = true;
69 * Dictionary with user preferences keys
72 private $userprefs_keys;
75 * Dictionary with disallowed user preferences keys
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();
93 * Registers form in form manager
95 * @param string $form_name
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
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']
119 public function process($allow_partial_save = true, $check_form_submit = true)
121 if ($check_form_submit && !isset($_POST['submit_save'])) {
126 if (count($this->forms
) > 0) {
127 return $this->save(array_keys($this->forms
), $allow_partial_save);
133 * Runs validation for all registered forms
135 * @uses ConfigFile::getInstance()
136 * @uses ConfigFile::getValue()
137 * @uses PMA_config_validate()
139 private function _validate()
141 if ($this->is_validated
) {
145 $cf = ConfigFile
::getInstance();
148 foreach ($this->forms
as $form) {
149 /* @var $form Form */
150 $paths[] = $form->name
;
151 // collect values and paths
152 foreach ($form->fields
as $path) {
153 $work_path = array_search($path, $this->system_paths
);
154 $values[$path] = $cf->getValue($work_path);
160 $errors = PMA_config_validate($paths, $values, false);
162 // change error keys from canonical paths to work paths
163 if (is_array($errors) && count($errors) > 0) {
164 $this->errors
= array();
165 foreach ($errors as $path => $error_list) {
166 $work_path = array_search($path, $this->system_paths
);
169 // form error, fix path
172 $this->errors
[$work_path] = $error_list;
175 $this->is_validated
= true;
179 * Outputs HTML for forms
181 * @uses ConfigFile::getInstance()
182 * @uses ConfigFile::get()
183 * @uses display_fieldset_bottom()
184 * @uses display_fieldset_top()
185 * @uses display_form_bottom()
186 * @uses display_form_top()
188 * @uses display_tabs_bottom()
189 * @uses display_tabs_top()
190 * @uses js_validate()
191 * @uses PMA_config_get_validators()
192 * @uses PMA_jsFormat()
194 * @param bool $tabbed_form
195 * @param bool $show_restore_default whether show "restore default" button besides the input field
197 public function display($tabbed_form = false, $show_restore_default = false)
199 static $js_lang_sent = false;
202 $js_default = array();
203 $tabbed_form = $tabbed_form && (count($this->forms
) > 1);
204 $validators = PMA_config_get_validators();
210 foreach ($this->forms
as $form) {
211 $tabs[$form->name
] = PMA_lang("Form_$form->name");
213 display_tabs_top($tabs);
216 // valdiate only when we aren't displaying a "new server" form
217 $is_new_server = false;
218 foreach ($this->forms
as $form) {
219 /* @var $form Form */
220 if ($form->index
=== 0) {
221 $is_new_server = true;
225 if (!$is_new_server) {
230 $this->_loadUserprefsInfo();
233 foreach ($this->forms
as $form) {
234 /* @var $form Form */
235 $form_desc = isset($GLOBALS["strConfigForm_{$form->name}_desc"])
236 ?
PMA_lang("Form_{$form->name}_desc")
238 $form_errors = isset($this->errors
[$form->name
])
239 ?
$this->errors
[$form->name
] : null;
240 display_fieldset_top(PMA_lang("Form_$form->name"),
241 $form_desc, $form_errors, array('id' => $form->name
));
243 foreach ($form->fields
as $field => $path) {
244 $work_path = array_search($path, $this->system_paths
);
245 $translated_path = $this->translated_paths
[$work_path];
246 // always true/false for user preferences display
248 $userprefs_allow = isset($this->userprefs_keys
[$path])
249 ?
!isset($this->userprefs_disallow
[$path])
252 $this->_displayFieldInput($form, $field, $path, $work_path,
253 $translated_path, $show_restore_default, $userprefs_allow, $js_default);
254 // register JS validators for this field
255 if (isset($validators[$path])) {
256 js_validate($translated_path, $validators[$path], $js);
259 display_fieldset_bottom();
263 display_tabs_bottom();
265 display_form_bottom();
267 // if not already done, send strings used for valdiation to JavaScript
268 if (!$js_lang_sent) {
269 $js_lang_sent = true;
271 foreach ($this->js_lang_strings
as $strName => $strValue) {
272 $js_lang[] = "'$strName': '" . PMA_jsFormat($strValue, false) . '\'';
274 $js[] = "$.extend(PMA_messages, {\n\t" . implode(",\n\t", $js_lang) . '})';
277 $js[] = "$.extend(defaultValues, {\n\t" . implode(",\n\t", $js_default) . '})';
282 * Prepares data for input field display and outputs HTML code
284 * @uses ConfigFile::get()
285 * @uses ConfigFile::getDefault()
286 * @uses ConfigFile::getInstance()
287 * @uses display_group_footer()
288 * @uses display_group_header()
289 * @uses display_input()
290 * @uses Form::getOptionType()
291 * @uses Form::getOptionValueList()
292 * @uses PMA_escapeJsString()
293 * @uses PMA_lang_desc()
294 * @uses PMA_lang_name()
296 * @param string $field field name as it appears in $form
297 * @param string $system_path field path, eg. Servers/1/verbose
298 * @param string $work_path work path, eg. Servers/4/verbose
299 * @param string $translated_path work path changed so that it can be used as XHTML id
300 * @param bool $show_restore_default whether show "restore default" button besides the input field
301 * @param mixed $userprefs_allow whether user preferences are enabled for this field
302 * (null - no support, true/false - enabled/disabled)
303 * @param array &$js_default array which stores JavaScript code to be displayed
305 private function _displayFieldInput(Form
$form, $field, $system_path, $work_path,
306 $translated_path, $show_restore_default, $userprefs_allow, array &$js_default)
308 $name = PMA_lang_name($system_path);
309 $description = PMA_lang_name($system_path, 'desc', '');
311 $cf = ConfigFile
::getInstance();
312 $value = $cf->get($work_path);
313 $value_default = $cf->getDefault($system_path);
314 $value_is_default = false;
315 if ($value === null ||
$value === $value_default) {
316 $value = $value_default;
317 $value_is_default = true;
321 'doc' => $this->getDocLink($system_path),
322 'wiki' => $this->getWikiLink($system_path),
323 'show_restore_default' => $show_restore_default,
324 'userprefs_allow' => $userprefs_allow,
325 'userprefs_comment' => PMA_lang_name($system_path, 'cmt', ''));
326 if (isset($form->default[$system_path])) {
327 $opts['setvalue'] = $form->default[$system_path];
330 if (isset($this->errors
[$work_path])) {
331 $opts['errors'] = $this->errors
[$work_path];
333 switch ($form->getOptionType($field)) {
338 $type = 'short_text';
342 $type = 'number_text';
349 $opts['values'] = $form->getOptionValueList($form->fields
[$field]);
353 $value = (array) $value;
354 $value_default = (array) $value_default;
357 if (substr($field, 7, 4) != 'end:') { // :group:end is changed to :group:end:{unique id} in Form class
358 display_group_header(substr($field, 7));
360 display_group_footer();
364 trigger_error("Field $system_path has no type", E_USER_WARNING
);
368 // TrustedProxies requires changes before displaying
369 if ($system_path == 'TrustedProxies') {
370 foreach ($value as $ip => &$v) {
371 if (!preg_match('/^-\d+$/', $ip)) {
372 $v = $ip . ': ' . $v;
376 $this->_setComments($system_path, $opts);
378 // send default value to form's JS
379 $js_line = '\'' . $translated_path . '\': ';
384 $js_line .= '\'' . PMA_escapeJsString($value_default) . '\'';
387 $js_line .= $value_default ?
'true' : 'false';
390 $value_default_js = is_bool($value_default)
391 ?
(int) $value_default
393 $js_line .= '[\'' . PMA_escapeJsString($value_default_js) . '\']';
396 $js_line .= '\'' . PMA_escapeJsString(implode("\n", $value_default)) . '\'';
399 $js_default[] = $js_line;
401 display_input($translated_path, $name, $description, $type,
402 $value, $value_is_default, $opts);
408 * @uses display_errors()
409 * @uses PMA_lang_name()
411 public function displayErrors()
414 if (count($this->errors
) == 0) {
418 foreach ($this->errors
as $system_path => $error_list) {
419 if (isset($this->system_paths
[$system_path])) {
420 $path = $this->system_paths
[$system_path];
421 $name = PMA_lang_name($path);
423 $name = $GLOBALS["strConfigForm_$system_path"];
425 display_errors($name, $error_list);
430 * Reverts erroneous fields to their default values
432 * @uses ConfigFile::getDefault()
433 * @uses ConfigFile::getInstance()
434 * @uses ConfigFile::set()
437 public function fixErrors()
440 if (count($this->errors
) == 0) {
444 $cf = ConfigFile
::getInstance();
445 foreach (array_keys($this->errors
) as $work_path) {
446 if (!isset($this->system_paths
[$work_path])) {
449 $canonical_path = $this->system_paths
[$work_path];
450 $cf->set($work_path, $cf->getDefault($canonical_path));
455 * Validates select field and casts $value to correct type
457 * @param string $value
458 * @param array $allowed
461 private function _validateSelect(&$value, array $allowed)
463 $value_cmp = is_bool($value)
466 foreach ($allowed as $vk => $v) {
467 // equality comparison only if both values are numeric or not numeric
468 // (allows to skip 0 == 'string' equalling to true) or identity (for string-string)
469 if (($vk == $value && !(is_numeric($value_cmp) xor is_numeric($vk)))
471 // keep boolean value as boolean
472 if (!is_bool($value)) {
473 settype($value, gettype($vk));
482 * Validates and saves form data to session
484 * @uses ConfigFile::get()
485 * @uses ConfigFile::getInstance()
486 * @uses ConfigFile::getServerCount()
487 * @uses ConfigFile::set()
488 * @uses Form::getOptionType()
489 * @uses Form::getOptionValueList()
490 * @uses PMA_lang_name()
491 * @param array|string $forms array of form names
492 * @param bool $allow_partial_save allows for partial form saving on failed validation
493 * @return boolean true on success (no errors and all saved)
495 public function save($forms, $allow_partial_save = true)
498 $cf = ConfigFile
::getInstance();
499 $forms = (array) $forms;
503 $is_setup_script = defined('PMA_SETUP');
504 if ($is_setup_script) {
505 $this->_loadUserprefsInfo();
508 $this->errors
= array();
509 foreach ($forms as $form) {
510 /* @var $form Form */
511 if (isset($this->forms
[$form])) {
512 $form = $this->forms
[$form];
516 // get current server id
517 $change_index = $form->index
=== 0
518 ?
$cf->getServerCount() +
1
521 foreach ($form->fields
as $field => $system_path) {
522 $work_path = array_search($system_path, $this->system_paths
);
523 $key = $this->translated_paths
[$work_path];
524 $type = $form->getOptionType($field);
527 if ($type == 'group') {
531 // ensure the value is set
532 if (!isset($_POST[$key])) {
533 // checkboxes aren't set by browsers if they're off
534 if ($type == 'boolean') {
535 $_POST[$key] = false;
537 $this->errors
[$form->name
][] = sprintf(
538 __('Missing data for %s'),
539 '<i>' . PMA_lang_name($system_path) . '</i>');
545 // user preferences allow/disallow
546 if ($is_setup_script && isset($this->userprefs_keys
[$system_path])) {
547 if (isset($this->userprefs_disallow
[$system_path])
548 && isset($_POST[$key . '-userprefs-allow'])) {
549 unset($this->userprefs_disallow
[$system_path]);
550 } else if (!isset($_POST[$key . '-userprefs-allow'])) {
551 $this->userprefs_disallow
[$system_path] = true;
555 // cast variables to correct type
558 settype($_POST[$key], 'float');
562 if ($_POST[$key] !== '') {
563 settype($_POST[$key], $type);
567 // special treatment for NavigationBarIconic and PropertiesIconic
568 if ($key === 'NavigationBarIconic' ||
$key === 'PropertiesIconic') {
569 if ($_POST[$key] !== 'both') {
570 settype($_POST[$key], 'boolean');
573 if (!$this->_validateSelect($_POST[$key], $form->getOptionValueList($system_path))) {
574 $this->errors
[$work_path][] = __('Incorrect value');
581 $_POST[$key] = trim($_POST[$key]);
584 // eliminate empty values and ensure we have an array
585 $post_values = is_array($_POST[$key])
587 : explode("\n", $_POST[$key]);
588 $_POST[$key] = array();
589 foreach ($post_values as $v) {
598 // now we have value with proper type
599 $values[$system_path] = $_POST[$key];
600 if ($change_index !== false) {
601 $work_path = str_replace("Servers/$form->index/",
602 "Servers/$change_index/", $work_path);
604 $to_save[$work_path] = $system_path;
609 if ($allow_partial_save ||
empty($this->errors
)) {
610 foreach ($to_save as $work_path => $path) {
611 // TrustedProxies requires changes before saving
612 if ($path == 'TrustedProxies') {
615 foreach ($values[$path] as $value) {
617 if (preg_match("/^(.+):(?:[ ]?)(\\w+)$/", $value, $matches)) {
618 // correct 'IP: HTTP header' pair
619 $ip = trim($matches[1]);
620 $proxies[$ip] = trim($matches[2]);
622 // save also incorrect values
623 $proxies["-$i"] = $value;
627 $values[$path] = $proxies;
629 $cf->set($work_path, $values[$path], $path);
631 if ($is_setup_script) {
632 $cf->set('UserprefsDisallow', array_keys($this->userprefs_disallow
));
636 // don't look for non-critical errors
643 * Tells whether form validation failed
647 public function hasErrors()
649 return count($this->errors
) > 0;
654 * Returns link to documentation
656 * @param string $path
659 public function getDocLink($path)
661 $test = substr($path, 0, 6);
662 if ($test == 'Import' ||
$test == 'Export') {
665 return 'Documentation.html#cfg_' . $this->_getOptName($path);
669 * Returns link to wiki
671 * @param string $path
674 public function getWikiLink($path)
676 $opt_name = $this->_getOptName($path);
677 if (substr($opt_name, 0, 7) == 'Servers') {
678 $opt_name = substr($opt_name, 8);
679 if (strpos($opt_name, 'AllowDeny') === 0) {
680 $opt_name = str_replace('_', '_.28', $opt_name) . '.29';
683 $test = substr($path, 0, 6);
684 if ($test == 'Import') {
685 $opt_name = substr($opt_name, 7);
686 if ($opt_name == 'format') {
687 $opt_name = 'format_2';
690 if ($test == 'Export') {
691 $opt_name = substr($opt_name, 7);
693 return PMA_linkURL('http://wiki.phpmyadmin.net/pma/Config#' . $opt_name);
697 * Changes path so it can be used in URLs
699 * @param string $path
702 private function _getOptName($path)
704 return str_replace(array('Servers/1/', '/'), array('Servers/', '_'), $path);
708 * Fills out {@link userprefs_keys} and {@link userprefs_disallow}
710 * @uses PMA_read_userprefs_fieldnames()
712 private function _loadUserprefsInfo()
714 if ($this->userprefs_keys
=== null) {
715 $this->userprefs_keys
= array_flip(PMA_read_userprefs_fieldnames());
716 // read real config for user preferences display
717 $userprefs_disallow = defined('PMA_SETUP')
718 ? ConfigFile
::getInstance()->get('UserprefsDisallow', array())
719 : $GLOBALS['cfg']['UserprefsDisallow'];
720 $this->userprefs_disallow
= array_flip($userprefs_disallow);
725 * Sets field comments and warnings based on current environment
727 * @param string $system_path
730 private function _setComments($system_path, array &$opts)
732 // RecodingEngine - mark unavailable types
733 if ($system_path == 'RecodingEngine') {
735 if (!function_exists('iconv')) {
736 $opts['values']['iconv'] .= ' (' . __('unavailable') . ')';
737 $comment = sprintf(__('"%s" requires %s extension'), 'iconv', 'iconv');
739 if (!function_exists('recode_string')) {
740 $opts['values']['recode'] .= ' (' . __('unavailable') . ')';
741 $comment .= ($comment ?
", " : '') . sprintf(__('"%s" requires %s extension'),
744 $opts['comment'] = $comment;
745 $opts['comment_warning'] = true;
747 // ZipDump, GZipDump, BZipDump - check function availability
748 if ($system_path == 'ZipDump' ||
$system_path == 'GZipDump' ||
$system_path == 'BZipDump') {
751 'ZipDump' => array('zip_open', 'gzcompress'),
752 'GZipDump' => array('gzopen', 'gzencode'),
753 'BZipDump' => array('bzopen', 'bzcompress'));
754 if (!function_exists($funcs[$system_path][0])) {
755 $comment = sprintf(__('import will not work, missing function (%s)'),
756 $funcs[$system_path][0]);
758 if (!function_exists($funcs[$system_path][1])) {
759 $comment .= ($comment ?
'; ' : '') . sprintf(__('export will not work, missing function (%s)'),
760 $funcs[$system_path][1]);
762 $opts['comment'] = $comment;
763 $opts['comment_warning'] = true;
765 if ($system_path == 'SQLQuery/Validate' && !$GLOBALS['cfg']['SQLValidator']['use']) {
766 $opts['comment'] = __('SQL Validator is disabled');
767 $opts['comment_warning'] = true;
769 if ($system_path == 'SQLValidator/use') {
770 if (!class_exists('SOAPClient')) {
771 @include_once
'SOAP/Client.php';
772 if (!class_exists('SOAP_Client')) {
773 $opts['comment'] = __('SOAP extension not found');
774 $opts['comment_warning'] = true;
778 if (!defined('PMA_SETUP')) {
779 if (($system_path == 'MaxDbList' ||
$system_path == 'MaxTableList'
780 ||
$system_path == 'QueryHistoryMax')) {
781 $opts['comment'] = sprintf(__('maximum %s'), $GLOBALS['cfg'][$system_path]);