|
1
|
|
|
<?php |
|
2
|
|
|
namespace ReCaptcha2\Form\View\Helper\Captcha; |
|
3
|
|
|
|
|
4
|
|
|
use Traversable; |
|
5
|
|
|
use Zend\Captcha\AdapterInterface; |
|
6
|
|
|
use Zend\Form\Element\Captcha; |
|
7
|
|
|
use Zend\Form\ElementInterface; |
|
8
|
|
|
use Zend\Form\Exception; |
|
9
|
|
|
use Zend\Form\View\Helper\FormInput; |
|
10
|
|
|
|
|
11
|
|
|
class ReCaptcha2 extends FormInput |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @param ElementInterface $element |
|
15
|
|
|
* @return string |
|
16
|
|
|
*/ |
|
17
|
|
|
public function __invoke(ElementInterface $element = null) |
|
18
|
|
|
{ |
|
19
|
|
|
if (!$element) { |
|
20
|
|
|
return $this; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
return $this->render($element); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param ElementInterface $element |
|
28
|
|
|
* @throws Exception\DomainException |
|
29
|
|
|
* @return string |
|
30
|
|
|
*/ |
|
31
|
|
|
public function render(ElementInterface $element) |
|
32
|
|
|
{ |
|
33
|
|
|
if (!$element instanceof Captcha) { |
|
34
|
|
|
throw new Exception\InvalidArgumentException(sprintf( |
|
35
|
|
|
'%s expects a valid implementation of Zend\Form\Element\Captcha; received "%s"', |
|
36
|
|
|
__METHOD__, |
|
37
|
|
|
(is_object($element) ? get_class($element) : gettype($element)) |
|
38
|
|
|
)); |
|
39
|
|
|
} |
|
40
|
|
|
$captcha = $element->getCaptcha(); |
|
41
|
|
|
|
|
42
|
|
|
if ($captcha === null || !$captcha instanceof AdapterInterface) { |
|
43
|
|
|
throw new Exception\DomainException(sprintf( |
|
44
|
|
|
'%s requires that the element has a "captcha" attribute implementing %s; none found', |
|
45
|
|
|
__METHOD__, |
|
46
|
|
|
AdapterInterface::class |
|
47
|
|
|
)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/* @var $service \ReCaptcha2\Captcha\ServiceInterface */ |
|
51
|
|
|
$service = $captcha->getService(); |
|
52
|
|
|
|
|
53
|
|
|
$siteKey = $service->getSiteKey(); |
|
54
|
|
|
if ($siteKey === null) { |
|
55
|
|
|
throw new Exception\DomainException('Missing site key'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$params = array_filter($service->getParams(), function ($param) { |
|
59
|
|
|
return $param !== null; |
|
60
|
|
|
}); |
|
61
|
|
|
|
|
62
|
|
|
$apiPath = $service->getServiceUri(); |
|
63
|
|
|
$scriptPath = sprintf('%s.js', $apiPath); |
|
64
|
|
|
$queryString = ''; |
|
65
|
|
|
if (!empty($params)) { |
|
66
|
|
|
$queryString = http_build_query($params, '', '&'); |
|
67
|
|
|
$scriptPath .= '?' . $queryString; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$attributes = $element->getAttributes(); |
|
71
|
|
|
if ($attributes instanceof Traversable) { |
|
72
|
|
|
$attributes = iterator_to_array($attributes); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$attributes['class'] = 'g-recaptcha'; |
|
76
|
|
|
$attributes['data-sitekey'] = $siteKey; |
|
77
|
|
|
$attributeString = $this->createAttributesString($attributes); |
|
78
|
|
|
|
|
79
|
|
|
$template = <<<HTML |
|
80
|
|
|
<input type="hidden" name="%s" value="1"> |
|
81
|
|
|
<div %s></div> |
|
82
|
|
|
<script src="%s" async defer></script> |
|
83
|
|
|
HTML; |
|
84
|
|
|
$markup = sprintf($template, $element->getName(), $attributeString, $scriptPath); |
|
85
|
|
|
|
|
86
|
|
|
$queryString .= ($queryString ? '&' : ''); |
|
87
|
|
|
$markup .= <<<HTML |
|
88
|
|
|
<noscript> |
|
89
|
|
|
<div style="width: 302px; height: 422px;"> |
|
90
|
|
|
<div style="width: 302px; height: 422px; position: relative;"> |
|
91
|
|
|
<div style="width: 302px; height: 422px; position: absolute;"> |
|
92
|
|
|
<iframe src="{$apiPath}/fallback?{$queryString}k={$siteKey}" |
|
93
|
|
|
frameborder="0" scrolling="no" |
|
94
|
|
|
style="width: 302px; height:422px; border-style: none;"> |
|
95
|
|
|
</iframe> |
|
96
|
|
|
</div> |
|
97
|
|
|
<div style="width: 300px; height: 60px; border-style: none; |
|
98
|
|
|
bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px; |
|
99
|
|
|
background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px;"> |
|
100
|
|
|
<textarea id="g-recaptcha-response" name="g-recaptcha-response" |
|
101
|
|
|
class="g-recaptcha-response" |
|
102
|
|
|
style="width: 250px; height: 40px; border: 1px solid #c1c1c1; |
|
103
|
|
|
margin: 10px 25px; padding: 0px; resize: none;" > |
|
104
|
|
|
</textarea> |
|
105
|
|
|
</div> |
|
106
|
|
|
</div> |
|
107
|
|
|
</div> |
|
108
|
|
|
</noscript> |
|
109
|
|
|
HTML; |
|
110
|
|
|
|
|
111
|
|
|
return $markup; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|