1
|
|
|
<?php namespace Arcanedev\NoCaptcha; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Support\PackageServiceProvider as ServiceProvider; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class NoCaptchaServiceProvider |
7
|
|
|
* |
8
|
|
|
* @package Arcanedev\NoCaptcha |
9
|
|
|
* @author ARCANEDEV <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class NoCaptchaServiceProvider extends ServiceProvider |
12
|
|
|
{ |
13
|
|
|
/* ------------------------------------------------------------------------------------------------ |
14
|
|
|
| Properties |
15
|
|
|
| ------------------------------------------------------------------------------------------------ |
16
|
|
|
*/ |
17
|
|
|
/** |
18
|
|
|
* Package name. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $package = 'no-captcha'; |
23
|
|
|
|
24
|
|
|
/* ------------------------------------------------------------------------------------------------ |
25
|
|
|
| Getters & Setters |
26
|
|
|
| ------------------------------------------------------------------------------------------------ |
27
|
|
|
*/ |
28
|
|
|
/** |
29
|
|
|
* Get the base path of the package. |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
84 |
|
public function getBasePath() |
34
|
|
|
{ |
35
|
84 |
|
return dirname(__DIR__); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/* ------------------------------------------------------------------------------------------------ |
39
|
|
|
| Main Functions |
40
|
|
|
| ------------------------------------------------------------------------------------------------ |
41
|
|
|
*/ |
42
|
|
|
/** |
43
|
|
|
* Register the service provider. |
44
|
|
|
*/ |
45
|
84 |
|
public function register() |
46
|
|
|
{ |
47
|
84 |
|
$this->registerConfig(); |
48
|
84 |
|
$this->registerNoCaptcha(); |
49
|
84 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Bootstrap the application events. |
53
|
|
|
*/ |
54
|
84 |
|
public function boot() |
55
|
|
|
{ |
56
|
84 |
|
parent::boot(); |
57
|
|
|
|
58
|
84 |
|
$this->publishConfig(); |
59
|
84 |
|
$this->registerValidatorRules($this->app); |
60
|
84 |
|
$this->registerFormMacros($this->app); |
61
|
84 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the services provided by the provider. |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
12 |
|
public function provides() |
69
|
|
|
{ |
70
|
|
|
return [ |
71
|
12 |
|
Contracts\NoCaptcha::class, |
72
|
4 |
|
'arcanedev.no-captcha', |
73
|
4 |
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/* ------------------------------------------------------------------------------------------------ |
77
|
|
|
| Other Functions |
78
|
|
|
| ------------------------------------------------------------------------------------------------ |
79
|
|
|
*/ |
80
|
|
|
/** |
81
|
|
|
* Register NoCaptcha service. |
82
|
|
|
*/ |
83
|
84 |
|
private function registerNoCaptcha() |
84
|
|
|
{ |
85
|
|
|
$this->singleton(Contracts\NoCaptcha::class, function($app) { |
86
|
|
|
/** @var \Illuminate\Config\Repository $config */ |
87
|
48 |
|
$config = $app['config']; |
88
|
|
|
|
89
|
48 |
|
return new NoCaptcha( |
90
|
48 |
|
$config->get('no-captcha.secret'), |
91
|
48 |
|
$config->get('no-captcha.sitekey'), |
92
|
48 |
|
$config->get('no-captcha.lang'), |
93
|
48 |
|
$config->get('no-captcha.attributes', []) |
94
|
16 |
|
); |
95
|
84 |
|
}); |
96
|
|
|
|
97
|
84 |
|
$this->singleton('arcanedev.no-captcha', Contracts\NoCaptcha::class); |
98
|
84 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Register Validator rules. |
102
|
|
|
* |
103
|
|
|
* @param \Illuminate\Foundation\Application $app |
104
|
|
|
*/ |
105
|
84 |
|
private function registerValidatorRules($app) |
106
|
|
|
{ |
107
|
|
|
$app['validator']->extend('captcha', function($attribute, $value) use ($app) { |
108
|
36 |
|
unset($attribute); |
109
|
36 |
|
$ip = $app['request']->getClientIp(); |
110
|
|
|
|
111
|
36 |
|
return $app[Contracts\NoCaptcha::class]->verify($value, $ip); |
112
|
84 |
|
}); |
113
|
84 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Register Form Macros. |
117
|
|
|
* |
118
|
|
|
* @param \Illuminate\Foundation\Application $app |
119
|
|
|
*/ |
120
|
84 |
|
private function registerFormMacros($app) |
121
|
|
|
{ |
122
|
84 |
|
if ($app->bound('form')) { |
123
|
|
|
$app['form']->macro('captcha', function($name = null, array $attributes = []) use ($app) { |
124
|
|
|
return $app[Contracts\NoCaptcha::class]->display($name, $attributes); |
125
|
|
|
}); |
126
|
|
|
} |
127
|
84 |
|
} |
128
|
|
|
} |
129
|
|
|
|