PMA\libraries\config\FormDisplay::save PHP Method

save() public method

Validates and saves form data to session
public save ( array | string $forms, boolean $allow_partial_save = true ) : boolean
$forms array | string array of form names
$allow_partial_save boolean allows for partial form saving on failed validation
return boolean true on success (no errors and all saved)
    public function save($forms, $allow_partial_save = true)
    {
        $result = true;
        $forms = (array) $forms;
        $values = array();
        $to_save = array();
        $is_setup_script = defined('PMA_SETUP');
        if ($is_setup_script) {
            $this->_loadUserprefsInfo();
        }
        $this->_errors = array();
        foreach ($forms as $form_name) {
            /* @var $form Form */
            if (isset($this->_forms[$form_name])) {
                $form = $this->_forms[$form_name];
            } else {
                continue;
            }
            // get current server id
            $change_index = $form->index === 0 ? $this->_configFile->getServerCount() + 1 : false;
            // grab POST values
            foreach ($form->fields as $field => $system_path) {
                $work_path = array_search($system_path, $this->_systemPaths);
                $key = $this->_translatedPaths[$work_path];
                $type = $form->getOptionType($field);
                // skip groups
                if ($type == 'group') {
                    continue;
                }
                // ensure the value is set
                if (!isset($_POST[$key])) {
                    // checkboxes aren't set by browsers if they're off
                    if ($type == 'boolean') {
                        $_POST[$key] = false;
                    } else {
                        $this->_errors[$form->name][] = sprintf(__('Missing data for %s'), '<i>' . PMA_langName($system_path) . '</i>');
                        $result = false;
                        continue;
                    }
                }
                // user preferences allow/disallow
                if ($is_setup_script && isset($this->_userprefsKeys[$system_path])) {
                    if (isset($this->_userprefsDisallow[$system_path]) && isset($_POST[$key . '-userprefs-allow'])) {
                        unset($this->_userprefsDisallow[$system_path]);
                    } else {
                        if (!isset($_POST[$key . '-userprefs-allow'])) {
                            $this->_userprefsDisallow[$system_path] = true;
                        }
                    }
                }
                // cast variables to correct type
                switch ($type) {
                    case 'double':
                        $_POST[$key] = Util::requestString($_POST[$key]);
                        settype($_POST[$key], 'float');
                        break;
                    case 'boolean':
                    case 'integer':
                        if ($_POST[$key] !== '') {
                            $_POST[$key] = Util::requestString($_POST[$key]);
                            settype($_POST[$key], $type);
                        }
                        break;
                    case 'select':
                        $successfully_validated = $this->_validateSelect($_POST[$key], $form->getOptionValueList($system_path));
                        if (!$successfully_validated) {
                            $this->_errors[$work_path][] = __('Incorrect value!');
                            $result = false;
                            continue;
                        }
                        break;
                    case 'string':
                    case 'short_string':
                        $_POST[$key] = Util::requestString($_POST[$key]);
                        break;
                    case 'array':
                        // eliminate empty values and ensure we have an array
                        $post_values = is_array($_POST[$key]) ? $_POST[$key] : explode("\n", $_POST[$key]);
                        $_POST[$key] = array();
                        $this->_fillPostArrayParameters($post_values, $key);
                        break;
                }
                // now we have value with proper type
                $values[$system_path] = $_POST[$key];
                if ($change_index !== false) {
                    $work_path = str_replace("Servers/{$form->index}/", "Servers/{$change_index}/", $work_path);
                }
                $to_save[$work_path] = $system_path;
            }
        }
        // save forms
        if (!$allow_partial_save && !empty($this->_errors)) {
            // don't look for non-critical errors
            $this->_validate();
            return $result;
        }
        foreach ($to_save as $work_path => $path) {
            // TrustedProxies requires changes before saving
            if ($path == 'TrustedProxies') {
                $proxies = array();
                $i = 0;
                foreach ($values[$path] as $value) {
                    $matches = array();
                    $match = preg_match("/^(.+):(?:[ ]?)(\\w+)\$/", $value, $matches);
                    if ($match) {
                        // correct 'IP: HTTP header' pair
                        $ip = trim($matches[1]);
                        $proxies[$ip] = trim($matches[2]);
                    } else {
                        // save also incorrect values
                        $proxies["-{$i}"] = $value;
                        $i++;
                    }
                }
                $values[$path] = $proxies;
            }
            $this->_configFile->set($work_path, $values[$path], $path);
        }
        if ($is_setup_script) {
            $this->_configFile->set('UserprefsDisallow', array_keys($this->_userprefsDisallow));
        }
        // don't look for non-critical errors
        $this->_validate();
        return $result;
    }

Usage Example

Beispiel #1
0
 *
 * @param string $config_file_path
 *
 * @return array
 */
function loadConfig($config_file_path)
{
    $cfg = array();
    if (file_exists($config_file_path)) {
        include $config_file_path;
    }
    return $cfg;
}
$form_display = new FormDisplay($GLOBALS['ConfigFile']);
$form_display->registerForm('_config.php', $forms['_config.php']);
$form_display->save('_config.php');
$config_file_path = $GLOBALS['ConfigFile']->getFilePath();
if (isset($_POST['eol'])) {
    $_SESSION['eol'] = $_POST['eol'] == 'unix' ? 'unix' : 'win';
}
if (PMA_ifSetOr($_POST['submit_clear'], '')) {
    //
    // Clear current config and return to main page
    //
    $GLOBALS['ConfigFile']->resetConfigData();
    // drop post data
    header('HTTP/1.1 303 See Other');
    header('Location: index.php' . PMA_URL_getCommon());
    exit;
} elseif (PMA_ifSetOr($_POST['submit_download'], '')) {
    //