1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2017 - present |
4
|
|
|
* LaravelGoogleRecaptcha - ReCaptchaBuilderV2.php |
5
|
|
|
* author: Roberto Belotti - [email protected] |
6
|
|
|
* web : robertobelotti.com, github.com/biscolab |
7
|
|
|
* Initial version created on: 12/9/2018 |
8
|
|
|
* MIT license: https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Biscolab\ReCaptcha; |
12
|
|
|
|
13
|
|
|
use Biscolab\ReCaptcha\Exceptions\InvalidConfigurationException; |
14
|
|
|
use Illuminate\Support\Arr; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class ReCaptchaBuilderV2 |
18
|
|
|
* @package Biscolab\ReCaptcha |
19
|
|
|
*/ |
20
|
|
|
class ReCaptchaBuilderV2 extends ReCaptchaBuilder |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* ReCaptchaBuilderV2 constructor. |
25
|
|
|
* |
26
|
|
|
* @param string $api_site_key |
27
|
|
|
* @param string $api_secret_key |
28
|
|
|
*/ |
29
|
28 |
|
public function __construct(string $api_site_key, string $api_secret_key) |
30
|
|
|
{ |
31
|
|
|
|
32
|
28 |
|
parent::__construct($api_site_key, $api_secret_key, 'v2'); |
33
|
28 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Write ReCAPTCHA HTML tag in your FORM |
37
|
|
|
* Insert before </form> tag |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
3 |
|
public function htmlFormSnippet(): string |
41
|
|
|
{ |
42
|
|
|
|
43
|
3 |
|
$data_attributes = []; |
44
|
3 |
|
$config_data_attributes = $this->getTagAttributes(); |
45
|
|
|
|
46
|
3 |
|
foreach ($config_data_attributes as $k => $v) { |
47
|
3 |
|
if ($v) { |
48
|
3 |
|
$data_attributes[] = 'data-' . $k . '="' . $v . '"'; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
$html = '<div class="g-recaptcha" ' . implode(" ", $data_attributes) . ' id="recaptcha-element"></div>'; |
53
|
|
|
|
54
|
3 |
|
return $html; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return array |
59
|
|
|
* @throws InvalidConfigurationException |
60
|
|
|
*/ |
61
|
8 |
|
public function getTagAttributes(): array |
62
|
|
|
{ |
63
|
|
|
|
64
|
|
|
$tag_attributes = [ |
65
|
8 |
|
'sitekey' => $this->api_site_key |
66
|
|
|
]; |
67
|
|
|
|
68
|
8 |
|
$tag_attributes = array_merge($tag_attributes, config('recaptcha.tag_attributes', [])); |
69
|
|
|
|
70
|
8 |
|
if (Arr::get($tag_attributes, 'callback') === ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION) { |
71
|
1 |
|
throw new InvalidConfigurationException('Property "callback" ("data-callback") must be different from "' . ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION . '"'); |
72
|
|
|
} |
73
|
|
|
|
74
|
7 |
|
if (Arr::get($tag_attributes, 'expired-callback') === ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION) { |
75
|
|
|
throw new InvalidConfigurationException('Property "expired-callback" ("data-expired-callback") must be different from "' . ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION . '"'); |
76
|
|
|
} |
77
|
|
|
|
78
|
7 |
|
if (Arr::get($tag_attributes, 'error-callback') === ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION) { |
79
|
|
|
throw new InvalidConfigurationException('Property "error-callback" ("data-error-callback") must be different from "' . ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION . '"'); |
80
|
|
|
} |
81
|
|
|
|
82
|
7 |
|
return $tag_attributes; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
3 |
|
public function getOnLoadCallback(): string |
89
|
|
|
{ |
90
|
|
|
|
91
|
3 |
|
$attributes = $this->getTagAttributes(); |
92
|
|
|
|
93
|
2 |
|
return "<script>var biscolabOnloadCallback = function() {grecaptcha.render('recaptcha-element', " . json_encode($attributes) . ");};</script>"; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |