Really Simple CAPTCHA - Version 1.1

Version Description

Download this release

Release Info

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

Code changes from version 1.0 to 1.1

Files changed (4) hide show
  1. README.txt +27 -5
  2. really-simple-captcha.php +104 -60
  3. screenshot-1.png +0 -0
  4. tmp/index.php +3 -0
README.txt CHANGED
@@ -2,15 +2,15 @@
2
  Contributors: takayukister
3
  Donate link: http://pledgie.com/campaigns/3259
4
  Tags: captcha
5
- Requires at least: 2.2
6
- Tested up to: 2.7.1
7
- Stable tag: 1.0
8
 
9
  Really Simple CAPTCHA is a CAPTCHA module intended to be called from other plugins. It is originally created for my Contact Form 7 plugin.
10
 
11
  == Description ==
12
 
13
- Really Simple CAPTCHA does not work alone and is intended to work with other plugins. It is originally created for Contact Form 7, however, you can use it with your own plugin.
14
 
15
  Note: This product is "really simple" as its name suggests, i.e., it is not strongly secure. If you need perfect security, you should try other solutions.
16
 
@@ -24,6 +24,8 @@ The two files have the same (random) prefix in their file names, for example, "a
24
 
25
  = How to use with your plugin =
26
 
 
 
27
  First, create an instance of ReallySimpleCaptcha class:
28
 
29
  `$captcha_instance = new ReallySimpleCaptcha();`
@@ -69,4 +71,24 @@ However, if you install this manually, follow these steps:
69
  1. Upload the entire `really-simple-captcha` folder to the `/wp-content/plugins/` directory.
70
  1. Activate the plugin through the 'Plugins' menu in WordPress.
71
 
72
- FYI: There is no "control panel" for this plugin.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  Contributors: takayukister
3
  Donate link: http://pledgie.com/campaigns/3259
4
  Tags: captcha
5
+ Requires at least: 2.8
6
+ Tested up to: 2.8.5
7
+ Stable tag: 1.1
8
 
9
  Really Simple CAPTCHA is a CAPTCHA module intended to be called from other plugins. It is originally created for my Contact Form 7 plugin.
10
 
11
  == Description ==
12
 
