]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/Captcha.php
php_closing_tag [PSR-2] The closing ?> tag MUST be omitted from files containing...
[SourceForge/phpwiki.git] / lib / Captcha.php
1 <?php
2
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 along
21  * with This File; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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'] = HTML::img(array('src' => $url, 'alt' => 'captcha'));
68             $el['CAPTCHA_LABEL'] = HTML::label(array('for' => 'edit-captcha_input'),
69                                                _("Type word above:"));
70         }
71         return $el;
72     }
73
74     function get_word () {
75         if (USE_CAPTCHA_RANDOM_WORD)
76             return get_dictionary_word();
77         else
78             return rand_ascii_readable($this->length); // lib/stdlib.php
79     }
80
81     function get_dictionary_word () {
82         // Load In the Word List
83         $fp = fopen(findfile("lib/captcha/dictionary"), "r");
84         while ( !feof($fp) )
85             $text[] = trim(fgets($fp, 1024));
86         fclose($fp);
87
88         // Pick a Word
89         $word = "";
90         better_srand();
91         while ( strlen(trim($word)) == 0 ) {
92             if (function_exists('mt_rand'))
93                 $x = mt_rand(0, count($text));
94             else
95                 $x = rand(0, count($text));
96             return $text[$x];
97         }
98     }
99
100     // Draw the Spiral
101     function spiral( &$im, $origin_x = 100, $origin_y = 100, $r = 0, $g = 0, $b = 0 ) {
102         $theta = 1;
103         $thetac = 6;
104         $radius = 15;
105         $circles = 10;
106         $points = 35;
107         $lcolor = imagecolorallocate( $im, $r, $g, $b );
108         for( $i = 0; $i < ( $circles * $points ) - 1; $i++ ) {
109             $theta = $theta + $thetac;
110             $rad = $radius * ( $i / $points );
111             $x = ( $rad * cos( $theta ) ) + $origin_x;
112             $y = ( $rad * sin( $theta ) ) + $origin_y;
113             $theta = $theta + $thetac;
114             $rad1 = $radius * ( ( $i + 1 ) / $points );
115             $x1 = ( $rad1 * cos( $theta ) ) + $origin_x;
116             $y1 = ( $rad1 * sin( $theta ) ) + $origin_y;
117             imageline( $im, $x, $y, $x1, $y1, $lcolor );
118             $theta = $theta - $thetac;
119         }
120     }
121
122     function image ( $word ) {
123         $width  =& $this->width;
124         $height =& $this->height;
125
126         // Create the Image
127         $jpg = ImageCreate($width,$height);
128         $bg  = ImageColorAllocate($jpg,255,255,255);
129         $tx  = ImageColorAllocate($jpg,185,140,140);
130         ImageFilledRectangle($jpg,0,0,$width,$height,$bg);
131
132         $x = rand(0, $width);
133         $y = rand(0, $height);
134         $this->spiral($jpg, $x, $y, $width-25, 190, 190);
135
136         $x = rand(10, 30);
137         $y = rand(50, $height-20); //50-60
138
139         // randomize the chars
140         for ($i=0; $i < strlen($word); $i++) {
141             $angle += rand(-5, 5);
142             if ( $angle > 25 )  $angle = 15;
143             elseif ( $angle < -25 ) $angle = -15;
144             $size  = rand(14, 20);
145             $y += rand(-10, 10);
146             if ( $y < 10 )  $y = 11;
147             elseif ( $y > $height-10 ) $y = $height-11;
148             $x += rand($size, $size*2);
149             imagettftext($jpg, $size, $angle, $x, $y, $tx,
150                          realpath(findfile("lib/captcha/Vera.ttf")),
151                          $word[$i]);
152         }
153
154         $x = rand(0, $width+30);
155         $y = rand(0, $height+35); // 115
156         $this->spiral($jpg, $x, $y, 255,190,190);
157
158         imageline($jpg, 0,0,$width-1,0,$tx);
159         imageline($jpg, 0,0,0,$height-1,$tx);
160         imageline($jpg, 0,$height-1,$width-1,$height-1,$tx);
161         imageline($jpg, $width-1,0,$width-1,$height-1,$tx);
162
163     if (function_exists("ImageJpeg")) {
164         header("Content-type: image/jpeg");
165         ImageJpeg($jpg);
166     } elseif (function_exists("ImagePNG")) {
167         header("Content-type: image/png");
168         ImagePNG($jpg);
169     } elseif (function_exists("ImageGIF")) {
170         header("Content-type: image/gif");
171         ImageGIF($jpg);
172     } else {
173         trigger_error("missing GD bitmap support", E_USER_WARNING);
174     }
175     }
176
177 }
178
179 // Local Variables:
180 // mode: php
181 // tab-width: 8
182 // c-basic-offset: 4
183 // c-hanging-comment-ender-p: nil
184 // indent-tabs-mode: nil
185 // End: