-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcaptcha.php
More file actions
134 lines (120 loc) · 4.04 KB
/
captcha.php
File metadata and controls
134 lines (120 loc) · 4.04 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
<?php
//Copyright (c) 2019-2020 Divested Computing Group
//
//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU Lesser General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public License
//along with this program. If not, see <https://www.gnu.org/licenses/>.
function generateRandomColor($image) {
imagecolorallocate($image, rand(20, 255), rand(20, 255), rand(20, 255));
}
function applyRandomImageFilter($image) {
switch(random_int(0, 4)) {
case 0:
//do nothing
break;
case 1:
imagefilter($image, IMG_FILTER_EDGEDETECT);
break;
case 2:
imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
break;
case 3:
imagefilter($image, IMG_FILTER_MEAN_REMOVAL);
break;
case 4:
imagefilter($image, IMG_FILTER_SMOOTH, 1);
break;
}
}
function generateTextCaptcha($captchaText) {
$captcha = imagecreate(300, 100);
generateRandomColor($captcha); //Set random background color
$textColor = imagecolorallocate($captcha, 0, 0, 0); //Black text
imagettftext($captcha, 24, rand(-10, 10), rand(15, 150), 50, $textColor, getRandomFile("./captcha_fonts/"), $captchaText); //Draw the captcha text
for ($i = 0; $i < rand(8, 20); $i++) { //Add lines
imagestring($captcha, 1, rand(0, 300), rand(0, 100), generateRandomString(rand(4, 20), false), $textColor);
imageline($captcha, rand(0, 300), rand(0, 100), rand(0, 300), rand(0, 100), $textColor);
}
for ($i = 0; $i < rand(1, 8); $i++) { //Add letters
imagestring($captcha, 3, rand(0, 300), rand(0, 100), generateRandomString(6, false), $textColor);
}
//applyRandomImageFilter($captcha);
return $captcha;
}
function getCaptchaText($clear = true) {
if($clear) { clearCaptchaStore(); }
$captchaText = generateRandomString(6, true);
appendCaptchaStore($captchaText, implode(' ', str_split($captchaText)));
return generateTextCaptcha($captchaText);
}
function getCaptchaMath($clear = true) {
if($clear) { clearCaptchaStore(); }
$num1 = random_int(0, 20);
$num2 = random_int(0, 20);
$place = random_int(0, 1);
if($place) { $captchaText = "= "; }
$captchaText .= $num1 . " + " . $num2;
if(!$place) { $captchaText .= " ="; }
appendCaptchaStore(($num1 + $num2), $captchaText);
return generateTextCaptcha($captchaText);
}
function getCaptchaRandom($clear = true) {
switch(random_int(0, 1)) {
case 0:
return getCaptchaText($clear);
case 1:
return getCaptchaMath($clear);
}
}
function getCaptchaMultiple($numParts = 2) {
clearCaptchaStore();
$outputImage = imagecreatetruecolor(300, 100 * $numParts);
for ($i = 0; $i < $numParts; $i++) {
imagecopy($outputImage, getCaptchaRandom(false), 0, 100 * $i, 0, 0, 300, 100);
}
return $outputImage;
}
function clearCaptchaStore() {
$_SESSION['SBNR_CAPTCHA_ANSWER'] = "";
$_SESSION['SBNR_CAPTCHA_SPEAK'] = "";
}
function appendCaptchaStore($answer, $raw) {
$_SESSION['SBNR_CAPTCHA_ANSWER'] .= " " . $answer;
if(strlen($_SESSION['SBNR_CAPTCHA_SPEAK']) > 0) {
$_SESSION['SBNR_CAPTCHA_SPEAK'] .= " and";
}
$_SESSION['SBNR_CAPTCHA_SPEAK'] .= " " . $raw;
}
function relaxString($text) {
$text = strtolower($text); //Set to lowercase
$text = preg_replace('/\s+/', '', $text); //Remove all whitespace
return $text;
}
function checkCaptchaAnswer($response, $relaxed) {
if($relaxed) {
$response = relaxString($response);
$_SESSION['SBNR_CAPTCHA_ANSWER'] = relaxString($_SESSION['SBNR_CAPTCHA_ANSWER']);
}
return $response == $_SESSION['SBNR_CAPTCHA_ANSWER'];
}
function getImage($image) {
ob_start();
imagepng($image);
$imagePNG = ob_get_contents();
ob_end_clean();
return $imagePNG;
}
function getImageBase64($image) {
$imagePNG = getImage($image);
return 'data:image/png;base64,' . base64_encode($imagePNG);
}
?>