示例#1
0
function wpcf7_textarea_validation_filter($result, $tag)
{
    $tag = new WPCF7_FormTag($tag);
    $type = $tag->type;
    $name = $tag->name;
    $value = isset($_POST[$name]) ? (string) $_POST[$name] : '';
    if ($tag->is_required() && '' == $value) {
        $result->invalidate($tag, wpcf7_get_message('invalid_required'));
    }
    if (!empty($value)) {
        $maxlength = $tag->get_maxlength_option();
        $minlength = $tag->get_minlength_option();
        if ($maxlength && $minlength && $maxlength < $minlength) {
            $maxlength = $minlength = null;
        }
        $code_units = wpcf7_count_code_units(stripslashes($value));
        if (false !== $code_units) {
            if ($maxlength && $maxlength < $code_units) {
                $result->invalidate($tag, wpcf7_get_message('invalid_too_long'));
            } elseif ($minlength && $code_units < $minlength) {
                $result->invalidate($tag, wpcf7_get_message('invalid_too_short'));
            }
        }
    }
    return $result;
}
示例#2
0
function wpcf7_acceptance_validation_filter($result, $tag)
{
    if (!wpcf7_acceptance_as_validation()) {
        return $result;
    }
    $tag = new WPCF7_FormTag($tag);
    $name = $tag->name;
    $value = !empty($_POST[$name]) ? 1 : 0;
    $invert = $tag->has_option('invert');
    if ($invert && $value || !$invert && !$value) {
        $result->invalidate($tag, wpcf7_get_message('accept_terms'));
    }
    return $result;
}
示例#3
0
function wpcf7_text_validation_filter($result, $tag)
{
    $tag = new WPCF7_FormTag($tag);
    $name = $tag->name;
    $value = isset($_POST[$name]) ? trim(wp_unslash(strtr((string) $_POST[$name], "\n", " "))) : '';
    if ('text' == $tag->basetype) {
        if ($tag->is_required() && '' == $value) {
            $result->invalidate($tag, wpcf7_get_message('invalid_required'));
        }
    }
    if ('email' == $tag->basetype) {
        if ($tag->is_required() && '' == $value) {
            $result->invalidate($tag, wpcf7_get_message('invalid_required'));
        } elseif ('' != $value && !wpcf7_is_email($value)) {
            $result->invalidate($tag, wpcf7_get_message('invalid_email'));
        }
    }
    if ('url' == $tag->basetype) {
        if ($tag->is_required() && '' == $value) {
            $result->invalidate($tag, wpcf7_get_message('invalid_required'));
        } elseif ('' != $value && !wpcf7_is_url($value)) {
            $result->invalidate($tag, wpcf7_get_message('invalid_url'));
        }
    }
    if ('tel' == $tag->basetype) {
        if ($tag->is_required() && '' == $value) {
            $result->invalidate($tag, wpcf7_get_message('invalid_required'));
        } elseif ('' != $value && !wpcf7_is_tel($value)) {
            $result->invalidate($tag, wpcf7_get_message('invalid_tel'));
        }
    }
    if (!empty($value)) {
        $maxlength = $tag->get_maxlength_option();
        $minlength = $tag->get_minlength_option();
        if ($maxlength && $minlength && $maxlength < $minlength) {
            $maxlength = $minlength = null;
        }
        $code_units = wpcf7_count_code_units(stripslashes($value));
        if (false !== $code_units) {
            if ($maxlength && $maxlength < $code_units) {
                $result->invalidate($tag, wpcf7_get_message('invalid_too_long'));
            } elseif ($minlength && $code_units < $minlength) {
                $result->invalidate($tag, wpcf7_get_message('invalid_too_short'));
            }
        }
    }
    return $result;
}
示例#4
0
function wpcf7_date_validation_filter($result, $tag)
{
    $tag = new WPCF7_FormTag($tag);
    $name = $tag->name;
    $min = $tag->get_date_option('min');
    $max = $tag->get_date_option('max');
    $value = isset($_POST[$name]) ? trim(strtr((string) $_POST[$name], "\n", " ")) : '';
    if ($tag->is_required() && '' == $value) {
        $result->invalidate($tag, wpcf7_get_message('invalid_required'));
    } elseif ('' != $value && !wpcf7_is_date($value)) {
        $result->invalidate($tag, wpcf7_get_message('invalid_date'));
    } elseif ('' != $value && !empty($min) && $value < $min) {
        $result->invalidate($tag, wpcf7_get_message('date_too_early'));
    } elseif ('' != $value && !empty($max) && $max < $value) {
        $result->invalidate($tag, wpcf7_get_message('date_too_late'));
    }
    return $result;
}
示例#5
0
function wpcf7_number_validation_filter($result, $tag)
{
    $tag = new WPCF7_FormTag($tag);
    $name = $tag->name;
    $value = isset($_POST[$name]) ? trim(strtr((string) $_POST[$name], "\n", " ")) : '';
    $min = $tag->get_option('min', 'signed_int', true);
    $max = $tag->get_option('max', 'signed_int', true);
    if ($tag->is_required() && '' == $value) {
        $result->invalidate($tag, wpcf7_get_message('invalid_required'));
    } elseif ('' != $value && !wpcf7_is_number($value)) {
        $result->invalidate($tag, wpcf7_get_message('invalid_number'));
    } elseif ('' != $value && '' != $min && (double) $value < (double) $min) {
        $result->invalidate($tag, wpcf7_get_message('number_too_small'));
    } elseif ('' != $value && '' != $max && (double) $max < (double) $value) {
        $result->invalidate($tag, wpcf7_get_message('number_too_large'));
    }
    return $result;
}
示例#6
0
function wpcf7_submit_form_tag_handler($tag)
{
    $tag = new WPCF7_FormTag($tag);
    $class = wpcf7_form_controls_class($tag->type);
    $atts = array();
    $atts['class'] = $tag->get_class_option($class);
    $atts['id'] = $tag->get_id_option();
    $atts['tabindex'] = $tag->get_option('tabindex', 'int', true);
    $value = isset($tag->values[0]) ? $tag->values[0] : '';
    if (empty($value)) {
        $value = __('Send', 'contact-form-7');
    }
    $atts['type'] = 'submit';
    $atts['value'] = $value;
    $atts = wpcf7_format_atts($atts);
    $html = sprintf('<input %1$s />', $atts);
    return $html;
}
示例#7
0
function wpcf7_hidden_form_tag_handler($tag)
{
    $tag = new WPCF7_FormTag($tag);
    if (empty($tag->name)) {
        return '';
    }
    $atts = array();
    $class = wpcf7_form_controls_class($tag->type);
    $atts['class'] = $tag->get_class_option($class);
    $atts['id'] = $tag->get_id_option();
    $value = (string) reset($tag->values);
    $value = $tag->get_default_option($value);
    $atts['value'] = $value;
    $atts['type'] = 'hidden';
    $atts['name'] = $tag->name;
    $atts = wpcf7_format_atts($atts);
    $html = sprintf('<input %s />', $atts);
    return $html;
}
示例#8
0
function wpcf7_count_form_tag_handler($tag)
{
    $tag = new WPCF7_FormTag($tag);
    if (empty($tag->name)) {
        return '';
    }
    $targets = wpcf7_scan_form_tags(array('name' => $tag->name));
    $maxlength = $minlength = null;
    while ($targets) {
        $target = array_shift($targets);
        $target = new WPCF7_FormTag($target);
        if ('count' != $target->type) {
            $maxlength = $target->get_maxlength_option();
            $minlength = $target->get_minlength_option();
            break;
        }
    }
    if ($maxlength && $minlength && $maxlength < $minlength) {
        $maxlength = $minlength = null;
    }
    if ($tag->has_option('down')) {
        $value = (int) $maxlength;
        $class = 'wpcf7-character-count down';
    } else {
        $value = '0';
        $class = 'wpcf7-character-count up';
    }
    $atts = array();
    $atts['id'] = $tag->get_id_option();
    $atts['class'] = $tag->get_class_option($class);
    $atts['data-target-name'] = $tag->name;
    $atts['data-starting-value'] = $value;
    $atts['data-current-value'] = $value;
    $atts['data-maximum-value'] = $maxlength;
    $atts['data-minimum-value'] = $minlength;
    $atts = wpcf7_format_atts($atts);
    $html = sprintf('<span %1$s>%2$s</span>', $atts, $value);
    return $html;
}
示例#9
0
function wpcf7_akismet_submitted_params()
{
    $params = array('author' => '', 'author_email' => '', 'author_url' => '', 'content' => '');
    $has_akismet_option = false;
    foreach ((array) $_POST as $key => $val) {
        if ('_wpcf7' == substr($key, 0, 6) || '_wpnonce' == $key) {
            continue;
        }
        if (is_array($val)) {
            $val = implode(', ', wpcf7_array_flatten($val));
        }
        $val = trim($val);
        if (0 == strlen($val)) {
            continue;
        }
        if ($tags = wpcf7_scan_form_tags(array('name' => $key))) {
            $tag = $tags[0];
            $tag = new WPCF7_FormTag($tag);
            $akismet = $tag->get_option('akismet', '(author|author_email|author_url)', true);
            if ($akismet) {
                $has_akismet_option = true;
                if ('author' == $akismet) {
                    $params[$akismet] = trim($params[$akismet] . ' ' . $val);
                } elseif ('' == $params[$akismet]) {
                    $params[$akismet] = $val;
                }
            }
        }
        $params['content'] .= "\n\n" . $val;
    }
    if (!$has_akismet_option) {
        return false;
    }
    $params['content'] = trim($params['content']);
    return $params;
}
示例#10
0
function wpcf7_quiz_form_tag_handler($tag)
{
    $tag = new WPCF7_FormTag($tag);
    if (empty($tag->name)) {
        return '';
    }
    $validation_error = wpcf7_get_validation_error($tag->name);
    $class = wpcf7_form_controls_class($tag->type);
    if ($validation_error) {
        $class .= ' wpcf7-not-valid';
    }
    $atts = array();
    $atts['size'] = $tag->get_size_option('40');
    $atts['maxlength'] = $tag->get_maxlength_option();
    $atts['minlength'] = $tag->get_minlength_option();
    if ($atts['maxlength'] && $atts['minlength'] && $atts['maxlength'] < $atts['minlength']) {
        unset($atts['maxlength'], $atts['minlength']);
    }
    $atts['class'] = $tag->get_class_option($class);
    $atts['id'] = $tag->get_id_option();
    $atts['tabindex'] = $tag->get_option('tabindex', 'int', true);
    $atts['autocomplete'] = 'off';
    $atts['aria-required'] = 'true';
    $atts['aria-invalid'] = $validation_error ? 'true' : 'false';
    $pipes = $tag->pipes;
    if ($pipes instanceof WPCF7_Pipes && !$pipes->zero()) {
        $pipe = $pipes->random_pipe();
        $question = $pipe->before;
        $answer = $pipe->after;
    } else {
        // default quiz
        $question = '1+1=?';
        $answer = '2';
    }
    $answer = wpcf7_canonicalize($answer);
    $atts['type'] = 'text';
    $atts['name'] = $tag->name;
    $atts = wpcf7_format_atts($atts);
    $html = sprintf('<span class="wpcf7-form-control-wrap %1$s"><label><span class="wpcf7-quiz-label">%2$s</span> <input %3$s /></label><input type="hidden" name="_wpcf7_quiz_answer_%4$s" value="%5$s" />%6$s</span>', sanitize_html_class($tag->name), esc_html($question), $atts, $tag->name, wp_hash($answer, 'wpcf7_quiz'), $validation_error);
    return $html;
}
示例#11
0
function wpcf7_select_validation_filter($result, $tag)
{
    $tag = new WPCF7_FormTag($tag);
    $name = $tag->name;
    if (isset($_POST[$name]) && is_array($_POST[$name])) {
        foreach ($_POST[$name] as $key => $value) {
            if ('' === $value) {
                unset($_POST[$name][$key]);
            }
        }
    }
    $empty = !isset($_POST[$name]) || empty($_POST[$name]) && '0' !== $_POST[$name];
    if ($tag->is_required() && $empty) {
        $result->invalidate($tag, wpcf7_get_message('invalid_required'));
    }
    return $result;
}
示例#12
0
function wpcf7_checkbox_posted_data($posted_data)
{
    $tags = wpcf7_scan_form_tags(array('type' => array('checkbox', 'checkbox*', 'radio')));
    if (empty($tags)) {
        return $posted_data;
    }
    foreach ($tags as $tag) {
        $tag = new WPCF7_FormTag($tag);
        if (!isset($posted_data[$tag->name])) {
            continue;
        }
        $posted_items = (array) $posted_data[$tag->name];
        if ($tag->has_option('free_text')) {
            if (WPCF7_USE_PIPE) {
                $values = $tag->pipes->collect_afters();
            } else {
                $values = $tag->values;
            }
            $last = array_pop($values);
            $last = html_entity_decode($last, ENT_QUOTES, 'UTF-8');
            if (in_array($last, $posted_items)) {
                $posted_items = array_diff($posted_items, array($last));
                $free_text_name = sprintf('_wpcf7_%1$s_free_text_%2$s', $tag->basetype, $tag->name);
                $free_text = $posted_data[$free_text_name];
                if (!empty($free_text)) {
                    $posted_items[] = trim($last . ' ' . $free_text);
                } else {
                    $posted_items[] = $last;
                }
            }
        }
        $posted_data[$tag->name] = $posted_items;
    }
    return $posted_data;
}
示例#13
0
function wpcf7_file_validation_filter($result, $tag)
{
    $tag = new WPCF7_FormTag($tag);
    $name = $tag->name;
    $id = $tag->get_id_option();
    $file = isset($_FILES[$name]) ? $_FILES[$name] : null;
    if ($file['error'] && UPLOAD_ERR_NO_FILE != $file['error']) {
        $result->invalidate($tag, wpcf7_get_message('upload_failed_php_error'));
        return $result;
    }
    if (empty($file['tmp_name']) && $tag->is_required()) {
        $result->invalidate($tag, wpcf7_get_message('invalid_required'));
        return $result;
    }
    if (!is_uploaded_file($file['tmp_name'])) {
        return $result;
    }
    $allowed_file_types = array();
    if ($file_types_a = $tag->get_option('filetypes')) {
        foreach ($file_types_a as $file_types) {
            $file_types = explode('|', $file_types);
            foreach ($file_types as $file_type) {
                $file_type = trim($file_type, '.');
                $file_type = str_replace(array('.', '+', '*', '?'), array('\\.', '\\+', '\\*', '\\?'), $file_type);
                $allowed_file_types[] = $file_type;
            }
        }
    }
    $allowed_file_types = array_unique($allowed_file_types);
    $file_type_pattern = implode('|', $allowed_file_types);
    $allowed_size = 1048576;
    // default size 1 MB
    if ($file_size_a = $tag->get_option('limit')) {
        $limit_pattern = '/^([1-9][0-9]*)([kKmM]?[bB])?$/';
        foreach ($file_size_a as $file_size) {
            if (preg_match($limit_pattern, $file_size, $matches)) {
                $allowed_size = (int) $matches[1];
                if (!empty($matches[2])) {
                    $kbmb = strtolower($matches[2]);
                    if ('kb' == $kbmb) {
                        $allowed_size *= 1024;
                    } elseif ('mb' == $kbmb) {
                        $allowed_size *= 1024 * 1024;
                    }
                }
                break;
            }
        }
    }
    /* File type validation */
    // Default file-type restriction
    if ('' == $file_type_pattern) {
        $file_type_pattern = 'jpg|jpeg|png|gif|pdf|doc|docx|ppt|pptx|odt|avi|ogg|m4a|mov|mp3|mp4|mpg|wav|wmv';
    }
    $file_type_pattern = trim($file_type_pattern, '|');
    $file_type_pattern = '(' . $file_type_pattern . ')';
    $file_type_pattern = '/\\.' . $file_type_pattern . '$/i';
    if (!preg_match($file_type_pattern, $file['name'])) {
        $result->invalidate($tag, wpcf7_get_message('upload_file_type_invalid'));
        return $result;
    }
    /* File size validation */
    if ($file['size'] > $allowed_size) {
        $result->invalidate($tag, wpcf7_get_message('upload_file_too_large'));
        return $result;
    }
    wpcf7_init_uploads();
    // Confirm upload dir
    $uploads_dir = wpcf7_upload_tmp_dir();
    $uploads_dir = wpcf7_maybe_add_random_dir($uploads_dir);
    $filename = $file['name'];
    $filename = wpcf7_canonicalize($filename);
    $filename = sanitize_file_name($filename);
    $filename = wpcf7_antiscript_file_name($filename);
    $filename = wp_unique_filename($uploads_dir, $filename);
    $new_file = trailingslashit($uploads_dir) . $filename;
    if (false === @move_uploaded_file($file['tmp_name'], $new_file)) {
        $result->invalidate($tag, wpcf7_get_message('upload_failed'));
        return $result;
    }
    // Make sure the uploaded file is only readable for the owner process
    @chmod($new_file, 0400);
    if ($submission = WPCF7_Submission::get_instance()) {
        $submission->add_uploaded_file($name, $new_file);
    }
    return $result;
}
示例#14
0
 public function replace_mail_tags_with_minimum_input($matches)
 {
     // allow [[foo]] syntax for escaping a tag
     if ($matches[1] == '[' && $matches[4] == ']') {
         return substr($matches[0], 1, -1);
     }
     $tag = $matches[0];
     $tagname = $matches[2];
     $values = $matches[3];
     if (!empty($values)) {
         preg_match_all('/"[^"]*"|\'[^\']*\'/', $values, $matches);
         $values = wpcf7_strip_quote_deep($matches[0]);
     }
     $do_not_heat = false;
     if (preg_match('/^_raw_(.+)$/', $tagname, $matches)) {
         $tagname = trim($matches[1]);
         $do_not_heat = true;
     }
     $format = '';
     if (preg_match('/^_format_(.+)$/', $tagname, $matches)) {
         $tagname = trim($matches[1]);
         $format = $values[0];
     }
     $example_email = '*****@*****.**';
     $example_text = 'example';
     $example_blank = '';
     $form_tags = $this->contact_form->scan_form_tags(array('name' => $tagname));
     if ($form_tags) {
         $form_tag = new WPCF7_FormTag($form_tags[0]);
         $is_required = $form_tag->is_required() || 'radio' == $form_tag->type;
         if (!$is_required) {
             return $example_blank;
         }
         $is_selectable = in_array($form_tag->basetype, array('radio', 'checkbox', 'select'));
         if ($is_selectable) {
             if ($form_tag->pipes instanceof WPCF7_Pipes) {
                 if ($do_not_heat) {
                     $before_pipes = $form_tag->pipes->collect_befores();
                     $last_item = array_pop($before_pipes);
                 } else {
                     $after_pipes = $form_tag->pipes->collect_afters();
                     $last_item = array_pop($after_pipes);
                 }
             } else {
                 $last_item = array_pop($form_tag->values);
             }
             if ($last_item && wpcf7_is_mailbox_list($last_item)) {
                 return $example_email;
             } else {
                 return $example_text;
             }
         }
         if ('email' == $form_tag->basetype) {
             return $example_email;
         } else {
             return $example_text;
         }
     } else {
         $tagname = preg_replace('/^wpcf7\\./', '_', $tagname);
         // for back-compat
         if ('_post_author_email' == $tagname) {
             return $example_email;
         } elseif ('_' == substr($tagname, 0, 1)) {
             // maybe special mail tag
             return $example_text;
         }
     }
     return $tag;
 }
