Really Simple CAPTCHA - Version 1.8

Version Description

  • The required WordPress version changed to 3.7 and higher.
  • Introduce normalize_path() to normalize file paths on different file systems.
Download this release

Release Info

Developer takayukister
Plugin Icon 128x128 Really Simple CAPTCHA
Version 1.8
Comparing to
See all releases

Code changes from version 1.7 to 1.8

Files changed (3) hide show
  1. readme.txt +9 -19
  2. really-simple-captcha.php +49 -25
  3. screenshot-1.png +0 -0
readme.txt CHANGED
@@ -2,9 +2,9 @@
2
  Contributors: takayukister
3
  Donate link: http://contactform7.com/donate/
4
  Tags: captcha
5
- Requires at least: 3.5
6
- Tested up to: 3.6
7
- Stable tag: 1.7
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -83,7 +83,7 @@ Really Simple CAPTCHA needs GD and FreeType library installed on your server. As
83
 
84
  Also, make the temporary file folder writable. The location of the temporary file folder is managed by the instance variable `tmp_dir` of ReallySimpleCaptcha class. Note that the setting varies depending on the calling plugin. For example, Contact Form 7 uses `wp-contents/uploads/wpcf7_captcha` as the temporary folder basically, but it can use different folder depending on your settings.
85
 
