/
Captcha.php
40 lines (32 loc) · 977 Bytes
/
Captcha.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
/**
* This file is part of JohnCMS Content Management System.
*
* @copyright JohnCMS Community
* @license https://opensource.org/licenses/GPL-3.0 GPL-3.0
* @link https://johncms.com JohnCMS Project
*/
declare(strict_types=1);
namespace Johncms\Validator\Rules;
use Johncms\Http\Session;
use Laminas\Validator\AbstractValidator;
class Captcha extends AbstractValidator
{
public const CAPTCHA = 'captcha';
public const SESSION_FIELD = 'code';
protected array $messageTemplates = [
self::CAPTCHA => "The security code is not correct",
];
public function isValid($value): bool
{
$value = (string) $value;
$this->setValue($value);
$isValid = true;
$code = di(Session::class)->get(Captcha::SESSION_FIELD);
if (empty($code) || strtolower($code) !== strtolower($value)) {
$this->error(self::CAPTCHA);
$isValid = false;
}
return $isValid;
}
}