Font
3 years ago
CaptchaBuilder.php
8 months ago
CaptchaBuilderInterface.php
4 years ago
ImageFileHandler.php
4 years ago
PhraseBuilder.php
4 years ago
PhraseBuilderInterface.php
4 years ago
index.php
3 years ago
CaptchaBuilder.php
425 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Gregwar\Captcha; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use Exception; |
| 5 | class CaptchaBuilder implements CaptchaBuilderInterface |
| 6 | { |
| 7 | protected $fingerprint = array(); |
| 8 | protected $useFingerprint = \false; |
| 9 | protected $textColor = array(); |
| 10 | protected $lineColor = null; |
| 11 | protected $backgroundColor = null; |
| 12 | protected $background = null; |
| 13 | protected $backgroundImages = array(); |
| 14 | protected $contents = null; |
| 15 | protected $phrase = null; |
| 16 | protected $builder; |
| 17 | protected $distortion = \true; |
| 18 | protected $maxFrontLines = null; |
| 19 | protected $maxBehindLines = null; |
| 20 | protected $maxAngle = 8; |
| 21 | protected $maxOffset = 5; |
| 22 | protected $interpolation = \true; |
| 23 | protected $ignoreAllEffects = \false; |
| 24 | protected $allowedBackgroundImageTypes = array('image/png', 'image/jpeg', 'image/gif'); |
| 25 | public function getContents() |
| 26 | { |
| 27 | return $this->contents; |
| 28 | } |
| 29 | public function setInterpolation($interpolate = \true) |
| 30 | { |
| 31 | $this->interpolation = $interpolate; |
| 32 | return $this; |
| 33 | } |
| 34 | public $tempDir = 'temp/'; |
| 35 | public function __construct($phrase = null,?PhraseBuilderInterface $builder = null) |
| 36 | { |
| 37 | if ($builder === null) { |
| 38 | $this->builder = new PhraseBuilder(); |
| 39 | } else { |
| 40 | $this->builder = $builder; |
| 41 | } |
| 42 | $this->phrase = \is_string($phrase) ? $phrase : $this->builder->build($phrase); |
| 43 | } |
| 44 | public function setPhrase($phrase) |
| 45 | { |
| 46 | $this->phrase = (string) $phrase; |
| 47 | } |
| 48 | public function setDistortion($distortion) |
| 49 | { |
| 50 | $this->distortion = (bool) $distortion; |
| 51 | return $this; |
| 52 | } |
| 53 | public function setMaxBehindLines($maxBehindLines) |
| 54 | { |
| 55 | $this->maxBehindLines = $maxBehindLines; |
| 56 | return $this; |
| 57 | } |
| 58 | public function setMaxFrontLines($maxFrontLines) |
| 59 | { |
| 60 | $this->maxFrontLines = $maxFrontLines; |
| 61 | return $this; |
| 62 | } |
| 63 | public function setMaxAngle($maxAngle) |
| 64 | { |
| 65 | $this->maxAngle = $maxAngle; |
| 66 | return $this; |
| 67 | } |
| 68 | public function setMaxOffset($maxOffset) |
| 69 | { |
| 70 | $this->maxOffset = $maxOffset; |
| 71 | return $this; |
| 72 | } |
| 73 | public function getPhrase() |
| 74 | { |
| 75 | return $this->phrase; |
| 76 | } |
| 77 | public function testPhrase($phrase) |
| 78 | { |
| 79 | return $this->builder->niceize($phrase) == $this->builder->niceize($this->getPhrase()); |
| 80 | } |
| 81 | public static function create($phrase = null) |
| 82 | { |
| 83 | return new self($phrase); |
| 84 | } |
| 85 | public function setTextColor($r, $g, $b) |
| 86 | { |
| 87 | $this->textColor = array($r, $g, $b); |
| 88 | return $this; |
| 89 | } |
| 90 | public function setBackgroundColor($r, $g, $b) |
| 91 | { |
| 92 | $this->backgroundColor = array($r, $g, $b); |
| 93 | return $this; |
| 94 | } |
| 95 | public function setLineColor($r, $g, $b) |
| 96 | { |
| 97 | $this->lineColor = array($r, $g, $b); |
| 98 | return $this; |
| 99 | } |
| 100 | public function setIgnoreAllEffects($ignoreAllEffects) |
| 101 | { |
| 102 | $this->ignoreAllEffects = $ignoreAllEffects; |
| 103 | return $this; |
| 104 | } |
| 105 | public function setBackgroundImages(array $backgroundImages) |
| 106 | { |
| 107 | $this->backgroundImages = $backgroundImages; |
| 108 | return $this; |
| 109 | } |
| 110 | protected function drawLine($image, $width, $height, $tcol = null) |
| 111 | { |
| 112 | if ($this->lineColor === null) { |
| 113 | $red = $this->rand(100, 255); |
| 114 | $green = $this->rand(100, 255); |
| 115 | $blue = $this->rand(100, 255); |
| 116 | } else { |
| 117 | $red = $this->lineColor[0]; |
| 118 | $green = $this->lineColor[1]; |
| 119 | $blue = $this->lineColor[2]; |
| 120 | } |
| 121 | if ($tcol === null) { |
| 122 | $tcol = \imagecolorallocate($image, $red, $green, $blue); |
| 123 | } |
| 124 | if ($this->rand(0, 1)) { |
| 125 | // Horizontal |
| 126 | $Xa = $this->rand(0, $width / 2); |
| 127 | $Ya = $this->rand(0, $height); |
| 128 | $Xb = $this->rand($width / 2, $width); |
| 129 | $Yb = $this->rand(0, $height); |
| 130 | } else { |
| 131 | // Vertical |
| 132 | $Xa = $this->rand(0, $width); |
| 133 | $Ya = $this->rand(0, $height / 2); |
| 134 | $Xb = $this->rand(0, $width); |
| 135 | $Yb = $this->rand($height / 2, $height); |
| 136 | } |
| 137 | \imagesetthickness($image, $this->rand(1, 3)); |
| 138 | \imageline($image, $Xa, $Ya, $Xb, $Yb, $tcol); |
| 139 | } |
| 140 | protected function postEffect($image) |
| 141 | { |
| 142 | if (!\function_exists('imagefilter')) { |
| 143 | return; |
| 144 | } |
| 145 | if ($this->backgroundColor != null || $this->textColor != null) { |
| 146 | return; |
| 147 | } |
| 148 | // Negate ? |
| 149 | if ($this->rand(0, 1) == 0) { |
| 150 | \imagefilter($image, \IMG_FILTER_NEGATE); |
| 151 | } |
| 152 | // Edge ? |
| 153 | if ($this->rand(0, 10) == 0) { |
| 154 | \imagefilter($image, \IMG_FILTER_EDGEDETECT); |
| 155 | } |
| 156 | // Contrast |
| 157 | \imagefilter($image, \IMG_FILTER_CONTRAST, $this->rand(-50, 10)); |
| 158 | // Colorize |
| 159 | if ($this->rand(0, 5) == 0) { |
| 160 | \imagefilter($image, \IMG_FILTER_COLORIZE, $this->rand(-80, 50), $this->rand(-80, 50), $this->rand(-80, 50)); |
| 161 | } |
| 162 | } |
| 163 | protected function writePhrase($image, $phrase, $font, $width, $height) |
| 164 | { |
| 165 | $length = \mb_strlen($phrase); |
| 166 | if ($length === 0) { |
| 167 | return \imagecolorallocate($image, 0, 0, 0); |
| 168 | } |
| 169 | // Gets the text size and start position |
| 170 | $size = (int) \round($width / $length) - $this->rand(0, 3) - 1; |
| 171 | $box = \imagettfbbox($size, 0, $font, $phrase); |
| 172 | $textWidth = $box[2] - $box[0]; |
| 173 | $textHeight = $box[1] - $box[7]; |
| 174 | $x = (int) \round(($width - $textWidth) / 2); |
| 175 | $y = (int) \round(($height - $textHeight) / 2) + $size; |
| 176 | if (!$this->textColor) { |
| 177 | $textColor = array($this->rand(0, 150), $this->rand(0, 150), $this->rand(0, 150)); |
| 178 | } else { |
| 179 | $textColor = $this->textColor; |
| 180 | } |
| 181 | $col = \imagecolorallocate($image, $textColor[0], $textColor[1], $textColor[2]); |
| 182 | // Write the letters one by one, with random angle |
| 183 | for ($i = 0; $i < $length; $i++) { |
| 184 | $symbol = \mb_substr($phrase, $i, 1); |
| 185 | $box = \imagettfbbox($size, 0, $font, $symbol); |
| 186 | $w = $box[2] - $box[0]; |
| 187 | $angle = $this->rand(-$this->maxAngle, $this->maxAngle); |
| 188 | $offset = $this->rand(-$this->maxOffset, $this->maxOffset); |
| 189 | \imagettftext($image, $size, $angle, $x, $y + $offset, $col, $font, $symbol); |
| 190 | $x += $w; |
| 191 | } |
| 192 | return $col; |
| 193 | } |
| 194 | public function isOCRReadable() |
| 195 | { |
| 196 | if (!\is_dir($this->tempDir)) { |
| 197 | @\mkdir($this->tempDir, 0755, \true); |
| 198 | } |
| 199 | $tempj = $this->tempDir . \uniqid('captcha', \true) . '.jpg'; |
| 200 | $tempp = $this->tempDir . \uniqid('captcha', \true) . '.pgm'; |
| 201 | $this->save($tempj); |
| 202 | \shell_exec("convert {$tempj} {$tempp}"); |
| 203 | $value = \trim(\strtolower(\shell_exec("ocrad {$tempp}"))); |
| 204 | @\unlink($tempj); |
| 205 | @\unlink($tempp); |
| 206 | return $this->testPhrase($value); |
| 207 | } |
| 208 | public function buildAgainstOCR($width = 150, $height = 40, $font = null, $fingerprint = null) |
| 209 | { |
| 210 | do { |
| 211 | $this->build($width, $height, $font, $fingerprint); |
| 212 | } while ($this->isOCRReadable()); |
| 213 | } |
| 214 | public function build($width = 150, $height = 40, $font = null, $fingerprint = null) |
| 215 | { |
| 216 | if (null !== $fingerprint) { |
| 217 | $this->fingerprint = $fingerprint; |
| 218 | $this->useFingerprint = \true; |
| 219 | } else { |
| 220 | $this->fingerprint = array(); |
| 221 | $this->useFingerprint = \false; |
| 222 | } |
| 223 | if ($font === null) { |
| 224 | $font = __DIR__ . '/Font/captcha' . $this->rand(0, 5) . '.ttf'; |
| 225 | } |
| 226 | if (empty($this->backgroundImages)) { |
| 227 | // if background images list is not set, use a color fill as a background |
| 228 | $image = \imagecreatetruecolor($width, $height); |
| 229 | if ($this->backgroundColor == null) { |
| 230 | $bg = \imagecolorallocate($image, $this->rand(200, 255), $this->rand(200, 255), $this->rand(200, 255)); |
| 231 | } else { |
| 232 | $color = $this->backgroundColor; |
| 233 | $bg = \imagecolorallocate($image, $color[0], $color[1], $color[2]); |
| 234 | } |
| 235 | \imagefill($image, 0, 0, $bg); |
| 236 | } else { |
| 237 | // use a random background image |
| 238 | $randomBackgroundImage = $this->backgroundImages[\rand(0, \count($this->backgroundImages) - 1)]; |
| 239 | $imageType = $this->validateBackgroundImage($randomBackgroundImage); |
| 240 | $image = $this->createBackgroundImageFromType($randomBackgroundImage, $imageType); |
| 241 | } |
| 242 | // Apply effects |
| 243 | if (!$this->ignoreAllEffects) { |
| 244 | $square = $width * $height; |
| 245 | $effects = $this->rand($square / 3000, $square / 2000); |
| 246 | // set the maximum number of lines to draw in front of the text |
| 247 | if ($this->maxBehindLines != null && $this->maxBehindLines > 0) { |
| 248 | $effects = \min($this->maxBehindLines, $effects); |
| 249 | } |
| 250 | if ($this->maxBehindLines !== 0) { |
| 251 | for ($e = 0; $e < $effects; $e++) { |
| 252 | $this->drawLine($image, $width, $height); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | // Write CAPTCHA text |
| 257 | $color = $this->writePhrase($image, $this->phrase, $font, $width, $height); |
| 258 | // Apply effects |
| 259 | if (!$this->ignoreAllEffects) { |
| 260 | $square = $width * $height; |
| 261 | $effects = $this->rand($square / 3000, $square / 2000); |
| 262 | // set the maximum number of lines to draw in front of the text |
| 263 | if ($this->maxFrontLines != null && $this->maxFrontLines > 0) { |
| 264 | $effects = \min($this->maxFrontLines, $effects); |
| 265 | } |
| 266 | if ($this->maxFrontLines !== 0) { |
| 267 | for ($e = 0; $e < $effects; $e++) { |
| 268 | $this->drawLine($image, $width, $height, $color); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | // Distort the image |
| 273 | if ($this->distortion && !$this->ignoreAllEffects) { |
| 274 | $image = $this->distort($image, $width, $height, $bg); |
| 275 | } |
| 276 | // Post effects |
| 277 | if (!$this->ignoreAllEffects) { |
| 278 | $this->postEffect($image); |
| 279 | } |
| 280 | $this->contents = $image; |
| 281 | return $this; |
| 282 | } |
| 283 | public function distort($image, $width, $height, $bg) |
| 284 | { |
| 285 | $contents = \imagecreatetruecolor($width, $height); |
| 286 | $X = $this->rand(0, $width); |
| 287 | $Y = $this->rand(0, $height); |
| 288 | $phase = $this->rand(0, 10); |
| 289 | $scale = 1.1 + $this->rand(0, 10000) / 30000; |
| 290 | for ($x = 0; $x < $width; $x++) { |
| 291 | for ($y = 0; $y < $height; $y++) { |
| 292 | $Vx = $x - $X; |
| 293 | $Vy = $y - $Y; |
| 294 | $Vn = \sqrt($Vx * $Vx + $Vy * $Vy); |
| 295 | if ($Vn != 0) { |
| 296 | $Vn2 = $Vn + 4 * \sin($Vn / 30); |
| 297 | $nX = $X + $Vx * $Vn2 / $Vn; |
| 298 | $nY = $Y + $Vy * $Vn2 / $Vn; |
| 299 | } else { |
| 300 | $nX = $X; |
| 301 | $nY = $Y; |
| 302 | } |
| 303 | $nY = $nY + $scale * \sin($phase + $nX * 0.2); |
| 304 | if ($this->interpolation) { |
| 305 | $p = $this->interpolate($nX - \floor($nX), $nY - \floor($nY), $this->getCol($image, \floor($nX), \floor($nY), $bg), $this->getCol($image, \ceil($nX), \floor($nY), $bg), $this->getCol($image, \floor($nX), \ceil($nY), $bg), $this->getCol($image, \ceil($nX), \ceil($nY), $bg)); |
| 306 | } else { |
| 307 | $p = $this->getCol($image, \round($nX), \round($nY), $bg); |
| 308 | } |
| 309 | if ($p == 0) { |
| 310 | $p = $bg; |
| 311 | } |
| 312 | \imagesetpixel($contents, $x, $y, $p); |
| 313 | } |
| 314 | } |
| 315 | return $contents; |
| 316 | } |
| 317 | public function save($filename, $quality = 90) |
| 318 | { |
| 319 | \imagejpeg($this->contents, $filename, $quality); |
| 320 | } |
| 321 | public function getGd() |
| 322 | { |
| 323 | return $this->contents; |
| 324 | } |
| 325 | public function get($quality = 90) |
| 326 | { |
| 327 | \ob_start(); |
| 328 | $this->output($quality); |
| 329 | return \ob_get_clean(); |
| 330 | } |
| 331 | public function inline($quality = 90) |
| 332 | { |
| 333 | return 'data:image/jpeg;base64,' . \base64_encode($this->get($quality)); |
| 334 | } |
| 335 | public function output($quality = 90) |
| 336 | { |
| 337 | \imagejpeg($this->contents, null, $quality); |
| 338 | } |
| 339 | public function getFingerprint() |
| 340 | { |
| 341 | return $this->fingerprint; |
| 342 | } |
| 343 | protected function rand($min, $max) |
| 344 | { |
| 345 | if (!\is_array($this->fingerprint)) { |
| 346 | $this->fingerprint = array(); |
| 347 | } |
| 348 | if ($this->useFingerprint) { |
| 349 | $value = \current($this->fingerprint); |
| 350 | \next($this->fingerprint); |
| 351 | } else { |
| 352 | $value = \mt_rand((int) $min, (int) $max); |
| 353 | $this->fingerprint[] = $value; |
| 354 | } |
| 355 | return $value; |
| 356 | } |
| 357 | protected function interpolate($x, $y, $nw, $ne, $sw, $se) |
| 358 | { |
| 359 | list($r0, $g0, $b0) = $this->getRGB($nw); |
| 360 | list($r1, $g1, $b1) = $this->getRGB($ne); |
| 361 | list($r2, $g2, $b2) = $this->getRGB($sw); |
| 362 | list($r3, $g3, $b3) = $this->getRGB($se); |
| 363 | $cx = 1.0 - $x; |
| 364 | $cy = 1.0 - $y; |
| 365 | $m0 = $cx * $r0 + $x * $r1; |
| 366 | $m1 = $cx * $r2 + $x * $r3; |
| 367 | $r = (int) ($cy * $m0 + $y * $m1); |
| 368 | $m0 = $cx * $g0 + $x * $g1; |
| 369 | $m1 = $cx * $g2 + $x * $g3; |
| 370 | $g = (int) ($cy * $m0 + $y * $m1); |
| 371 | $m0 = $cx * $b0 + $x * $b1; |
| 372 | $m1 = $cx * $b2 + $x * $b3; |
| 373 | $b = (int) ($cy * $m0 + $y * $m1); |
| 374 | return $r << 16 | $g << 8 | $b; |
| 375 | } |
| 376 | protected function getCol($image, $x, $y, $background) |
| 377 | { |
| 378 | $L = \imagesx($image); |
| 379 | $H = \imagesy($image); |
| 380 | if ($x < 0 || $x >= $L || $y < 0 || $y >= $H) { |
| 381 | return $background; |
| 382 | } |
| 383 | return \imagecolorat($image, $x, $y); |
| 384 | } |
| 385 | protected function getRGB($col) |
| 386 | { |
| 387 | return array((int) ($col >> 16) & 0xff, (int) ($col >> 8) & 0xff, (int) $col & 0xff); |
| 388 | } |
| 389 | protected function validateBackgroundImage($backgroundImage) |
| 390 | { |
| 391 | // check if file exists |
| 392 | if (!\file_exists($backgroundImage)) { |
| 393 | $backgroundImageExploded = \explode('/', $backgroundImage); |
| 394 | $imageFileName = \count($backgroundImageExploded) > 1 ? $backgroundImageExploded[\count($backgroundImageExploded) - 1] : $backgroundImage; |
| 395 | throw new Exception('Invalid background image: ' . $imageFileName); |
| 396 | } |
| 397 | // check image type |
| 398 | $finfo = \finfo_open(\FILEINFO_MIME_TYPE); |
| 399 | // return mime type ala mimetype extension |
| 400 | $imageType = \finfo_file($finfo, $backgroundImage); |
| 401 | \finfo_close($finfo); |
| 402 | if (!\in_array($imageType, $this->allowedBackgroundImageTypes)) { |
| 403 | throw new Exception('Invalid background image type! Allowed types are: ' . \join(', ', $this->allowedBackgroundImageTypes)); |
| 404 | } |
| 405 | return $imageType; |
| 406 | } |
| 407 | protected function createBackgroundImageFromType($backgroundImage, $imageType) |
| 408 | { |
| 409 | switch ($imageType) { |
| 410 | case 'image/jpeg': |
| 411 | $image = \imagecreatefromjpeg($backgroundImage); |
| 412 | break; |
| 413 | case 'image/png': |
| 414 | $image = \imagecreatefrompng($backgroundImage); |
| 415 | break; |
| 416 | case 'image/gif': |
| 417 | $image = \imagecreatefromgif($backgroundImage); |
| 418 | break; |
| 419 | default: |
| 420 | throw new Exception('Not supported file type for background image!'); |
| 421 | } |
| 422 | return $image; |
| 423 | } |
| 424 | } |
| 425 |