86
- If you have any further questions, please submit them [to the support forum](http://wordpress.org/tags/really-simple-captcha?forum_id=10#postform).
87
 
88
  == Screenshots ==
89
 
@@ -91,6 +91,11 @@ If you have any further questions, please submit them [to the support forum](htt
91
 
92
  == Changelog ==
93
 
 
 
 
 
 
94
  = 1.7 =
95
 
96
  * The required WordPress version changed to 3.5 and higher.
@@ -106,18 +111,3 @@ If you have any further questions, please submit them [to the support forum](htt
106
 
107
  * The required WordPress version changed to 3.2 and higher.
108
  * Use plain text file as answer file (again). This time, hash value generated with hash_hmac() is stored in the file.
109
-
110
- = 1.4 =
111
-
112
- * Reverted answer file to PHP. As plain text file is visible from client side, that's not good.
113
-
114
- = 1.3 =
115
-
116
- * Use plain text file as answer file.
117
-
118
- = 1.2 =
119
- * File name sanitization added.
120
-
121
- = 1.1 =
122
- * The required WordPress version changed to 2.8 and higher.
123
- * cleanup() method added.
2
  Contributors: takayukister
3
  Donate link: http://contactform7.com/donate/
4
  Tags: captcha
5
+ Requires at least: 3.7
6
+ Tested up to: 3.8.1
7
+ Stable tag: 1.8
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
83
 
84
  Also, make the temporary file folder writable. The location of the temporary file folder is managed by the instance variable `tmp_dir` of ReallySimpleCaptcha class. Note that the setting varies depending on the calling plugin. For example, Contact Form 7 uses `wp-contents/uploads/wpcf7_captcha` as the temporary folder basically, but it can use different folder depending on your settings.
85
 
86
+ If you have any further questions, please submit them [to the support forum](http://wordpress.org/support/plugin/really-simple-captcha).
87
 
88
  == Screenshots ==
89
 
91
 
92
  == Changelog ==
93
 
94
+ = 1.8 =
95
+
96
+ * The required WordPress version changed to 3.7 and higher.
97
+ * Introduce normalize_path() to normalize file paths on different file systems.
98
+
99
  = 1.7 =
100
 
101
  * The required WordPress version changed to 3.5 and higher.
111
 
112
  * The required WordPress version changed to 3.2 and higher.
113
  * Use plain text file as answer file (again). This time, hash value generated with hash_hmac() is stored in the file.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
really-simple-captcha.php CHANGED
@@ -4,11 +4,11 @@ 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
- Version: 1.7
8
  Author URI: http://ideasilo.wordpress.com/
9
  */
10
 
11
- /* Copyright 2007-2013 Takayuki Miyoshi (email: takayukister at gmail.com)
12
 
13
  This program is free software; you can redistribute it and/or modify
14
  it under the terms of the GNU General Public License as published by
@@ -25,12 +25,11 @@ Author URI: http://ideasilo.wordpress.com/
25
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
  */
27
 
28
- define( 'REALLYSIMPLECAPTCHA_VERSION', '1.7' );
29
 
30
  class ReallySimpleCaptcha {
31
 
32
- function ReallySimpleCaptcha() {
33
-
34
  /* Characters available in images */
35
  $this->chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
36
 
@@ -80,7 +79,7 @@ class ReallySimpleCaptcha {
80
  *
81
  * @return string Random word with $chars characters x $char_length length
82
  */
83
- function generate_random_word() {
84
  $word = '';
85
 
86
  for ( $i = 0; $i < $this->char_length; $i++ ) {
@@ -99,7 +98,7 @@ class ReallySimpleCaptcha {
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
- function generate_image( $prefix, $word ) {
103
  if ( ! $this->make_tmp_dir() )
104
  return false;
105
 
@@ -118,10 +117,7 @@ class ReallySimpleCaptcha {
118
 
119
  for ( $i = 0; $i < strlen( $word ); $i++ ) {
120
  $font = $this->fonts[array_rand( $this->fonts )];
121
-
122
- // sanitize for Win32 installs
123
- $font = str_replace( '\\', '/', $font );
124
- $font = preg_replace( '|/+|', '/', $font );
125
 
126
  imagettftext( $im, $this->font_size, mt_rand( -12, 12 ), $x,
127
  $this->base[1] + mt_rand( -2, 2 ), $fg, $font, $word[$i] );
@@ -131,20 +127,23 @@ class ReallySimpleCaptcha {
131
  switch ( $this->img_type ) {
132
  case 'jpeg':
133
  $filename = sanitize_file_name( $prefix . '.jpeg' );
134
- imagejpeg( $im, $dir . $filename );
 
135
  break;
136
  case 'gif':
137
  $filename = sanitize_file_name( $prefix . '.gif' );
138
- imagegif( $im, $dir . $filename );
 
139
  break;
140
  case 'png':
141
  default:
142
  $filename = sanitize_file_name( $prefix . '.png' );
143
- imagepng( $im, $dir . $filename );
 
144
  }
145
 
146
  imagedestroy( $im );
147
- @chmod( $dir . $filename, $this->file_mode );
148
  }
149
 
150
  $this->generate_answer_file( $prefix, $word );
@@ -158,9 +157,10 @@ class ReallySimpleCaptcha {
158
  * @param string $prefix File prefix used for answer file
159
  * @param string $word Random word generated by generate_random_word()
160
  */
161
- function generate_answer_file( $prefix, $word ) {
162
  $dir = trailingslashit( $this->tmp_dir );
163
  $answer_file = $dir . sanitize_file_name( $prefix . '.txt' );
 
164
 
165
  if ( $fh = @fopen( $answer_file, 'w' ) ) {
166
  $word = strtoupper( $word );
@@ -183,13 +183,17 @@ class ReallySimpleCaptcha {
183
  * @param string $response CAPTCHA response
184
  * @return bool Return true if the two match, otherwise return false.
185
  */
186
- function check( $prefix, $response ) {
 
 
 
 
187
  $response = str_replace( array( " ", "\t" ), '', $response );
188
  $response = strtoupper( $response );
189
 
190
  $dir = trailingslashit( $this->tmp_dir );
191
  $filename = sanitize_file_name( $prefix . '.txt' );
192
- $file = $dir . $filename;
193
 
194
  if ( @is_readable( $file ) && ( $code = file_get_contents( $file ) ) ) {
195
  $code = explode( '|', $code, 2 );
@@ -209,14 +213,17 @@ class ReallySimpleCaptcha {
209
  *
210
  * @param string $prefix File prefix
211
  */
212
- function remove( $prefix ) {
213
  $suffixes = array( '.jpeg', '.gif', '.png', '.php', '.txt' );
214
 
215
  foreach ( $suffixes as $suffix ) {
 
216
  $filename = sanitize_file_name( $prefix . $suffix );
217
- $file = trailingslashit( $this->tmp_dir ) . $filename;
218
- if ( @is_file( $file ) )
 
219
  unlink( $file );
 
220
  }
221
  }
222
 
@@ -226,8 +233,9 @@ class ReallySimpleCaptcha {
226
  * @param int $minutes Consider older files than this time as dead files
227
  * @return int|bool The number of removed files. Return false if error occurred.
228
  */
229
- function cleanup( $minutes = 60 ) {
230
  $dir = trailingslashit( $this->tmp_dir );
 
231
 
232
  if ( ! @is_dir( $dir ) || ! @is_readable( $dir ) )
233
  return false;
@@ -244,7 +252,7 @@ class ReallySimpleCaptcha {
244
  if ( ! preg_match( '/^[0-9]+\.(php|txt|png|gif|jpeg)$/', $filename ) )
245
  continue;
246
 
247
- $file = $dir . $filename;
248
 
249
  $stat = @stat( $file );
250
  if ( ( $stat['mtime'] + $minutes * 60 ) < time() ) {
@@ -264,13 +272,14 @@ class ReallySimpleCaptcha {
264
  *
265
  * @return bool True on successful create, false on failure.
266
  */
267
- function make_tmp_dir() {
268
  $dir = trailingslashit( $this->tmp_dir );
 
269
 
270
  if ( ! wp_mkdir_p( $dir ) )
271
  return false;
272
 
273
- $htaccess_file = $dir . '.htaccess';
274
 
275
  if ( file_exists( $htaccess_file ) )
276
  return true;
@@ -286,6 +295,21 @@ class ReallySimpleCaptcha {
286
 
287
  return true;
288
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
  }
290
 
291
  ?>
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
+ Version: 1.8
8
  Author URI: http://ideasilo.wordpress.com/
9
  */
10
 
11
+ /* Copyright 2007-2014 Takayuki Miyoshi (email: takayukister at gmail.com)
12
 
13
  This program is free software; you can redistribute it and/or modify
14
  it under the terms of the GNU General Public License as published by
25
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
  */
27
 
28
+ define( 'REALLYSIMPLECAPTCHA_VERSION', '1.8' );
29
 
30
  class ReallySimpleCaptcha {
31
 
32
+ public function __construct() {
 
33
  /* Characters available in images */
34
  $this->chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
35
 
79
  *
80
  * @return string Random word with $chars characters x $char_length length
81
  */
82
+ public function generate_random_word() {
83
  $word = '';
84
 
85
  for ( $i = 0; $i < $this->char_length; $i++ ) {
98
  * @param string $word Random word generated by generate_random_word()
99
  * @return string|bool The file name of the CAPTCHA image. Return false if temp directory is not available.
100
  */
101
+ public function generate_image( $prefix, $word ) {
102
  if ( ! $this->make_tmp_dir() )
103
  return false;
104
 
117
 
118
  for ( $i = 0; $i < strlen( $word ); $i++ ) {
119
  $font = $this->fonts[array_rand( $this->fonts )];
120
+ $font = $this->normalize_path( $font );
 
 
 
121
 
122
  imagettftext( $im, $this->font_size, mt_rand( -12, 12 ), $x,
123
  $this->base[1] + mt_rand( -2, 2 ), $fg, $font, $word[$i] );
127
  switch ( $this->img_type ) {
128
  case 'jpeg':
129
  $filename = sanitize_file_name( $prefix . '.jpeg' );
130
+ $file = $this->normalize_path( $dir . $filename );
131
+ imagejpeg( $im, $file );
132
  break;
133
  case 'gif':
134
  $filename = sanitize_file_name( $prefix . '.gif' );
135
+ $file = $this->normalize_path( $dir . $filename );
136
+ imagegif( $im, $file );
137
  break;
138
  case 'png':
139
  default:
140
  $filename = sanitize_file_name( $prefix . '.png' );
141
+ $file = $this->normalize_path( $dir . $filename );
142
+ imagepng( $im, $file );
143
  }
144
 
145
  imagedestroy( $im );
146
+ @chmod( $file, $this->file_mode );
147
  }
148
 
149
  $this->generate_answer_file( $prefix, $word );
157
  * @param string $prefix File prefix used for answer file
158
  * @param string $word Random word generated by generate_random_word()
159
  */
160
+ public function generate_answer_file( $prefix, $word ) {
161
  $dir = trailingslashit( $this->tmp_dir );
162
  $answer_file = $dir . sanitize_file_name( $prefix . '.txt' );
163
+ $answer_file = $this->normalize_path( $answer_file );
164
 
165
  if ( $fh = @fopen( $answer_file, 'w' ) ) {
166
  $word = strtoupper( $word );
183
  * @param string $response CAPTCHA response
184
  * @return bool Return true if the two match, otherwise return false.
185
  */
186
+ public function check( $prefix, $response ) {
187
+ if ( 0 == strlen( $prefix ) ) {
188
+ return false;
189
+ }
190
+
191
  $response = str_replace( array( " ", "\t" ), '', $response );
192
  $response = strtoupper( $response );
193
 
194
  $dir = trailingslashit( $this->tmp_dir );
195
  $filename = sanitize_file_name( $prefix . '.txt' );
196
+ $file = $this->normalize_path( $dir . $filename );
197
 
198
  if ( @is_readable( $file ) && ( $code = file_get_contents( $file ) ) ) {
199
  $code = explode( '|', $code, 2 );
213
  *
214
  * @param string $prefix File prefix
215
  */
216
+ public function remove( $prefix ) {
217
  $suffixes = array( '.jpeg', '.gif', '.png', '.php', '.txt' );
218
 
219
  foreach ( $suffixes as $suffix ) {
220
+ $dir = trailingslashit( $this->tmp_dir );
221
  $filename = sanitize_file_name( $prefix . $suffix );
222
+ $file = $this->normalize_path( $dir . $filename );
223
+
224
+ if ( @is_file( $file ) ) {
225
  unlink( $file );
226
+ }
227
  }
228
  }
229
 
233
  * @param int $minutes Consider older files than this time as dead files
234
  * @return int|bool The number of removed files. Return false if error occurred.
235
  */
236
+ public function cleanup( $minutes = 60 ) {
237
  $dir = trailingslashit( $this->tmp_dir );
238
+ $dir = $this->normalize_path( $dir );
239
 
240
  if ( ! @is_dir( $dir ) || ! @is_readable( $dir ) )
241
  return false;
252
  if ( ! preg_match( '/^[0-9]+\.(php|txt|png|gif|jpeg)$/', $filename ) )
253
  continue;
254
 
255
+ $file = $this->normalize_path( $dir . $filename );
256
 
257
  $stat = @stat( $file );
258
  if ( ( $stat['mtime'] + $minutes * 60 ) < time() ) {
272
  *
273
  * @return bool True on successful create, false on failure.
274
  */
275
+ public function make_tmp_dir() {
276
  $dir = trailingslashit( $this->tmp_dir );
277
+ $dir = $this->normalize_path( $dir );
278
 
279
  if ( ! wp_mkdir_p( $dir ) )
280
  return false;
281
 
282
+ $htaccess_file = $this->normalize_path( $dir . '.htaccess' );
283
 
284
  if ( file_exists( $htaccess_file ) )
285
  return true;
295
 
296
  return true;
297
  }
298
+
299
+ /**
300
+ * Normalize a filesystem path.
301
+ *
302
+ * This should be replaced by wp_normalize_path when the plugin's
303
+ * minimum requirement becomes WordPress 3.9 or higher.
304
+ *
305
+ * @param string $path Path to normalize.
306
+ * @return string Normalized path.
307
+ */
308
+ private function normalize_path( $path ) {
309
+ $path = str_replace( '\\', '/', $path );
310
+ $path = preg_replace( '|/+|', '/', $path );
311
+ return $path;
312
+ }
313
  }
314
 
315
  ?>
screenshot-1.png DELETED
Binary file