1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Del\Form\Field\Captcha; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Del\Form\Validator\ValidatorInterface; |
7
|
|
|
use Del\SessionManager; |
8
|
|
|
use Zend\Text\Figlet\Figlet; |
9
|
|
|
|
10
|
|
|
class FigletCaptcha implements CaptchaAdapterInterface, ValidatorInterface |
11
|
|
|
{ |
12
|
|
|
/** @var SessionManager $session */ |
13
|
|
|
private $session; |
14
|
|
|
|
15
|
|
|
/** @var string $timeout */ |
16
|
|
|
private $timeout; |
17
|
|
|
|
18
|
|
|
/** @var string $timeout */ |
19
|
|
|
private $length; |
20
|
|
|
|
21
|
|
|
/** @var string $word */ |
22
|
|
|
private $word; |
23
|
|
|
|
24
|
|
|
/** @var array $errors */ |
25
|
|
|
private $errors = []; |
26
|
|
|
|
27
|
|
|
public function __construct(SessionManager $session, string $timeout = '+3 minutes', int $length = 6) |
28
|
|
|
{ |
29
|
|
|
$this->session = $session; |
30
|
|
|
$this->timeout = $timeout; |
31
|
|
|
$this->length = $length; |
|
|
|
|
32
|
|
|
$this->generate(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
public function generate(): string |
39
|
|
|
{ |
40
|
|
|
$expiry = new DateTime(); |
41
|
|
|
$session = $this->session->get('captcha'); |
|
|
|
|
42
|
|
|
|
43
|
|
|
if ($session && $session['expiry'] > $expiry) { |
44
|
|
|
$this->word = $session['word']; |
45
|
|
|
} else { |
46
|
|
|
$this->word = $this->generateWord(); |
47
|
|
|
$expiry->modify($this->timeout); |
48
|
|
|
|
49
|
|
|
$this->session->set('captcha', [ |
50
|
|
|
'word' => $this->word, |
51
|
|
|
'expiry' => $expiry, |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->word; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function render(): string |
62
|
|
|
{ |
63
|
|
|
$word = $this->word; |
64
|
|
|
$figlet = new Figlet(); |
65
|
|
|
|
66
|
|
|
return '<div class="mono">' . ($figlet->render($word)) . '</div>'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
private function generateWord(): string |
73
|
|
|
{ |
74
|
|
|
$word = ''; |
75
|
|
|
|
76
|
|
|
for ($x = 0; $x < $this->length; $x ++) { |
77
|
|
|
$word .= chr(rand(97, 122)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $word; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param mixed $value |
85
|
|
|
* @return bool |
86
|
|
|
* @throws Exception If validation of $value is impossible |
87
|
|
|
*/ |
88
|
|
|
public function isValid($value) |
89
|
|
|
{ |
90
|
|
|
$now = new DateTime(); |
91
|
|
|
$captcha = $this->session->get('captcha'); |
|
|
|
|
92
|
|
|
$expiry = $captcha['expiry']; |
93
|
|
|
$word = $captcha['word']; |
94
|
|
|
|
95
|
|
|
if ($now > $expiry) { |
96
|
|
|
$this->errors = ['The CAPTCHA has expired']; |
97
|
|
|
|
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (!is_null($value) && $value !== $word) { |
102
|
|
|
$this->errors = ['The CAPTCHA value did not match.']; |
103
|
|
|
|
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (!is_null($value)) { |
108
|
|
|
$this->session->destroy('captcha'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function getMessages() |
118
|
|
|
{ |
119
|
|
|
return $this->errors; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.