Validator
1 month ago
BehavioralSignals.php
1 month 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
5 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
CaptchaSession.php
61 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Captcha; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Util\Security; |
| 9 | use MailPoet\WP\Functions as WPFunctions; |
| 10 | |
| 11 | class CaptchaSession { |
| 12 | const EXPIRATION = 1800; // 30 minutes |
| 13 | const ID_LENGTH = 32; |
| 14 | |
| 15 | const SESSION_HASH_KEY = 'hash'; |
| 16 | const SESSION_FORM_KEY = 'form'; |
| 17 | |
| 18 | private WPFunctions $wp; |
| 19 | |
| 20 | public function __construct( |
| 21 | WPFunctions $wp |
| 22 | ) { |
| 23 | $this->wp = $wp; |
| 24 | } |
| 25 | |
| 26 | public function generateSessionId(): string { |
| 27 | return Security::generateRandomString(self::ID_LENGTH); |
| 28 | } |
| 29 | |
| 30 | public function reset(string $sessionId): void { |
| 31 | $formKey = $this->getKey($sessionId, self::SESSION_FORM_KEY); |
| 32 | $hashKey = $this->getKey($sessionId, self::SESSION_HASH_KEY); |
| 33 | $this->wp->deleteTransient($formKey); |
| 34 | $this->wp->deleteTransient($hashKey); |
| 35 | } |
| 36 | |
| 37 | public function setFormData(string $sessionId, array $data): void { |
| 38 | $key = $this->getKey($sessionId, self::SESSION_FORM_KEY); |
| 39 | $this->wp->setTransient($key, $data, self::EXPIRATION); |
| 40 | } |
| 41 | |
| 42 | public function getFormData(string $sessionId) { |
| 43 | $key = $this->getKey($sessionId, self::SESSION_FORM_KEY); |
| 44 | return $this->wp->getTransient($key); |
| 45 | } |
| 46 | |
| 47 | public function setCaptchaHash(string $sessionId, $hash): void { |
| 48 | $key = $this->getKey($sessionId, self::SESSION_HASH_KEY); |
| 49 | $this->wp->setTransient($key, $hash, self::EXPIRATION); |
| 50 | } |
| 51 | |
| 52 | public function getCaptchaHash(string $sessionId) { |
| 53 | $key = $this->getKey($sessionId, self::SESSION_HASH_KEY); |
| 54 | return $this->wp->getTransient($key); |
| 55 | } |
| 56 | |
| 57 | private function getKey(string $sessionId, string $type): string { |
| 58 | return implode('_', ['MAILPOET', $sessionId, $type]); |
| 59 | } |
| 60 | } |
| 61 |