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