3.3.7-rc1 release
[phpmyadmin/madhuracj.git] / setup / lib / FormDisplay.class.php
blobb229a1a2426990e420d6e1da1cfd10fb7ea1ba4c
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 * @author Piotr Przybylski <piotrprz@gmail.com>
13 * @license http://www.gnu.org/licenses/gpl.html GNU GPL 2.0
14 * @version $Id$
17 /**
18 * Core libraries.
20 require_once './setup/lib/FormDisplay.tpl.php';
21 require_once './setup/lib/validate.lib.php';
22 require_once './libraries/js_escape.lib.php';
24 /**
25 * Form management class, displays and processes forms
26 * @package phpMyAdmin-setup
28 class FormDisplay
30 /**
31 * Form list
32 * @var array
34 private $forms = array();
36 /**
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
40 * @var array
42 private $errors = array();
44 /**
45 * Paths changed so that they can be used as HTML ids, indexed by paths
46 * @var array
48 private $translated_paths = array();
50 /**
51 * Server paths change indexes so we define maps from current server
52 * path to the first one, indexed by work path
53 * @var array
55 private $system_paths = array();
57 /**
58 * Language strings which will be sent to PMA_messages JS variable
59 * Will be looked up in $GLOBALS: str{value} or strSetup{value}
60 * @var array
62 private $js_lang_strings = array('error_nan_p', 'error_nan_nneg',
63 'error_incorrect_port');
65 /**
66 * Tells whether forms have been validated
67 * @var bool
69 private $is_valdiated = true;
71 /**
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
83 ? $path
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);
90 /**
91 * Processes forms, returns true on successful save
93 * @param bool $allow_partial_save allows for partial form saving on failed validation
94 * @return boolean
96 public function process($allow_partial_save = true)
98 // gather list of forms to save
99 if (!isset($_POST['submit_save'])) {
100 return false;
103 // save forms
104 if (count($this->forms) > 0) {
105 return $this->save(array_keys($this->forms), $allow_partial_save);
107 return false;
111 * Runs validation for all registered forms
113 private function _validate()
115 if ($this->is_valdiated) {
116 return;
119 $cf = ConfigFile::getInstance();
120 $paths = array();
121 $values = array();
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);
129 $paths[] = $path;
133 // run validation
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);
141 // field error
142 if (!$work_path) {
143 // form error, fix path
144 $work_path = $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;
163 $js = array();
164 $js_default = array();
165 $tabbed_form = $tabbed_form && (count($this->forms) > 1);
166 $validators = ConfigFile::getInstance()->getDbEntry('_validators');
168 display_form_top();
170 if ($tabbed_form) {
171 $tabs = array();
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;
184 break;
187 if (!$is_new_server) {
188 $this->_validate();
191 // display forms
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")
196 : '';
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];
205 // display input
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();
216 if ($tabbed_form) {
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;
224 $js_lang = array();
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) . '})';
235 display_js($js);
239 * Prepares data for input field display and outputs HTML code
241 * @param Form $form
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;
264 $opts = array(
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)) {
276 case 'string':
277 $type = 'text';
278 break;
279 case 'double':
280 $type = 'text';
281 break;
282 case 'integer':
283 $type = 'text';
284 break;
285 case 'boolean':
286 $type = 'checkbox';
287 break;
288 case 'select':
289 $type = 'select';
290 $opts['values'] = array();
291 $values = $form->getOptionValueList($form->fields[$field]);
292 foreach ($values as $v) {
293 $opts['values'][$v] = $v;
295 break;
296 case 'array':
297 $type = 'list';
298 $value = (array) $value;
299 $value_default = (array) $value_default;
300 break;
301 case 'NULL':
302 trigger_error("Field $system_path has no type", E_USER_WARNING);
303 return;
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 . '\': ';
317 switch ($type) {
318 case 'text':
319 $js_line .= '\'' . PMA_escapeJsString($value_default) . '\'';
320 break;
321 case 'checkbox':
322 $js_line .= $value_default ? 'true' : 'false';
323 break;
324 case 'select':
325 $value_default_js = is_bool($value_default)
326 ? (int) $value_default
327 : $value_default;
328 $js_line .= '[\'' . PMA_escapeJsString($value_default_js) . '\']';
329 break;
330 case 'list':
331 $js_line .= '\'' . PMA_escapeJsString(implode("\n", $value_default)) . '\'';
332 break;
334 $js_default[] = $js_line;
336 display_input($translated_path, $name, $description, $type,
337 $value, $value_is_default, $opts);
341 * Displays errors
343 public function displayErrors()
345 $this->_validate();
346 if (count($this->errors) == 0) {
347 return;
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);
354 } else {
355 $name = $GLOBALS["strSetupForm_$system_path"];
357 display_errors($name, $error_list);
362 * Reverts erroneous fields to their default values
364 public function fixErrors()
366 $this->_validate();
367 if (count($this->errors) == 0) {
368 return;
371 $cf = ConfigFile::getInstance();
372 foreach (array_keys($this->errors) as $work_path) {
373 if (!isset($this->system_paths[$work_path])) {
374 continue;
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
386 * @return bool
388 private function _validateSelect(&$value, array $allowed)
390 foreach ($allowed as $v) {
391 if ($value == $v) {
392 settype($value, gettype($v));
393 return true;
396 return false;
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)
408 $result = true;
409 $cf = ConfigFile::getInstance();
410 $forms = (array) $forms;
412 $values = array();
413 $to_save = array();
414 $this->errors = array();
415 foreach ($forms as $form) {
416 /* @var $form Form */
417 if (isset($this->forms[$form])) {
418 $form = $this->forms[$form];
419 } else {
420 continue;
422 // get current server id
423 $change_index = $form->index === 0
424 ? $cf->getServerCount() + 1
425 : false;
426 // grab POST values
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;
436 } else {
437 $this->errors[$form->name][] = PMA_lang(
438 'error_missing_field_data',
439 '<i>' . PMA_lang_name($system_path) . '</i>');
440 $result = false;
441 continue;
445 // cast variables to correct type
446 $type = $form->getOptionType($field);
447 switch ($type) {
448 case 'double':
449 settype($_POST[$key], 'float');
450 break;
451 case 'boolean':
452 case 'integer':
453 if ($_POST[$key] !== '') {
454 settype($_POST[$key], $type);
456 break;
457 case 'select':
458 if (!$this->_validateSelect($_POST[$key], $form->getOptionValueList($system_path))) {
459 $this->errors[$work_path][] = $GLOBALS["strSetuperror_incorrect_value"];
460 $result = false;
461 continue;
463 break;
464 case 'string':
465 $_POST[$key] = trim($_POST[$key]);
466 break;
467 case 'array':
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) {
472 $v = trim($v);
473 if ($v !== '') {
474 $_POST[$key][] = $v;
477 break;
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;
490 // save forms
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') {
495 $proxies = array();
496 $i = 0;
497 foreach ($values[$path] as $value) {
498 $matches = array();
499 if (preg_match("/^(.+):(?:[ ]?)(\\w+)$/", $value, $matches)) {
500 // correct 'IP: HTTP header' pair
501 $ip = trim($matches[1]);
502 $proxies[$ip] = trim($matches[2]);
503 } else {
504 // save also incorrect values
505 $proxies["-$i"] = $value;
506 $i++;
509 $values[$path] = $proxies;
511 $cf->set($work_path, $values[$path], $path);
515 // don't look for non-critical errors
516 $this->_validate();
518 return $result;
522 * Tells whether form validation failed
524 * @return boolean
526 public function hasErrors()
528 return count($this->errors) > 0;
533 * Returns link to documentation
535 * @param string $path
536 * @return string
538 public function getDocLink($path)
540 $test = substr($path, 0, 6);
541 if ($test == 'Import' || $test == 'Export') {
542 return '';
544 return '../Documentation.html#cfg_' . self::_getOptName($path);
548 * Returns link to wiki
550 * @param string $path
551 * @return string
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
579 * @return string
581 private static function _getOptName($path)
583 return str_replace(array('Servers/1/', '/'), array('Servers/', '_'), $path);