source: trunk/www.guidonia.net/wp/wp-content/plugins/contact-form-7/modules/captcha.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 9.1 KB
Line 
1<?php
2/**
3** A base module for [captchac] and [captchar]
4**/
5
6/* Shortcode handler */
7
8function wpcf7_captcha_shortcode_handler( $tag ) {
9 global $wpcf7_contact_form;
10
11 if ( ! is_array( $tag ) )
12 return '';
13
14 $type = $tag['type'];
15 $name = $tag['name'];
16 $options = (array) $tag['options'];
17 $values = (array) $tag['values'];
18
19 if ( empty( $name ) )
20 return '';
21
22 $validation_error = '';
23 if ( is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) )
24 $validation_error = $wpcf7_contact_form->validation_error( $name );
25
26 $atts = '';
27 $id_att = '';
28 $class_att = '';
29 $size_att = '';
30 $maxlength_att = '';
31
32 if ( 'captchac' == $type )
33 $class_att .= ' wpcf7-captcha-' . $name;
34
35 foreach ( $options as $option ) {
36 if ( preg_match( '%^id:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
37 $id_att = $matches[1];
38
39 } elseif ( preg_match( '%^class:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
40 $class_att .= ' ' . $matches[1];
41
42 } elseif ( preg_match( '%^([0-9]*)[/x]([0-9]*)$%', $option, $matches ) ) {
43 $size_att = (int) $matches[1];
44 $maxlength_att = (int) $matches[2];
45 }
46 }
47
48 if ( $id_att )
49 $atts .= ' id="' . trim( $id_att ) . '"';
50
51 if ( $class_att )
52 $atts .= ' class="' . trim( $class_att ) . '"';
53
54 // Value.
55 if ( is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) && $wpcf7_contact_form->is_posted() )
56 $value = '';
57 else
58 $value = $values[0];
59
60 if ( 'captchac' == $type ) {
61 if ( ! class_exists( 'ReallySimpleCaptcha' ) ) {
62 return '<em>' . __( 'To use CAPTCHA, you need <a href="http://wordpress.org/extend/plugins/really-simple-captcha/">Really Simple CAPTCHA</a> plugin installed.', 'wpcf7' ) . '</em>';
63 }
64
65 $op = array();
66 // Default
67 $op['img_size'] = array( 72, 24 );
68 $op['base'] = array( 6, 18 );
69 $op['font_size'] = 14;
70 $op['font_char_width'] = 15;
71
72 $op = array_merge( $op, wpcf7_captchac_options( $options ) );
73
74 if ( ! $filename = wpcf7_generate_captcha( $op ) )
75 return '';
76
77 if ( is_array( $op['img_size'] ) )
78 $atts .= ' width="' . $op['img_size'][0] . '" height="' . $op['img_size'][1] . '"';
79
80 $captcha_url = trailingslashit( wpcf7_captcha_tmp_url() ) . $filename;
81 $html = '<img alt="captcha" src="' . $captcha_url . '"' . $atts . ' />';
82 $ref = substr( $filename, 0, strrpos( $filename, '.' ) );
83 $html = '<input type="hidden" name="_wpcf7_captcha_challenge_' . $name . '" value="' . $ref . '" />' . $html;
84
85 return $html;
86
87 } elseif ( 'captchar' == $type ) {
88 if ( $size_att )
89 $atts .= ' size="' . $size_att . '"';
90 else
91 $atts .= ' size="40"'; // default size
92
93 if ( $maxlength_att )
94 $atts .= ' maxlength="' . $maxlength_att . '"';
95
96 $html = '<input type="text" name="' . $name . '" value="' . esc_attr( $value ) . '"' . $atts . ' />';
97 $html = '<span class="wpcf7-form-control-wrap ' . $name . '">' . $html . $validation_error . '</span>';
98
99 return $html;
100 }
101}
102
103wpcf7_add_shortcode( 'captchac', 'wpcf7_captcha_shortcode_handler', true );
104wpcf7_add_shortcode( 'captchar', 'wpcf7_captcha_shortcode_handler', true );
105
106
107/* Validation filter */
108
109function wpcf7_captcha_validation_filter( $result, $tag ) {
110 global $wpcf7_contact_form;
111
112 $type = $tag['type'];
113 $name = $tag['name'];
114
115 $_POST[$name] = (string) $_POST[$name];
116
117 $captchac = '_wpcf7_captcha_challenge_' . $name;
118
119 if ( ! wpcf7_check_captcha( $_POST[$captchac], $_POST[$name] ) ) {
120 $result['valid'] = false;
121 $result['reason'][$name] = $wpcf7_contact_form->message( 'captcha_not_match' );
122 }
123
124 wpcf7_remove_captcha( $_POST[$captchac] );
125
126 return $result;
127}
128
129add_filter( 'wpcf7_validate_captchar', 'wpcf7_captcha_validation_filter', 10, 2 );
130
131
132/* Ajax echo filter */
133
134function wpcf7_captcha_ajax_echo_filter( $items ) {
135 global $wpcf7_contact_form;
136
137 if ( ! is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) )
138 return $items;
139
140 if ( ! is_array( $items ) )
141 return $items;
142
143 $fes = $wpcf7_contact_form->form_scan_shortcode(
144 array( 'type' => 'captchac' ) );
145
146 if ( empty( $fes ) )
147 return $items;
148
149 $refill = array();
150
151 foreach ( $fes as $fe ) {
152 $name = $fe['name'];
153 $options = $fe['options'];
154
155 if ( empty( $name ) )
156 continue;
157
158 $op = wpcf7_captchac_options( $options );
159 if ( $filename = wpcf7_generate_captcha( $op ) ) {
160 $captcha_url = trailingslashit( wpcf7_captcha_tmp_url() ) . $filename;
161 $refill[$name] = $captcha_url;
162 }
163 }
164
165 if ( ! empty( $refill ) )
166 $items['captcha'] = $refill;
167
168 return $items;
169}
170
171add_filter( 'wpcf7_ajax_json_echo', 'wpcf7_captcha_ajax_echo_filter' );
172
173
174/* CAPTCHA functions */
175
176function wpcf7_init_captcha() {
177 global $wpcf7_captcha;
178
179 if ( ! class_exists( 'ReallySimpleCaptcha' ) )
180 return false;
181
182 if ( ! is_object( $wpcf7_captcha ) )
183 $wpcf7_captcha = new ReallySimpleCaptcha();
184 $captcha =& $wpcf7_captcha;
185
186 $captcha->tmp_dir = trailingslashit( wpcf7_captcha_tmp_dir() );
187 wp_mkdir_p( $captcha->tmp_dir );
188 return true;
189}
190
191function wpcf7_generate_captcha( $options = null ) {
192 global $wpcf7_captcha;
193
194 if ( ! wpcf7_init_captcha() )
195 return false;
196 $captcha =& $wpcf7_captcha;
197
198 if ( ! is_dir( $captcha->tmp_dir ) || ! is_writable( $captcha->tmp_dir ) )
199 return false;
200
201 $img_type = imagetypes();
202 if ( $img_type & IMG_PNG )
203 $captcha->img_type = 'png';
204 elseif ( $img_type & IMG_GIF )
205 $captcha->img_type = 'gif';
206 elseif ( $img_type & IMG_JPG )
207 $captcha->img_type = 'jpeg';
208 else
209 return false;
210
211 if ( is_array( $options ) ) {
212 if ( isset( $options['img_size'] ) )
213 $captcha->img_size = $options['img_size'];
214 if ( isset( $options['base'] ) )
215 $captcha->base = $options['base'];
216 if ( isset( $options['font_size'] ) )
217 $captcha->font_size = $options['font_size'];
218 if ( isset( $options['font_char_width'] ) )
219 $captcha->font_char_width = $options['font_char_width'];
220 if ( isset( $options['fg'] ) )
221 $captcha->fg = $options['fg'];
222 if ( isset( $options['bg'] ) )
223 $captcha->bg = $options['bg'];
224 }
225
226 $prefix = mt_rand();
227 $captcha_word = $captcha->generate_random_word();
228 return $captcha->generate_image( $prefix, $captcha_word );
229}
230
231function wpcf7_check_captcha( $prefix, $response ) {
232 global $wpcf7_captcha;
233
234 if ( ! wpcf7_init_captcha() )
235 return false;
236 $captcha =& $wpcf7_captcha;
237
238 return $captcha->check( $prefix, $response );
239}
240
241function wpcf7_remove_captcha( $prefix ) {
242 global $wpcf7_captcha;
243
244 if ( ! wpcf7_init_captcha() )
245 return false;
246 $captcha =& $wpcf7_captcha;
247
248 $captcha->remove( $prefix );
249}
250
251function wpcf7_cleanup_captcha_files() {
252 $dir = trailingslashit( wpcf7_captcha_tmp_dir() );
253
254 if ( ! is_dir( $dir ) )
255 return false;
256 if ( ! is_readable( $dir ) )
257 return false;
258 if ( ! is_writable( $dir ) )
259 return false;
260
261 if ( $handle = @opendir( $dir ) ) {
262 while ( false !== ( $file = readdir( $handle ) ) ) {
263 if ( ! preg_match( '/^[0-9]+\.(php|png|gif|jpeg)$/', $file ) )
264 continue;
265
266 $stat = stat( $dir . $file );
267 if ( $stat['mtime'] + 21600 < time() ) // 21600 secs == 6 hours
268 @unlink( $dir . $file );
269 }
270 closedir( $handle );
271 }
272}
273
274if ( ! is_admin() && 'GET' == $_SERVER['REQUEST_METHOD'] )
275 wpcf7_cleanup_captcha_files();
276
277function wpcf7_captchac_options( $options ) {
278 if ( ! is_array( $options ) )
279 return array();
280
281 $op = array();
282 $image_size_array = preg_grep( '%^size:[smlSML]$%', $options );
283
284 if ( $image_size = array_shift( $image_size_array ) ) {
285 preg_match( '%^size:([smlSML])$%', $image_size, $is_matches );
286 switch ( strtolower( $is_matches[1] ) ) {
287 case 's':
288 $op['img_size'] = array( 60, 20 );
289 $op['base'] = array( 6, 15 );
290 $op['font_size'] = 11;
291 $op['font_char_width'] = 13;
292 break;
293 case 'l':
294 $op['img_size'] = array( 84, 28 );
295 $op['base'] = array( 6, 20 );
296 $op['font_size'] = 17;
297 $op['font_char_width'] = 19;
298 break;
299 case 'm':
300 default:
301 $op['img_size'] = array( 72, 24 );
302 $op['base'] = array( 6, 18 );
303 $op['font_size'] = 14;
304 $op['font_char_width'] = 15;
305 }
306 }
307
308 $fg_color_array = preg_grep( '%^fg:#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$%', $options );
309 if ( $fg_color = array_shift( $fg_color_array ) ) {
310 preg_match( '%^fg:#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$%', $fg_color, $fc_matches );
311 if ( 3 == strlen( $fc_matches[1] ) ) {
312 $r = substr( $fc_matches[1], 0, 1 );
313 $g = substr( $fc_matches[1], 1, 1 );
314 $b = substr( $fc_matches[1], 2, 1 );
315 $op['fg'] = array( hexdec( $r . $r ), hexdec( $g . $g ), hexdec( $b . $b ) );
316 } elseif ( 6 == strlen( $fc_matches[1] ) ) {
317 $r = substr( $fc_matches[1], 0, 2 );
318 $g = substr( $fc_matches[1], 2, 2 );
319 $b = substr( $fc_matches[1], 4, 2 );
320 $op['fg'] = array( hexdec( $r ), hexdec( $g ), hexdec( $b ) );
321 }
322 }
323
324 $bg_color_array = preg_grep( '%^bg:#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$%', $options );
325 if ( $bg_color = array_shift( $bg_color_array ) ) {
326 preg_match( '%^bg:#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$%', $bg_color, $bc_matches );
327 if ( 3 == strlen( $bc_matches[1] ) ) {
328 $r = substr( $bc_matches[1], 0, 1 );
329 $g = substr( $bc_matches[1], 1, 1 );
330 $b = substr( $bc_matches[1], 2, 1 );
331 $op['bg'] = array( hexdec( $r . $r ), hexdec( $g . $g ), hexdec( $b . $b ) );
332 } elseif ( 6 == strlen( $bc_matches[1] ) ) {
333 $r = substr( $bc_matches[1], 0, 2 );
334 $g = substr( $bc_matches[1], 2, 2 );
335 $b = substr( $bc_matches[1], 4, 2 );
336 $op['bg'] = array( hexdec( $r ), hexdec( $g ), hexdec( $b ) );
337 }
338 }
339
340 return $op;
341}
342
343$wpcf7_captcha = null;
344
345?>
Note: See TracBrowser for help on using the repository browser.