-
-
Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathCaptcha.php
More file actions
171 lines (145 loc) · 4.38 KB
/
Captcha.php
File metadata and controls
171 lines (145 loc) · 4.38 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
<?php
/*
* UserFrosting (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/UserFrosting
* @copyright Copyright (c) 2019 Alexander Weissman
* @license https://github.com/userfrosting/UserFrosting/blob/master/LICENSE.md (MIT License)
*/
namespace UserFrosting\Sprinkle\Core\Util;
use UserFrosting\Session\Session;
/**
* Captcha Class.
*
* Implements the captcha for user registration.
*
* @author r3wt
* @author Alex Weissman (https://alexanderweissman.com)
*
* @see http://www.userfrosting.com/components/#messages
*/
class Captcha
{
/**
* @var string The randomly generated captcha code.
*/
protected $code;
/**
* @var string The captcha image, represented as a binary string.
*/
protected $image;
/**
* @var Session We use the session object so that the hashed captcha token will automatically appear in the session.
*/
protected $session;
/**
* @var string
*/
protected $key;
/**
* Create a new captcha.
*
* @param Session $session
* @param string $key
*/
public function __construct(Session $session, $key)
{
$this->session = $session;
$this->key = $key;
if (!$this->session->has($key)) {
$this->session[$key] = [];
}
}
/**
* Generates a new captcha for the user registration form.
*
* This generates a random 5-character captcha and stores it in the session with an md5 hash.
* Also, generates the corresponding captcha image.
*/
public function generateRandomCode()
{
$this->code = substr( bin2hex( random_bytes( 3 ) ), 0, 5 );
$enc = md5($this->code);
// Store the generated captcha value to the session
$this->session[$this->key] = $enc;
$this->generateImage();
}
/**
* Returns the captcha code.
*
* @return string
*/
public function getCaptcha()
{
return $this->code;
}
/**
* Returns the captcha image.
*
* @return string
*/
public function getImage()
{
return $this->image;
}
/**
* Check that the specified code, when hashed, matches the code in the session.
* Also, stores the specified code in the session with an md5 hash.
*
* @param string $code
*
* @return bool
*/
public function verifyCode($code)
{
return md5($code) == $this->session[$this->key];
}
/**
* Generate the image for the current captcha.
* This generates an image as a binary string.
*
* @return string
*/
protected function generateImage()
{
$width = 150;
$height = 30;
$image = imagecreatetruecolor(150, 30);
//color pallette
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
$red = imagecolorallocate($image, 255, 0, 0);
$yellow = imagecolorallocate($image, 255, 255, 0);
$dark_grey = imagecolorallocate($image, 64, 64, 64);
$blue = imagecolorallocate($image, 0, 0, 255);
//create white rectangle
imagefilledrectangle($image, 0, 0, 150, 30, $white);
//add some lines
for ($i = 0; $i < 2; $i++) {
imageline($image, 0, rand() % 10, 10, rand() % 30, $dark_grey);
imageline($image, 0, rand() % 30, 150, rand() % 30, $red);
imageline($image, 0, rand() % 30, 150, rand() % 30, $yellow);
}
// RandTab color pallette
$randc[0] = imagecolorallocate($image, 0, 0, 0);
$randc[1] = imagecolorallocate($image, 255, 0, 0);
$randc[2] = imagecolorallocate($image, 255, 255, 0);
$randc[3] = imagecolorallocate($image, 64, 64, 64);
$randc[4] = imagecolorallocate($image, 0, 0, 255);
//add some dots
for ($i = 0; $i < 1000; $i++) {
imagesetpixel($image, rand() % 200, rand() % 50, $randc[rand() % 5]);
}
//calculate center of text
$x = (150 - 0 - imagefontwidth(5) * strlen($this->code)) / 2 + 0 + 5;
//write string twice
imagestring($image, 5, $x, 7, $this->code, $black);
imagestring($image, 5, $x, 7, $this->code, $black);
//start ob
ob_start();
imagepng($image);
//get binary image data
$this->image = ob_get_clean();
return $this->image;
}
}