| 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.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
define( 'REALLYSIMPLECAPTCHA_VERSION', '2.0' ); |
| 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 |
if ( $im = imagecreatetruecolor( $this->img_size[0], $this->img_size[1] ) ) { |
| 111 |
$bg = imagecolorallocate( $im, $this->bg[0], $this->bg[1], $this->bg[2] ); |
| 112 |
$fg = imagecolorallocate( $im, $this->fg[0], $this->fg[1], $this->fg[2] ); |
| 113 |
|
| 114 |
imagefill( $im, 0, 0, $bg ); |
| 115 |
|
| 116 |
$x = $this->base[0] + mt_rand( -2, 2 ); |
| 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] ); |
| 124 |
$x += $this->font_char_width; |
| 125 |
} |
| 126 |
|
| 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 ); |
| 150 |
|
| 151 |
return $filename; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Generate answer file corresponding to CAPTCHA image. |
| 156 |
* |
| 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 ); |
| 167 |
$salt = wp_generate_password( 64 ); |
| 168 |
$hash = hash_hmac( 'md5', $word, $salt ); |
| 169 |
$code = $salt . '|' . $hash; |
| 170 |
fwrite( $fh, $code ); |
| 171 |
fclose( $fh ); |
| 172 |
} |
| 173 |
|
| 174 |
chmod( $answer_file, $this->answer_file_mode ); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Check a response against the code kept in the temporary file. |
| 179 |
* |
| 180 |
* @param string $prefix File prefix used for both files |
| 181 |
* @param string $response CAPTCHA response |
| 182 |
* @return bool Return true if the two match, otherwise return false. |
| 183 |
*/ |
| 184 |
public function check( $prefix, $response ) { |
| 185 |
if ( 0 == strlen( $prefix ) ) { |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
$response = str_replace( array( " ", "\t" ), '', $response ); |
| 190 |
$response = strtoupper( $response ); |
| 191 |
|
| 192 |
$dir = trailingslashit( $this->tmp_dir ); |
| 193 |
$filename = sanitize_file_name( $prefix . '.txt' ); |
| 194 |
$file = $this->normalize_path( $dir . $filename ); |
| 195 |
|
| 196 |
if ( is_readable( $file ) && ( $code = file_get_contents( $file ) ) ) { |
| 197 |
$code = explode( '|', $code, 2 ); |
| 198 |
$salt = $code[0]; |
| 199 |
$hash = $code[1]; |
| 200 |
|
| 201 |
if ( hash_hmac( 'md5', $response, $salt ) == $hash ) { |
| 202 |
return true; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Remove temporary files with given prefix. |
| 211 |
* |
| 212 |
* @param string $prefix File prefix |
| 213 |
*/ |
| 214 |
public function remove( $prefix ) { |
| 215 |
$dir = trailingslashit( $this->tmp_dir ); |
| 216 |
$suffixes = array( '.jpeg', '.gif', '.png', '.php', '.txt' ); |
| 217 |
|
| 218 |
foreach ( $suffixes as $suffix ) { |
| 219 |
$filename = sanitize_file_name( $prefix . $suffix ); |
| 220 |
$file = $this->normalize_path( $dir . $filename ); |
| 221 |
|
| 222 |
if ( is_file( $file ) ) { |
| 223 |
unlink( $file ); |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Clean up dead files older than given length of time. |
| 230 |
* |
| 231 |
* @param int $minutes Consider older files than this time as dead files |
| 232 |
* @return int|bool The number of removed files. Return false if error occurred. |
| 233 |
*/ |
| 234 |
public function cleanup( $minutes = 60, $max = 100 ) { |
| 235 |
$dir = trailingslashit( $this->tmp_dir ); |
| 236 |
$dir = $this->normalize_path( $dir ); |
| 237 |
|
| 238 |
if ( ! is_dir( $dir ) || ! is_readable( $dir ) ) { |
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
$is_win = ( 'WIN' === strtoupper( substr( PHP_OS, 0, 3 ) ) ); |
| 243 |
|
| 244 |
if ( ! ( $is_win ? win_is_writable( $dir ) : is_writable( $dir ) ) ) { |
| 245 |
return false; |
| 246 |
} |
| 247 |
|
| 248 |
$count = 0; |
| 249 |
|
| 250 |
if ( $handle = opendir( $dir ) ) { |
| 251 |
while ( false !== ( $filename = readdir( $handle ) ) ) { |
| 252 |
if ( ! preg_match( '/^[0-9]+\.(php|txt|png|gif|jpeg)$/', $filename ) ) { |
| 253 |
continue; |
| 254 |
} |
| 255 |
|
| 256 |
$file = $this->normalize_path( $dir . $filename ); |
| 257 |
$stat = stat( $file ); |
| 258 |
|
| 259 |
if ( ( $stat['mtime'] + $minutes * 60 ) < time() ) { |
| 260 |
if ( ! unlink( $file ) ) { |
| 261 |
chmod( $file, 0644 ); |
| 262 |
unlink( $file ); |
| 263 |
} |
| 264 |
|
| 265 |
$count += 1; |
| 266 |
} |
| 267 |
|
| 268 |
if ( $max <= $count ) { |
| 269 |
break; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
closedir( $handle ); |
| 274 |
} |
| 275 |
|
| 276 |
return $count; |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Make a temporary directory and generate .htaccess file in it. |
| 281 |
* |
| 282 |
* @return bool True on successful create, false on failure. |
| 283 |
*/ |
| 284 |
public function make_tmp_dir() { |
| 285 |
$dir = trailingslashit( $this->tmp_dir ); |
| 286 |
$dir = $this->normalize_path( $dir ); |
| 287 |
|
| 288 |
if ( ! wp_mkdir_p( $dir ) ) { |
| 289 |
return false; |
| 290 |
} |
| 291 |
|
| 292 |
$htaccess_file = $this->normalize_path( $dir . '.htaccess' ); |
| 293 |
|
| 294 |
if ( file_exists( $htaccess_file ) ) { |
| 295 |
return true; |
| 296 |
} |
| 297 |
|
| 298 |
if ( $handle = fopen( $htaccess_file, 'w' ) ) { |
| 299 |
fwrite( $handle, 'Order deny,allow' . "\n" ); |
| 300 |
fwrite( $handle, 'Deny from all' . "\n" ); |
| 301 |
fwrite( $handle, '<Files ~ "^[0-9A-Za-z]+\\.(jpeg|gif|png)$">' . "\n" ); |
| 302 |
fwrite( $handle, ' Allow from all' . "\n" ); |
| 303 |
fwrite( $handle, '</Files>' . "\n" ); |
| 304 |
fclose( $handle ); |
| 305 |
} |
| 306 |
|
| 307 |
return true; |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Normalize a filesystem path. |
| 312 |
* |
| 313 |
* This should be replaced by wp_normalize_path when the plugin's |
| 314 |
* minimum requirement becomes WordPress 3.9 or higher. |
| 315 |
* |
| 316 |
* @param string $path Path to normalize. |
| 317 |
* @return string Normalized path. |
| 318 |
*/ |
| 319 |
private function normalize_path( $path ) { |
| 320 |
$path = str_replace( '\\', '/', $path ); |
| 321 |
$path = preg_replace( '|/+|', '/', $path ); |
| 322 |
return $path; |
| 323 |
} |
| 324 |
} |
| 325 |
|