PluginProbe
Really Simple CAPTCHA / 1.9
Really Simple CAPTCHA v1.9
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 1.9, at really-simple-captcha.php

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