| 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: Rock Lobster Inc. |
| 7 |
* Author URI: https://github.com/rocklobster-in/ |
| 8 |
* License: GPL v2 or later |
| 9 |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 10 |
* Version: 2.5 |
| 11 |
* Requires at least: 7.1 |
| 12 |
* Requires PHP: 8.3 |
| 13 |
*/ |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 16 |
|
| 17 |
define( 'REALLYSIMPLECAPTCHA_VERSION', '2.5' ); |
| 18 |
|
| 19 |
require_once __DIR__ . '/includes/filesystem.php'; |
| 20 |
|
| 21 |
class ReallySimpleCaptcha { |
| 22 |
|
| 23 |
use ReallySimpleCaptcha_Filesystem; |
| 24 |
|
| 25 |
/** |
| 26 |
* Characters available in a CAPTCHA image. |
| 27 |
* |
| 28 |
* @var string All characters that can be used in a CAPTCHA image. |
| 29 |
*/ |
| 30 |
public $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Number of characters that are displayed in a CAPTCHA image. |
| 34 |
* |
| 35 |
* @var int |
| 36 |
*/ |
| 37 |
public $char_length = 4; |
| 38 |
|
| 39 |
/** |
| 40 |
* Font paths. Randomly picked up from the list per character. |
| 41 |
* |
| 42 |
* @var array |
| 43 |
*/ |
| 44 |
public $fonts = array( |
| 45 |
__DIR__ . '/gentium/GenBkBasR.ttf', |
| 46 |
__DIR__ . '/gentium/GenBkBasI.ttf', |
| 47 |
__DIR__ . '/gentium/GenBkBasBI.ttf', |
| 48 |
__DIR__ . '/gentium/GenBkBasB.ttf', |
| 49 |
); |
| 50 |
|
| 51 |
/** |
| 52 |
* Temp directory for CAPTCHA images and text files. |
| 53 |
* |
| 54 |
* @var string |
| 55 |
*/ |
| 56 |
public $tmp_dir = __DIR__ . '/tmp'; |
| 57 |
|
| 58 |
/** |
| 59 |
* Array of CAPTCHA image size. |
| 60 |
* |
| 61 |
* @var array 0: Width, 1: Height (in pixels) |
| 62 |
*/ |
| 63 |
public $img_size = array( 72, 24 ); |
| 64 |
|
| 65 |
/** |
| 66 |
* Background color of a CAPTCHA image in RGB-notation. |
| 67 |
* |
| 68 |
* @var array 0: R, 1: G, 2: B (each within 0-255) |
| 69 |
*/ |
| 70 |
public $bg = array( 255, 255, 255 ); |
| 71 |
|
| 72 |
/** |
| 73 |
* Foreground (character) color of a CAPTCHA image in RGB-notation. |
| 74 |
* |
| 75 |
* @var array 0: R, 1: G, 2: B (each within 0-255) |
| 76 |
*/ |
| 77 |
public $fg = array( 0, 0, 0 ); |
| 78 |
|
| 79 |
/** |
| 80 |
* Coordinates for a text in an image. I don't know the meaning. Just adjust. |
| 81 |
* |
| 82 |
* @var array |
| 83 |
*/ |
| 84 |
public $base = array( 6, 18 ); |
| 85 |
|
| 86 |
/** |
| 87 |
* Font size. |
| 88 |
* |
| 89 |
* @var int |
| 90 |
*/ |
| 91 |
public $font_size = 14; |
| 92 |
|
| 93 |
/** |
| 94 |
* Width of a character. |
| 95 |
* |
| 96 |
* @var int |
| 97 |
*/ |
| 98 |
public $font_char_width = 15; |
| 99 |
|
| 100 |
/** |
| 101 |
* Image type. |
| 102 |
* |
| 103 |
* @var string png, gif, or jpeg. |
| 104 |
*/ |
| 105 |
public $img_type = 'png'; |
| 106 |
|
| 107 |
/** |
| 108 |
* File mode set for a CAPTCHA image. |
| 109 |
* |
| 110 |
* @var int |
| 111 |
*/ |
| 112 |
public $file_mode = 0644; |
| 113 |
|
| 114 |
/** |
| 115 |
* File mode set for an answer text file. |
| 116 |
* |
| 117 |
* @var int |
| 118 |
*/ |
| 119 |
public $answer_file_mode = 0640; |
| 120 |
|
| 121 |
|
| 122 |
/** |
| 123 |
* Constructor. |
| 124 |
*/ |
| 125 |
public function __construct() { |
| 126 |
$this->connect(); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
/** |
| 131 |
* Generate and return a random word. |
| 132 |
* |
| 133 |
* @return string Random word with $chars characters x $char_length length |
| 134 |
*/ |
| 135 |
public function generate_random_word() { |
| 136 |
$word = ''; |
| 137 |
|
| 138 |
for ( $i = 0; $i < $this->char_length; $i++ ) { |
| 139 |
$pos = wp_rand( 0, strlen( $this->chars ) - 1 ); |
| 140 |
$char = $this->chars[$pos]; |
| 141 |
$word .= $char; |
| 142 |
} |
| 143 |
|
| 144 |
return $word; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
/** |
| 149 |
* Generate CAPTCHA image and corresponding answer file. |
| 150 |
* |
| 151 |
* @param string $prefix File prefix used for both files |
| 152 |
* @param string $word Random word generated by generate_random_word() |
| 153 |
* @return string|bool The file name of the CAPTCHA image. Return false if temp directory is not available. |
| 154 |
*/ |
| 155 |
public function generate_image( $prefix, $word ) { |
| 156 |
if ( ! $this->make_tmp_dir() ) { |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
$this->cleanup(); |
| 161 |
|
| 162 |
$dir = trailingslashit( $this->tmp_dir ); |
| 163 |
$filename = null; |
| 164 |
|
| 165 |
$im = imagecreatetruecolor( |
| 166 |
$this->img_size[0], |
| 167 |
$this->img_size[1] |
| 168 |
); |
| 169 |
|
| 170 |
if ( $im ) { |
| 171 |
$bg = imagecolorallocate( $im, $this->bg[0], $this->bg[1], $this->bg[2] ); |
| 172 |
$fg = imagecolorallocate( $im, $this->fg[0], $this->fg[1], $this->fg[2] ); |
| 173 |
|
| 174 |
imagefill( $im, 0, 0, $bg ); |
| 175 |
|
| 176 |
$x = $this->base[0] + wp_rand( -2, 2 ); |
| 177 |
|
| 178 |
for ( $i = 0; $i < strlen( $word ); $i++ ) { |
| 179 |
$font = $this->fonts[array_rand( $this->fonts )]; |
| 180 |
$font = wp_normalize_path( $font ); |
| 181 |
|
| 182 |
imagettftext( |
| 183 |
$im, $this->font_size, wp_rand( -12, 12 ), $x, |
| 184 |
$this->base[1] + wp_rand( -2, 2 ), $fg, $font, $word[$i] |
| 185 |
); |
| 186 |
|
| 187 |
$x += $this->font_char_width; |
| 188 |
} |
| 189 |
|
| 190 |
switch ( $this->img_type ) { |
| 191 |
case 'jpeg': |
| 192 |
$filename = sanitize_file_name( $prefix . '.jpeg' ); |
| 193 |
$file = wp_normalize_path( path_join( $dir, $filename ) ); |
| 194 |
imagejpeg( $im, $file ); |
| 195 |
break; |
| 196 |
case 'gif': |
| 197 |
$filename = sanitize_file_name( $prefix . '.gif' ); |
| 198 |
$file = wp_normalize_path( path_join( $dir, $filename ) ); |
| 199 |
imagegif( $im, $file ); |
| 200 |
break; |
| 201 |
case 'png': |
| 202 |
default: |
| 203 |
$filename = sanitize_file_name( $prefix . '.png' ); |
| 204 |
$file = wp_normalize_path( path_join( $dir, $filename ) ); |
| 205 |
imagepng( $im, $file ); |
| 206 |
} |
| 207 |
|
| 208 |
imagedestroy( $im ); |
| 209 |
|
| 210 |
$this->chmod( $file, $this->file_mode ); |
| 211 |
} |
| 212 |
|
| 213 |
$this->generate_answer_file( $prefix, $word ); |
| 214 |
|
| 215 |
return $filename; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
/** |
| 220 |
* Generate answer file corresponding to CAPTCHA image. |
| 221 |
* |
| 222 |
* @param string $prefix File prefix used for answer file |
| 223 |
* @param string $word Random word generated by generate_random_word() |
| 224 |
*/ |
| 225 |
public function generate_answer_file( $prefix, $word ) { |
| 226 |
$dir = trailingslashit( $this->tmp_dir ); |
| 227 |
$answer_file = path_join( $dir, sanitize_file_name( $prefix . '.txt' ) ); |
| 228 |
$answer_file = wp_normalize_path( $answer_file ); |
| 229 |
|
| 230 |
$word = strtoupper( $word ); |
| 231 |
$salt = wp_generate_password( 64 ); |
| 232 |
$hash = hash_hmac( 'sha256', $word, $salt ); |
| 233 |
$code = $salt . '|' . $hash; |
| 234 |
|
| 235 |
$this->put_contents( $answer_file, $code, $this->answer_file_mode ); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
/** |
| 240 |
* Check a response against the code kept in the temporary file. |
| 241 |
* |
| 242 |
* @param string $prefix File prefix used for both files |
| 243 |
* @param string $response CAPTCHA response |
| 244 |
* @return bool Return true if the two match, otherwise return false. |
| 245 |
*/ |
| 246 |
public function check( $prefix, $response ) { |
| 247 |
if ( 0 === strlen( $prefix ) ) { |
| 248 |
return false; |
| 249 |
} |
| 250 |
|
| 251 |
$response = str_replace( array( " ", "\t" ), '', $response ); |
| 252 |
$response = strtoupper( $response ); |
| 253 |
|
| 254 |
$dir = trailingslashit( $this->tmp_dir ); |
| 255 |
$filename = sanitize_file_name( $prefix . '.txt' ); |
| 256 |
$file = wp_normalize_path( path_join( $dir, $filename ) ); |
| 257 |
|
| 258 |
if ( is_readable( $file ) and $code = $this->get_contents( $file ) ) { |
| 259 |
$code = explode( '|', $code, 2 ); |
| 260 |
$salt = $code[0]; |
| 261 |
$hash = $code[1]; |
| 262 |
|
| 263 |
return hash_equals( $hash, hash_hmac( 'sha256', $response, $salt ) ); |
| 264 |
} |
| 265 |
|
| 266 |
return false; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
/** |
| 271 |
* Remove temporary files with given prefix. |
| 272 |
* |
| 273 |
* @param string $prefix File prefix |
| 274 |
*/ |
| 275 |
public function remove( $prefix ) { |
| 276 |
$dir = trailingslashit( $this->tmp_dir ); |
| 277 |
$suffixes = array( '.jpeg', '.gif', '.png', '.php', '.txt' ); |
| 278 |
|
| 279 |
foreach ( $suffixes as $suffix ) { |
| 280 |
$filename = sanitize_file_name( $prefix . $suffix ); |
| 281 |
$file = wp_normalize_path( path_join( $dir, $filename ) ); |
| 282 |
|
| 283 |
if ( is_file( $file ) ) { |
| 284 |
$this->delete( $file ); |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
/** |
| 291 |
* Clean up dead files older than given length of time. |
| 292 |
* |
| 293 |
* @param int $minutes Consider older files than this time as dead files |
| 294 |
* @return int|bool The number of removed files. Return false if error occurred. |
| 295 |
*/ |
| 296 |
public function cleanup( $minutes = 60, $max = 100 ) { |
| 297 |
$dir = trailingslashit( $this->tmp_dir ); |
| 298 |
$dir = wp_normalize_path( $dir ); |
| 299 |
|
| 300 |
if ( |
| 301 |
! is_dir( $dir ) or |
| 302 |
! is_readable( $dir ) or |
| 303 |
! wp_is_writable( $dir ) |
| 304 |
) { |
| 305 |
return false; |
| 306 |
} |
| 307 |
|
| 308 |
$count = 0; |
| 309 |
|
| 310 |
if ( $handle = opendir( $dir ) ) { |
| 311 |
while ( false !== ( $filename = readdir( $handle ) ) ) { |
| 312 |
if ( ! preg_match( '/^[0-9]+\.(php|txt|png|gif|jpeg)$/', $filename ) ) { |
| 313 |
continue; |
| 314 |
} |
| 315 |
|
| 316 |
$file = wp_normalize_path( path_join( $dir, $filename ) ); |
| 317 |
|
| 318 |
if ( ! file_exists( $file ) or ! $stat = stat( $file ) ) { |
| 319 |
continue; |
| 320 |
} |
| 321 |
|
| 322 |
if ( ( $stat['mtime'] + $minutes * MINUTE_IN_SECONDS ) < time() ) { |
| 323 |
if ( ! $this->delete( $file ) ) { |
| 324 |
$this->chmod( $file, 0644 ); |
| 325 |
$this->delete( $file ); |
| 326 |
} |
| 327 |
|
| 328 |
$count += 1; |
| 329 |
} |
| 330 |
|
| 331 |
if ( $max <= $count ) { |
| 332 |
break; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
closedir( $handle ); |
| 337 |
} |
| 338 |
|
| 339 |
return $count; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
/** |
| 344 |
* Make a temporary directory and generate .htaccess file in it. |
| 345 |
* |
| 346 |
* @return bool True on successful create, false on failure. |
| 347 |
*/ |
| 348 |
public function make_tmp_dir() { |
| 349 |
$dir = trailingslashit( $this->tmp_dir ); |
| 350 |
$dir = wp_normalize_path( $dir ); |
| 351 |
|
| 352 |
if ( ! wp_mkdir_p( $dir ) ) { |
| 353 |
return false; |
| 354 |
} |
| 355 |
|
| 356 |
$htaccess_file = wp_normalize_path( path_join( $dir, '.htaccess' ) ); |
| 357 |
|
| 358 |
if ( file_exists( $htaccess_file ) ) { |
| 359 |
list( $first_line_comment ) = (array) file( |
| 360 |
$htaccess_file, |
| 361 |
FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES |
| 362 |
); |
| 363 |
|
| 364 |
if ( '# Apache 2.4+' === $first_line_comment ) { |
| 365 |
return true; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
$htaccess_body = ' |
| 370 |
# Apache 2.4+ |
| 371 |
<IfModule authz_core_module> |
| 372 |
Require all denied |
| 373 |
<FilesMatch "^\w+\.(jpe?g|gif|png)$"> |
| 374 |
Require all granted |
| 375 |
</FilesMatch> |
| 376 |
</IfModule> |
| 377 |
|
| 378 |
# Apache 2.2 |
| 379 |
<IfModule !authz_core_module> |
| 380 |
Order deny,allow |
| 381 |
Deny from all |
| 382 |
<FilesMatch "^\w+\.(jpe?g|gif|png)$"> |
| 383 |
Allow from all |
| 384 |
</FilesMatch> |
| 385 |
</IfModule> |
| 386 |
'; |
| 387 |
|
| 388 |
return $this->put_contents( $htaccess_file, ltrim( $htaccess_body ), 0644 ); |
| 389 |
} |
| 390 |
|
| 391 |
} |
| 392 |
|