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 './libraries/config/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();
64 * Tells whether forms have been validated
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')
80 * Registers form in form manager
82 * @param string $form_name
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
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);
100 * Processes forms, returns true on successful save
102 * @param bool $allow_partial_save allows for partial form saving on failed validation
105 public function process($allow_partial_save = true)
107 // gather list of forms to save
108 if (!isset($_POST['submit_save'])) {
113 if (count($this->forms
) > 0) {
114 return $this->save(array_keys($this->forms
), $allow_partial_save);
120 * Runs validation for all registered forms
122 private function _validate()
124 if ($this->is_valdiated
) {
128 $cf = ConfigFile
::getInstance();
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);
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
);
152 // form error, fix 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;
173 $js_default = array();
174 $tabbed_form = $tabbed_form && (count($this->forms
) > 1);
175 $validators = ConfigFile
::getInstance()->getDbEntry('_validators', 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;
196 if (!$is_new_server) {
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")
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];
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();
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;
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) . '})';
245 * Prepares data for input field display and outputs HTML code
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;
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)) {
296 $opts['values'] = array();
297 $values = $form->getOptionValueList($form->fields
[$field]);
298 foreach ($values as $v) {
299 $opts['values'][$v] = $v;
304 $value = (array) $value;
305 $value_default = (array) $value_default;
308 trigger_error("Field $system_path has no type", E_USER_WARNING
);
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 . '\': ';
325 $js_line .= '\'' . PMA_escapeJsString($value_default) . '\'';
328 $js_line .= $value_default ?
'true' : 'false';
331 $value_default_js = is_bool($value_default)
332 ?
(int) $value_default
334 $js_line .= '[\'' . PMA_escapeJsString($value_default_js) . '\']';
337 $js_line .= '\'' . PMA_escapeJsString(implode("\n", $value_default)) . '\'';
340 $js_default[] = $js_line;
342 display_input($translated_path, $name, $description, $type,
343 $value, $value_is_default, $opts);
349 public function displayErrors()
352 if (count($this->errors
) == 0) {
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);
361 $name = $GLOBALS["strSetupForm_$system_path"];
363 display_errors($name, $error_list);
368 * Reverts erroneous fields to their default values
370 public function fixErrors()
373 if (count($this->errors
) == 0) {
377 $cf = ConfigFile
::getInstance();
378 foreach (array_keys($this->errors
) as $work_path) {
379 if (!isset($this->system_paths
[$work_path])) {
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
394 private function _validateSelect(&$value, array $allowed)
396 foreach ($allowed as $v) {
398 settype($value, gettype($v));
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)
415 $cf = ConfigFile
::getInstance();
416 $forms = (array) $forms;
420 $this->errors
= array();
421 foreach ($forms as $form) {
422 /* @var $form Form */
423 if (isset($this->forms
[$form])) {
424 $form = $this->forms
[$form];
428 // get current server id
429 $change_index = $form->index
=== 0
430 ?
$cf->getServerCount() +
1
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;
443 $this->errors
[$form->name
][] = sprintf(
444 __('Missing data for %s'),
445 '<i>' . PMA_lang_name($system_path) . '</i>');
451 // cast variables to correct type
452 $type = $form->getOptionType($field);
455 settype($_POST[$key], 'float');
459 if ($_POST[$key] !== '') {
460 settype($_POST[$key], $type);
464 if (!$this->_validateSelect($_POST[$key], $form->getOptionValueList($system_path))) {
465 $this->errors
[$work_path][] = __('Incorrect value');
471 $_POST[$key] = trim($_POST[$key]);
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) {
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;
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') {
503 foreach ($values[$path] as $value) {
505 if (preg_match("/^(.+):(?:[ ]?)(\\w+)$/", $value, $matches)) {
506 // correct 'IP: HTTP header' pair
507 $ip = trim($matches[1]);
508 $proxies[$ip] = trim($matches[2]);
510 // save also incorrect values
511 $proxies["-$i"] = $value;
515 $values[$path] = $proxies;
517 $cf->set($work_path, $values[$path], $path);
521 // don't look for non-critical errors
528 * Tells whether form validation failed
532 public function hasErrors()
534 return count($this->errors
) > 0;
539 * Returns link to documentation
541 * @param string $path
544 public function getDocLink($path)
546 $test = substr($path, 0, 6);
547 if ($test == 'Import' ||
$test == 'Export') {
550 return 'Documentation.html#cfg_' . self
::_getOptName($path);
554 * Returns link to wiki
556 * @param string $path
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
587 private static function _getOptName($path)
589 return str_replace(array('Servers/1/', '/'), array('Servers/', '_'), $path);