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 * @author Piotr Przybylski <piotrprz@gmail.com>
13 * @license http://www.gnu.org/licenses/gpl.html GNU GPL 2.0
20 require_once './setup/lib/FormDisplay.tpl.php';
21 require_once './setup/lib/validate.lib.php';
22 require_once './libraries/js_escape.lib.php';
25 * Form management class, displays and processes forms
26 * @package phpMyAdmin-setup
34 private $forms = array();
37 * Stores validation errors, indexed by paths
38 * [ Form_name ] is an array of form errors
39 * [path] is a string storing error associated with single field
42 private $errors = array();
45 * Paths changed so that they can be used as HTML ids, indexed by paths
48 private $translated_paths = array();
51 * Server paths change indexes so we define maps from current server
52 * path to the first one, indexed by work path
55 private $system_paths = array();
58 * Language strings which will be sent to PMA_messages JS variable
59 * Will be looked up in $GLOBALS: str{value} or strSetup{value}
62 private $js_lang_strings = array('error_nan_p', 'error_nan_nneg',
63 'error_incorrect_port');
66 * Tells whether forms have been validated
69 private $is_valdiated = true;
72 * Registers form in form manager
74 * @param string $form_name
75 * @param int $server_id 0 if new server, validation; >= 1 if editing a server
77 public function registerForm($form_name, $server_id = null)
79 $this->forms
[$form_name] = new Form($form_name, $server_id);
80 $this->is_valdiated
= false;
81 foreach ($this->forms
[$form_name]->fields
as $path) {
82 $work_path = $server_id === null
84 : str_replace('Servers/1/', "Servers/$server_id/", $path);
85 $this->system_paths
[$work_path] = $path;
86 $this->translated_paths
[$work_path] = str_replace('/', '-', $work_path);
91 * Processes forms, returns true on successful save
93 * @param bool $allow_partial_save allows for partial form saving on failed validation
96 public function process($allow_partial_save = true)
98 // gather list of forms to save
99 if (!isset($_POST['submit_save'])) {
104 if (count($this->forms
) > 0) {
105 return $this->save(array_keys($this->forms
), $allow_partial_save);
111 * Runs validation for all registered forms
113 private function _validate()
115 if ($this->is_valdiated
) {
119 $cf = ConfigFile
::getInstance();
122 foreach ($this->forms
as $form) {
123 /* @var $form Form */
124 $paths[] = $form->name
;
125 // collect values and paths
126 foreach ($form->fields
as $path) {
127 $work_path = array_search($path, $this->system_paths
);
128 $values[$path] = $cf->getValue($work_path);
134 $errors = validate($paths, $values, false);
136 // change error keys from canonical paths to work paths
137 if (is_array($errors) && count($errors) > 0) {
138 $this->errors
= array();
139 foreach ($errors as $path => $error_list) {
140 $work_path = array_search($path, $this->system_paths
);
143 // form error, fix path
146 $this->errors
[$work_path] = $error_list;
149 $this->is_valdiated
= true;
154 * Outputs HTML for forms
156 * @param bool $tabbed_form
157 * @param bool $show_restore_default whether show "restore default" button besides the input field
159 public function display($tabbed_form = false, $show_restore_default = false)
161 static $js_lang_sent = false;
164 $js_default = array();
165 $tabbed_form = $tabbed_form && (count($this->forms
) > 1);
166 $validators = ConfigFile
::getInstance()->getDbEntry('_validators');
172 foreach ($this->forms
as $form) {
173 $tabs[$form->name
] = PMA_lang("Form_$form->name");
175 display_tabs_top($tabs);
178 // valdiate only when we aren't displaying a "new server" form
179 $is_new_server = false;
180 foreach ($this->forms
as $form) {
181 /* @var $form Form */
182 if ($form->index
=== 0) {
183 $is_new_server = true;
187 if (!$is_new_server) {
192 foreach ($this->forms
as $form) {
193 /* @var $form Form */
194 $form_desc = isset($GLOBALS["strSetupForm_{$form->name}_desc"])
195 ?
PMA_lang("Form_{$form->name}_desc")
197 $form_errors = isset($this->errors
[$form->name
])
198 ?
$this->errors
[$form->name
] : null;
199 display_fieldset_top(PMA_lang("Form_$form->name"),
200 $form_desc, $form_errors, array('id' => $form->name
));
202 foreach ($form->fields
as $field => $path) {
203 $work_path = array_search($path, $this->system_paths
);
204 $translated_path = $this->translated_paths
[$work_path];
206 $this->_displayFieldInput($form, $field, $path, $work_path,
207 $translated_path, $show_restore_default, $js_default);
208 // register JS validators for this field
209 if (isset($validators[$path])) {
210 js_validate($translated_path, $validators[$path], $js);
213 display_fieldset_bottom();
217 display_tabs_bottom();
219 display_form_bottom();
221 // if not already done, send strings used for valdiation to JavaScript
222 if (!$js_lang_sent) {
223 $js_lang_sent = true;
225 foreach ($this->js_lang_strings
as $str) {
226 $lang = isset($GLOBALS["strSetup$str"])
227 ?
$GLOBALS["strSetup$str"]
228 : filter_input($GLOBALS["str$str"]); // null if not set
229 $js_lang[] = "'$str': '" . PMA_jsFormat($lang, false) . '\'';
231 $js[] = '$extend(PMA_messages, {' . implode(",\n\t", $js_lang) . '})';
234 $js[] = '$extend(defaultValues, {' . implode(",\n\t", $js_default) . '})';
239 * Prepares data for input field display and outputs HTML code
242 * @param string $field field name as it appears in $form
243 * @param string $system_path field path, eg. Servers/1/verbose
244 * @param string $work_path work path, eg. Servers/4/verbose
245 * @param string $translated_path work path changed so that it can be used as XHTML id
246 * @param bool $show_restore_default whether show "restore default" button besides the input field
247 * @param array &$js_default array which stores JavaScript code to be displayed
249 private function _displayFieldInput(Form
$form, $field, $system_path, $work_path,
250 $translated_path, $show_restore_default, array &$js_default)
252 $name = PMA_lang_name($system_path);
253 $description = PMA_lang_desc($system_path);
255 $cf = ConfigFile
::getInstance();
256 $value = $cf->get($work_path);
257 $value_default = $cf->getDefault($system_path);
258 $value_is_default = false;
259 if ($value === null ||
$value === $value_default) {
260 $value = $value_default;
261 $value_is_default = true;
265 'doc' => $this->getDocLink($system_path),
266 'wiki' => $this->getWikiLink($system_path),
267 'show_restore_default' => $show_restore_default);
268 if (isset($form->default[$system_path])) {
269 $opts['setvalue'] = $form->default[$system_path];
272 if (isset($this->errors
[$work_path])) {
273 $opts['errors'] = $this->errors
[$work_path];
275 switch ($form->getOptionType($field)) {
290 $opts['values'] = array();
291 $values = $form->getOptionValueList($form->fields
[$field]);
292 foreach ($values as $v) {
293 $opts['values'][$v] = $v;
298 $value = (array) $value;
299 $value_default = (array) $value_default;
302 trigger_error("Field $system_path has no type", E_USER_WARNING
);
306 // TrustedProxies requires changes before displaying
307 if ($system_path == 'TrustedProxies') {
308 foreach ($value as $ip => &$v) {
309 if (!preg_match('/^-\d+$/', $ip)) {
310 $v = $ip . ': ' . $v;
315 // send default value to form's JS
316 $js_line = '\'' . $translated_path . '\': ';
319 $js_line .= '\'' . PMA_escapeJsString($value_default) . '\'';
322 $js_line .= $value_default ?
'true' : 'false';
325 $value_default_js = is_bool($value_default)
326 ?
(int) $value_default
328 $js_line .= '[\'' . PMA_escapeJsString($value_default_js) . '\']';
331 $js_line .= '\'' . PMA_escapeJsString(implode("\n", $value_default)) . '\'';
334 $js_default[] = $js_line;
336 display_input($translated_path, $name, $description, $type,
337 $value, $value_is_default, $opts);
343 public function displayErrors()
346 if (count($this->errors
) == 0) {
350 foreach ($this->errors
as $system_path => $error_list) {
351 if (isset($this->system_paths
[$system_path])) {
352 $path = $this->system_paths
[$system_path];
353 $name = PMA_lang_name($path);
355 $name = $GLOBALS["strSetupForm_$system_path"];
357 display_errors($name, $error_list);
362 * Reverts erroneous fields to their default values
364 public function fixErrors()
367 if (count($this->errors
) == 0) {
371 $cf = ConfigFile
::getInstance();
372 foreach (array_keys($this->errors
) as $work_path) {
373 if (!isset($this->system_paths
[$work_path])) {
376 $canonical_path = $this->system_paths
[$work_path];
377 $cf->set($work_path, $cf->getDefault($canonical_path));
382 * Validates select field and casts $value to correct type
384 * @param string $value
385 * @param array $allowed
388 private function _validateSelect(&$value, array $allowed)
390 foreach ($allowed as $v) {
392 settype($value, gettype($v));
400 * Validates and saves form data to session
402 * @param array|string $forms array of form names
403 * @param bool $allow_partial_save allows for partial form saving on failed validation
404 * @return boolean true on success (no errors and all saved)
406 public function save($forms, $allow_partial_save = true)
409 $cf = ConfigFile
::getInstance();
410 $forms = (array) $forms;
414 $this->errors
= array();
415 foreach ($forms as $form) {
416 /* @var $form Form */
417 if (isset($this->forms
[$form])) {
418 $form = $this->forms
[$form];
422 // get current server id
423 $change_index = $form->index
=== 0
424 ?
$cf->getServerCount() +
1
427 foreach ($form->fields
as $field => $system_path) {
428 $work_path = array_search($system_path, $this->system_paths
);
429 $key = $this->translated_paths
[$work_path];
431 // ensure the value is set
432 if (!isset($_POST[$key])) {
433 // checkboxes aren't set by browsers if they're off
434 if ($form->getOptionType($field) == 'boolean') {
435 $_POST[$key] = false;
437 $this->errors
[$form->name
][] = PMA_lang(
438 'error_missing_field_data',
439 '<i>' . PMA_lang_name($system_path) . '</i>');
445 // cast variables to correct type
446 $type = $form->getOptionType($field);
449 settype($_POST[$key], 'float');
453 if ($_POST[$key] !== '') {
454 settype($_POST[$key], $type);
458 if (!$this->_validateSelect($_POST[$key], $form->getOptionValueList($system_path))) {
459 $this->errors
[$work_path][] = $GLOBALS["strSetuperror_incorrect_value"];
465 $_POST[$key] = trim($_POST[$key]);
468 // eliminate empty values and ensure we have an array
469 $post_values = explode("\n", $_POST[$key]);
470 $_POST[$key] = array();
471 foreach ($post_values as $v) {
480 // now we have value with proper type
481 $values[$system_path] = $_POST[$key];
482 if ($change_index !== false) {
483 $work_path = str_replace("Servers/$form->index/",
484 "Servers/$change_index/", $work_path);
486 $to_save[$work_path] = $system_path;
491 if ($allow_partial_save ||
empty($this->errors
)) {
492 foreach ($to_save as $work_path => $path) {
493 // TrustedProxies requires changes before saving
494 if ($path == 'TrustedProxies') {
497 foreach ($values[$path] as $value) {
499 if (preg_match("/^(.+):(?:[ ]?)(\\w+)$/", $value, $matches)) {
500 // correct 'IP: HTTP header' pair
501 $ip = trim($matches[1]);
502 $proxies[$ip] = trim($matches[2]);
504 // save also incorrect values
505 $proxies["-$i"] = $value;
509 $values[$path] = $proxies;
511 $cf->set($work_path, $values[$path], $path);
515 // don't look for non-critical errors
522 * Tells whether form validation failed
526 public function hasErrors()
528 return count($this->errors
) > 0;
533 * Returns link to documentation
535 * @param string $path
538 public function getDocLink($path)
540 $test = substr($path, 0, 6);
541 if ($test == 'Import' ||
$test == 'Export') {
544 return '../Documentation.html#cfg_' . self
::_getOptName($path);
548 * Returns link to wiki
550 * @param string $path
553 public function getWikiLink($path)
555 $opt_name = self
::_getOptName($path);
556 if (substr($opt_name, 0, 7) == 'Servers') {
557 $opt_name = substr($opt_name, 8);
558 if (strpos($opt_name, 'AllowDeny') === 0) {
559 $opt_name = str_replace('_', '_.28', $opt_name) . '.29';
562 $test = substr($path, 0, 6);
563 if ($test == 'Import') {
564 $opt_name = substr($opt_name, 7);
565 if ($opt_name == 'format') {
566 $opt_name = 'format_2';
569 if ($test == 'Export') {
570 $opt_name = substr($opt_name, 7);
572 return 'http://wiki.phpmyadmin.net/pma/Config#' . $opt_name;
576 * Changes path so it can be used in URLs
578 * @param string $path
581 private static function _getOptName($path)
583 return str_replace(array('Servers/1/', '/'), array('Servers/', '_'), $path);