Нет описания

CaptchaSession.php 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace MailPoet\Subscription;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Util\Security;
  5. use MailPoet\WP\Functions as WPFunctions;
  6. class CaptchaSession {
  7. const EXPIRATION = 1800; // 30 minutes
  8. const ID_LENGTH = 32;
  9. const SESSION_HASH_KEY = 'hash';
  10. const SESSION_FORM_KEY = 'form';
  11. /** @var WPFunctions */
  12. private $wp;
  13. /** @var string */
  14. private $id;
  15. public function __construct(
  16. WPFunctions $wp
  17. ) {
  18. $this->wp = $wp;
  19. }
  20. public function init($id = null) {
  21. $this->id = $id ?: Security::generateRandomString(self::ID_LENGTH);
  22. }
  23. public function getId() {
  24. if ($this->id === null) {
  25. throw new \Exception("MailPoet captcha session not initialized.");
  26. }
  27. return $this->id;
  28. }
  29. public function reset() {
  30. $this->wp->deleteTransient($this->getKey(self::SESSION_FORM_KEY));
  31. $this->wp->deleteTransient($this->getKey(self::SESSION_HASH_KEY));
  32. }
  33. public function setFormData(array $data) {
  34. $this->wp->setTransient($this->getKey(self::SESSION_FORM_KEY), $data, self::EXPIRATION);
  35. }
  36. public function getFormData() {
  37. return $this->wp->getTransient($this->getKey(self::SESSION_FORM_KEY));
  38. }
  39. public function setCaptchaHash($hash) {
  40. $this->wp->setTransient($this->getKey(self::SESSION_HASH_KEY), $hash, self::EXPIRATION);
  41. }
  42. public function getCaptchaHash() {
  43. return $this->wp->getTransient($this->getKey(self::SESSION_HASH_KEY));
  44. }
  45. private function getKey($type) {
  46. return implode('_', ['MAILPOET', $this->getId(), $type]);
  47. }
  48. }