user preferences saving and integration
[phpmyadmin.git] / libraries / config / FormDisplay.class.php
blob55a02952d685a2b43c3e22511b56da231c465b19
1 <?php
2 /**
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
13 * @version $Id$
16 /**
17 * Core libraries.
19 require_once './libraries/config/FormDisplay.tpl.php';
20 require_once './setup/lib/validate.lib.php';
21 require_once './libraries/js_escape.lib.php';
23 /**
24 * Form management class, displays and processes forms
25 * @package phpMyAdmin-setup
27 class FormDisplay
29 /**
30 * Form list
31 * @var Form[]
33 private $forms = array();
35 /**
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
39 * @var array
41 private $errors = array();
43 /**
44 * Paths changed so that they can be used as HTML ids, indexed by paths
45 * @var array
47 private $translated_paths = array();
49 /**
50 * Server paths change indexes so we define maps from current server
51 * path to the first one, indexed by work path
52 * @var array
54 private $system_paths = array();
56 /**
57 * Language strings which will be sent to PMA_messages JS variable
58 * Will be looked up in $GLOBALS: str{value} or strSetup{value}
59 * @var array
61 private $js_lang_strings = array();
63 /**
64 * Tells whether forms have been validated
65 * @var bool
67 private $is_valdiated = true;
69 public function __construct()
71 $this->js_lang_strings = array(
72 'error_nan_p' => __('Not a positive number'),
73 'error_nan_nneg' => __('Not a non-negative number'),
74 'error_incorrect_port' => __('Not a valid port number'),
75 'error_invalid_value' => __('Incorrect value')
79 /**
80 * Registers form in form manager
82 * @param string $form_name
83 * @param array $form
84 * @param int $server_id 0 if new server, validation; >= 1 if editing a server
86 public function registerForm($form_name, array $form, $server_id = null)
88 $this->forms[$form_name] = new Form($form_name, $form, $server_id);
89 $this->is_valdiated = false;
90 foreach ($this->forms[$form_name]->fields as $path) {
91 $work_path = $server_id === null
92 ? $path
93 : str_replace('Servers/1/', "Servers/$server_id/", $path);
94 $this->system_paths[$work_path] = $path;
95 $this->translated_paths[$work_path] = str_replace('/', '-', $work_path);
99 /**
100 * Processes forms, returns true on successful save
102 * @param bool $allow_partial_save allows for partial form saving on failed validation
103 * @return boolean
105 public function process($allow_partial_save = true)
107 // gather list of forms to save
108 if (!isset($_POST['submit_save'])) {
109 return false;
112 // save forms
113 if (count($this->forms) > 0) {
114 return $this->save(array_keys($this->forms), $allow_partial_save);
116 return false;
120 * Runs validation for all registered forms
122 private function _validate()
124 if ($this->is_valdiated) {
125 return;
128 $cf = ConfigFile::getInstance();
129 $paths = array();
130 $values = array();
131 foreach ($this->forms as $form) {
132 /* @var $form Form */
133 $paths[] = $form->name;
134 // collect values and paths
135 foreach ($form->fields as $path) {
136 $work_path = array_search($path, $this->system_paths);
137 $values[$path] = $cf->getValue($work_path);
138 $paths[] = $path;
142 // run validation
143 $errors = validate($paths, $values, false);
145 // change error keys from canonical paths to work paths
146 if (is_array($errors) && count($errors) > 0) {
147 $this->errors = array();
148 foreach ($errors as $path => $error_list) {
149 $work_path = array_search($path, $this->system_paths);
150 // field error
151 if (!$work_path) {
152 // form error, fix path
153 $work_path = $path;
155 $this->errors[$work_path] = $error_list;
158 $this->is_valdiated = true;
163 * Outputs HTML for forms
165 * @param bool $tabbed_form
166 * @param bool $show_restore_default whether show "restore default" button besides the input field
168 public function display($tabbed_form = false, $show_restore_default = false)
170 static $js_lang_sent = false;
172 $js = array();
173 $js_default = array();
174 $tabbed_form = $tabbed_form && (count($this->forms) > 1);
175 $validators = ConfigFile::getInstance()->getDbEntry('_validators', array());
177 display_form_top();
179 if ($tabbed_form) {
180 $tabs = array();
181 foreach ($this->forms as $form) {
182 $tabs[$form->name] = PMA_lang("Form_$form->name");
184 display_tabs_top($tabs);
187 // valdiate only when we aren't displaying a "new server" form
188 $is_new_server = false;
189 foreach ($this->forms as $form) {
190 /* @var $form Form */
191 if ($form->index === 0) {
192 $is_new_server = true;
193 break;
196 if (!$is_new_server) {
197 $this->_validate();
200 // display forms
201 foreach ($this->forms as $form) {
202 /* @var $form Form */
203 $form_desc = isset($GLOBALS["strSetupForm_{$form->name}_desc"])
204 ? PMA_lang("Form_{$form->name}_desc")
205 : '';
206 $form_errors = isset($this->errors[$form->name])
207 ? $this->errors[$form->name] : null;
208 display_fieldset_top(PMA_lang("Form_$form->name"),
209 $form_desc, $form_errors, array('id' => $form->name));
211 foreach ($form->fields as $field => $path) {
212 $work_path = array_search($path, $this->system_paths);
213 $translated_path = $this->translated_paths[$work_path];
214 // display input
215 $this->_displayFieldInput($form, $field, $path, $work_path,
216 $translated_path, $show_restore_default, $js_default);
217 // register JS validators for this field
218 if (isset($validators[$path])) {
219 js_validate($translated_path, $validators[$path], $js);
222 display_fieldset_bottom();
225 if ($tabbed_form) {
226 display_tabs_bottom();
228 display_form_bottom();
230 // if not already done, send strings used for valdiation to JavaScript
231 if (!$js_lang_sent) {
232 $js_lang_sent = true;
233 $js_lang = array();
234 foreach ($this->js_lang_strings as $strName => $strValue) {
235 $js_lang[] = "'$strName': '" . PMA_jsFormat($strValue, false) . '\'';
237 $js[] = '$.extend(PMA_messages, {' . implode(",\n\t", $js_lang) . '})';
240 $js[] = '$.extend(defaultValues, {' . implode(",\n\t", $js_default) . '})';
241 display_js($js);
245 * Prepares data for input field display and outputs HTML code
247 * @param Form $form
248 * @param string $field field name as it appears in $form
249 * @param string $system_path field path, eg. Servers/1/verbose
250 * @param string $work_path work path, eg. Servers/4/verbose
251 * @param string $translated_path work path changed so that it can be used as XHTML id
252 * @param bool $show_restore_default whether show "restore default" button besides the input field
253 * @param array &$js_default array which stores JavaScript code to be displayed
255 private function _displayFieldInput(Form $form, $field, $system_path, $work_path,
256 $translated_path, $show_restore_default, array &$js_default)
258 $name = PMA_lang_name($system_path);
259 $description = PMA_lang_desc($system_path);
261 $cf = ConfigFile::getInstance();
262 $value = $cf->get($work_path);
263 $value_default = $cf->getDefault($system_path);
264 $value_is_default = false;
265 if ($value === null || $value === $value_default) {
266 $value = $value_default;
267 $value_is_default = true;
270 $opts = array(
271 'doc' => $this->getDocLink($system_path),
272 'wiki' => $this->getWikiLink($system_path),
273 'show_restore_default' => $show_restore_default);
274 if (isset($form->default[$system_path])) {
275 $opts['setvalue'] = $form->default[$system_path];
278 if (isset($this->errors[$work_path])) {
279 $opts['errors'] = $this->errors[$work_path];
281 switch ($form->getOptionType($field)) {
282 case 'string':
283 $type = 'text';
284 break;
285 case 'double':
286 $type = 'text';
287 break;
288 case 'integer':
289 $type = 'text';
290 break;
291 case 'boolean':
292 $type = 'checkbox';
293 break;
294 case 'select':
295 $type = 'select';
296 $opts['values'] = array();
297 $values = $form->getOptionValueList($form->fields[$field]);
298 foreach ($values as $v) {
299 $opts['values'][$v] = $v;
301 break;
302 case 'array':
303 $type = 'list';
304 $value = (array) $value;
305 $value_default = (array) $value_default;
306 break;
307 case 'NULL':
308 trigger_error("Field $system_path has no type", E_USER_WARNING);
309 return;
312 // TrustedProxies requires changes before displaying
313 if ($system_path == 'TrustedProxies') {
314 foreach ($value as $ip => &$v) {
315 if (!preg_match('/^-\d+$/', $ip)) {
316 $v = $ip . ': ' . $v;
321 // send default value to form's JS
322 $js_line = '\'' . $translated_path . '\': ';
323 switch ($type) {
324 case 'text':
325 $js_line .= '\'' . PMA_escapeJsString($value_default) . '\'';
326 break;
327 case 'checkbox':
328 $js_line .= $value_default ? 'true' : 'false';
329 break;
330 case 'select':
331 $value_default_js = is_bool($value_default)
332 ? (int) $value_default
333 : $value_default;
334 $js_line .= '[\'' . PMA_escapeJsString($value_default_js) . '\']';
335 break;
336 case 'list':
337 $js_line .= '\'' . PMA_escapeJsString(implode("\n", $value_default)) . '\'';
338 break;
340 $js_default[] = $js_line;
342 display_input($translated_path, $name, $description, $type,
343 $value, $value_is_default, $opts);
347 * Displays errors
349 public function displayErrors()
351 $this->_validate();
352 if (count($this->errors) == 0) {
353 return;
356 foreach ($this->errors as $system_path => $error_list) {
357 if (isset($this->system_paths[$system_path])) {
358 $path = $this->system_paths[$system_path];
359 $name = PMA_lang_name($path);
360 } else {
361 $name = $GLOBALS["strSetupForm_$system_path"];
363 display_errors($name, $error_list);
368 * Reverts erroneous fields to their default values
370 public function fixErrors()
372 $this->_validate();
373 if (count($this->errors) == 0) {
374 return;
377 $cf = ConfigFile::getInstance();
378 foreach (array_keys($this->errors) as $work_path) {
379 if (!isset($this->system_paths[$work_path])) {
380 continue;
382 $canonical_path = $this->system_paths[$work_path];
383 $cf->set($work_path, $cf->getDefault($canonical_path));
388 * Validates select field and casts $value to correct type
390 * @param string $value
391 * @param array $allowed
392 * @return bool
394 private function _validateSelect(&$value, array $allowed)
396 foreach ($allowed as $v) {
397 if ($value == $v) {
398 settype($value, gettype($v));
399 return true;
402 return false;
406 * Validates and saves form data to session
408 * @param array|string $forms array of form names
409 * @param bool $allow_partial_save allows for partial form saving on failed validation
410 * @return boolean true on success (no errors and all saved)
412 public function save($forms, $allow_partial_save = true)
414 $result = true;
415 $cf = ConfigFile::getInstance();
416 $forms = (array) $forms;
418 $values = array();
419 $to_save = array();
420 $this->errors = array();
421 foreach ($forms as $form) {
422 /* @var $form Form */
423 if (isset($this->forms[$form])) {
424 $form = $this->forms[$form];
425 } else {
426 continue;
428 // get current server id
429 $change_index = $form->index === 0
430 ? $cf->getServerCount() + 1
431 : false;
432 // grab POST values
433 foreach ($form->fields as $field => $system_path) {
434 $work_path = array_search($system_path, $this->system_paths);
435 $key = $this->translated_paths[$work_path];
437 // ensure the value is set
438 if (!isset($_POST[$key])) {
439 // checkboxes aren't set by browsers if they're off
440 if ($form->getOptionType($field) == 'boolean') {
441 $_POST[$key] = false;
442 } else {
443 $this->errors[$form->name][] = sprintf(
444 __('Missing data for %s'),
445 '<i>' . PMA_lang_name($system_path) . '</i>');
446 $result = false;
447 continue;
451 // cast variables to correct type
452 $type = $form->getOptionType($field);
453 switch ($type) {
454 case 'double':
455 settype($_POST[$key], 'float');
456 break;
457 case 'boolean':
458 case 'integer':
459 if ($_POST[$key] !== '') {
460 settype($_POST[$key], $type);
462 break;
463 case 'select':
464 if (!$this->_validateSelect($_POST[$key], $form->getOptionValueList($system_path))) {
465 $this->errors[$work_path][] = __('Incorrect value');
466 $result = false;
467 continue;
469 break;
470 case 'string':
471 $_POST[$key] = trim($_POST[$key]);
472 break;
473 case 'array':
474 // eliminate empty values and ensure we have an array
475 $post_values = explode("\n", $_POST[$key]);
476 $_POST[$key] = array();
477 foreach ($post_values as $v) {
478 $v = trim($v);
479 if ($v !== '') {
480 $_POST[$key][] = $v;
483 break;
486 // now we have value with proper type
487 $values[$system_path] = $_POST[$key];
488 if ($change_index !== false) {
489 $work_path = str_replace("Servers/$form->index/",
490 "Servers/$change_index/", $work_path);
492 $to_save[$work_path] = $system_path;
496 // save forms
497 if ($allow_partial_save || empty($this->errors)) {
498 foreach ($to_save as $work_path => $path) {
499 // TrustedProxies requires changes before saving
500 if ($path == 'TrustedProxies') {
501 $proxies = array();
502 $i = 0;
503 foreach ($values[$path] as $value) {
504 $matches = array();
505 if (preg_match("/^(.+):(?:[ ]?)(\\w+)$/", $value, $matches)) {
506 // correct 'IP: HTTP header' pair
507 $ip = trim($matches[1]);
508 $proxies[$ip] = trim($matches[2]);
509 } else {
510 // save also incorrect values
511 $proxies["-$i"] = $value;
512 $i++;
515 $values[$path] = $proxies;
517 $cf->set($work_path, $values[$path], $path);
521 // don't look for non-critical errors
522 $this->_validate();
524 return $result;
528 * Tells whether form validation failed
530 * @return boolean
532 public function hasErrors()
534 return count($this->errors) > 0;
539 * Returns link to documentation
541 * @param string $path
542 * @return string
544 public function getDocLink($path)
546 $test = substr($path, 0, 6);
547 if ($test == 'Import' || $test == 'Export') {
548 return '';
550 return 'Documentation.html#cfg_' . self::_getOptName($path);
554 * Returns link to wiki
556 * @param string $path
557 * @return string
559 public function getWikiLink($path)
561 $opt_name = self::_getOptName($path);
562 if (substr($opt_name, 0, 7) == 'Servers') {
563 $opt_name = substr($opt_name, 8);
564 if (strpos($opt_name, 'AllowDeny') === 0) {
565 $opt_name = str_replace('_', '_.28', $opt_name) . '.29';
568 $test = substr($path, 0, 6);
569 if ($test == 'Import') {
570 $opt_name = substr($opt_name, 7);
571 if ($opt_name == 'format') {
572 $opt_name = 'format_2';
575 if ($test == 'Export') {
576 $opt_name = substr($opt_name, 7);
578 return 'http://wiki.phpmyadmin.net/pma/Config#' . $opt_name;
582 * Changes path so it can be used in URLs
584 * @param string $path
585 * @return string
587 private static function _getOptName($path)
589 return str_replace(array('Servers/1/', '/'), array('Servers/', '_'), $path);