Skip to content

Commit 425d38d

Browse files
committed
UPDATED
1 parent 1c6d0e0 commit 425d38d

13 files changed

+95
-80
lines changed

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
"Laravel reCAPTCHA"
1818
],
1919
"require": {
20-
"php": ">=5.5",
21-
"illuminate/support": "~5"
20+
"php" : ">=7.1.0",
21+
"illuminate/container": "~5.7.0",
22+
"illuminate/support": "~5.7.0",
23+
"illuminate/http": "~5.7.0",
24+
"guzzlehttp/guzzle": "^6.2"
2225
},
2326
"require-dev": {
2427
"phpunit/phpunit": "~7.0",

config/googlerecaptchav3.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
| Host Name
2020
|--------------------------------------------------------------------------
2121
| Type: string
22+
| Default will be empty, assign value only if you want domain check with Google response
2223
| Google reCAPTCHA host name, https://www.google.com/recaptcha/admin
2324
|
2425
*/
@@ -73,7 +74,6 @@
7374
'setting' => [
7475
[
7576
'action' => 'contact_us',
76-
'id' => 'contact_us_id',
7777
'threshold' => 0,
7878
'is_enabled' => false
7979
]
@@ -82,12 +82,11 @@
8282
|--------------------------------------------------------------------------
8383
| Options
8484
|--------------------------------------------------------------------------
85-
| Used for request
85+
| Custom option field for your request setting, which will be used for RequestClientInterface
8686
|
8787
*/
8888
'options' => [
89-
'curl_timeout' => 1,
90-
'curl_verify' => 1,
89+
9190
],
9291
/*
9392
|--------------------------------------------------------------------------
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<input type="hidden" id="{{$id}}" name="{{$name}}">
2+
3+
<script>
4+
grecaptcha.ready(function () {
5+
grecaptcha.execute('{{$publicKey}}', {action: '{{$action}}'}).then(function (token) {
6+
console.log(token);
7+
document.getElementById('{{$id}}').value = token;
8+
});
9+
});
10+
</script>

resources/views/googlerecaptcha/googlerecaptchav3.blade.php

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<script src="https://www.google.com/recaptcha/api.js?render={{$publicKey}}"></script>

src/Configurations/ReCaptchaConfigV3.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,6 @@ public function getSecretKey()
4747
return config('googlerecaptchav3.secret_key');
4848
}
4949

50-
/**
51-
* @return int
52-
*/
53-
public function getCurlTimeout()
54-
{
55-
return config('googlerecaptchav3.options.curl_timeout');
56-
}
57-
58-
/**
59-
* @return bool
60-
*/
61-
public function getCurlVerify()
62-
{
63-
return config('googlerecaptchav3.options.curl_verify');
64-
}
65-
6650
/**
6751
* @return string
6852
*/
@@ -93,7 +77,7 @@ public function getOptions()
9377
/**
9478
* @return array
9579
*/
96-
public function getScoreSetting()
80+
public function getSetting()
9781
{
9882
return config('googlerecaptchav3.setting');
9983
}

src/Core/CurlRequestClient.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,9 @@ public function post($url, $body, $options = [])
2020
curl_setopt($curl, CURLOPT_HEADER, false);
2121
curl_setopt($curl, CURLOPT_POST, true);
2222
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
23-
curl_setopt($curl, CURLOPT_TIMEOUT, $options['curl_timeout']);
2423
curl_setopt($curl, CURLOPT_POSTFIELDS,
2524
http_build_query($body));
2625

27-
if ($options['curl_verify'] === false) {
28-
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
29-
}
30-
3126
$response = curl_exec($curl);
3227

3328
if (false === $response) {

src/Core/GoogleReCaptchaV3Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct($data, $ip, $message = '')
3131
$this->hostname = isset($data['hostname']) ? $data['hostname'] : '';
3232
$this->errorCodes = isset($data['error-codes']) ? $data['error-codes'] : [];
3333
$this->ip = $ip;
34-
$this->message = $this->errorCodes ? $this->errorCodes[0] : '';
34+
$this->message = $this->errorCodes ? $this->errorCodes[0] : $message;
3535
}
3636

3737
/**

src/Core/GuzzleRequestClient.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: dadeng
5+
* Date: 2018/11/23
6+
* Time: 下午7:24
7+
*/
8+
9+
namespace RyanDeng\GoogleReCaptcha\Core;
10+
11+
12+
use GuzzleHttp\Exception\ClientException;
13+
use RyanDeng\GoogleReCaptcha\Interfaces\RequestClientInterface;
14+
use GuzzleHttp\Client;
15+
16+
17+
class GuzzleRequestClient implements RequestClientInterface
18+
{
19+
20+
public function post($url, $body, $options = [])
21+
{
22+
$client = new Client();
23+
try {
24+
$response = $client->post($url, [
25+
'form_params' => $body,
26+
]);
27+
return $response->getBody();
28+
} catch (ClientException $e) {
29+
$response = $e->getResponse();
30+
$responseBodyAsString = $response->getBody()->getContents();
31+
app('log')->error('[Recaptcha] Guzzle error: ' . $responseBodyAsString);
32+
return $responseBodyAsString;
33+
}
34+
}
35+
}

src/Facades/GoogleReCaptchaV3.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* @method static GoogleReCaptchaV3Response verifyResponse(array $data)
1010
* @method static \RyanDeng\GoogleReCaptcha\GoogleReCaptchaV3 setAction(string $value)
11-
* @method static render(...$names)
11+
* @method static render($action)
1212
* @see ReCaptcha
1313
*/
1414
class GoogleReCaptchaV3 extends Facade

src/GoogleReCaptchaV3.php

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,44 @@ class GoogleReCaptchaV3
1717
private $config;
1818
private $requestClient;
1919
private $action;
20-
private $defaultView = 'GoogleReCaptchaV3::googlerecaptcha.googlerecaptchav3';
20+
private $defaultFieldView = 'GoogleReCaptchaV3::googlerecaptcha.field';
21+
private $defaultHeaderView = 'GoogleReCaptchaV3::googlerecaptcha.header';
22+
private $request;
2123

2224
public function __construct(ReCaptchaConfigV3Interface $config, RequestClientInterface $requestClient)
2325
{
2426
$this->config = $config;
2527
$this->requestClient = $requestClient;
28+
$this->request = app('request');
2629
}
2730

28-
/**
29-
* @param mixed ...$actions
30-
* @return mixed
31-
*/
32-
public function render(...$actions)
31+
32+
public function render($action, $name = 'g-recaptcha-response')
3333
{
3434
if ($this->config->isServiceEnabled() === false) {
3535
return null;
3636
}
3737

38-
$mapping = collect($this->config->getScoreSetting())
39-
->whereIn('action', $actions)
40-
->pluck('id', 'action')
41-
->toArray();
42-
4338
$data = [
4439
'publicKey' => value($this->config->getSiteKey()),
45-
'rows' => $mapping
40+
'action' => $action,
41+
'name' => $name,
42+
'id' => uniqid($name . '-', false)
4643
];
4744

4845
$view = $this->getView();
4946

5047
return app('view')->make($view, $data);
5148
}
5249

50+
public function init()
51+
{
52+
return app('view')->make(
53+
$this->defaultHeaderView,
54+
['publicKey' => $this->getConfig()->getSiteKey()]
55+
);
56+
}
57+
5358
/**
5459
* @return mixed|string
5560
*/
@@ -58,27 +63,29 @@ protected function getView()
5863
$configTemplate = $this->config->getTemplate();
5964

6065
if (!empty($configTemplate)) {
61-
$this->defaultView = $configTemplate;
66+
$this->defaultFieldView = $configTemplate;
6267
}
63-
return $this->defaultView;
68+
return $this->defaultFieldView;
6469
}
6570

6671
/**
6772
* @param $response
68-
* @param null $ip
6973
* @return GoogleReCaptchaV3Response
7074
*/
71-
public function verifyResponse($response, $ip = null)
75+
public function verifyResponse($response)
7276
{
7377

78+
$ip = $this->request->getClientIp();
7479
if (!$this->config->isServiceEnabled()) {
7580
$res = new GoogleReCaptchaV3Response([], $ip);
7681
$res->setSuccess(true);
7782
return $res;
7883
}
7984

8085
if (empty($response)) {
81-
return new GoogleReCaptchaV3Response([], $ip, 'Missing input response.');
86+
$res = new GoogleReCaptchaV3Response([], $ip, 'Missing input response.');
87+
$res->setSuccess(false);
88+
return $res;
8289
}
8390

8491
$verifiedResponse = $this->requestClient->post(
@@ -88,10 +95,7 @@ public function verifyResponse($response, $ip = null)
8895
'remoteip' => $ip,
8996
'response' => $response,
9097
],
91-
[
92-
'curl_timeout' => $this->config->getCurlTimeout(),
93-
'curl_verify' => $this->config->getCurlVerify(),
94-
]
98+
$this->config->getOptions()
9599
);
96100

97101
if (is_null($verifiedResponse) || empty($verifiedResponse)) {
@@ -105,7 +109,7 @@ public function verifyResponse($response, $ip = null)
105109
return $rawResponse;
106110
}
107111

108-
if (strcasecmp($this->config->getHostName(), $rawResponse->getHostname()) !== 0) {
112+
if (!empty($this->config->getHostName()) && strcasecmp($this->config->getHostName(), $rawResponse->getHostname()) !== 0) {
109113
$rawResponse->setMessage('Hostname does not match.');
110114
$rawResponse->setSuccess(false);
111115
return $rawResponse;
@@ -118,7 +122,7 @@ public function verifyResponse($response, $ip = null)
118122
}
119123

120124
if ($this->getConfig()->isScoreEnabled()) {
121-
$count = collect($this->getConfig()->getScoreSetting())
125+
$count = collect($this->getConfig()->getSetting())
122126
->where('action', '=', $rawResponse->getAction())
123127
->where('is_enabled', '=', true)
124128
->where('threshold', '>', $rawResponse->getScore())

src/Interfaces/ReCaptchaConfigV3Interface.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,6 @@ public function isScoreEnabled();
2727
*/
2828
public function getSecretKey();
2929

30-
/**
31-
* @return int
32-
*/
33-
public function getCurlTimeout();
34-
35-
/**
36-
* @return bool
37-
*/
38-
public function getCurlVerify();
39-
4030
/**
4131
* @return string
4232
*/
@@ -55,7 +45,7 @@ public function getOptions();
5545
/**
5646
* @return array
5747
*/
58-
public function getScoreSetting();
48+
public function getSetting();
5949

6050
/**
6151
* @return string

src/Providers/GoogleReCaptchaV3ServiceProvider.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55

66
use RyanDeng\GoogleReCaptcha\Configurations\ReCaptchaConfigV3;
7-
use RyanDeng\GoogleReCaptcha\Core\CurlRequestClient;
7+
use RyanDeng\GoogleReCaptcha\Core\GuzzleRequestClient;
88
use RyanDeng\GoogleReCaptcha\GoogleReCaptchaV3;
99
use RyanDeng\GoogleReCaptcha\Interfaces\ReCaptchaConfigV3Interface;
1010
use RyanDeng\GoogleReCaptcha\Interfaces\RequestClientInterface;
1111
use Illuminate\Support\ServiceProvider;
12-
13-
12+
use \RyanDeng\GoogleReCaptcha\Facades\GoogleReCaptchaV3 as GoogleReCaptchaV3Facade;
13+
use Illuminate\Support\Facades\Blade;
1414
class GoogleReCaptchaV3ServiceProvider extends ServiceProvider
1515
{
1616

@@ -30,6 +30,11 @@ public function boot()
3030
if ($this->app->runningInConsole()) {
3131
$this->bootForConsole();
3232
}
33+
34+
Blade::directive('recaptchav3', function ($expression) {
35+
return GoogleReCaptchaV3Facade::render($expression);
36+
});
37+
3338
}
3439

3540

@@ -47,10 +52,10 @@ public function register()
4752
ReCaptchaConfigV3Interface::class,
4853
ReCaptchaConfigV3::class
4954
);
50-
55+
5156
$this->app->bind(
5257
RequestClientInterface::class,
53-
CurlRequestClient::class
58+
GuzzleRequestClient::class
5459
);
5560

5661
$this->app->bind('GoogleReCaptchaV3', function () {

0 commit comments

Comments
 (0)