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: FormDisplay.class.php 11673 2008-10-23 11:09:33Z crackpl $
17
require_once './setup/lib/FormDisplay.tpl.php';
18
require_once './setup/lib/validate.lib.php';
19
require_once './libraries/js_escape.lib.php';
22
* Form management class, displays and processes forms
30
private $forms = array();
33
* Stores validation errors, indexed by paths
34
* [ Form_name ] is an array of form errors
35
* [path] is a string storing error associated with single field
38
private $errors = array();
41
* Paths changed so that they can be used as HTML ids, indexed by paths
44
private $translated_paths = array();
47
* Server paths change indexes so we define maps from current server
48
* path to the first one, indexed by work path
51
private $system_paths = array();
54
* Language strings which will be sent to PMA_messages JS variable
55
* Will be looked up in $GLOBALS: str{value} or strSetup{value}
58
private $js_lang_strings = array('error_nan_p', 'error_nan_nneg',
59
'error_incorrect_port');
62
* Tells whether forms have been validated
65
private $is_valdiated = true;
68
* Registers form in form manager
70
* @param string $form_name
71
* @param int $server_id 0 if new server, validation; >= 1 if editing a server
73
public function registerForm($form_name, $server_id = null)
75
$this->forms[$form_name] = new Form($form_name, $server_id);
76
$this->is_valdiated = false;
77
foreach ($this->forms[$form_name]->fields as $path) {
78
$work_path = $server_id === null
80
: str_replace('Servers/1/', "Servers/$server_id/", $path);
81
$this->system_paths[$work_path] = $path;
82
$this->translated_paths[$work_path] = str_replace('/', '-', $work_path);
87
* Processes forms, returns true on successful save
89
* @param bool $allow_partial_save allows for partial form saving on failed validation
92
public function process($allow_partial_save = true)
94
// gather list of forms to save
95
if (!isset($_POST['submit_save'])) {
100
if (count($this->forms) > 0) {
101
return $this->save(array_keys($this->forms), $allow_partial_save);
107
* Runs validation for all registered forms
109
private function _validate()
111
if ($this->is_valdiated) {
115
$cf = ConfigFile::getInstance();
118
foreach ($this->forms as $form) {
119
/* @var $form Form */
120
$paths[] = $form->name;
121
// collect values and paths
122
foreach ($form->fields as $path) {
123
$work_path = array_search($path, $this->system_paths);
124
$values[$path] = $cf->getValue($work_path);
130
$errors = validate($paths, $values, false);
132
// change error keys from canonical paths to work paths
133
if (is_array($errors) && count($errors) > 0) {
134
$this->errors = array();
135
foreach ($errors as $path => $error_list) {
136
$work_path = array_search($path, $this->system_paths);
139
// form error, fix path
142
$this->errors[$work_path] = $error_list;
145
$this->is_valdiated = true;
150
* Outputs HTML for forms
152
* @param bool $tabbed_form
153
* @param bool $show_restore_default whether show "restore default" button besides the input field
155
public function display($tabbed_form = false, $show_restore_default = false)
157
static $js_lang_sent = false;
160
$js_default = array();
161
$tabbed_form = $tabbed_form && (count($this->forms) > 1);
162
$validators = ConfigFile::getInstance()->getDbEntry('_validators');
168
foreach ($this->forms as $form) {
169
$tabs[$form->name] = PMA_lang("Form_$form->name");
171
display_tabs_top($tabs);
174
// valdiate only when we aren't displaying a "new server" form
175
$is_new_server = false;
176
foreach ($this->forms as $form) {
177
/* @var $form Form */
178
if ($form->index === 0) {
179
$is_new_server = true;
183
if (!$is_new_server) {
188
foreach ($this->forms as $form) {
189
/* @var $form Form */
190
$form_desc = isset($GLOBALS["strSetupForm_{$form->name}_desc"])
191
? PMA_lang("Form_{$form->name}_desc")
193
$form_errors = isset($this->errors[$form->name])
194
? $this->errors[$form->name] : null;
195
display_fieldset_top(PMA_lang("Form_$form->name"),
196
$form_desc, $form_errors, array('id' => $form->name));
198
foreach ($form->fields as $field => $path) {
199
$work_path = array_search($path, $this->system_paths);
200
$translated_path = $this->translated_paths[$work_path];
202
$this->_displayFieldInput($form, $field, $path, $work_path,
203
$translated_path, $show_restore_default, $js_default);
204
// register JS validators for this field
205
if (isset($validators[$path])) {
206
js_validate($translated_path, $validators[$path], $js);
209
display_fieldset_bottom();
213
display_tabs_bottom();
215
display_form_bottom();
217
// if not already done, send strings used for valdiation to JavaScript
218
if (!$js_lang_sent) {
219
$js_lang_sent = true;
221
foreach ($this->js_lang_strings as $str) {
222
$lang = isset($GLOBALS["strSetup$str"])
223
? $GLOBALS["strSetup$str"]
224
: filter_input($GLOBALS["str$str"]); // null if not set
225
$js_lang[] = "'$str': '" . PMA_jsFormat($lang, false) . '\'';
227
$js[] = '$extend(PMA_messages, {' . implode(",\n\t", $js_lang) . '})';
230
$js[] = '$extend(defaultValues, {' . implode(",\n\t", $js_default) . '})';
235
* Prepares data for input field display and outputs HTML code
238
* @param string $field field name as it appears in $form
239
* @param string $system_path field path, eg. Servers/1/verbose
240
* @param string $work_path work path, eg. Servers/4/verbose
241
* @param string $translated_path work path changed so that it can be used as XHTML id
242
* @param bool $show_restore_default whether show "restore default" button besides the input field
243
* @param array &$js_default array which stores JavaScript code to be displayed
245
private function _displayFieldInput(Form $form, $field, $system_path, $work_path,
246
$translated_path, $show_restore_default, array &$js_default)
248
$name = PMA_lang_name($system_path);
249
$description = PMA_lang_desc($system_path);
251
$cf = ConfigFile::getInstance();
252
$value = $cf->get($work_path);
253
$value_default = $cf->getDefault($system_path);
254
$value_is_default = false;
255
if ($value === null || $value === $value_default) {
256
$value = $value_default;
257
$value_is_default = true;
261
'doc' => $this->getDocLink($system_path),
262
'wiki' => $this->getWikiLink($system_path),
263
'show_restore_default' => $show_restore_default);
264
if (isset($form->default[$system_path])) {
265
$opts['setvalue'] = $form->default[$system_path];
268
if (isset($this->errors[$work_path])) {
269
$opts['errors'] = $this->errors[$work_path];
271
switch ($form->getOptionType($field)) {
286
$opts['values'] = array();
287
$values = $form->getOptionValueList($form->fields[$field]);
288
foreach ($values as $v) {
289
$opts['values'][$v] = $v;
294
$value = (array) $value;
295
$value_default = (array) $value_default;
298
trigger_error("Field $system_path has no type", E_USER_WARNING);
302
// TrustedProxies requires changes before displaying
303
if ($system_path == 'TrustedProxies') {
304
foreach ($value as $ip => &$v) {
305
if (!preg_match('/^-\d+$/', $ip)) {
306
$v = $ip . ': ' . $v;
311
// send default value to form's JS
312
$js_line = '\'' . $translated_path . '\': ';
315
$js_line .= '\'' . PMA_escapeJsString($value_default) . '\'';
318
$js_line .= $value_default ? 'true' : 'false';
321
$value_default_js = is_bool($value_default)
322
? (int) $value_default
324
$js_line .= '[\'' . PMA_escapeJsString($value_default_js) . '\']';
327
$js_line .= '\'' . PMA_escapeJsString(implode("\n", $value_default)) . '\'';
330
$js_default[] = $js_line;
332
display_input($translated_path, $name, $description, $type,
333
$value, $value_is_default, $opts);
339
public function displayErrors()
342
if (count($this->errors) == 0) {
346
foreach ($this->errors as $system_path => $error_list) {
347
if (isset($this->system_paths[$system_path])) {
348
$path = $this->system_paths[$system_path];
349
$name = PMA_lang_name($path);
351
$name = $GLOBALS["strSetupForm_$system_path"];
353
display_errors($name, $error_list);
358
* Reverts erroneous fields to their default values
360
public function fixErrors()
363
if (count($this->errors) == 0) {
367
$cf = ConfigFile::getInstance();
368
foreach (array_keys($this->errors) as $work_path) {
369
if (!isset($this->system_paths[$work_path])) {
372
$canonical_path = $this->system_paths[$work_path];
373
$cf->set($work_path, $cf->getDefault($canonical_path));
378
* Validates select field and casts $value to correct type
380
* @param string $value
381
* @param array $allowed
384
private function _validateSelect(&$value, array $allowed)
386
foreach ($allowed as $v) {
388
settype($value, gettype($v));
396
* Validates and saves form data to session
398
* @param array|string $forms array of form names
399
* @param bool $allow_partial_save allows for partial form saving on failed validation
400
* @return boolean true on success (no errors and all saved)
402
public function save($forms, $allow_partial_save = true)
405
$cf = ConfigFile::getInstance();
406
$forms = (array) $forms;
410
$this->errors = array();
411
foreach ($forms as $form) {
412
/* @var $form Form */
413
if (isset($this->forms[$form])) {
414
$form = $this->forms[$form];
418
// get current server id
419
$change_index = $form->index === 0
420
? $cf->getServerCount() + 1
423
foreach ($form->fields as $field => $system_path) {
424
$work_path = array_search($system_path, $this->system_paths);
425
$key = $this->translated_paths[$work_path];
427
// ensure the value is set
428
if (!isset($_POST[$key])) {
429
// checkboxes aren't set by browsers if they're off
430
if ($form->getOptionType($field) == 'boolean') {
431
$_POST[$key] = false;
433
$this->errors[$form->name][] = PMA_lang(
434
'error_missing_field_data',
435
'<i>' . PMA_lang_name($system_path) . '</i>');
441
// cast variables to correct type
442
$type = $form->getOptionType($field);
445
settype($_POST[$key], 'float');
449
if ($_POST[$key] !== '') {
450
settype($_POST[$key], $type);
454
if (!$this->_validateSelect($_POST[$key], $form->getOptionValueList($system_path))) {
455
$this->errors[$work_path][] = $GLOBALS["strstrSetuperror_incorrect_value"];
461
$_POST[$key] = trim($_POST[$key]);
464
// eliminate empty values and ensure we have an array
465
$post_values = explode("\n", $_POST[$key]);
466
$_POST[$key] = array();
467
foreach ($post_values as $v) {
476
// now we have value with proper type
477
$values[$system_path] = $_POST[$key];
478
if ($change_index !== false) {
479
$work_path = str_replace("Servers/$form->index/",
480
"Servers/$change_index/", $work_path);
482
$to_save[$work_path] = $system_path;
487
if ($allow_partial_save || empty($this->errors)) {
488
foreach ($to_save as $work_path => $path) {
489
// TrustedProxies requires changes before saving
490
if ($path == 'TrustedProxies') {
493
foreach ($values[$path] as $value) {
495
if (preg_match("/^(.+):(?:[ ]?)(\\w+)$/", $value, $matches)) {
496
// correct 'IP: HTTP header' pair
497
$ip = trim($matches[1]);
498
$proxies[$ip] = trim($matches[2]);
500
// save also incorrect values
501
$proxies["-$i"] = $value;
505
$values[$path] = $proxies;
507
$cf->set($work_path, $values[$path], $path);
511
// don't look for non-critical errors
518
* Tells whether form validation failed
522
public function hasErrors()
524
return count($this->errors) > 0;
529
* Returns link to documentation
531
* @param string $path
534
public function getDocLink($path)
536
$test = substr($path, 0, 6);
537
if ($test == 'Import' || $test == 'Export') {
540
return '../Documentation.html#cfg_' . self::_getOptName($path);
544
* Returns link to wiki
546
* @param string $path
549
public function getWikiLink($path)
551
$opt_name = self::_getOptName($path);
552
if (substr($opt_name, 0, 7) == 'Servers') {
553
$opt_name = substr($opt_name, 8);
554
if (strpos($opt_name, 'AllowDeny') === 0) {
555
$opt_name = str_replace('_', '_.28', $opt_name) . '.29';
558
$test = substr($path, 0, 6);
559
if ($test == 'Import') {
560
$opt_name = substr($opt_name, 7);
561
if ($opt_name == 'format') {
562
$opt_name = 'format_2';
565
if ($test == 'Export') {
566
$opt_name = substr($opt_name, 7);
568
return 'http://wiki.cihar.com/pma/Config#' . $opt_name;
572
* Changes path so it can be used in URLs
574
* @param string $path
577
private static function _getOptName($path)
579
return str_replace(array('Servers/1/', '/'), array('Servers/', '_'), $path);
b'\\ No newline at end of file'