PluginProbe
Really Simple CAPTCHA / 2.1
Really Simple CAPTCHA v2.1
trunk 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.8.0.1 1.9 2.0 2.0.1 2.0.2 2.1 2.3 2.4 2.5
really-simple-captcha / really-simple-captcha.php

really-simple-captcha.php in Really Simple CAPTCHA 2.1, at really-simple-captcha.php

336 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Really Simple CAPTCHA
4 Plugin URI: https://contactform7.com/captcha/
5 Description: Really Simple CAPTCHA is a CAPTCHA module intended to be called from other plugins. It is originally created for my Contact Form 7 plugin.
6 Author: Takayuki Miyoshi
7 Author URI: https://ideasilo.wordpress.com/
8 Text Domain: really-simple-captcha
9 Version: 2.1
10 */
11
12 define( 'REALLYSIMPLECAPTCHA_VERSION', '2.1' );
13
14 class ReallySimpleCaptcha {
15
16 public $chars;
17 public $char_length;
18 public $fonts;
19 public $tmp_dir;
20 public $img_size;
21 public $bg;
22 public $fg;
23 public $base;
24 public $font_size;
25 public $font_char_width;
26 public $img_type;
27 public $file_mode;
28 public $answer_file_mode;
29
30 public function __construct() {
31 /* Characters available in images */
32 $this->chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
33
34 /* Length of a word in an image */
35 $this->char_length = 4;
36
37 /* Array of fonts. Randomly picked up per character */
38 $this->fonts = array(
39 dirname( __FILE__ ) . '/gentium/GenBkBasR.ttf',
40 dirname( __FILE__ ) . '/gentium/GenBkBasI.ttf',
41 dirname( __FILE__ ) . '/gentium/GenBkBasBI.ttf',
42 dirname( __FILE__ ) . '/gentium/GenBkBasB.ttf',
43 );
44
45 /* Directory temporary keeping CAPTCHA images and corresponding text files */
46 $this->tmp_dir = path_join( dirname( __FILE__ ), 'tmp' );
47
48 /* Array of CAPTCHA image size. Width and height */
49 $this->img_size = array( 72, 24 );
50
51 /* Background color of CAPTCHA image. RGB color 0-255 */
52 $this->bg = array( 255, 255, 255 );
53
54 /* Foreground (character) color of CAPTCHA image. RGB color 0-255 */
55 $this->fg = array( 0, 0, 0 );
56
57 /* Coordinates for a text in an image. I don't know the meaning. Just adjust. */
58 $this->base = array( 6, 18 );
59
60 /* Font size */
61 $this->font_size = 14;
62
63 /* Width of a character */
64 $this->font_char_width = 15;
65
66 /* Image type. 'png', 'gif' or 'jpeg' */
67 $this->img_type = 'png';
68
69 /* Mode of temporary image files */
70 $this->file_mode = 0644;
71
72 /* Mode of temporary answer text files */
73 $this->answer_file_mode = 0640;
74 }
75
76 /**
77 * Generate and return a random word.
78 *
79 * @return string Random word with $chars characters x $char_length length
80 */
81 public function generate_random_word() {
82 $word = '';
83
84 for ( $i = 0; $i < $this->char_length; $i++ ) {
85 $pos = mt_rand( 0, strlen( $this->chars ) - 1 );
86 $char = $this->chars[$pos];
87 $word .= $char;
88 }
89
90 return $word;
91 }
92
93 /**
94 * Generate CAPTCHA image and corresponding answer file.
95 *
96 * @param string $prefix File prefix used for both files
97 * @param string $word Random word generated by generate_random_word()
98 * @return string|bool The file name of the CAPTCHA image. Return false if temp directory is not available.
99 */
100 public function generate_image( $prefix, $word ) {
101 if ( ! $this->make_tmp_dir() ) {
102 return false;
103 }
104
105 $this->cleanup();
106
107 $dir = trailingslashit( $this->tmp_dir );
108 $filename = null;
109
110 $im = imagecreatetruecolor(
111 $this->img_size[0],
112 $this->img_size[1]
113 );
114
115 if ( $im ) {
116 $bg = imagecolorallocate( $im, $this->bg[0], $this->bg[1], $this->bg[2] );
117 $fg = imagecolorallocate( $im, $this->fg[0], $this->fg[1], $this->fg[2] );
118
119 imagefill( $im, 0, 0, $bg );
120
121 $x = $this->base[0] + mt_rand( -2, 2 );
122
123 for ( $i = 0; $i < strlen( $word ); $i++ ) {
124 $font = $this->fonts[array_rand( $this->fonts )];
125 $font = $this->normalize_path( $font );
126
127 imagettftext( $im, $this->font_size, mt_rand( -12, 12 ), $x,
128 $this->base[1] + mt_rand( -2, 2 ), $fg, $font, $word[$i] );
129 $x += $this->font_char_width;
130 }
131
132 switch ( $this->img_type ) {
133 case 'jpeg':
134 $filename = sanitize_file_name( $prefix . '.jpeg' );
135 $file = $this->normalize_path( $dir . $filename );
136 imagejpeg( $im, $file );
137 break;
138 case 'gif':
139 $filename = sanitize_file_name( $prefix . '.gif' );
140 $file = $this->normalize_path( $dir . $filename );
141 imagegif( $im, $file );
142 break;
143 case 'png':
144 default:
145 $filename = sanitize_file_name( $prefix . '.png' );
146 $file = $this->normalize_path( $dir . $filename );
147 imagepng( $im, $file );
148 }
149
150 imagedestroy( $im );
151 @chmod( $file, $this->file_mode );
152 }
153
154 $this->generate_answer_file( $prefix, $word );
155
156 return $filename;
157 }
158
159 /**
160 * Generate answer file corresponding to CAPTCHA image.
161 *
162 * @param string $prefix File prefix used for answer file
163 * @param string $word Random word generated by generate_random_word()
164 */
165 public function generate_answer_file( $prefix, $word ) {
166 $dir = trailingslashit( $this->tmp_dir );
167 $answer_file = $dir . sanitize_file_name( $prefix . '.txt' );
168 $answer_file = $this->normalize_path( $answer_file );
169
170 if ( $fh = fopen( $answer_file, 'w' ) ) {
171 $word = strtoupper( $word );
172 $salt = wp_generate_password( 64 );
173 $hash = hash_hmac( 'md5', $word, $salt );
174 $code = $salt . '|' . $hash;
175 fwrite( $fh, $code );
176 fclose( $fh );
177 }
178
179 @chmod( $answer_file, $this->answer_file_mode );
180 }
181
182 /**
183 * Check a response against the code kept in the temporary file.
184 *
185 * @param string $prefix File prefix used for both files
186 * @param string $response CAPTCHA response
187 * @return bool Return true if the two match, otherwise return false.
188 */
189 public function check( $prefix, $response ) {
190 if ( 0 == strlen( $prefix ) ) {
191 return false;
192 }
193
194 $response = str_replace( array( " ", "\t" ), '', $response );
195 $response = strtoupper( $response );
196
197 $dir = trailingslashit( $this->tmp_dir );
198 $filename = sanitize_file_name( $prefix . '.txt' );
199 $file = $this->normalize_path( $dir . $filename );
200
201 if ( is_readable( $file )
202 and $code = file_get_contents( $file ) ) {
203 $code = explode( '|', $code, 2 );
204 $salt = $code[0];
205 $hash = $code[1];
206
207 if ( hash_equals( $hash, hash_hmac( 'md5', $response, $salt ) ) ) {
208 return true;
209 }
210 }
211
212 return false;
213 }
214
215 /**
216 * Remove temporary files with given prefix.
217 *
218 * @param string $prefix File prefix
219 */
220 public function remove( $prefix ) {
221 $dir = trailingslashit( $this->tmp_dir );
222 $suffixes = array( '.jpeg', '.gif', '.png', '.php', '.txt' );
223
224 foreach ( $suffixes as $suffix ) {
225 $filename = sanitize_file_name( $prefix . $suffix );
226 $file = $this->normalize_path( $dir . $filename );
227
228 if ( is_file( $file ) ) {
229 @unlink( $file );
230 }
231 }
232 }
233
234 /**
235 * Clean up dead files older than given length of time.
236 *
237 * @param int $minutes Consider older files than this time as dead files
238 * @return int|bool The number of removed files. Return false if error occurred.
239 */
240 public function cleanup( $minutes = 60, $max = 100 ) {
241 $dir = trailingslashit( $this->tmp_dir );
242 $dir = $this->normalize_path( $dir );
243
244 if ( ! is_dir( $dir )
245 or ! is_readable( $dir ) ) {
246 return false;
247 }
248
249 $is_win = ( 'WIN' === strtoupper( substr( PHP_OS, 0, 3 ) ) );
250
251 if ( ! ( $is_win ? win_is_writable( $dir ) : is_writable( $dir ) ) ) {
252 return false;
253 }
254
255 $count = 0;
256
257 if ( $handle = opendir( $dir ) ) {
258 while ( false !== ( $filename = readdir( $handle ) ) ) {
259 if ( ! preg_match( '/^[0-9]+\.(php|txt|png|gif|jpeg)$/', $filename ) ) {
260 continue;
261 }
262
263 $file = $this->normalize_path( $dir . $filename );
264
265 if ( ! file_exists( $file )
266 or ! $stat = stat( $file ) ) {
267 continue;
268 }
269
270 if ( ( $stat['mtime'] + $minutes * 60 ) < time() ) {
271 if ( ! @unlink( $file ) ) {
272 @chmod( $file, 0644 );
273 @unlink( $file );
274 }
275
276 $count += 1;
277 }
278
279 if ( $max <= $count ) {
280 break;
281 }
282 }
283
284 closedir( $handle );
285 }
286
287 return $count;
288 }
289
290 /**
291 * Make a temporary directory and generate .htaccess file in it.
292 *
293 * @return bool True on successful create, false on failure.
294 */
295 public function make_tmp_dir() {
296 $dir = trailingslashit( $this->tmp_dir );
297 $dir = $this->normalize_path( $dir );
298
299 if ( ! wp_mkdir_p( $dir ) ) {
300 return false;
301 }
302
303 $htaccess_file = $this->normalize_path( $dir . '.htaccess' );
304
305 if ( file_exists( $htaccess_file ) ) {
306 return true;
307 }
308
309 if ( $handle = fopen( $htaccess_file, 'w' ) ) {
310 fwrite( $handle, 'Order deny,allow' . "\n" );
311 fwrite( $handle, 'Deny from all' . "\n" );
312 fwrite( $handle, '<Files ~ "^[0-9A-Za-z]+\\.(jpeg|gif|png)$">' . "\n" );
313 fwrite( $handle, ' Allow from all' . "\n" );
314 fwrite( $handle, '</Files>' . "\n" );
315 fclose( $handle );
316 }
317
318 return true;
319 }
320
321 /**
322 * Normalize a filesystem path.
323 *
324 * This should be replaced by wp_normalize_path when the plugin's
325 * minimum requirement becomes WordPress 3.9 or higher.
326 *
327 * @param string $path Path to normalize.
328 * @return string Normalized path.
329 */
330 private function normalize_path( $path ) {
331 $path = str_replace( '\\', '/', $path );
332 $path = preg_replace( '|/+|', '/', $path );
333 return $path;
334 }
335 }
336