-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcaptcha.php
executable file
·173 lines (163 loc) · 3.88 KB
/
captcha.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
<?php
/**
* @package Flatnux
* @author Alessandro Vernassa <[email protected]>
* @copyright Copyright (c) 2011
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License
*/
ob_start();
require_once "include/flatnux.php";
$CONFIG=array();
$t=FN_GetParam("t",$_GET,"html");
if ($t== "")
{
$t="security_code";
}
$session=FN_GetSessionValue("captcha");
if (!isset($session[$t]))
{
$text="error";
}
else
{
$text=$session[$t];
}
$CONFIG['font_id']=7;
$CONFIG['width']=120;
$CONFIG['height']=60;
/**
*
* @param type $rgb
* @return type
*/
function rgb_grayscale($rgb)
{
$color=array();
$color['r']=0.299 * $rgb['r'] + 0.587 * $rgb['g'] + 0.114 * $rgb['b'];
$color['g']=0.299 * $rgb['r'] + 0.587 * $rgb['g'] + 0.114 * $rgb['b'];
$color['b']=0.299 * $rgb['r'] + 0.587 * $rgb['g'] + 0.114 * $rgb['b'];
return $color;
}
/**
*
* @param array $rgb
* @return type
*/
function rgb_complementary($rgb)
{
$color=array();
$color['r']=255 - $rgb['r'];
$color['g']=255 - $rgb['g'];
$color['b']=255 - $rgb['b'];
return $color;
}
/**
*
* @param type $min
* @param type $max
* @return type
*/
function rgb_rand($min=0,$max=255)
{
$color=array();
$color['r']=rand($min,$max);
$color['g']=rand($min,$max);
$color['b']=rand($min,$max);
return $color;
}
function rgbcolor($r,$g,$b)
{
$color=array();
$color['r']=$r;
$color['g']=$g;
$color['b']=$b;
return $color;
}
/**
*
* @param type $r
* @param type $g
* @param type $b
* @return type
*/
function rgb_create($r=0,$g=0,$b=0)
{
$color=array();
$color['r']=$r;
$color['g']=$g;
$color['b']=$b;
return $color;
}
/**
*
* @param type $lhs
* @param type $rhs
* @return type
*/
function rgb_merge($lhs,$rhs)
{
$color=array();
$color['r']=( $lhs['r'] + $rhs['r'] ) >> 1;
$color['g']=( $lhs['g'] + $rhs['g'] ) >> 1;
$color['b']=( $lhs['b'] + $rhs['b'] ) >> 1;
return $color;
}
srand((double)microtime() * 1000000);
// Creates a simple image
$image=imagecreate($CONFIG['width'],$CONFIG['height']);
// Create random colors
$rgb=array();
$rgb['background']=rgbcolor(233,236,239);
$rgb['foreground']=rgbcolor(126,126,200);
if ($rgb['foreground']['r'] > 127)
{
$inicio=-127;
$rgb['foreground']=rgb_merge($rgb['foreground'],rgb_create(255,255,255));
$rgb['shadow']=rgb_merge(rgb_complementary($rgb['foreground']),rgb_create(0,0,0));
}
else
{
$inicio=0;
$rgb['foreground']=rgb_merge($rgb['foreground'],rgb_create(0,0,0));
$rgb['shadow']=rgb_merge(rgb_complementary($rgb['foreground']),rgb_create(255,255,255));
}
// Allocate color resources
$color=array();
foreach($rgb as $name=> $value)
{
$color[$name]=imagecolorallocate($image,$value['r'],$value['g'],$value['b']);
} // foreach
// Draw background
imagefilledrectangle($image,0,0,120,30,$color['background']);
// Write some random text on background
for($i=0; $i < rand(5,9); $i++)
{
$x=rand(0,$CONFIG['width']);
$y=rand(0,$CONFIG['height']);
$f=rand(0,5);
$c=rgb_grayscale(rgb_rand(127 - $inicio,254 - $inicio));
$color[$i]=imagecolorallocate($image,$c['r'],$c['g'],$c['b']);
imagestring($image,$f,$x,$y,"* * *",$color[$i]);
}
// Center the real captcha text
$x=( $CONFIG['width'] - 15 - ( ImageFontWidth($CONFIG['font_id']) * strlen($text) ) ) >> 1;
$y=( $CONFIG['height'] - ImageFontHeight($CONFIG['font_id']) ) >> 1;
putenv('GDFONTPATH='.realpath('./images'));
$font='captcha.ttf';
// Add some shadow to the text
imagettftext($image,22,10,0,$y + 22 + 1,$color['foreground'],$font,$text);
imagettftext($image,22,10,1,$y + 22,$color['foreground'],$font,$text);
while(false!== ob_get_clean()
);
// Returns the image
header('Content-type: image/png');
header("Cache-Control: no-cache");
header("Pragma: no-cache");
imagepng($image);
// Free resources
foreach($color as $name=> $value)
{
imagecolordeallocate($image,$value);
}
imagedestroy($image);
?>