Validator
2 months ago
BehavioralSignals.php
2 months ago
CaptchaConstants.php
2 months ago
CaptchaFormRenderer.php
2 weeks ago
CaptchaHooks.php
2 months ago
CaptchaPhrase.php
1 year ago
CaptchaRenderer.php
4 months ago
CaptchaSession.php
1 year ago
CaptchaUrlFactory.php
4 months ago
PageRenderer.php
6 months ago
ReCaptchaHooks.php
2 months ago
ReCaptchaRenderer.php
1 year ago
ReCaptchaValidator.php
1 year ago
TurnstileHooks.php
2 months ago
TurnstileRenderer.php
2 months ago
TurnstileValidator.php
2 months ago
index.php
1 year ago
CaptchaRenderer.php
87 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Captcha; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Config\Env; |
| 9 | use MailPoet\Util\Headers; |
| 10 | use MailPoetVendor\Gregwar\Captcha\CaptchaBuilder; |
| 11 | |
| 12 | class CaptchaRenderer { |
| 13 | const DEFAULT_WIDTH = 220; |
| 14 | const DEFAULT_HEIGHT = 60; |
| 15 | |
| 16 | private CaptchaPhrase $phrase; |
| 17 | |
| 18 | public function __construct( |
| 19 | CaptchaPhrase $phrase |
| 20 | ) { |
| 21 | $this->phrase = $phrase; |
| 22 | } |
| 23 | |
| 24 | public function isSupported(): bool { |
| 25 | return extension_loaded('gd') && function_exists('imagettftext'); |
| 26 | } |
| 27 | |
| 28 | public function renderAudio(string $sessionId): void { |
| 29 | $audioPath = Env::$assetsPath . '/audio/'; |
| 30 | $phrase = $this->getPhrase($sessionId); |
| 31 | |
| 32 | $files = []; |
| 33 | foreach (str_split($phrase) as $character) { |
| 34 | $file = $audioPath . strtolower($character) . '.mp3'; |
| 35 | if (!file_exists($file)) { |
| 36 | throw new \RuntimeException("File not found."); |
| 37 | } |
| 38 | $files[] = $file; |
| 39 | } |
| 40 | |
| 41 | Headers::setNoCacheHeaders(); |
| 42 | header('Content-Type: audio/mpeg'); |
| 43 | foreach ($files as $file) { |
| 44 | readfile($file); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | public function renderImage(string $sessionId): void { |
| 49 | if (!$this->isSupported()) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | $width = self::DEFAULT_WIDTH; |
| 54 | $height = self::DEFAULT_HEIGHT; |
| 55 | |
| 56 | $fontNumbers = array_merge(range(0, 3), [5]); // skip font #4 |
| 57 | $fontNumber = $fontNumbers[mt_rand(0, count($fontNumbers) - 1)]; |
| 58 | |
| 59 | $reflector = new \ReflectionClass(CaptchaBuilder::class); |
| 60 | $captchaDirectory = dirname((string)$reflector->getFileName()); |
| 61 | $font = $captchaDirectory . '/Font/captcha' . $fontNumber . '.ttf'; |
| 62 | |
| 63 | $phrase = $this->getPhrase($sessionId); |
| 64 | $builder = CaptchaBuilder::create($phrase) |
| 65 | ->setBackgroundColor(255, 255, 255) |
| 66 | ->setTextColor(1, 1, 1) |
| 67 | ->setMaxBehindLines(0) |
| 68 | ->build($width, $height, $font); |
| 69 | |
| 70 | Headers::setNoCacheHeaders(); |
| 71 | header('Content-Type: image/jpeg'); |
| 72 | $builder->output(); |
| 73 | } |
| 74 | |
| 75 | public function refreshPhrase(string $sessionId): string { |
| 76 | return $this->phrase->createPhrase($sessionId); |
| 77 | } |
| 78 | |
| 79 | private function getPhrase(string $sessionId): string { |
| 80 | $phrase = $this->phrase->getPhrase($sessionId); |
| 81 | if (!$phrase) { |
| 82 | throw new \RuntimeException("No CAPTCHA phrase was generated."); |
| 83 | } |
| 84 | return $phrase; |
| 85 | } |
| 86 | } |
| 87 |