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