Merge remote branch 'origin/master'
[phpmyadmin/dkf.git] / setup / lib / FormDisplay.class.php
blob7b6bfebb45ab20745d848dfeb8ed78d95898feae
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 './setup/lib/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 array
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('error_nan_p', 'error_nan_nneg',
62 'error_incorrect_port');
64 /**
65 * Tells whether forms have been validated
66 * @var bool
68 private $is_valdiated = true;
70 /**
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
82 ? $path
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);
89 /**
90 * Processes forms, returns true on successful save
92 * @param bool $allow_partial_save allows for partial form saving on failed validation
93 * @return boolean
95 public function process($allow_partial_save = true)
97 // gather list of forms to save
98 if (!isset($_POST['submit_save'])) {
99 return false;
102 // save forms
103 if (count($this->forms) > 0) {
104 return $this->save(array_keys($this->forms), $allow_partial_save);
106 return false;
110 * Runs validation for all registered forms
112 private function _validate()
114 if ($this->is_valdiated) {
115 return;
118 $cf = ConfigFile::getInstance();
119 $paths = array();
120 $values = array();
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);
128 $paths[] = $path;
132 // run validation
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);
140 // field error
141 if (!$work_path) {
142 // form error, fix path
143 $work_path = $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;
162 $js = array();
163 $js_default = array();
164 $tabbed_form = $tabbed_form && (count($this->forms) > 1);
165 $validators = ConfigFile::getInstance()->getDbEntry('_validators');
167 display_form_top();
169 if ($tabbed_form) {
170 $tabs = array();
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;
183 break;
186 if (!$is_new_server) {
187 $this->_validate();
190 // display forms
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")
195 : '';
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];
204 // display input
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();
215 if ($tabbed_form) {
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;
223 $js_lang = array();
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) . '})';
234 display_js($js);
238 * Prepares data for input field display and outputs HTML code
240 * @param Form $form
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;
263 $opts = array(
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)) {
275 case 'string':
276 $type = 'text';
277 break;
278 case 'double':
279 $type = 'text';
280 break;
281 case 'integer':
282 $type = 'text';
283 break;
284 case 'boolean':
285 $type = 'checkbox';
286 break;
287 case 'select':
288 $type = 'select';
289 $opts['values'] = array();
290 $values = $form->getOptionValueList($form->fields[$field]);
291 foreach ($values as $v) {
292 $opts['values'][$v] = $v;
294 break;
295 case 'array':
296 $type = 'list';
297 $value = (array) $value;
298 $value_default = (array) $value_default;
299 break;
300 case 'NULL':
301 trigger_error("Field $system_path has no type", E_USER_WARNING);
302 return;
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 . '\': ';
316 switch ($type) {
317 case 'text':
318 $js_line .= '\'' . PMA_escapeJsString($value_default) . '\'';
319 break;
320 case 'checkbox':
321 $js_line .= $value_default ? 'true' : 'false';
322 break;
323 case 'select':
324 $value_default_js = is_bool($value_default)
325 ? (int) $value_default
326 : $value_default;
327 $js_line .= '[\'' . PMA_escapeJsString($value_default_js) . '\']';
328 break;
329 case 'list':
330 $js_line .= '\'' . PMA_escapeJsString(implode("\n", $value_default)) . '\'';
331 break;
333 $js_default[] = $js_line;
335 display_input($translated_path, $name, $description, $type,
336 $value, $value_is_default, $opts);
340 * Displays errors
342 public function displayErrors()
344 $this->_validate();
345 if (count($this->errors) == 0) {
346 return;
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);
353 } else {
354 $name = $GLOBALS["strSetupForm_$system_path"];
356 display_errors($name, $error_list);
361 * Reverts erroneous fields to their default values
363 public function fixErrors()
365 $this->_validate();
366 if (count($this->errors) == 0) {
367 return;
370 $cf = ConfigFile::getInstance();
371 foreach (array_keys($this->errors) as $work_path) {
372 if (!isset($this->system_paths[$work_path])) {
373 continue;
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
385 * @return bool
387 private function _validateSelect(&$value, array $allowed)
389 foreach ($allowed as $v) {
390 if ($value == $v) {
391 settype($value, gettype($v));
392 return true;
395 return false;
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)
407 $result = true;
408 $cf = ConfigFile::getInstance();
409 $forms = (array) $forms;
411 $values = array();
412 $to_save = array();
413 $this->errors = array();
414 foreach ($forms as $form) {
415 /* @var $form Form */
416 if (isset($this->forms[$form])) {
417 $form = $this->forms[$form];
418 } else {
419 continue;
421 // get current server id
422 $change_index = $form->index === 0
423 ? $cf->getServerCount() + 1
424 : false;
425 // grab POST values
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;
435 } else {
436 $this->errors[$form->name][] = PMA_lang(
437 'error_missing_field_data',
438 '<i>' . PMA_lang_name($system_path) . '</i>');
439 $result = false;
440 continue;
444 // cast variables to correct type
445 $type = $form->getOptionType($field);
446 switch ($type) {
447 case 'double':
448 settype($_POST[$key], 'float');
449 break;
450 case 'boolean':
451 case 'integer':
452 if ($_POST[$key] !== '') {
453 settype($_POST[$key], $type);
455 break;
456 case 'select':
457 if (!$this->_validateSelect($_POST[$key], $form->getOptionValueList($system_path))) {
458 $this->errors[$work_path][] = __('Incorrect value');
459 $result = false;
460 continue;
462 break;
463 case 'string':
464 $_POST[$key] = trim($_POST[$key]);
465 break;
466 case 'array':
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) {
471 $v = trim($v);
472 if ($v !== '') {
473 $_POST[$key][] = $v;
476 break;
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;
489 // save forms
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') {
494 $proxies = array();
495 $i = 0;
496 foreach ($values[$path] as $value) {
497 $matches = array();
498 if (preg_match("/^(.+):(?:[ ]?)(\\w+)$/", $value, $matches)) {
499 // correct 'IP: HTTP header' pair
500 $ip = trim($matches[1]);
501 $proxies[$ip] = trim($matches[2]);
502 } else {
503 // save also incorrect values
504 $proxies["-$i"] = $value;
505 $i++;
508 $values[$path] = $proxies;
510 $cf->set($work_path, $values[$path], $path);
514 // don't look for non-critical errors
515 $this->_validate();
517 return $result;
521 * Tells whether form validation failed
523 * @return boolean
525 public function hasErrors()
527 return count($this->errors) > 0;
532 * Returns link to documentation
534 * @param string $path
535 * @return string
537 public function getDocLink($path)
539 $test = substr($path, 0, 6);
540 if ($test == 'Import' || $test == 'Export') {
541 return '';
543 return '../Documentation.html#cfg_' . self::_getOptName($path);
547 * Returns link to wiki
549 * @param string $path
550 * @return string
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
578 * @return string
580 private static function _getOptName($path)
582 return str_replace(array('Servers/1/', '/'), array('Servers/', '_'), $path);