-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCaptcha.php
More file actions
168 lines (136 loc) · 4.37 KB
/
Copy pathCaptcha.php
File metadata and controls
168 lines (136 loc) · 4.37 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace Jason\Captcha;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Http\Response;
use Illuminate\Session\Store as Session;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use Jason\Captcha\Generators\ChineseGenerator;
use Jason\Captcha\Generators\MathGenerator;
use Jason\Captcha\Generators\StringGenerator;
use Jason\Captcha\Image\ImageCreator;
use Jason\Captcha\Support\Config;
class Captcha
{
protected Config $config;
protected ImageCreator $imageCreator;
protected Session $session;
protected Hasher $hasher;
public function __construct(
Config $config,
ImageCreator $imageCreator,
Session $session,
Hasher $hasher
) {
$this->config = $config;
$this->imageCreator = $imageCreator;
$this->session = $session;
$this->hasher = $hasher;
}
/**
* Create captcha image or API response.
*/
public function create(string $style = 'default', bool $api = false): Response|array
{
$config = $this->config->get($style);
$generator = match (true) {
$config['math'] => new MathGenerator(),
$style === 'chinese' => new ChineseGenerator(),
default => new StringGenerator(),
};
$generatorResult = $generator->generate($config);
$image = $this->imageCreator->make($config, $generatorResult);
$hash = $this->hasher->make($generatorResult['key']);
if ($config['encrypt']) {
$hash = Crypt::encrypt($hash);
}
$this->session->put('captcha', [
'sensitive' => $generatorResult['sensitive'],
'key' => $hash,
'encrypt' => $config['encrypt'],
]);
Cache::put($this->getCacheKey($hash), $generatorResult['key'], $config['expire']);
if ($api) {
return [
'sensitive' => $generatorResult['sensitive'],
'key' => $hash,
'img' => $image->toJpg($config['quality'])->toDataUri(),
];
}
return new Response($image->toJpg($config['quality']), 200, [
'Content-Type' => 'image/jpeg',
'Cache-Control' => 'no-cache, no-store, max-age=0, must-revalidate',
'Pragma' => 'no-cache',
'Expires' => 'Fri, 01 Jan 1990 00:00:00 GMT',
]);
}
/**
* Validate captcha.
*/
public function check(string $value): bool
{
if (!$this->session->has('captcha')) {
return false;
}
$stored = $this->session->get('captcha');
$key = $stored['key'];
if (!Cache::pull($this->getCacheKey($key))) {
$this->session->forget('captcha');
return false;
}
if (!$stored['sensitive']) {
$value = Str::lower($value);
}
$hash = $stored['encrypt'] ? Crypt::decrypt($key) : $key;
$isValid = $this->hasher->check($value, $hash);
if ($isValid) {
$this->session->forget('captcha');
}
return $isValid;
}
/**
* Validate captcha for API.
*/
public function checkApi(string $value, string $key, string $style = 'default'): bool
{
if (!Cache::pull($this->getCacheKey($key))) {
return false;
}
$config = $this->config->get($style);
if (!$config['sensitive']) {
$value = Str::lower($value);
}
$hash = $config['encrypt'] ? Crypt::decrypt($key) : $key;
return $this->hasher->check($value, $hash);
}
/**
* Get the cache key for a captcha.
*/
protected function getCacheKey(string $key): string
{
return 'captcha_'.md5($key);
}
/**
* Generate captcha image source URL.
*/
public function src(string $style = 'default'): string
{
return url('captcha/'.$style).'?'.Str::random(8);
}
/**
* Generate captcha image HTML tag.
*/
public function img(string $style = 'default', array $attrs = []): HtmlString
{
$attrsStr = '';
foreach ($attrs as $attr => $value) {
if ($attr === 'src') {
continue;
}
$attrsStr .= " $attr=\"$value\"";
}
return new HtmlString('<img src="'.$this->src($style).'"'.trim($attrsStr).'>');
}
}