示例#1
0
文件: form.php 项目: Garth619/Femi9
 private function add_custom_input($var, $options)
 {
     if (empty($options['type'])) {
         trigger_error('add_custom_input called without a type option set');
     }
     $scrub_list['textarea']['value'] = true;
     $scrub_list['file']['value'] = true;
     $scrub_list['select']['value'] = true;
     $scrub_list['select']['type'] = true;
     $defaults = array('name' => $var);
     $input_group_name = $defaults['name'];
     $var = str_replace('[]', '', $var);
     $clean_var = trim(preg_replace('/[^a-z0-9_]+/i', '-', $var), '-');
     if (!empty($this->input_group)) {
         if (false === strpos($defaults['name'], '[')) {
             $defaults['name'] = "[{$defaults['name']}]";
         } else {
             $defaults['name'] = preg_replace('/^([^\\[]+)\\[/', '[$1][', $defaults['name']);
         }
         $input_group_name = $defaults['name'];
         $defaults['name'] = "{$this->input_group}{$defaults['name']}";
         $clean_var = trim(preg_replace('/[^a-z0-9_]+/i', '-', $defaults['name']), '-');
     }
     $val = $this->get_option($var);
     $defaults['id'] = "itsec-{$clean_var}";
     if (!empty($options['append_val_to_id']) && true === $options['append_val_to_id'] && !empty($options['value'])) {
         unset($options['append_val_to_id']);
         $defaults['id'] .= '-' . trim(preg_replace('/[^a-z0-9_]+/i', '-', $options['value']), '-');
     }
     $options = ITSEC_Form::merge_defaults($options, $defaults);
     if (!is_null($val)) {
         if (in_array($options['type'], array('checkbox', 'radio'))) {
             if (is_array($val) && in_array($options['value'], $val) || !is_array($val) && (string) $val === (string) $options['value']) {
                 $options['checked'] = 'checked';
             }
         } else {
             if ('select' !== $options['type']) {
                 $options['value'] = $val;
             }
         }
     }
     $attributes = '';
     if (false !== $options) {
         foreach ((array) $options as $name => $content) {
             if (!is_array($content) && (!isset($scrub_list[$options['type']][$name]) || true !== $scrub_list[$options['type']][$name])) {
                 if ('value' == $name) {
                     $content = ITSEC_Form::esc_value_attr($content);
                 } else {
                     if (!in_array($options['type'], array('submit', 'button'))) {
                         $content = esc_attr($content);
                     }
                 }
                 $attributes .= "{$name}=\"{$content}\" ";
             }
         }
     }
     if ('textarea' === $options['type']) {
         if (!isset($options['value'])) {
             $options['value'] = '';
         } else {
             if (is_array($options['value'])) {
                 $options['value'] = implode("\n", $options['value']);
                 echo '<input type="hidden" name="--itsec-form-convert-to-array[]" value="' . esc_attr($options['name']) . '" />' . "\n";
             }
         }
         echo "<textarea {$attributes} >" . ITSEC_Form::esc_value_attr($options['value']) . '</textarea>';
     } else {
         if ('select' === $options['type']) {
             echo "<select {$attributes}>\n";
             if (isset($options['value']) && is_array($options['value'])) {
                 foreach ((array) $options['value'] as $content => $name) {
                     if (is_array($name)) {
                         if (preg_match('/^__optgroup_\\d+$/', $content)) {
                             echo "<optgroup class='it-classes-optgroup-separator'>\n";
                         } else {
                             echo "<optgroup label='" . esc_attr($content) . "'>\n";
                         }
                         foreach ((array) $name as $content => $sub_name) {
                             if (is_array($val)) {
                                 $selected = '';
                                 foreach ($val as $set_val) {
                                     if ((string) $set_val === (string) $content) {
                                         $selected = ' selected="selected"';
                                     }
                                 }
                             } else {
                                 $selected = !is_null($val) && (string) $val === (string) $content ? ' selected="selected"' : '';
                             }
                             echo "<option value=\"" . ITSEC_Form::esc_value_attr($content) . "\"{$selected}>{$sub_name}</option>\n";
                         }
                         echo "</optgroup>\n";
                     } else {
                         if (is_array($val)) {
                             $selected = '';
                             foreach ($val as $set_val) {
                                 if ((string) $set_val === (string) $content) {
                                     $selected = ' selected="selected"';
                                 }
                             }
                         } else {
                             $selected = !is_null($val) && (string) $val === (string) $content ? ' selected="selected"' : '';
                         }
                         echo "<option value=\"" . ITSEC_Form::esc_value_attr($content) . "\"{$selected}>{$name}</option>\n";
                     }
                 }
             }
             echo "</select>\n";
         } else {
             echo '<input ' . $attributes . '/>' . "\n";
         }
     }
     if ('[]' === substr($options['name'], -2)) {
         if (!isset($this->tracked_arrays[$options['name']])) {
             echo '<input type="hidden" name="--itsec-form-tracked-arrays[]" value="' . esc_attr(substr($options['name'], 0, -2)) . '" />' . "\n";
             $this->tracked_arrays[$options['name']] = true;
         }
     } else {
         if ('checkbox' === $options['type']) {
             if (!isset($this->tracked_booleans[$options['name']])) {
                 echo '<input type="hidden" name="--itsec-form-tracked-booleans[]" value="' . esc_attr($options['name']) . '" />' . "\n";
                 $this->tracked_booleans[$options['name']] = true;
             }
         } else {
             if ('radio' === $options['type']) {
                 if (!isset($this->tracked_strings[$options['name']])) {
                     echo '<input type="hidden" name="--itsec-form-tracked-empty-strings[]" value="' . esc_attr($options['name']) . '" />' . "\n";
                     $this->tracked_strings[$options['name']] = true;
                 }
             }
         }
     }
 }