Esempio n. 1
0
/**
 * Runs validation $validator_id on values $values and returns error list.
 *
 * Return values:
 * o array, keys - field path or formset id, values - array of errors
 *   when $isPostSource is true values is an empty array to allow for error list
 *   cleanup in HTML documen
 * o false - when no validators match name(s) given by $validator_id
 *
 * @param string|array $validator_id ID of validator(s) to run
 * @param array        $values       Values to validate
 * @param bool         $isPostSource tells whether $values are directly from
 *                                   POST request
 *
 * @return bool|array
 */
function PMA_config_validate($validator_id, &$values, $isPostSource)
{
    // find validators
    $validator_id = (array) $validator_id;
    $validators = PMA_config_get_validators();
    $vids = array();
    $cf = ConfigFile::getInstance();
    foreach ($validator_id as &$vid) {
        $vid = $cf->getCanonicalPath($vid);
        if (isset($validators[$vid])) {
            $vids[] = $vid;
        }
    }
    if (empty($vids)) {
        return false;
    }
    // create argument list with canonical paths and remember path mapping
    $arguments = array();
    $key_map = array();
    foreach ($values as $k => $v) {
        $k2 = $isPostSource ? str_replace('-', '/', $k) : $k;
        $k2 = strpos($k2, '/') ? $cf->getCanonicalPath($k2) : $k2;
        $key_map[$k2] = $k;
        $arguments[$k2] = $v;
    }
    // validate
    $result = array();
    foreach ($vids as $vid) {
        // call appropriate validation functions
        foreach ((array) $validators[$vid] as $validator) {
            $vdef = (array) $validator;
            $vname = array_shift($vdef);
            $args = array_merge(array($vid, &$arguments), $vdef);
            $r = call_user_func_array($vname, $args);
            // merge results
            if (is_array($r)) {
                foreach ($r as $key => $error_list) {
                    // skip empty values if $isPostSource is false
                    if (!$isPostSource && empty($error_list)) {
                        continue;
                    }
                    if (!isset($result[$key])) {
                        $result[$key] = array();
                    }
                    $result[$key] = array_merge($result[$key], (array) $error_list);
                }
            }
        }
    }
    // restore original paths
    $new_result = array();
    foreach ($result as $k => $v) {
        $k2 = isset($key_map[$k]) ? $key_map[$k] : $k;
        $new_result[$k2] = $v;
    }
    return empty($new_result) ? true : $new_result;
}
 /**
  * Outputs HTML for forms
  *
  * @uses ConfigFile::getInstance()
  * @uses ConfigFile::get()
  * @uses display_fieldset_bottom()
  * @uses display_fieldset_top()
  * @uses display_form_bottom()
  * @uses display_form_top()
  * @uses display_js()
  * @uses display_tabs_bottom()
  * @uses display_tabs_top()
  * @uses js_validate()
  * @uses PMA_config_get_validators()
  * @uses PMA_jsFormat()
  * @uses PMA_lang()
  * @param bool $tabbed_form
  * @param bool   $show_restore_default  whether show "restore default" button besides the input field
  */
 public function display($tabbed_form = false, $show_restore_default = false)
 {
     static $js_lang_sent = false;
     $js = array();
     $js_default = array();
     $tabbed_form = $tabbed_form && count($this->forms) > 1;
     $validators = PMA_config_get_validators();
     display_form_top();
     if ($tabbed_form) {
         $tabs = array();
         foreach ($this->forms as $form) {
             $tabs[$form->name] = PMA_lang("Form_{$form->name}");
         }
         display_tabs_top($tabs);
     }
     // valdiate only when we aren't displaying a "new server" form
     $is_new_server = false;
     foreach ($this->forms as $form) {
         /* @var $form Form */
         if ($form->index === 0) {
             $is_new_server = true;
             break;
         }
     }
     if (!$is_new_server) {
         $this->_validate();
     }
     // user preferences
     $this->_loadUserprefsInfo();
     // display forms
     foreach ($this->forms as $form) {
         /* @var $form Form */
         $form_desc = isset($GLOBALS["strConfigForm_{$form->name}_desc"]) ? PMA_lang("Form_{$form->name}_desc") : '';
         $form_errors = isset($this->errors[$form->name]) ? $this->errors[$form->name] : null;
         display_fieldset_top(PMA_lang("Form_{$form->name}"), $form_desc, $form_errors, array('id' => $form->name));
         foreach ($form->fields as $field => $path) {
             $work_path = array_search($path, $this->system_paths);
             $translated_path = $this->translated_paths[$work_path];
             // always true/false for user preferences display
             // otherwise null
             $userprefs_allow = isset($this->userprefs_keys[$path]) ? !isset($this->userprefs_disallow[$path]) : null;
             // display input
             $this->_displayFieldInput($form, $field, $path, $work_path, $translated_path, $show_restore_default, $userprefs_allow, $js_default);
             // register JS validators for this field
             if (isset($validators[$path])) {
                 js_validate($translated_path, $validators[$path], $js);
             }
         }
         display_fieldset_bottom();
     }
     if ($tabbed_form) {
         display_tabs_bottom();
     }
     display_form_bottom();
     // if not already done, send strings used for valdiation to JavaScript
     if (!$js_lang_sent) {
         $js_lang_sent = true;
         $js_lang = array();
         foreach ($this->js_lang_strings as $strName => $strValue) {
             $js_lang[] = "'{$strName}': '" . PMA_jsFormat($strValue, false) . '\'';
         }
         $js[] = "\$.extend(PMA_messages, {\n\t" . implode(",\n\t", $js_lang) . '})';
     }
     $js[] = "\$.extend(defaultValues, {\n\t" . implode(",\n\t", $js_default) . '})';
     display_js($js);
 }