1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2017 - present |
4
|
|
|
* LaravelGoogleRecaptcha - ReCaptchaServiceProvider.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 Illuminate\Support\Facades\Route; |
14
|
|
|
use Illuminate\Support\ServiceProvider; |
15
|
|
|
use Validator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ReCaptchaServiceProvider |
19
|
|
|
* @package Biscolab\ReCaptcha |
20
|
|
|
*/ |
21
|
|
|
class ReCaptchaServiceProvider extends ServiceProvider |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Indicates if loading of the provider is deferred. |
26
|
|
|
* |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
protected $defer = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
*/ |
34
|
31 |
|
public function boot() |
35
|
|
|
{ |
36
|
|
|
|
37
|
31 |
|
$this->addValidationRule(); |
38
|
|
|
// $this->loadRoutesFrom(__DIR__ . '/routes/routes.php'); |
39
|
31 |
|
$this->registerRoutes(); |
40
|
31 |
|
$this->publishes([ |
41
|
31 |
|
__DIR__ . '/../config/recaptcha.php' => config_path('recaptcha.php'), |
42
|
|
|
]); |
43
|
|
|
|
44
|
31 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Extends Validator to include a recaptcha type |
48
|
|
|
*/ |
49
|
31 |
|
public function addValidationRule() |
50
|
|
|
{ |
51
|
|
|
|
52
|
|
|
Validator::extendImplicit('recaptcha', function ($attribute, $value, $parameters, $validator) { |
|
|
|
|
53
|
|
|
|
54
|
|
|
return app('recaptcha')->validate($value); |
|
|
|
|
55
|
31 |
|
}, trans('validation.recaptcha')); |
|
|
|
|
56
|
31 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Register the service provider. |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
31 |
|
public function register() |
64
|
|
|
{ |
65
|
|
|
|
66
|
31 |
|
$this->mergeConfigFrom( |
67
|
31 |
|
__DIR__ . '/../config/recaptcha.php', 'recaptcha' |
68
|
|
|
); |
69
|
|
|
|
70
|
31 |
|
$this->registerReCaptchaBuilder(); |
71
|
31 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the services provided by the provider. |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
public function provides(): array |
79
|
|
|
{ |
80
|
|
|
|
81
|
|
|
return ['recaptcha']; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return ReCaptchaServiceProvider |
86
|
|
|
* |
87
|
|
|
* @since v3.4.1 |
88
|
|
|
*/ |
89
|
31 |
|
protected function registerRoutes(): ReCaptchaServiceProvider |
90
|
|
|
{ |
91
|
|
|
|
92
|
31 |
|
Route::get( |
93
|
31 |
|
config('recaptcha.default_validation_route', 'biscolab-recaptcha/validate'), |
94
|
31 |
|
['uses' => 'Biscolab\ReCaptcha\Controllers\ReCaptchaController@validateV3'] |
95
|
31 |
|
)->middleware('web'); |
96
|
|
|
|
97
|
31 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Register the HTML builder instance. |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
31 |
|
protected function registerReCaptchaBuilder() |
106
|
|
|
{ |
107
|
|
|
|
108
|
|
|
$this->app->singleton('recaptcha', function ($app) { |
|
|
|
|
109
|
|
|
|
110
|
11 |
|
$recaptcha_class = ''; |
111
|
|
|
|
112
|
11 |
|
switch (config('recaptcha.version')) { |
113
|
11 |
|
case 'v3' : |
114
|
1 |
|
$recaptcha_class = ReCaptchaBuilderV3::class; |
115
|
1 |
|
break; |
116
|
10 |
|
case 'v2' : |
117
|
6 |
|
$recaptcha_class = ReCaptchaBuilderV2::class; |
118
|
6 |
|
break; |
119
|
4 |
|
case 'invisible': |
120
|
4 |
|
$recaptcha_class = ReCaptchaBuilderInvisible::class; |
121
|
4 |
|
break; |
122
|
|
|
} |
123
|
|
|
|
124
|
11 |
|
return new $recaptcha_class(config('recaptcha.api_site_key'), config('recaptcha.api_secret_key')); |
125
|
|
|
|
126
|
31 |
|
}); |
127
|
31 |
|
} |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.