|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the ReCaptcha Validator Component. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Ilya Pokamestov |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace DS\Component\ReCaptchaValidator\Validator; |
|
12
|
|
|
|
|
13
|
|
|
use DS\Library\ReCaptcha\Http\Driver\DriverInterface; |
|
14
|
|
|
use DS\Library\ReCaptcha\ReCaptcha; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
17
|
|
|
use Symfony\Component\Validator\Constraint; |
|
18
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
|
19
|
|
|
use Symfony\Component\Validator\Exception\InvalidArgumentException; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* ReCaptcha Validator. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Ilya Pokamestov <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class ReCaptchaValidator extends ConstraintValidator |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var Request */ |
|
29
|
|
|
protected $request; |
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
protected $privateKey; |
|
32
|
|
|
/** @var DriverInterface */ |
|
33
|
|
|
protected $driver; |
|
34
|
|
|
/** @var bool */ |
|
35
|
|
|
protected $enabled; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param Request|RequestStack $request |
|
39
|
|
|
* @param string $privateKey |
|
40
|
|
|
* @param DriverInterface $driver |
|
41
|
|
|
* @param bool $enabled |
|
42
|
|
|
* |
|
43
|
|
|
* @throws \InvalidArgumentException |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct($request, $privateKey, DriverInterface $driver = null, $enabled = true) |
|
46
|
|
|
{ |
|
47
|
|
|
// Typehint the $request argument for RequestStack when dropping support for Symfony 2.3 |
|
48
|
|
|
if ($request instanceof Request) { |
|
49
|
|
|
$this->request = $request; |
|
50
|
|
|
} elseif ($request instanceof RequestStack) { |
|
51
|
|
|
$this->request = $request->getCurrentRequest(); |
|
52
|
|
|
} else { |
|
53
|
|
|
throw new \InvalidArgumentException( |
|
54
|
|
|
'Argument 1 should be an instance of Symfony\Component\HttpFoundation\Request or ' |
|
55
|
|
|
.'Symfony\Component\HttpFoundation\RequestStack' |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$this->privateKey = $privateKey; |
|
60
|
|
|
$this->driver = $driver; |
|
61
|
|
|
$this->enabled = $enabled; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function validate($value, Constraint $constraint) |
|
68
|
|
|
{ |
|
69
|
|
|
if (!($constraint instanceof ReCaptchaConstraint)) { |
|
70
|
|
|
throw new InvalidArgumentException('Use ReCaptchaConstraint for ReCaptchaValidator.'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (false === $this->enabled || false === $constraint->enabled) { |
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if ($this->request->get('g-recaptcha-response', false)) { |
|
78
|
|
|
$reCaptcha = new ReCaptcha( |
|
79
|
|
|
$this->privateKey, |
|
80
|
|
|
$this->request->getClientIp(), |
|
81
|
|
|
$this->request->get('g-recaptcha-response', false) |
|
82
|
|
|
); |
|
83
|
|
|
$response = $reCaptcha->buildRequest($this->driver)->send(); |
|
84
|
|
|
if (!$response->isSuccess()) { |
|
85
|
|
|
$this->context->addViolation($constraint->message); |
|
86
|
|
|
} |
|
87
|
|
|
} else { |
|
88
|
|
|
$this->context->addViolation($constraint->message); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|