1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\NoCaptcha\Rules; |
6
|
|
|
|
7
|
|
|
use Illuminate\Contracts\Validation\Rule; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class CaptchaRule |
12
|
|
|
* |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class CaptchaRule implements Rule |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Properties |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** @var string|null */ |
23
|
|
|
protected $version; |
24
|
|
|
|
25
|
|
|
/** @var array */ |
26
|
|
|
protected $skipIps = []; |
27
|
|
|
|
28
|
|
|
/* ----------------------------------------------------------------- |
29
|
|
|
| Constructor |
30
|
|
|
| ----------------------------------------------------------------- |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* CaptchaRule constructor. |
35
|
|
|
* |
36
|
|
|
* @param string|null $version |
37
|
|
|
*/ |
38
|
16 |
|
public function __construct($version = null) |
39
|
|
|
{ |
40
|
16 |
|
$this->version($version); |
41
|
16 |
|
$this->skipIps( |
42
|
16 |
|
config()->get('no-captcha.skip-ips', []) |
43
|
|
|
); |
44
|
16 |
|
} |
45
|
|
|
|
46
|
|
|
/* ----------------------------------------------------------------- |
47
|
|
|
| Setters |
48
|
|
|
| ----------------------------------------------------------------- |
49
|
|
|
*/ |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Set the ReCaptcha version. |
53
|
|
|
* |
54
|
|
|
* @param string|null $version |
55
|
|
|
* |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
16 |
|
public function version($version) |
59
|
|
|
{ |
60
|
16 |
|
$this->version = $version; |
61
|
|
|
|
62
|
16 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set the ips to skip. |
67
|
|
|
* |
68
|
|
|
* @param string|array $ip |
69
|
|
|
* |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
16 |
|
public function skipIps($ip) |
73
|
|
|
{ |
74
|
16 |
|
$this->skipIps = Arr::wrap($ip); |
75
|
|
|
|
76
|
16 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/* ----------------------------------------------------------------- |
80
|
|
|
| Main methods |
81
|
|
|
| ----------------------------------------------------------------- |
82
|
|
|
*/ |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Determine if the validation rule passes. |
86
|
|
|
* |
87
|
|
|
* @param string $attribute |
88
|
|
|
* @param mixed $value |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
16 |
|
public function passes($attribute, $value) |
93
|
|
|
{ |
94
|
16 |
|
$ip = request()->ip(); |
95
|
|
|
|
96
|
16 |
|
if (in_array($ip, $this->skipIps)) |
97
|
8 |
|
return true; |
98
|
|
|
|
99
|
8 |
|
return no_captcha($this->version) |
100
|
8 |
|
->verify($value, $ip) |
101
|
8 |
|
->isSuccess(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the validation error message. |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
4 |
|
public function message() |
110
|
|
|
{ |
111
|
4 |
|
return (string) trans('validation.captcha'); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|