]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/Captcha.php
rcs_id no longer makes sense with Subversion global version number
[SourceForge/phpwiki.git] / lib / Captcha.php
1 <?php
2 // rcs_id('$Id$');
3 /**
4  * Session Captcha v1.0
5  *   by Gavin M. Roy <gmr@bteg.net>
6  * Modified by Benjamin Drieu <bdrieu@april.org> - 2005 for PhpWiki
7  * get_captcha_random_word() contributed by Dan Frankowski 2005 for PhpWiki
8  * objectified and randomized 2005 by Reini Urban
9  *
10  * This File is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This File is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with This File; if not, write to the Free Software Foundation,
22  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24
25 class Captcha {
26
27     function Captcha($meta = array(), $width = 250, $height = 80) {
28         $this->meta   =& $meta;
29         $this->width  = $width;
30         $this->height = $height;
31         $this->length = 8;
32         $this->failed_msg = _("Typed in verification word mismatch ... are you a bot?");
33         $this->request =& $GLOBALS['request'];
34     }
35
36     function captchaword() {
37         if ( ! $this->request->getSessionVar('captchaword')) {
38             $this->request->setSessionVar('captchaword', $this->get_word());
39         }
40         return $this->request->getSessionVar('captchaword');
41     }
42
43     function Failed () {
44         if ($this->request->getSessionVar('captcha_ok') == true)
45             return false;
46         
47         if ( ! array_key_exists ( 'captcha_input', $this->meta )
48              or ($this->request->getSessionVar('captchaword')
49                  and ($this->request->getSessionVar('captchaword') != $this->meta['captcha_input'])))
50             return true;
51         
52         $this->request->setSessionVar('captcha_ok', true);
53         return false;
54     }
55
56     function getFormElements() {
57         $el = array();
58         if (! $this->request->getSessionVar('captcha_ok')) {
59             $el['CAPTCHA_INPUT']
60                 = HTML::input(array('type'  => 'text',
61                                     'class' => 'wikitext',
62                                     'id'    => 'edit-captcha_input',
63                                     'name'  => 'edit[captcha_input]',
64                                     'size'  => $this->length + 2,
65                                     'maxlength' => 256));
66             $url = WikiURL("", array("action"=>"captcha","id"=>time()), false);
67             $el['CAPTCHA_IMAGE'] = "<img src=\"$url\" alt=\"captcha\" />";
68             $el['CAPTCHA_LABEL'] = '<label for="edit-captcha_input">'._("Type word above:").' </label>';
69         }
70         return $el;
71     }
72
73     function get_word () { 
74         if (USE_CAPTCHA_RANDOM_WORD)
75             return get_dictionary_word(); 
76         else
77             return rand_ascii_readable($this->length); // lib/stdlib.php
78     }
79
80     function get_dictionary_word () {
81         // Load In the Word List
82         $fp = fopen(findfile("lib/captcha/dictionary"), "r");
83         while ( !feof($fp) )
84             $text[] = trim(fgets($fp, 1024));
85         fclose($fp);
86
87         // Pick a Word
88         $word = "";
89         better_srand();
90         while ( strlen(trim($word)) == 0 ) {
91             if (function_exists('mt_rand'))
92                 $x = mt_rand(0, count($text));
93             else
94                 $x = rand(0, count($text));
95             return $text[$x];
96         }
97     }
98
99     // Draw the Spiral
100     function spiral( &$im, $origin_x = 100, $origin_y = 100, $r = 0, $g = 0, $b = 0 ) {
101         $theta = 1;
102         $thetac = 6;  
103         $radius = 15;  
104         $circles = 10;  
105         $points = 35;  
106         $lcolor = imagecolorallocate( $im, $r, $g, $b );
107         for( $i = 0; $i < ( $circles * $points ) - 1; $i++ ) {
108             $theta = $theta + $thetac;
109             $rad = $radius * ( $i / $points );
110             $x = ( $rad * cos( $theta ) ) + $origin_x;
111             $y = ( $rad * sin( $theta ) ) + $origin_y;
112             $theta = $theta + $thetac;
113             $rad1 = $radius * ( ( $i + 1 ) / $points );
114             $x1 = ( $rad1 * cos( $theta ) ) + $origin_x;
115             $y1 = ( $rad1 * sin( $theta ) ) + $origin_y;
116             imageline( $im, $x, $y, $x1, $y1, $lcolor );
117             $theta = $theta - $thetac;
118         }
119     }
120
121     function image ( $word ) {
122         $width  =& $this->width;
123         $height =& $this->height;
124     
125         // Create the Image
126         $jpg = ImageCreate($width,$height);
127         $bg  = ImageColorAllocate($jpg,255,255,255);
128         $tx  = ImageColorAllocate($jpg,185,140,140);
129         ImageFilledRectangle($jpg,0,0,$width,$height,$bg);
130
131         $x = rand(0, $width);
132         $y = rand(0, $height);
133         $this->spiral($jpg, $x, $y, $width-25, 190, 190);
134
135         $x = rand(10, 30);
136         $y = rand(50, $height-20); //50-60
137         
138         // randomize the chars
139         for ($i=0; $i < strlen($word); $i++) {
140             $angle += rand(-5, 5);
141             if ( $angle > 25 )  $angle = 15;
142             elseif ( $angle < -25 ) $angle = -15;
143             $size  = rand(14, 20);
144             $y += rand(-10, 10);
145             if ( $y < 10 )  $y = 11;
146             elseif ( $y > $height-10 ) $y = $height-11;
147             $x += rand($size, $size*2);
148             imagettftext($jpg, $size, $angle, $x, $y, $tx, 
149                          realpath(findfile("lib/captcha/Vera.ttf")), 
150                          $word[$i]);
151         }
152
153         $x = rand(0, $width+30);
154         $y = rand(0, $height+35); // 115
155         $this->spiral($jpg, $x, $y, 255,190,190);
156
157         imageline($jpg, 0,0,$width-1,0,$tx);
158         imageline($jpg, 0,0,0,$height-1,$tx);
159         imageline($jpg, 0,$height-1,$width-1,$height-1,$tx);
160         imageline($jpg, $width-1,0,$width-1,$height-1,$tx);
161
162         if (function_exists("ImageJpeg")) {
163             header("Content-type: image/jpeg");
164             ImageJpeg($jpg);
165         } elseif (function_exists("ImagePNG")) {
166             header("Content-type: image/png");
167             ImagePNG($jpg);
168         } elseif (function_exists("ImageGIF")) {
169             header("Content-type: image/gif");
170             ImageGIF($jpg);
171         } else {
172             trigger_error("missing GD bitmap support", E_USER_WARNING);
173         }
174     }
175
176 }
177
178 // Local Variables:
179 // mode: php
180 // tab-width: 8
181 // c-basic-offset: 4
182 // c-hanging-comment-ender-p: nil
183 // indent-tabs-mode: nil
184 // End:   
185 ?>