akismet
2 years ago
constant-contact
3 years ago
recaptcha
2 years ago
sendinblue
2 years ago
stripe
2 years ago
acceptance.php
3 years ago
checkbox.php
3 years ago
count.php
3 years ago
date.php
3 years ago
disallowed-list.php
4 years ago
doi-helper.php
4 years ago
file.php
3 years ago
flamingo.php
3 years ago
hidden.php
7 years ago
listo.php
3 years ago
number.php
3 years ago
quiz.php
4 years ago
really-simple-captcha.php
3 years ago
reflection.php
3 years ago
response.php
6 years ago
select.php
3 years ago
submit.php
4 years ago
text.php
3 years ago
textarea.php
3 years ago
really-simple-captcha.php
704 lines
| 1 | <?php |
| 2 | /** |
| 3 | ** A base module for [captchac] and [captchar] |
| 4 | **/ |
| 5 | |
| 6 | /* form_tag handler */ |
| 7 | |
| 8 | add_action( 'wpcf7_init', 'wpcf7_add_form_tag_captcha', 10, 0 ); |
| 9 | |
| 10 | function wpcf7_add_form_tag_captcha() { |
| 11 | // CAPTCHA-Challenge (image) |
| 12 | wpcf7_add_form_tag( 'captchac', |
| 13 | 'wpcf7_captchac_form_tag_handler', |
| 14 | array( |
| 15 | 'name-attr' => true, |
| 16 | 'zero-controls-container' => true, |
| 17 | 'not-for-mail' => true, |
| 18 | ) |
| 19 | ); |
| 20 | |
| 21 | // CAPTCHA-Response (input) |
| 22 | wpcf7_add_form_tag( 'captchar', |
| 23 | 'wpcf7_captchar_form_tag_handler', |
| 24 | array( |
| 25 | 'name-attr' => true, |
| 26 | 'do-not-store' => true, |
| 27 | 'not-for-mail' => true, |
| 28 | ) |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | function wpcf7_captchac_form_tag_handler( $tag ) { |
| 33 | if ( ! class_exists( 'ReallySimpleCaptcha' ) ) { |
| 34 | $error = sprintf( |
| 35 | /* translators: %s: link labeled 'Really Simple CAPTCHA' */ |
| 36 | esc_html( __( "To use CAPTCHA, you need %s plugin installed.", 'contact-form-7' ) ), |
| 37 | wpcf7_link( 'https://wordpress.org/plugins/really-simple-captcha/', 'Really Simple CAPTCHA' ) |
| 38 | ); |
| 39 | |
| 40 | return sprintf( '<em>%s</em>', $error ); |
| 41 | } |
| 42 | |
| 43 | if ( empty( $tag->name ) ) { |
| 44 | return ''; |
| 45 | } |
| 46 | |
| 47 | $class = wpcf7_form_controls_class( $tag->type ); |
| 48 | $class .= ' wpcf7-captcha-' . str_replace( ':', '', $tag->name ); |
| 49 | |
| 50 | $atts = array(); |
| 51 | $atts['class'] = $tag->get_class_option( $class ); |
| 52 | $atts['id'] = $tag->get_id_option(); |
| 53 | |
| 54 | $op = array( // Default |
| 55 | 'img_size' => array( 72, 24 ), |
| 56 | 'base' => array( 6, 18 ), |
| 57 | 'font_size' => 14, |
| 58 | 'font_char_width' => 15, |
| 59 | ); |
| 60 | |
| 61 | $op = array_merge( $op, wpcf7_captchac_options( $tag->options ) ); |
| 62 | |
| 63 | if ( ! $filename = wpcf7_generate_captcha( $op ) ) { |
| 64 | return ''; |
| 65 | } |
| 66 | |
| 67 | if ( ! empty( $op['img_size'] ) ) { |
| 68 | if ( isset( $op['img_size'][0] ) ) { |
| 69 | $atts['width'] = $op['img_size'][0]; |
| 70 | } |
| 71 | |
| 72 | if ( isset( $op['img_size'][1] ) ) { |
| 73 | $atts['height'] = $op['img_size'][1]; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | $atts['alt'] = 'captcha'; |
| 78 | $atts['src'] = wpcf7_captcha_url( $filename ); |
| 79 | |
| 80 | $atts = wpcf7_format_atts( $atts ); |
| 81 | |
| 82 | $prefix = substr( $filename, 0, strrpos( $filename, '.' ) ); |
| 83 | |
| 84 | $html = sprintf( |
| 85 | '<input type="hidden" name="%1$s" value="%2$s" /><img %3$s />', |
| 86 | esc_attr( sprintf( '_wpcf7_captcha_challenge_%s', $tag->name ) ), |
| 87 | esc_attr( $prefix ), |
| 88 | $atts |
| 89 | ); |
| 90 | |
| 91 | return $html; |
| 92 | } |
| 93 | |
| 94 | function wpcf7_captchar_form_tag_handler( $tag ) { |
| 95 | if ( empty( $tag->name ) ) { |
| 96 | return ''; |
| 97 | } |
| 98 | |
| 99 | $validation_error = wpcf7_get_validation_error( $tag->name ); |
| 100 | |
| 101 | $class = wpcf7_form_controls_class( $tag->type ); |
| 102 | |
| 103 | if ( $validation_error ) { |
| 104 | $class .= ' wpcf7-not-valid'; |
| 105 | } |
| 106 | |
| 107 | $atts = array(); |
| 108 | |
| 109 | $atts['size'] = $tag->get_size_option( '40' ); |
| 110 | $atts['maxlength'] = $tag->get_maxlength_option(); |
| 111 | $atts['minlength'] = $tag->get_minlength_option(); |
| 112 | |
| 113 | if ( $atts['maxlength'] and $atts['minlength'] |
| 114 | and $atts['maxlength'] < $atts['minlength'] ) { |
| 115 | unset( $atts['maxlength'], $atts['minlength'] ); |
| 116 | } |
| 117 | |
| 118 | $atts['class'] = $tag->get_class_option( $class ); |
| 119 | $atts['id'] = $tag->get_id_option(); |
| 120 | $atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true ); |
| 121 | $atts['autocomplete'] = 'off'; |
| 122 | |
| 123 | if ( $validation_error ) { |
| 124 | $atts['aria-invalid'] = 'true'; |
| 125 | $atts['aria-describedby'] = wpcf7_get_validation_error_reference( |
| 126 | $tag->name |
| 127 | ); |
| 128 | } else { |
| 129 | $atts['aria-invalid'] = 'false'; |
| 130 | } |
| 131 | |
| 132 | $value = (string) reset( $tag->values ); |
| 133 | |
| 134 | if ( wpcf7_is_posted() ) { |
| 135 | $value = ''; |
| 136 | } |
| 137 | |
| 138 | if ( $tag->has_option( 'placeholder' ) |
| 139 | or $tag->has_option( 'watermark' ) ) { |
| 140 | $atts['placeholder'] = $value; |
| 141 | $value = ''; |
| 142 | } |
| 143 | |
| 144 | $atts['value'] = $value; |
| 145 | $atts['type'] = 'text'; |
| 146 | $atts['name'] = $tag->name; |
| 147 | |
| 148 | $html = sprintf( |
| 149 | '<span class="wpcf7-form-control-wrap" data-name="%1$s"><input %2$s />%3$s</span>', |
| 150 | esc_attr( $tag->name ), |
| 151 | wpcf7_format_atts( $atts ), |
| 152 | $validation_error |
| 153 | ); |
| 154 | |
| 155 | return $html; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /* Validation filter */ |
| 160 | |
| 161 | add_filter( 'wpcf7_validate_captchar', |
| 162 | 'wpcf7_captcha_validation_filter', 10, 2 ); |
| 163 | |
| 164 | function wpcf7_captcha_validation_filter( $result, $tag ) { |
| 165 | $type = $tag->type; |
| 166 | $name = $tag->name; |
| 167 | |
| 168 | $captchac = '_wpcf7_captcha_challenge_' . $name; |
| 169 | |
| 170 | $prefix = isset( $_POST[$captchac] ) ? (string) $_POST[$captchac] : ''; |
| 171 | $response = isset( $_POST[$name] ) ? (string) $_POST[$name] : ''; |
| 172 | $response = wpcf7_canonicalize( $response ); |
| 173 | |
| 174 | if ( 0 === strlen( $prefix ) |
| 175 | or ! wpcf7_check_captcha( $prefix, $response ) ) { |
| 176 | $result->invalidate( $tag, wpcf7_get_message( 'captcha_not_match' ) ); |
| 177 | } |
| 178 | |
| 179 | if ( 0 !== strlen( $prefix ) ) { |
| 180 | wpcf7_remove_captcha( $prefix ); |
| 181 | } |
| 182 | |
| 183 | return $result; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /* Ajax echo filter */ |
| 188 | |
| 189 | add_filter( 'wpcf7_refill_response', 'wpcf7_captcha_ajax_refill', 10, 1 ); |
| 190 | add_filter( 'wpcf7_feedback_response', 'wpcf7_captcha_ajax_refill', 10, 1 ); |
| 191 | |
| 192 | function wpcf7_captcha_ajax_refill( $items ) { |
| 193 | if ( ! is_array( $items ) ) { |
| 194 | return $items; |
| 195 | } |
| 196 | |
| 197 | $tags = wpcf7_scan_form_tags( array( 'type' => 'captchac' ) ); |
| 198 | |
| 199 | if ( empty( $tags ) ) { |
| 200 | return $items; |
| 201 | } |
| 202 | |
| 203 | $refill = array(); |
| 204 | |
| 205 | foreach ( $tags as $tag ) { |
| 206 | $name = $tag->name; |
| 207 | $options = $tag->options; |
| 208 | |
| 209 | if ( empty( $name ) ) { |
| 210 | continue; |
| 211 | } |
| 212 | |
| 213 | $op = wpcf7_captchac_options( $options ); |
| 214 | |
| 215 | if ( $filename = wpcf7_generate_captcha( $op ) ) { |
| 216 | $captcha_url = wpcf7_captcha_url( $filename ); |
| 217 | $refill[$name] = $captcha_url; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | if ( ! empty( $refill ) ) { |
| 222 | $items['captcha'] = $refill; |
| 223 | } |
| 224 | |
| 225 | return $items; |
| 226 | } |
| 227 | |
| 228 | |
| 229 | /* Messages */ |
| 230 | |
| 231 | add_filter( 'wpcf7_messages', 'wpcf7_captcha_messages', 10, 1 ); |
| 232 | |
| 233 | function wpcf7_captcha_messages( $messages ) { |
| 234 | $messages = array_merge( $messages, array( |
| 235 | 'captcha_not_match' => array( |
| 236 | 'description' => |
| 237 | __( "The code that sender entered does not match the CAPTCHA", 'contact-form-7' ), |
| 238 | 'default' => |
| 239 | __( 'Your entered code is incorrect.', 'contact-form-7' ), |
| 240 | ), |
| 241 | ) ); |
| 242 | |
| 243 | return $messages; |
| 244 | } |
| 245 | |
| 246 | |
| 247 | /* Tag generator */ |
| 248 | |
| 249 | add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_captcha', 46, 0 ); |
| 250 | |
| 251 | function wpcf7_add_tag_generator_captcha() { |
| 252 | if ( ! wpcf7_use_really_simple_captcha() ) { |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | $tag_generator = WPCF7_TagGenerator::get_instance(); |
| 257 | $tag_generator->add( 'captcha', |
| 258 | __( 'CAPTCHA (Really Simple CAPTCHA)', 'contact-form-7' ), |
| 259 | 'wpcf7_tag_generator_captcha' ); |
| 260 | } |
| 261 | |
| 262 | function wpcf7_tag_generator_captcha( $contact_form, $args = '' ) { |
| 263 | $args = wp_parse_args( $args, array() ); |
| 264 | |
| 265 | if ( ! class_exists( 'ReallySimpleCaptcha' ) ) { |
| 266 | ?> |
| 267 | <div class="control-box"> |
| 268 | <fieldset> |
| 269 | <legend><?php |
| 270 | echo sprintf( |
| 271 | /* translators: %s: link labeled 'Really Simple CAPTCHA' */ |
| 272 | esc_html( __( "To use CAPTCHA, you first need to install and activate %s plugin.", 'contact-form-7' ) ), |
| 273 | wpcf7_link( 'https://wordpress.org/plugins/really-simple-captcha/', 'Really Simple CAPTCHA' ) |
| 274 | ); |
| 275 | ?></legend> |
| 276 | </fieldset> |
| 277 | </div> |
| 278 | <?php |
| 279 | |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | $description = __( "Generate form-tags for a CAPTCHA image and corresponding response input field. For more details, see %s.", 'contact-form-7' ); |
| 284 | |
| 285 | $desc_link = wpcf7_link( __( 'https://contactform7.com/captcha/', 'contact-form-7' ), __( 'CAPTCHA', 'contact-form-7' ) ); |
| 286 | |
| 287 | ?> |
| 288 | <div class="control-box"> |
| 289 | <fieldset> |
| 290 | <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend> |
| 291 | |
| 292 | <table class="form-table"> |
| 293 | <tbody> |
| 294 | <tr> |
| 295 | <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th> |
| 296 | <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td> |
| 297 | </tr> |
| 298 | </tbody> |
| 299 | </table> |
| 300 | |
| 301 | <table class="form-table scope captchac"> |
| 302 | <caption><?php echo esc_html( __( "Image settings", 'contact-form-7' ) ); ?></caption> |
| 303 | <tbody> |
| 304 | <tr> |
| 305 | <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-captchac-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th> |
| 306 | <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-captchac-id' ); ?>" /></td> |
| 307 | </tr> |
| 308 | |
| 309 | <tr> |
| 310 | <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-captchac-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th> |
| 311 | <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-captchac-class' ); ?>" /></td> |
| 312 | </tr> |
| 313 | </tbody> |
| 314 | </table> |
| 315 | |
| 316 | <table class="form-table scope captchar"> |
| 317 | <caption><?php echo esc_html( __( "Input field settings", 'contact-form-7' ) ); ?></caption> |
| 318 | <tbody> |
| 319 | <tr> |
| 320 | <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-captchar-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th> |
| 321 | <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-captchar-id' ); ?>" /></td> |
| 322 | </tr> |
| 323 | |
| 324 | <tr> |
| 325 | <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-captchar-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th> |
| 326 | <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-captchar-class' ); ?>" /></td> |
| 327 | </tr> |
| 328 | </tbody> |
| 329 | </table> |
| 330 | </fieldset> |
| 331 | </div> |
| 332 | |
| 333 | <div class="insert-box"> |
| 334 | <input type="text" name="captcha" class="tag code" readonly="readonly" onfocus="this.select()" /> |
| 335 | |
| 336 | <div class="submitbox"> |
| 337 | <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" /> |
| 338 | </div> |
| 339 | </div> |
| 340 | <?php |
| 341 | } |
| 342 | |
| 343 | |
| 344 | /* Warning message */ |
| 345 | |
| 346 | add_action( 'wpcf7_admin_warnings', |
| 347 | 'wpcf7_captcha_display_warning_message', 10, 3 ); |
| 348 | |
| 349 | function wpcf7_captcha_display_warning_message( $page, $action, $object ) { |
| 350 | if ( $object instanceof WPCF7_ContactForm ) { |
| 351 | $contact_form = $object; |
| 352 | } else { |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | $has_tags = (bool) $contact_form->scan_form_tags( |
| 357 | array( 'type' => array( 'captchac' ) ) ); |
| 358 | |
| 359 | if ( ! $has_tags ) { |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | if ( ! class_exists( 'ReallySimpleCaptcha' ) ) { |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | $uploads_dir = wpcf7_captcha_tmp_dir(); |
| 368 | wpcf7_init_captcha(); |
| 369 | |
| 370 | if ( ! is_dir( $uploads_dir ) |
| 371 | or ! wp_is_writable( $uploads_dir ) ) { |
| 372 | $message = sprintf( __( 'This contact form contains CAPTCHA fields, but the temporary folder for the files (%s) does not exist or is not writable. You can create the folder or change its permission manually.', 'contact-form-7' ), $uploads_dir ); |
| 373 | |
| 374 | echo '<div class="notice notice-warning"><p>' . esc_html( $message ) . '</p></div>'; |
| 375 | } |
| 376 | |
| 377 | if ( ! function_exists( 'imagecreatetruecolor' ) |
| 378 | or ! function_exists( 'imagettftext' ) ) { |
| 379 | $message = __( "This contact form contains CAPTCHA fields, but the necessary libraries (GD and FreeType) are not available on your server.", 'contact-form-7' ); |
| 380 | |
| 381 | echo '<div class="notice notice-warning"><p>' . esc_html( $message ) . '</p></div>'; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | |
| 386 | /* CAPTCHA functions */ |
| 387 | |
| 388 | function wpcf7_init_captcha() { |
| 389 | static $captcha = null; |
| 390 | |
| 391 | if ( $captcha ) { |
| 392 | return $captcha; |
| 393 | } |
| 394 | |
| 395 | if ( class_exists( 'ReallySimpleCaptcha' ) ) { |
| 396 | $captcha = new ReallySimpleCaptcha(); |
| 397 | } else { |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | $dir = trailingslashit( wpcf7_captcha_tmp_dir() ); |
| 402 | |
| 403 | $captcha->tmp_dir = $dir; |
| 404 | |
| 405 | if ( is_callable( array( $captcha, 'make_tmp_dir' ) ) ) { |
| 406 | $result = $captcha->make_tmp_dir(); |
| 407 | |
| 408 | if ( ! $result ) { |
| 409 | return false; |
| 410 | } |
| 411 | |
| 412 | return $captcha; |
| 413 | } |
| 414 | |
| 415 | $result = wp_mkdir_p( $dir ); |
| 416 | |
| 417 | if ( ! $result ) { |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | $htaccess_file = path_join( $dir, '.htaccess' ); |
| 422 | |
| 423 | if ( file_exists( $htaccess_file ) ) { |
| 424 | list( $first_line_comment ) = (array) file( |
| 425 | $htaccess_file, |
| 426 | FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES |
| 427 | ); |
| 428 | |
| 429 | if ( '# Apache 2.4+' === $first_line_comment ) { |
| 430 | return $captcha; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | if ( $handle = @fopen( $htaccess_file, 'w' ) ) { |
| 435 | fwrite( $handle, "# Apache 2.4+\n" ); |
| 436 | fwrite( $handle, "<IfModule authz_core_module>\n" ); |
| 437 | fwrite( $handle, " Require all denied\n" ); |
| 438 | fwrite( $handle, ' <FilesMatch "^\w+\.(jpe?g|gif|png)$">' . "\n" ); |
| 439 | fwrite( $handle, " Require all granted\n" ); |
| 440 | fwrite( $handle, " </FilesMatch>\n" ); |
| 441 | fwrite( $handle, "</IfModule>\n" ); |
| 442 | fwrite( $handle, "\n" ); |
| 443 | fwrite( $handle, "# Apache 2.2\n" ); |
| 444 | fwrite( $handle, "<IfModule !authz_core_module>\n" ); |
| 445 | fwrite( $handle, " Order deny,allow\n" ); |
| 446 | fwrite( $handle, " Deny from all\n" ); |
| 447 | fwrite( $handle, ' <FilesMatch "^\w+\.(jpe?g|gif|png)$">' . "\n" ); |
| 448 | fwrite( $handle, " Allow from all\n" ); |
| 449 | fwrite( $handle, " </FilesMatch>\n" ); |
| 450 | fwrite( $handle, "</IfModule>\n" ); |
| 451 | |
| 452 | fclose( $handle ); |
| 453 | } |
| 454 | |
| 455 | return $captcha; |
| 456 | } |
| 457 | |
| 458 | |
| 459 | /** |
| 460 | * Returns the directory path for Really Simple CAPTCHA files. |
| 461 | * |
| 462 | * @return string Directory path. |
| 463 | */ |
| 464 | function wpcf7_captcha_tmp_dir() { |
| 465 | if ( defined( 'WPCF7_CAPTCHA_TMP_DIR' ) ) { |
| 466 | $dir = path_join( WP_CONTENT_DIR, WPCF7_CAPTCHA_TMP_DIR ); |
| 467 | wp_mkdir_p( $dir ); |
| 468 | |
| 469 | if ( wpcf7_is_file_path_in_content_dir( $dir ) ) { |
| 470 | return $dir; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | $dir = path_join( wpcf7_upload_dir( 'dir' ), 'wpcf7_captcha' ); |
| 475 | wp_mkdir_p( $dir ); |
| 476 | return $dir; |
| 477 | } |
| 478 | |
| 479 | |
| 480 | function wpcf7_captcha_tmp_url() { |
| 481 | if ( defined( 'WPCF7_CAPTCHA_TMP_URL' ) ) { |
| 482 | return WPCF7_CAPTCHA_TMP_URL; |
| 483 | } else { |
| 484 | return path_join( wpcf7_upload_dir( 'url' ), 'wpcf7_captcha' ); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | function wpcf7_captcha_url( $filename ) { |
| 489 | $url = path_join( wpcf7_captcha_tmp_url(), $filename ); |
| 490 | |
| 491 | if ( is_ssl() |
| 492 | and 'http:' == substr( $url, 0, 5 ) ) { |
| 493 | $url = 'https:' . substr( $url, 5 ); |
| 494 | } |
| 495 | |
| 496 | return apply_filters( 'wpcf7_captcha_url', sanitize_url( $url ) ); |
| 497 | } |
| 498 | |
| 499 | function wpcf7_generate_captcha( $options = null ) { |
| 500 | if ( ! $captcha = wpcf7_init_captcha() ) { |
| 501 | return false; |
| 502 | } |
| 503 | |
| 504 | if ( ! is_dir( $captcha->tmp_dir ) |
| 505 | or ! wp_is_writable( $captcha->tmp_dir ) ) { |
| 506 | return false; |
| 507 | } |
| 508 | |
| 509 | $img_type = imagetypes(); |
| 510 | |
| 511 | if ( $img_type & IMG_PNG ) { |
| 512 | $captcha->img_type = 'png'; |
| 513 | } elseif ( $img_type & IMG_GIF ) { |
| 514 | $captcha->img_type = 'gif'; |
| 515 | } elseif ( $img_type & IMG_JPG ) { |
| 516 | $captcha->img_type = 'jpeg'; |
| 517 | } else { |
| 518 | return false; |
| 519 | } |
| 520 | |
| 521 | if ( is_array( $options ) ) { |
| 522 | if ( isset( $options['img_size'] ) ) { |
| 523 | $captcha->img_size = $options['img_size']; |
| 524 | } |
| 525 | |
| 526 | if ( isset( $options['base'] ) ) { |
| 527 | $captcha->base = $options['base']; |
| 528 | } |
| 529 | |
| 530 | if ( isset( $options['font_size'] ) ) { |
| 531 | $captcha->font_size = $options['font_size']; |
| 532 | } |
| 533 | |
| 534 | if ( isset( $options['font_char_width'] ) ) { |
| 535 | $captcha->font_char_width = $options['font_char_width']; |
| 536 | } |
| 537 | |
| 538 | if ( isset( $options['fg'] ) ) { |
| 539 | $captcha->fg = $options['fg']; |
| 540 | } |
| 541 | |
| 542 | if ( isset( $options['bg'] ) ) { |
| 543 | $captcha->bg = $options['bg']; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | $prefix = wp_rand(); |
| 548 | $captcha_word = $captcha->generate_random_word(); |
| 549 | return $captcha->generate_image( $prefix, $captcha_word ); |
| 550 | } |
| 551 | |
| 552 | function wpcf7_check_captcha( $prefix, $response ) { |
| 553 | if ( ! $captcha = wpcf7_init_captcha() ) { |
| 554 | return false; |
| 555 | } |
| 556 | |
| 557 | return $captcha->check( $prefix, $response ); |
| 558 | } |
| 559 | |
| 560 | function wpcf7_remove_captcha( $prefix ) { |
| 561 | if ( ! $captcha = wpcf7_init_captcha() ) { |
| 562 | return false; |
| 563 | } |
| 564 | |
| 565 | // Contact Form 7 generates $prefix with wp_rand() |
| 566 | if ( preg_match( '/[^0-9]/', $prefix ) ) { |
| 567 | return false; |
| 568 | } |
| 569 | |
| 570 | $captcha->remove( $prefix ); |
| 571 | } |
| 572 | |
| 573 | add_action( 'shutdown', 'wpcf7_cleanup_captcha_files', 20, 0 ); |
| 574 | |
| 575 | function wpcf7_cleanup_captcha_files() { |
| 576 | if ( ! $captcha = wpcf7_init_captcha() ) { |
| 577 | return false; |
| 578 | } |
| 579 | |
| 580 | if ( is_callable( array( $captcha, 'cleanup' ) ) ) { |
| 581 | return $captcha->cleanup(); |
| 582 | } |
| 583 | |
| 584 | $dir = trailingslashit( wpcf7_captcha_tmp_dir() ); |
| 585 | |
| 586 | if ( ! is_dir( $dir ) |
| 587 | or ! is_readable( $dir ) |
| 588 | or ! wp_is_writable( $dir ) ) { |
| 589 | return false; |
| 590 | } |
| 591 | |
| 592 | if ( $handle = opendir( $dir ) ) { |
| 593 | while ( false !== ( $file = readdir( $handle ) ) ) { |
| 594 | if ( ! preg_match( '/^[0-9]+\.(php|txt|png|gif|jpeg)$/', $file ) ) { |
| 595 | continue; |
| 596 | } |
| 597 | |
| 598 | $stat = stat( path_join( $dir, $file ) ); |
| 599 | |
| 600 | if ( $stat['mtime'] + HOUR_IN_SECONDS < time() ) { |
| 601 | @unlink( path_join( $dir, $file ) ); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | closedir( $handle ); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | function wpcf7_captchac_options( $options ) { |
| 610 | if ( ! is_array( $options ) ) { |
| 611 | return array(); |
| 612 | } |
| 613 | |
| 614 | $op = array(); |
| 615 | $image_size_array = preg_grep( '%^size:[smlSML]$%', $options ); |
| 616 | |
| 617 | if ( $image_size = array_shift( $image_size_array ) ) { |
| 618 | preg_match( '%^size:([smlSML])$%', $image_size, $is_matches ); |
| 619 | |
| 620 | switch ( strtolower( $is_matches[1] ) ) { |
| 621 | case 's': |
| 622 | $op['img_size'] = array( 60, 20 ); |
| 623 | $op['base'] = array( 6, 15 ); |
| 624 | $op['font_size'] = 11; |
| 625 | $op['font_char_width'] = 13; |
| 626 | break; |
| 627 | case 'l': |
| 628 | $op['img_size'] = array( 84, 28 ); |
| 629 | $op['base'] = array( 6, 20 ); |
| 630 | $op['font_size'] = 17; |
| 631 | $op['font_char_width'] = 19; |
| 632 | break; |
| 633 | case 'm': |
| 634 | default: |
| 635 | $op['img_size'] = array( 72, 24 ); |
| 636 | $op['base'] = array( 6, 18 ); |
| 637 | $op['font_size'] = 14; |
| 638 | $op['font_char_width'] = 15; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | $fg_color_array = preg_grep( |
| 643 | '%^fg:#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$%', $options ); |
| 644 | |
| 645 | if ( $fg_color = array_shift( $fg_color_array ) ) { |
| 646 | preg_match( '%^fg:#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$%', |
| 647 | $fg_color, $fc_matches ); |
| 648 | |
| 649 | if ( 3 == strlen( $fc_matches[1] ) ) { |
| 650 | $r = substr( $fc_matches[1], 0, 1 ); |
| 651 | $g = substr( $fc_matches[1], 1, 1 ); |
| 652 | $b = substr( $fc_matches[1], 2, 1 ); |
| 653 | |
| 654 | $op['fg'] = array( |
| 655 | hexdec( $r . $r ), |
| 656 | hexdec( $g . $g ), |
| 657 | hexdec( $b . $b ), |
| 658 | ); |
| 659 | } elseif ( 6 == strlen( $fc_matches[1] ) ) { |
| 660 | $r = substr( $fc_matches[1], 0, 2 ); |
| 661 | $g = substr( $fc_matches[1], 2, 2 ); |
| 662 | $b = substr( $fc_matches[1], 4, 2 ); |
| 663 | |
| 664 | $op['fg'] = array( |
| 665 | hexdec( $r ), |
| 666 | hexdec( $g ), |
| 667 | hexdec( $b ), |
| 668 | ); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | $bg_color_array = preg_grep( |
| 673 | '%^bg:#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$%', $options ); |
| 674 | |
| 675 | if ( $bg_color = array_shift( $bg_color_array ) ) { |
| 676 | preg_match( '%^bg:#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$%', |
| 677 | $bg_color, $bc_matches ); |
| 678 | |
| 679 | if ( 3 == strlen( $bc_matches[1] ) ) { |
| 680 | $r = substr( $bc_matches[1], 0, 1 ); |
| 681 | $g = substr( $bc_matches[1], 1, 1 ); |
| 682 | $b = substr( $bc_matches[1], 2, 1 ); |
| 683 | |
| 684 | $op['bg'] = array( |
| 685 | hexdec( $r . $r ), |
| 686 | hexdec( $g . $g ), |
| 687 | hexdec( $b . $b ), |
| 688 | ); |
| 689 | } elseif ( 6 == strlen( $bc_matches[1] ) ) { |
| 690 | $r = substr( $bc_matches[1], 0, 2 ); |
| 691 | $g = substr( $bc_matches[1], 2, 2 ); |
| 692 | $b = substr( $bc_matches[1], 4, 2 ); |
| 693 | |
| 694 | $op['bg'] = array( |
| 695 | hexdec( $r ), |
| 696 | hexdec( $g ), |
| 697 | hexdec( $b ), |
| 698 | ); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | return $op; |
| 703 | } |
| 704 |