forked from mewebstudio/captcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaptchaServiceProvider.php
86 lines (77 loc) · 2.9 KB
/
CaptchaServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace Mews\Captcha;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\Factory;
/**
* Class CaptchaServiceProvider
* @package Mews\Captcha
*/
class CaptchaServiceProvider extends ServiceProvider
{
/**
* Boot the service provider.
*
* @return void
*/
public function boot(): void
{
// Publish configuration files
$this->publishes([
__DIR__ . '/../config/captcha.php' => config_path('captcha.php')
], 'config');
// HTTP routing
if(!config('captcha.disable')){
if (strpos($this->app->version(), 'Lumen') !== false) {
/* @var Router $router */
$router = $this->app;
$router->get('captcha[/api/{config}]', 'Mews\Captcha\LumenCaptchaController@getCaptchaApi');
$router->get('captcha[/{config}]', 'Mews\Captcha\LumenCaptchaController@getCaptcha');
} else {
/* @var Router $router */
$router = $this->app['router'];
if ((double)$this->app->version() >= 5.2) {
$router->get('captcha/api/{config?}', '\Mews\Captcha\CaptchaController@getCaptchaApi')->middleware('web');
$router->get('captcha/{config?}', '\Mews\Captcha\CaptchaController@getCaptcha')->middleware('web');
} else {
$router->get('captcha/api/{config?}', '\Mews\Captcha\CaptchaController@getCaptchaApi');
$router->get('captcha/{config?}', '\Mews\Captcha\CaptchaController@getCaptcha');
}
}
}
/* @var Factory $validator */
$validator = $this->app['validator'];
// Validator extensions
$validator->extend('captcha', function ($attribute, $value, $parameters) {
return config('captcha.disable') || ($value && captcha_check($value));
});
// Validator extensions
$validator->extend('captcha_api', function ($attribute, $value, $parameters) {
return config('captcha.disable') || ($value && captcha_api_check($value, $parameters[0], $parameters[1] ?? 'default'));
});
}
/**
* Register the service provider.
*
* @return void
*/
public function register(): void
{
// Merge configs
$this->mergeConfigFrom(
__DIR__ . '/../config/captcha.php',
'captcha'
);
// Bind captcha
$this->app->bind('captcha', function ($app) {
return new Captcha(
$app['Illuminate\Filesystem\Filesystem'],
$app['Illuminate\Contracts\Config\Repository'],
$app['Intervention\Image\ImageManager'],
$app['Illuminate\Session\Store'],
$app['Illuminate\Hashing\BcryptHasher'],
$app['Illuminate\Support\Str']
);
});
}
}