示例#15
0
function wpcf7_captcha_form_tag_handler($tag)
{
    $tag = new WPCF7_FormTag($tag);
    if ('captchac' == $tag->type && !class_exists('ReallySimpleCaptcha')) {
        return '<em>' . __('To use CAPTCHA, you need <a href="http://wordpress.org/extend/plugins/really-simple-captcha/">Really Simple CAPTCHA</a> plugin installed.', 'contact-form-7') . '</em>';
    }
    if (empty($tag->name)) {
        return '';
    }
    $validation_error = wpcf7_get_validation_error($tag->name);
    $class = wpcf7_form_controls_class($tag->type);
    if ('captchac' == $tag->type) {
        // CAPTCHA-Challenge (image)
        $class .= ' wpcf7-captcha-' . $tag->name;
        $atts = array();
        $atts['class'] = $tag->get_class_option($class);
        $atts['id'] = $tag->get_id_option();
        $op = array('img_size' => array(72, 24), 'base' => array(6, 18), 'font_size' => 14, 'font_char_width' => 15);
        $op = array_merge($op, wpcf7_captchac_options($tag->options));
        if (!($filename = wpcf7_generate_captcha($op))) {
            return '';
        }
        if (!empty($op['img_size'])) {
            if (isset($op['img_size'][0])) {
                $atts['width'] = $op['img_size'][0];
            }
            if (isset($op['img_size'][1])) {
                $atts['height'] = $op['img_size'][1];
            }
        }
        $atts['alt'] = 'captcha';
        $atts['src'] = wpcf7_captcha_url($filename);
        $atts = wpcf7_format_atts($atts);
        $prefix = substr($filename, 0, strrpos($filename, '.'));
        $html = sprintf('<input type="hidden" name="_wpcf7_captcha_challenge_%1$s" value="%2$s" /><img %3$s />', $tag->name, $prefix, $atts);
        return $html;
    } elseif ('captchar' == $tag->type) {
        // CAPTCHA-Response (input)
        if ($validation_error) {
            $class .= ' wpcf7-not-valid';
        }
        $atts = array();
        $atts['size'] = $tag->get_size_option('40');
        $atts['maxlength'] = $tag->get_maxlength_option();
        $atts['minlength'] = $tag->get_minlength_option();
        if ($atts['maxlength'] && $atts['minlength'] && $atts['maxlength'] < $atts['minlength']) {
            unset($atts['maxlength'], $atts['minlength']);
        }
        $atts['class'] = $tag->get_class_option($class);
        $atts['id'] = $tag->get_id_option();
        $atts['tabindex'] = $tag->get_option('tabindex', 'int', true);
        $atts['autocomplete'] = 'off';
        $atts['aria-invalid'] = $validation_error ? 'true' : 'false';
        $value = (string) reset($tag->values);
        if (wpcf7_is_posted()) {
            $value = '';
        }
        if ($tag->has_option('placeholder') || $tag->has_option('watermark')) {
            $atts['placeholder'] = $value;
            $value = '';
        }
        $atts['value'] = $value;
        $atts['type'] = 'text';
        $atts['name'] = $tag->name;
        $atts = wpcf7_format_atts($atts);
        $html = sprintf('<span class="wpcf7-form-control-wrap %1$s"><input %2$s />%3$s</span>', sanitize_html_class($tag->name), $atts, $validation_error);
        return $html;
    }
}
示例#16
0
function wpcf7_recaptcha_form_tag_handler($tag)
{
    wp_enqueue_script('google-recaptcha');
    $tag = new WPCF7_FormTag($tag);
    $atts = array();
    $recaptcha = WPCF7_RECAPTCHA::get_instance();
    $atts['data-sitekey'] = $recaptcha->get_sitekey();
    $atts['data-theme'] = $tag->get_option('theme', '(dark|light)', true);
    $atts['data-type'] = $tag->get_option('type', '(audio|image)', true);
    $atts['data-size'] = $tag->get_option('size', '(compact|normal)', true);
    $atts['data-tabindex'] = $tag->get_option('tabindex', 'int', true);
    $atts['data-callback'] = $tag->get_option('callback', '', true);
    $atts['data-expired-callback'] = $tag->get_option('expired_callback', '', true);
    $atts['class'] = $tag->get_class_option(wpcf7_form_controls_class($tag->type, 'g-recaptcha'));
    $atts['id'] = $tag->get_id_option();
    $html = sprintf('<div %1$s></div>', wpcf7_format_atts($atts));
    $html .= wpcf7_recaptcha_noscript(array('sitekey' => $atts['data-sitekey']));
    $html = sprintf('<div class="wpcf7-form-control-wrap">%s</div>', $html);
    return $html;
}
示例#17
0
 public function __construct($tag)
 {
     wpcf7_deprecated_function('WPCF7_Shortcode', '4.6', 'WPCF7_FormTag');
     parent::__construct($tag);
 }