1 | <?php |
---|
2 | /** |
---|
3 | * Ptitcaptcha : simple php captcha system |
---|
4 | * |
---|
5 | * @author Jean-Pierre Morfin |
---|
6 | * @license Creative Commons By |
---|
7 | * @license http://creativecommons.org/licenses/by/2.0/fr/ |
---|
8 | */ |
---|
9 | |
---|
10 | /* Change it to have a specific encoding ! */ |
---|
11 | define("PTITCAPTCHA_ENTROPY","what-ever-you-want"); |
---|
12 | |
---|
13 | /* Choose length (max 32) */ |
---|
14 | define("PTITCAPTCHA_LENGTH",5); |
---|
15 | |
---|
16 | $GLOBALS["ptitcaptcha_akey"] = md5(uniqid(rand(), true)); |
---|
17 | |
---|
18 | /** |
---|
19 | * Helper to generate html form tags |
---|
20 | * |
---|
21 | */ |
---|
22 | class PtitCaptchaHelper |
---|
23 | { |
---|
24 | /** |
---|
25 | * Generate IMG Tag |
---|
26 | * |
---|
27 | * @param string $baseuri : relative or absolute path to folder containing this file on web |
---|
28 | * @return IMG Tag |
---|
29 | */ |
---|
30 | function generateImgTags($baseuri) |
---|
31 | { |
---|
32 | return "<a href=\"#\"><img alt=\"???\" title=\"?\"". |
---|
33 | " src=\"".$baseuri."ptitcaptcha.php?pck=".$GLOBALS['ptitcaptcha_akey']."\"". |
---|
34 | " id=\"ptitcaptcha\"". |
---|
35 | " onclick=\"javascript:this.src='".$baseuri."ptitcaptcha.php?pck=". |
---|
36 | $GLOBALS['ptitcaptcha_akey']. |
---|
37 | "&z='+Math.random();return false;\" /></a>\n"; |
---|
38 | } |
---|
39 | |
---|
40 | /** |
---|
41 | * Generate hidden tag (must be in a form) |
---|
42 | * |
---|
43 | * @return input hidden tag |
---|
44 | */ |
---|
45 | function generateHiddenTags() |
---|
46 | { |
---|
47 | return "<input type=\"hidden\" name=\"ptitcaptcha_key\" value=\"".$GLOBALS['ptitcaptcha_akey']."\"/>"; |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * Generate input tag (must be in a form) |
---|
52 | * |
---|
53 | * @return input tag |
---|
54 | */ |
---|
55 | function generateInputTags() |
---|
56 | { |
---|
57 | return "<input type=\"text\" name=\"ptitcaptcha_entry\" value=\"\"/>"; |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * Check if user input is correct |
---|
62 | * |
---|
63 | * @return boolean (true=correct, false=incorrect) |
---|
64 | */ |
---|
65 | function checkCaptcha() |
---|
66 | { |
---|
67 | if( isset($_POST['ptitcaptcha_entry']) && |
---|
68 | $_POST['ptitcaptcha_entry'] == PtitCaptchaHelper::_getDisplayText($_POST['ptitcaptcha_key'])) |
---|
69 | { |
---|
70 | return true; |
---|
71 | } |
---|
72 | return false; |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * Internal function |
---|
77 | * |
---|
78 | * @param string $pck |
---|
79 | * @return string |
---|
80 | */ |
---|
81 | function _getDisplayText($pck) // internal function |
---|
82 | { |
---|
83 | $src=md5(PTITCAPTCHA_ENTROPY.$pck); |
---|
84 | $txt=""; |
---|
85 | for($i=0;$i<PTITCAPTCHA_LENGTH;$i++) |
---|
86 | $txt.=substr($src,$i*32/PTITCAPTCHA_LENGTH,1); |
---|
87 | return $txt; |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | // If script called directly : generate image |
---|
93 | if(basename($_SERVER["SCRIPT_NAME"])=="ptitcaptcha.php" && isset($_GET["pck"])) |
---|
94 | { |
---|
95 | $width = PTITCAPTCHA_LENGTH*10+10; |
---|
96 | $height = 30; |
---|
97 | |
---|
98 | $image = imagecreatetruecolor($width, $height); |
---|
99 | $bgCol = imagecolorallocate($image, rand(128,255), rand(128,255), rand(128,255)); |
---|
100 | imagefilledrectangle($image,0,0,$width,$height,$bgCol); |
---|
101 | |
---|
102 | $txt = PtitCaptchaHelper::_getDisplayText($_GET["pck"]); |
---|
103 | |
---|
104 | for($c=0;$c<PTITCAPTCHA_LENGTH*2;$c++) |
---|
105 | { |
---|
106 | $bgCol = imagecolorallocate($image, rand(100,255), rand(100,255), rand(100,255)); |
---|
107 | $x=rand(0,$width); |
---|
108 | $y=rand(0,$height); |
---|
109 | $w=rand(5,$width/2); |
---|
110 | $h=rand(5,$height/2); |
---|
111 | imagefilledrectangle($image,$x,$y,$x+$w,$y+$h,$bgCol); |
---|
112 | imagecolordeallocate($image,$bgCol); |
---|
113 | } |
---|
114 | for($c=0;$c<PTITCAPTCHA_LENGTH;$c++) |
---|
115 | { |
---|
116 | $txtCol = imagecolorallocate($image, rand(0,128) , rand(0,128), rand(0,128)); |
---|
117 | imagestring($image,5,5+10*$c,rand(0,10),substr($txt,$c,1),$txtCol); |
---|
118 | imagecolordeallocate($image,$txtCol); |
---|
119 | } |
---|
120 | |
---|
121 | header("Content-type: image/png"); |
---|
122 | imagepng($image); |
---|
123 | imagedestroy($image); |
---|
124 | } |
---|
125 | |
---|
126 | ?> |
---|