3 * Form management class, displays and processes forms
5 * Explanation of used terms:
6 * o work_path - original field path, eg. Servers/4/verbose
7 * o system_path - work_path modified so that it points to the first server, eg. Servers/1/verbose
8 * o translated_path - work_path modified for HTML field name, a path with
9 * slashes changed to hyphens, eg. Servers-4-verbose
11 * @package phpMyAdmin-setup
12 * @license http://www.gnu.org/licenses/gpl.html GNU GPL 2.0
19 require_once './setup/lib/FormDisplay.tpl.php';
20 require_once './setup/lib/validate.lib.php';
21 require_once './libraries/js_escape.lib.php';
24 * Form management class, displays and processes forms
25 * @package phpMyAdmin-setup
33 private $forms = array();
36 * Stores validation errors, indexed by paths
37 * [ Form_name ] is an array of form errors
38 * [path] is a string storing error associated with single field
41 private $errors = array();
44 * Paths changed so that they can be used as HTML ids, indexed by paths
47 private $translated_paths = array();
50 * Server paths change indexes so we define maps from current server
51 * path to the first one, indexed by work path
54 private $system_paths = array();
57 * Language strings which will be sent to PMA_messages JS variable
58 * Will be looked up in $GLOBALS: str{value} or strSetup{value}
61 private $js_lang_strings = array('error_nan_p', 'error_nan_nneg',
62 'error_incorrect_port');
65 * Tells whether forms have been validated
68 private $is_valdiated = true;
71 * Registers form in form manager
73 * @param string $form_name
74 * @param int $server_id 0 if new server, validation; >= 1 if editing a server
76 public function registerForm($form_name, $server_id = null)
78 $this->forms
[$form_name] = new Form($form_name, $server_id);
79 $this->is_valdiated
= false;
80 foreach ($this->forms
[$form_name]->fields
as $path) {
81 $work_path = $server_id === null
83 : str_replace('Servers/1/', "Servers/$server_id/", $path);
84 $this->system_paths
[$work_path] = $path;
85 $this->translated_paths
[$work_path] = str_replace('/', '-', $work_path);
90 * Processes forms, returns true on successful save
92 * @param bool $allow_partial_save allows for partial form saving on failed validation
95 public function process($allow_partial_save = true)
97 // gather list of forms to save
98 if (!isset($_POST['submit_save'])) {
103 if (count($this->forms
) > 0) {
104 return $this->save(array_keys($this->forms
), $allow_partial_save);
110 * Runs validation for all registered forms
112 private function _validate()
114 if ($this->is_valdiated
) {
118 $cf = ConfigFile
::getInstance();
121 foreach ($this->forms
as $form) {
122 /* @var $form Form */
123 $paths[] = $form->name
;
124 // collect values and paths
125 foreach ($form->fields
as $path) {
126 $work_path = array_search($path, $this->system_paths
);
127 $values[$path] = $cf->getValue($work_path);
133 $errors = validate($paths, $values, false);
135 // change error keys from canonical paths to work paths
136 if (is_array($errors) && count($errors) > 0) {
137 $this->errors
= array();
138 foreach ($errors as $path => $error_list) {
139 $work_path = array_search($path, $this->system_paths
);
142 // form error, fix path
145 $this->errors
[$work_path] = $error_list;
148 $this->is_valdiated
= true;
153 * Outputs HTML for forms
155 * @param bool $tabbed_form
156 * @param bool $show_restore_default whether show "restore default" button besides the input field
158 public function display($tabbed_form = false, $show_restore_default = false)
160 static $js_lang_sent = false;
163 $js_default = array();
164 $tabbed_form = $tabbed_form && (count($this->forms
) > 1);
165 $validators = ConfigFile
::getInstance()->getDbEntry('_validators');
171 foreach ($this->forms
as $form) {
172 $tabs[$form->name
] = PMA_lang("Form_$form->name");
174 display_tabs_top($tabs);
177 // valdiate only when we aren't displaying a "new server" form
178 $is_new_server = false;
179 foreach ($this->forms
as $form) {
180 /* @var $form Form */
181 if ($form->index
=== 0) {
182 $is_new_server = true;
186 if (!$is_new_server) {
191 foreach ($this->forms
as $form) {
192 /* @var $form Form */
193 $form_desc = isset($GLOBALS["strSetupForm_{$form->name}_desc"])
194 ?
PMA_lang("Form_{$form->name}_desc")
196 $form_errors = isset($this->errors
[$form->name
])
197 ?
$this->errors
[$form->name
] : null;
198 display_fieldset_top(PMA_lang("Form_$form->name"),
199 $form_desc, $form_errors, array('id' => $form->name
));
201 foreach ($form->fields
as $field => $path) {
202 $work_path = array_search($path, $this->system_paths
);
203 $translated_path = $this->translated_paths
[$work_path];
205 $this->_displayFieldInput($form, $field, $path, $work_path,
206 $translated_path, $show_restore_default, $js_default);
207 // register JS validators for this field
208 if (isset($validators[$path])) {
209 js_validate($translated_path, $validators[$path], $js);
212 display_fieldset_bottom();
216 display_tabs_bottom();
218 display_form_bottom();
220 // if not already done, send strings used for valdiation to JavaScript
221 if (!$js_lang_sent) {
222 $js_lang_sent = true;
224 foreach ($this->js_lang_strings
as $str) {
225 $lang = isset($GLOBALS["strSetup$str"])
226 ?
$GLOBALS["strSetup$str"]
227 : filter_input($GLOBALS["str$str"]); // null if not set
228 $js_lang[] = "'$str': '" . PMA_jsFormat($lang, false) . '\'';
230 $js[] = '$.extend(PMA_messages, {' . implode(",\n\t", $js_lang) . '})';
233 $js[] = '$.extend(defaultValues, {' . implode(",\n\t", $js_default) . '})';
238 * Prepares data for input field display and outputs HTML code
241 * @param string $field field name as it appears in $form
242 * @param string $system_path field path, eg. Servers/1/verbose
243 * @param string $work_path work path, eg. Servers/4/verbose
244 * @param string $translated_path work path changed so that it can be used as XHTML id
245 * @param bool $show_restore_default whether show "restore default" button besides the input field
246 * @param array &$js_default array which stores JavaScript code to be displayed
248 private function _displayFieldInput(Form
$form, $field, $system_path, $work_path,
249 $translated_path, $show_restore_default, array &$js_default)
251 $name = PMA_lang_name($system_path);
252 $description = PMA_lang_desc($system_path);
254 $cf = ConfigFile
::getInstance();
255 $value = $cf->get($work_path);
256 $value_default = $cf->getDefault($system_path);
257 $value_is_default = false;
258 if ($value === null ||
$value === $value_default) {
259 $value = $value_default;
260 $value_is_default = true;
264 'doc' => $this->getDocLink($system_path),
265 'wiki' => $this->getWikiLink($system_path),
266 'show_restore_default' => $show_restore_default);
267 if (isset($form->default[$system_path])) {
268 $opts['setvalue'] = $form->default[$system_path];
271 if (isset($this->errors
[$work_path])) {
272 $opts['errors'] = $this->errors
[$work_path];
274 switch ($form->getOptionType($field)) {
289 $opts['values'] = array();
290 $values = $form->getOptionValueList($form->fields
[$field]);
291 foreach ($values as $v) {
292 $opts['values'][$v] = $v;
297 $value = (array) $value;
298 $value_default = (array) $value_default;
301 trigger_error("Field $system_path has no type", E_USER_WARNING
);
305 // TrustedProxies requires changes before displaying
306 if ($system_path == 'TrustedProxies') {
307 foreach ($value as $ip => &$v) {
308 if (!preg_match('/^-\d+$/', $ip)) {
309 $v = $ip . ': ' . $v;
314 // send default value to form's JS
315 $js_line = '\'' . $translated_path . '\': ';
318 $js_line .= '\'' . PMA_escapeJsString($value_default) . '\'';
321 $js_line .= $value_default ?
'true' : 'false';
324 $value_default_js = is_bool($value_default)
325 ?
(int) $value_default
327 $js_line .= '[\'' . PMA_escapeJsString($value_default_js) . '\']';
330 $js_line .= '\'' . PMA_escapeJsString(implode("\n", $value_default)) . '\'';
333 $js_default[] = $js_line;
335 display_input($translated_path, $name, $description, $type,
336 $value, $value_is_default, $opts);
342 public function displayErrors()
345 if (count($this->errors
) == 0) {
349 foreach ($this->errors
as $system_path => $error_list) {
350 if (isset($this->system_paths
[$system_path])) {
351 $path = $this->system_paths
[$system_path];
352 $name = PMA_lang_name($path);
354 $name = $GLOBALS["strSetupForm_$system_path"];
356 display_errors($name, $error_list);
361 * Reverts erroneous fields to their default values
363 public function fixErrors()
366 if (count($this->errors
) == 0) {
370 $cf = ConfigFile
::getInstance();
371 foreach (array_keys($this->errors
) as $work_path) {
372 if (!isset($this->system_paths
[$work_path])) {
375 $canonical_path = $this->system_paths
[$work_path];
376 $cf->set($work_path, $cf->getDefault($canonical_path));
381 * Validates select field and casts $value to correct type
383 * @param string $value
384 * @param array $allowed
387 private function _validateSelect(&$value, array $allowed)
389 foreach ($allowed as $v) {
391 settype($value, gettype($v));
399 * Validates and saves form data to session
401 * @param array|string $forms array of form names
402 * @param bool $allow_partial_save allows for partial form saving on failed validation
403 * @return boolean true on success (no errors and all saved)
405 public function save($forms, $allow_partial_save = true)
408 $cf = ConfigFile
::getInstance();
409 $forms = (array) $forms;
413 $this->errors
= array();
414 foreach ($forms as $form) {
415 /* @var $form Form */
416 if (isset($this->forms
[$form])) {
417 $form = $this->forms
[$form];
421 // get current server id
422 $change_index = $form->index
=== 0
423 ?
$cf->getServerCount() +
1
426 foreach ($form->fields
as $field => $system_path) {
427 $work_path = array_search($system_path, $this->system_paths
);
428 $key = $this->translated_paths
[$work_path];
430 // ensure the value is set
431 if (!isset($_POST[$key])) {
432 // checkboxes aren't set by browsers if they're off
433 if ($form->getOptionType($field) == 'boolean') {
434 $_POST[$key] = false;
436 $this->errors
[$form->name
][] = PMA_lang(
437 'error_missing_field_data',
438 '<i>' . PMA_lang_name($system_path) . '</i>');
444 // cast variables to correct type
445 $type = $form->getOptionType($field);
448 settype($_POST[$key], 'float');
452 if ($_POST[$key] !== '') {
453 settype($_POST[$key], $type);
457 if (!$this->_validateSelect($_POST[$key], $form->getOptionValueList($system_path))) {
458 $this->errors
[$work_path][] = __('Incorrect value');
464 $_POST[$key] = trim($_POST[$key]);
467 // eliminate empty values and ensure we have an array
468 $post_values = explode("\n", $_POST[$key]);
469 $_POST[$key] = array();
470 foreach ($post_values as $v) {
479 // now we have value with proper type
480 $values[$system_path] = $_POST[$key];
481 if ($change_index !== false) {
482 $work_path = str_replace("Servers/$form->index/",
483 "Servers/$change_index/", $work_path);
485 $to_save[$work_path] = $system_path;
490 if ($allow_partial_save ||
empty($this->errors
)) {
491 foreach ($to_save as $work_path => $path) {
492 // TrustedProxies requires changes before saving
493 if ($path == 'TrustedProxies') {
496 foreach ($values[$path] as $value) {
498 if (preg_match("/^(.+):(?:[ ]?)(\\w+)$/", $value, $matches)) {
499 // correct 'IP: HTTP header' pair
500 $ip = trim($matches[1]);
501 $proxies[$ip] = trim($matches[2]);
503 // save also incorrect values
504 $proxies["-$i"] = $value;
508 $values[$path] = $proxies;
510 $cf->set($work_path, $values[$path], $path);
514 // don't look for non-critical errors
521 * Tells whether form validation failed
525 public function hasErrors()
527 return count($this->errors
) > 0;
532 * Returns link to documentation
534 * @param string $path
537 public function getDocLink($path)
539 $test = substr($path, 0, 6);
540 if ($test == 'Import' ||
$test == 'Export') {
543 return '../Documentation.html#cfg_' . self
::_getOptName($path);
547 * Returns link to wiki
549 * @param string $path
552 public function getWikiLink($path)
554 $opt_name = self
::_getOptName($path);
555 if (substr($opt_name, 0, 7) == 'Servers') {
556 $opt_name = substr($opt_name, 8);
557 if (strpos($opt_name, 'AllowDeny') === 0) {
558 $opt_name = str_replace('_', '_.28', $opt_name) . '.29';
561 $test = substr($path, 0, 6);
562 if ($test == 'Import') {
563 $opt_name = substr($opt_name, 7);
564 if ($opt_name == 'format') {
565 $opt_name = 'format_2';
568 if ($test == 'Export') {
569 $opt_name = substr($opt_name, 7);
571 return 'http://wiki.phpmyadmin.net/pma/Config#' . $opt_name;
575 * Changes path so it can be used in URLs
577 * @param string $path
580 private static function _getOptName($path)
582 return str_replace(array('Servers/1/', '/'), array('Servers/', '_'), $path);