-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAntiBotLinks.php
executable file
·185 lines (158 loc) · 6.25 KB
/
AntiBotLinks.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace Miguilim\AntiBotLinks;
use Intervention\Image\Colors\Rgb\Color;
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\Geometry\Factories\LineFactory;
use Intervention\Image\Typography\FontFactory;
use Miguilim\AntiBotLinks\CacheAdapters\AbstractCacheAdapter;
class AntiBotLinks
{
use Traits\HasOptions;
use Traits\HasHelpers;
/**
* Fonts file list.
*/
protected array $fonts = [
'Exo-Regular.otf',
'GoodDog.otf',
'Lobster_1.3.otf',
'Rubik-Medium.ttf',
'ShadowsIntoLightTwo-Regular.ttf',
'SlimJim.ttf',
];
/**
* Identifier hash for caching.
*/
protected string $identifierHash;
/**
* Scan font directory in order to list available fonts.
*/
public function __construct(protected string $identifier, protected AbstractCacheAdapter $cacheAdapter, protected int $expiresIn)
{
$this->identifierHash = md5('antibotlinks.' . $this->identifier);
}
/**
* Make new AntiBotLinks instance.
*/
public static function make(string $identifier, AbstractCacheAdapter $cacheAdapter, int $expiresIn = 300): self
{
return new static($identifier, $cacheAdapter, $expiresIn);
}
/**
* Generate links randomly with images.
*/
public function generateLinks(int $amount = 4): array
{
return $this->cacheAdapter->remember($this->identifierHash, $this->expiresIn, function () use ($amount) {
// Randomly get set of words and randomize it words order
$words = $this->wordUniverse[array_rand($this->wordUniverse)];
$universe = $this->getRandomShuffled((array) $words, $amount);
// 50% of chance to flip solution/answers
if (random_int(0, 1)) {
$universe = array_flip($universe);
}
// Generate images without any repeated
$options = array_map(fn ($after) => [
'id' => random_int(1, 99999),
'image' => $this->generateRandomImage($after),
], $universe);
$optionsShuffled = $options;
shuffle($optionsShuffled);
return [
'links' => [
'phrase' => $this->generateRandomImage(join(', ', array_keys($universe))),
'options' => $optionsShuffled, // IMPORTANT: Shuffle images order only after generate the phrase! (or shuffle with a different variable)
],
'solution' => array_column($options, 'id'),
];
})['links'];
}
/**
* Flush generated links cache.
*/
public function flushLinks(): bool
{
return $this->cacheAdapter->forget($this->identifierHash);
}
/**
* Validate given AntiBotLinks answer.
*/
public function validateAnswer(string $value): bool
{
$solution = $this->cacheAdapter->get($this->identifierHash)['solution'] ?? null;
if (! $solution) {
return false;
}
return join('', $solution) === trim($value);
}
/**
* Generate random image from a word.
*/
protected function generateRandomImage(string $word): array
{
$fontFile = $this->asset("fonts/{$this->fonts[array_rand($this->fonts)]}");
$width = (strlen($word) + 1) * ((strlen($word) >= 10) ? 12 : 14);
$height = 40;
// Random Settings
$angle = ($width > 125) ? random_int(-2, 2) : random_int(-7, 7);
$textColor = $this->generateRandomColor(isShadowColor: false);
$shadowColor = $this->generateRandomColor(isShadowColor: true);
// Create blank canvas
$manager = new ImageManager(new Driver());
$image = $manager->create($width, $height);
// Add link background
// TODO: Reimplement backgrounds. Not working with current version of InterventionImage.
// if ($this->background) {
// $image->fill($this->asset('backgrounds/bg-' . random_int(1, 3) . '-' . (($this->darkTheme) ? 'l' : 'd') . '.png'));
// // ->brightness(($this->darkTheme) ? mt_rand(5, 15) : mt_rand(55, 75));
// }
// Add link text
$image->text($word, (int) ($width / 2), (int) ($height / 2) + 1, function (FontFactory $font) use ($angle, $fontFile, $shadowColor): void {
$font->filename($fontFile);
$font->angle($angle);
$font->size(22);
$font->align('center');
$font->valign('middle');
$font->color($shadowColor);
})->text($word, (int) ($width / 2), (int) ($height / 2), function (FontFactory $font) use ($angle, $fontFile, $textColor): void {
$font->filename($fontFile);
$font->angle($angle);
$font->size(22);
$font->align('center');
$font->valign('middle');
$font->color($textColor);
});
// Add link noise
if ($this->noise) {
for ($i = 0; $i < round($width / random_int(16, 22) * 10); ++$i) {
$x = random_int(1, $width - 3);
$y = random_int(1, $height - 3);
$image->drawLine(function (LineFactory $line) use ($x, $y, $textColor) {
$line->from($x, $y);
$line->to($x + random_int(1, 2), $y + ((random_int(0, 1)) ? -1 : +1));
$line->color($textColor);
});
}
}
return [
'width' => $width,
'base64' => $image->toJpeg()->toDataUri()
];
}
/**
* Generate random rgb() color array.
*/
protected function generateRandomColor(bool $isShadowColor = false): Color
{
// TODO: Reimplement 4th channel (rgba). Not working with current version of InterventionImage.
if ($this->darkTheme) {
return ($isShadowColor)
? new Color(random_int(1, 80), random_int(1, 80), random_int(1, 80))
: new Color(random_int(214, 254), random_int(214, 254), random_int(214, 254));
}
return ($isShadowColor)
? new Color(random_int(174, 254), random_int(174, 254), random_int(174, 254))
: new Color(random_int(1, 80), random_int(1, 80), random_int(1, 80));
}
}