add_action("wpcf7_init", function(){
if( function_exists( 'wpcf7_add_form_tag' ) ) {
wpcf7_add_form_tag("validcode", "juxin_validcode_handler", true);
wpcf7_add_form_tag("validcode*", "juxin_validcode_handler", true);
}
});
function juxin_validcode_handler($tag){
$tag = new WPCF7_FormTag( $tag );
if( empty($tag->name) ) return '';
$atts = array();
if ( $tag->is_required() ) {
$atts['aria-required'] = 'true';
}
$validation_error = wpcf7_get_validation_error( $tag->name );
$atts['aria-invalid'] = $validation_error ? 'true' : 'false';
$value = (string) reset( $tag->values );
if ( $tag->has_option( 'placeholder' )
or $tag->has_option( 'watermark' ) ) {
$atts['placeholder'] = $value;
$value = '';
}
$atts['name'] = $tag->name;
$atts['type'] = "text";
$atts['bind'] = $tag->get_option('bind', 'class', true);
$atts = wpcf7_format_atts( $atts );
$html = sprintf(
'<span class="wpcf7-form-control-wrap validcode %1$s"><input autocomplete="off" id="validcode_input" %2$s />%3$s</span><div id="validcode_image"><img src="/?get_validatecode=get_validatecode" /></div>',
sanitize_html_class( $tag->name ), $atts, $validation_error
);
$html.= '
<script>
jQuery(function($){
$("#validcode_input").focus(function(){
$("#validcode_image img").click()
})
$("#validcode_image img").click(function(){
$(this).attr("src","/?get_validatecode=get_validatecode&time='.time().'")
})
});
</script>
';
return $html;
}
add_filter( 'wpcf7_validate_validcode', "juxin_validcode_validation_filter", 10, 2 );
add_filter( 'wpcf7_validate_validcode*', "juxin_validcode_validation_filter", 10, 2 );
function juxin_validcode_validation_filter( $result, $tag ) {
$name = $tag->name;
$value = isset( $_POST[$name] )
? trim( wp_unslash( strtr( (string) $_POST[$name], "\n", " " ) ) )
: '';
if ( $value === '' ) {
$result->invalidate( $tag, "请输入验证码!" );
} else {
if (strcmp($value,$_SESSION['custom_validcode']) <> 0 ) {
$result->invalidate( $tag, '验证码错误' );
}
}
return $result;
}
add_action('init', function(){
session_start();
if( $_GET['get_validatecode'] == 'get_validatecode') {
$ValidateCode = new ValidateCode();
$ValidateCode->doimg();
$_SESSION['custom_validcode'] = $ValidateCode->getCode();
die;
} else if ($_GET['eeee'] == 'eeee') {
echo $_SESSION['custom_validcode'];
die;
}
});
//验证码类
class ValidateCode {
private $charset = '*+?abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789'; //随机因子
private $code; //验证码
private $codelen = 6; //验证码长度
private $width = 200; //宽度
private $height = 50; //高度
private $img; //图形资源句柄
private $font; //指定的字体
private $fontsize = 20; //指定字体大小
private $fontcolor; //指定字体颜色
//构造方法初始化
public function __construct() {
$this->font = WP_CONTENT_DIR.'/uploads/Fonts/elephant.ttf';
}
//生成随机码
private function createCode() {
$_len = strlen($this->charset)-1;
for ($i=0;$i<$this->codelen;$i++) {
$this->code .= $this->charset[mt_rand(0,$_len)];
}
}
//生成背景
private function createBg() {
$this->img = imagecreatetruecolor($this->width, $this->height);
$color = imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
}
//生成文字
private function createFont() {
$_x = $this->width / $this->codelen;
for ($i=0;$i<$this->codelen;$i++) {
$this->fontcolor = imagecolorallocate($this->img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,$this->code[$i]);
}
}
//生成线条、雪花
private function createLine() {
for ($i=0;$i<6;$i++) {
$color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
for ($i=0;$i<100;$i++) {
$color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
}
}
//输出
private function outPut() {
header('Content-type:image/png');
imagepng($this->img);
imagedestroy($this->img);
}
//对外生成
public function doimg() {
$this->createBg();
$this->createCode();
$this->createLine();
$this->createFont();
$this->outPut();
}
//获取验证码
public function getCode() {
// return strtolower($this->code);
return $this->code;
}
}