-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaptcha.class.php
More file actions
189 lines (161 loc) · 5.27 KB
/
Copy pathcaptcha.class.php
File metadata and controls
189 lines (161 loc) · 5.27 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* Captcha
* Class responsible to create and validate a captcha
*
* Created by Gonçalo Gonçalves - goncalofbg@gmail.com
*
*/
class Captcha {
public $devMode = true;
public $captchaCode = "";
public $imageWidth = 180;
public $imageHeight = 50;
public $charNumber = 6;
public $arrayFonts = array('claredon.ttf', 'courier_bold.ttf', 'toledo.ttf','valken.ttf', 'impact.ttf');
public $possibleChars = '23456789ABCDEFGHKLMNPQRSTUVYWXZ';
//public $possibleChars = 'ABCDEFGHKLMNPQRSTUVYWXZ';
public $fontPath = "";
public $addDots = true;
public $addLines = false;
public $addRectangles = false;
public $addCircles = false;
public $numberDots = 3000;
public $numberLines = 10;
public $numberRectangles = 20;
public $numberCircles = 30;
/**
* Construct
* @param string $fontPath the path of the fonts
*/
public function __construct($fontPath = "fonts/"){
$this->fontPath = $fontPath;
}
/**
* Create a new captcha
* @return string html of the captcha
*/
public function createCaptcha() {
ob_start();
$temp_code = '';
$i = 0;
// create temp code
while ($i < $this->charNumber) {
// get random letter
$temp_code .= substr($this->possibleChars, mt_rand(0, strlen($this->possibleChars)-1), 1);
$i++;
}
//header('Content-Type: image/png');
$image = imagecreatetruecolor($this->imageWidth , $this->imageHeight);
$colorWhite = imagecolorallocate($image, 255, 255, 255);
// background rectangle
imagefilledrectangle($image, 0, 0, $this->imageWidth, $this->imageHeight, $colorWhite);
// add dots
if ($this->addDots) {
for ($c = 0; $c < $this->numberDots; $c++){
$colorGrey = rand(0, 200);
$colorDot = imagecolorallocate($image, $colorGrey, $colorGrey, $colorGrey);
$x = rand(0,$this->imageWidth-1);
$y = rand(0,$this->imageHeight-1);
imagesetpixel($image, $x, $y, $colorDot);
}
}
// add lines
if ($this->addLines) {
for ($c = 0; $c < $this->numberLines; $c++){
$randomColor = strtoupper(dechex(rand(0,10000000)));
$x = rand(0,$this->imageWidth-1);
$y = rand(0,$this->imageHeight-1);
$x2 = rand(0,$this->imageWidth-1);
$y2 = rand(0,$this->imageHeight-1);
imageline($image, $x, $y, $x2, $y2, $randomColor);
}
}
// add rectangles
if ($this->addRectangles) {
for ($c = 0; $c < $this->numberRectangles; $c++){
$randomColor = strtoupper(dechex(rand(0,10000000)));
$x = rand(0,$this->imageWidth-1);
$y = rand(0,$this->imageHeight-1);
$x2 = rand(0,$this->imageWidth-1);
$y2 = rand(0,$this->imageHeight-1);
imagerectangle($image, $x, $y, $x2, $y2, $randomColor);
}
}
// add circles
if ($this->addCircles) {
for ($c = 0; $c < $this->numberCircles; $c++){
$colorGrey = rand(0, 200);
$colorCircle = imagecolorallocate($image, $colorGrey, $colorGrey, $colorGrey);
$x = rand(0,$this->imageWidth-1);
$y = rand(0,$this->imageHeight-1);
$circleSize = rand(3, 6);
imagefilledellipse($image, $x, $y, $circleSize, $circleSize, $colorCircle);
}
}
// add text, letter by letter
for ($c = 0; $c < $this->charNumber; $c++){
$colorGrey = rand(0, 100);
$colorText = imagecolorallocate($image, $colorGrey, $colorGrey, $colorGrey);
$font = $this->fontPath.$this->arrayFonts[rand(0, sizeof($this->arrayFonts)-1)];
$angle = rand(-20, 20);
$letterSize = rand(20, 28);
$x = $c*25+15;
$y = rand(30,40);
$letter = substr($this->possibleChars, mt_rand(0, strlen($this->possibleChars)-1), 1);
// bonus
switch ($c) {
case 0: $letter = "G"; break;
case 1: $letter = "I"; break;
case 2: $letter = "T"; break;
case 3: $letter = "H"; break;
case 4: $letter = "U"; break;
case 5: $letter = "B"; break;
//case 6: $letter = ""; break;
}
imagettftext($image, $letterSize, $angle, $x, $y, $colorText, $font, $letter);
$this->captchaCode = $this->captchaCode.$letter;
}
imagepng($image);
imagedestroy($image);
$finalImage = ob_get_clean();
session_start();
$_SESSION['captchaCode'] = $this->captchaCode;
$htmlCaptchaImage = "<img alt='Captcha' title='Captcha' src='data:image/jpeg;base64," . base64_encode( $finalImage )."'>";
$htmlCaptcha = '<table class="captcha-table">';
if (isset($_GET['captcha_state']) && $_GET['captcha_state'] == "false") {
$htmlCaptcha .= '<tr><td class="captcha-wrong">Wrong code!</td></tr>';
}
$htmlCaptcha .= '
<tr>
<td>'.$htmlCaptchaImage.'
<div class="captcha-bar-tools">
<a href="javascript:createCaptcha();" title="Change image"><img src="imgs/refresh.png" alt="create new captcha"></a>
</div>
</td>
</tr>
<tr>
<td><input id="user-input-captcha" name="user-input-captcha" class="captcha-input"></td>
</tr>';
if ($this->devMode == true) {
$htmlCaptcha .= '<tr><td>'.$this->captchaCode.'</td></tr>';
}
$htmlCaptcha .= '</table>';
return $htmlCaptcha;
}
/**
* Validate captcha code
* @param string $value the string inserted by the user
* @return boolean if code is correct
*/
public function validateCaptcha($value){
$validCode = false;
session_start();
$captchaCode = $_SESSION['captchaCode'];
if ($captchaCode == $value) {
$validCode = true;
}
return $validCode;
}
}
?>