13
+ Really Simple CAPTCHA does not work alone and is intended to work with other plugins. It is originally created for [Contact Form 7](http://contactform7.com/), however, you can use it with your own plugin.
14
 
15
  Note: This product is "really simple" as its name suggests, i.e., it is not strongly secure. If you need perfect security, you should try other solutions.
16
 
24
 
25
  = How to use with your plugin =
26
 
27
+ Note: Below are instructions for plugin developers.
28
+
29
  First, create an instance of ReallySimpleCaptcha class:
30
 
31
  `$captcha_instance = new ReallySimpleCaptcha();`
71
  1. Upload the entire `really-simple-captcha` folder to the `/wp-content/plugins/` directory.
72
  1. Activate the plugin through the 'Plugins' menu in WordPress.
73
 
74
+ FYI: There is no "control panel" for this plugin.
75
+
76
+ == Frequently Asked Questions ==
77
+
78
+ = CAPTCHA does not work; the image does not show up. =
79
+
80
+ Really Simple CAPTCHA needs GD and FreeType library installed on your server. Ask your server administrator if they are installed.
81
+
82
+ 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.
83
+
84
+ If you have any further questions, please submit them [to the support forum](http://wordpress.org/tags/really-simple-captcha?forum_id=10#postform).
85
+
86
+ == Screenshots ==
87
+
88
+ 1. screenshot-1.png
89
+
90
+ == Changelog ==
91
+
92
+ = 1.1 =
93
+ * The required WordPress version changed to 2.8 and higher.
94
+ * `cleanup()` method added.
really-simple-captcha.php CHANGED
@@ -4,7 +4,7 @@ Plugin Name: Really Simple CAPTCHA
4
  Plugin URI: http://ideasilo.wordpress.com/2009/03/14/really-simple-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.0
8
  Author URI: http://ideasilo.wordpress.com/
9
  */
10
 
@@ -29,118 +29,162 @@ class ReallySimpleCaptcha {
29
 
30
  function ReallySimpleCaptcha() {
31
 
32
- /* Characters you can use in an image */
33
  $this->chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
34
 
35
- /* Length of a word in an image */
36
  $this->char_length = 4;
37
 
38
- /* Array of fonts. Randomly picked up per character */
39
  $this->fonts = array(
40
- dirname(__FILE__) . '/gentium/GenAI102.TTF',
41
- dirname(__FILE__) . '/gentium/GenAR102.TTF',
42
- dirname(__FILE__) . '/gentium/GenI102.TTF',
43
- dirname(__FILE__) . '/gentium/GenR102.TTF');
44
 
45
- /* Directory CAPTCHA image and corresponding code kept temporary */
46
- $this->tmp_dir = 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 files */
70
- $this->file_mode = 0755;
71
  }
72
 
73
- /* Generate and return random word with $chars characters x $char_length length */
 
74
  function generate_random_word() {
75
  $word = '';
76
- for ($i = 0; $i < $this->char_length; $i++) {
77
- $pos = mt_rand(0, strlen($this->chars) - 1);
78
  $char = $this->chars[$pos];
79
  $word .= $char;
80
  }
81
  return $word;
82
  }
83
 
84
- /* Generate CAPTCHA code and corresponding code and save them into $tmp_dir directory.
85
- $prefix is file prefix for both files. $captcha is a random word usually generated by generate_random_word()
86
- This function returns the filename of the CAPTCHA image temporary file */
87
- function generate_image($prefix, $word) {
 
 
88
  $filename = null;
89
- if ($im = imagecreatetruecolor($this->img_size[0], $this->img_size[1])) {
90
- $bg = imagecolorallocate($im, $this->bg[0], $this->bg[1], $this->bg[2]);
91
- $fg = imagecolorallocate($im, $this->fg[0], $this->fg[1], $this->fg[2]);
92
- imagefill($im, 0, 0, $bg);
93
- $x = $this->base[0] + mt_rand(-2, 2);
94
- for ($i = 0; $i < strlen($word); $i++) {
95
- $font = $this->fonts[array_rand($this->fonts)];
96
- imagettftext($im, $this->font_size, mt_rand(-2, 2), $x, $this->base[1] + mt_rand(-2, 2), $fg, $font, $word[$i]);
 
 
 
 
 
97
  $x += $this->font_char_width;
98
  }
99
- switch ($this->img_type) {
 
100
  case 'jpeg':
101
  $filename = $prefix . '.jpeg';
102
- imagejpeg($im, $this->tmp_dir . $filename);
103
  break;
104
  case 'gif':
105
  $filename = $prefix . '.gif';
106
- imagegif($im, $this->tmp_dir . $filename);
107
  break;
108
  case 'png':
109
  default:
110
  $filename = $prefix . '.png';
111
- imagepng($im, $this->tmp_dir . $filename);
112
  }
113
- imagedestroy($im);
114
- @chmod($this->tmp_dir . $filename, $this->file_mode);
 
115
  }
116
- if ($fh = fopen($this->tmp_dir . $prefix . '.php', 'w')) {
117
- @chmod($this->tmp_dir . $prefix . '.php', $this->file_mode);
118
- fwrite($fh, '<?php $captcha = "' . $word . '"; ?>');
119
- fclose($fh);
 
120
  }
 
121
  return $filename;
122
  }
123
 
124
- /* Check a $response against the code kept in the temporary file with $prefix
125
- Return true if the two match, otherwise return false. */
126
- function check($prefix, $response) {
127
- if (is_readable($this->tmp_dir . $prefix . '.php')) {
128
- include($this->tmp_dir . $prefix . '.php');
129
- if (0 == strcasecmp($response, $captcha))
 
130
  return true;
131
  }
132
  return false;
133
  }
134
 
135
- /* Remove temporary files with $prefix */
136
- function remove($prefix) {
137
- $suffixes = array('.jpeg', '.gif', '.png', '.php');
138
- foreach ($suffixes as $suffix) {
 
 
139
  $file = $this->tmp_dir . $prefix . $suffix;
140
- if (is_file($file))
141
- unlink($file);
142
  }
143
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  }
145
 
146
  ?>
4
  Plugin URI: http://ideasilo.wordpress.com/2009/03/14/really-simple-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.1
8
  Author URI: http://ideasilo.wordpress.com/
9
  */
10
 
29
 
30
  function ReallySimpleCaptcha() {
31
 
32
+ /* Characters available in images */
33
  $this->chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
34
 
35
+ /* Length of a word in an image */
36
  $this->char_length = 4;
37
 
38
+ /* Array of fonts. Randomly picked up per character */
39
  $this->fonts = array(
40
+ dirname( __FILE__ ) . '/gentium/GenAI102.TTF',
41
+ dirname( __FILE__ ) . '/gentium/GenAR102.TTF',
42
+ dirname( __FILE__ ) . '/gentium/GenI102.TTF',
43
+ dirname( __FILE__ ) . '/gentium/GenR102.TTF' );
44
 
45
+ /* Directory temporary keeping CAPTCHA images and corresponding codes */
46
+ $this->tmp_dir = 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 files */
70
+ $this->file_mode = 0755;
71
  }
72
 
73
+ /* Generate and return random word with $chars characters x $char_length length */
74
+
75
  function generate_random_word() {
76
  $word = '';
77
+ for ( $i = 0; $i < $this->char_length; $i++ ) {
78
+ $pos = mt_rand( 0, strlen( $this->chars ) - 1 );
79
  $char = $this->chars[$pos];
80
  $word .= $char;
81
  }
82
  return $word;
83
  }
84
 
85
+ /* Generate CAPTCHA code and corresponding code and save them into $tmp_dir directory.
86
+ $prefix is file prefix for both files.
87
+ $captcha is a random word usually generated by generate_random_word()
88
+ This function returns the filename of the CAPTCHA image temporary file */
89
+
90
+ function generate_image( $prefix, $word ) {
91
  $filename = null;
92
+
93
+ if ( $im = imagecreatetruecolor( $this->img_size[0], $this->img_size[1] ) ) {
94
+ $bg = imagecolorallocate( $im, $this->bg[0], $this->bg[1], $this->bg[2] );
95
+ $fg = imagecolorallocate( $im, $this->fg[0], $this->fg[1], $this->fg[2] );
96
+
97
+ imagefill( $im, 0, 0, $bg );
98
+
99
+ $x = $this->base[0] + mt_rand( -2, 2 );
100
+
101
+ for ( $i = 0; $i < strlen( $word ); $i++ ) {
102
+ $font = $this->fonts[array_rand( $this->fonts )];
103
+ imagettftext( $im, $this->font_size, mt_rand( -2, 2 ), $x,
104
+ $this->base[1] + mt_rand( -2, 2 ), $fg, $font, $word[$i] );
105
  $x += $this->font_char_width;
106
  }
107
+
108
+ switch ( $this->img_type ) {
109
  case 'jpeg':
110
  $filename = $prefix . '.jpeg';
111
+ imagejpeg( $im, $this->tmp_dir . $filename );
112
  break;
113
  case 'gif':
114
  $filename = $prefix . '.gif';
115
+ imagegif( $im, $this->tmp_dir . $filename );
116
  break;
117
  case 'png':
118
  default:
119
  $filename = $prefix . '.png';
120
+ imagepng( $im, $this->tmp_dir . $filename );
121
  }
122
+
123
+ imagedestroy( $im );
124
+ @chmod( $this->tmp_dir . $filename, $this->file_mode );
125
  }
126
+
127
+ if ( $fh = fopen( $this->tmp_dir . $prefix . '.php', 'w' ) ) {
128
+ @chmod( $this->tmp_dir . $prefix . '.php', $this->file_mode );
129
+ fwrite( $fh, '<?php $captcha = "' . $word . '"; ?>' );
130
+ fclose( $fh );
131
  }
132
+
133
  return $filename;
134
  }
135
 
136
+ /* Check a $response against the code kept in the temporary file with $prefix
137
+ Return true if the two match, otherwise return false. */
138
+
139
+ function check( $prefix, $response ) {
140
+ if ( is_readable( $this->tmp_dir . $prefix . '.php' ) ) {
141
+ include( $this->tmp_dir . $prefix . '.php' );
142
+ if ( 0 == strcasecmp( $response, $captcha ) )
143
  return true;
144
  }
145
  return false;
146
  }
147
 
148
+ /* Remove temporary files with $prefix */
149
+
150
+ function remove( $prefix ) {
151
+ $suffixes = array( '.jpeg', '.gif', '.png', '.php' );
152
+
153
+ foreach ( $suffixes as $suffix ) {
154
  $file = $this->tmp_dir . $prefix . $suffix;
155
+ if ( is_file( $file ) )
156
+ unlink( $file );
157
  }
158
  }
159
+
160
+ /* Clean up dead files older than $minutes in the tmp folder */
161
+
162
+ function cleanup( $minutes = 60 ) {
163
+ $dir = $this->tmp_dir;
164
+
165
+ if ( ! is_dir( $dir ) || ! is_readable( $dir ) || ! is_writable( $dir ) )
166
+ return false;
167
+
168
+ $count = 0;
169
+
170
+ if ( $handle = @opendir( $dir ) ) {
171
+ while ( false !== ( $file = readdir( $handle ) ) ) {
172
+ if ( ! preg_match( '/^[0-9]+\.(php|png|gif|jpeg)$/', $file ) )
173
+ continue;
174
+
175
+ $stat = @stat( $dir . $file );
176
+ if ( ( $stat['mtime'] + $minutes * 60 ) < time() ) {
177
+ @unlink( $dir . $file );
178
+ $count += 1;
179
+ }
180
+ }
181
+
182
+ closedir( $handle );
183
+ }
184
+
185
+ return $count;
186
+ }
187
+
188
  }
189
 
190
  ?>
screenshot-1.png ADDED
Binary file
tmp/index.php CHANGED
@@ -0,0 +1,3 @@
 
 
 
1
+ <?php
2
+ // Silence is golden.
3
+ ?>