]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/Captcha.php
Allow bold, italics or underlined for numbers
[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
28     function Captcha($meta = array(), $width = 250, $height = 80)
29     {
30         $this->meta =& $meta;
31         $this->width = $width;
32         $this->height = $height;
33         $this->length = 8;
34         $this->failed_msg = _("Typed in verification word mismatch ... are you a bot?");
35         $this->request =& $GLOBALS['request'];
36     }
37
38     function captchaword()
39     {
40         if (!$this->request->getSessionVar('captchaword')) {
41             $this->request->setSessionVar('captchaword', $this->get_word());
42         }
43         return $this->request->getSessionVar('captchaword');
44     }
45
46     function Failed()
47     {
48         if ($this->request->getSessionVar('captcha_ok') == true)
49             return false;
50
51         if (!array_key_exists('captcha_input', $this->meta)
52             or ($this->request->getSessionVar('captchaword')
53                 and ($this->request->getSessionVar('captchaword') != $this->meta['captcha_input']))
54         )
55             return true;
56
57         $this->request->setSessionVar('captcha_ok', true);
58         return false;
59     }
60
61     function getFormElements()
62     {
63         $el = array();
64         if (!$this->request->getSessionVar('captcha_ok')) {
65             $el['CAPTCHA_INPUT']
66                 = HTML::input(array('type' => 'text',
67                 'class' => 'wikitext',
68                 'id' => 'edit-captcha_input',
69                 'name' => 'edit[captcha_input]',
70                 'size' => $this->length + 2,
71                 'maxlength' => 256));
72             $url = WikiURL("", array("action" => "captcha", "id" => time()), false);
73             $el['CAPTCHA_IMAGE'] = HTML::img(array('src' => $url, 'alt' => 'captcha'));
74             $el['CAPTCHA_LABEL'] = HTML::label(array('for' => 'edit-captcha_input'),
75                 _("Type word above:"));
76         }
77         return $el;
78     }
79
80     function get_word()
81     {
82         if (USE_CAPTCHA_RANDOM_WORD)
83             return get_dictionary_word();
84         else
85             return rand_ascii_readable($this->length); // lib/stdlib.php
86     }
87
88     function get_dictionary_word()
89     {
90         // Load In the Word List
91         $fp = fopen(findfile("lib/captcha/dictionary"), "r");
92         while (!feof($fp))
93             $text[] = trim(fgets($fp, 1024));
94         fclose($fp);
95
96         // Pick a Word
97         $word = "";
98         better_srand();
99         while (strlen(trim($word)) == 0) {
100             if (function_exists('mt_rand'))
101                 $x = mt_rand(0, count($text));
102             else
103                 $x = rand(0, count($text));
104             return $text[$x];
105         }
106     }
107
108     // Draw the Spiral
109     function spiral(&$im, $origin_x = 100, $origin_y = 100, $r = 0, $g = 0, $b = 0)
110     {
111         $theta = 1;
112         $thetac = 6;
113         $radius = 15;
114         $circles = 10;
115         $points = 35;
116         $lcolor = imagecolorallocate($im, $r, $g, $b);
117         for ($i = 0; $i < ($circles * $points) - 1; $i++) {
118             $theta = $theta + $thetac;
119             $rad = $radius * ($i / $points);
120             $x = ($rad * cos($theta)) + $origin_x;
121             $y = ($rad * sin($theta)) + $origin_y;
122             $theta = $theta + $thetac;
123             $rad1 = $radius * (($i + 1) / $points);
124             $x1 = ($rad1 * cos($theta)) + $origin_x;
125             $y1 = ($rad1 * sin($theta)) + $origin_y;
126             imageline($im, $x, $y, $x1, $y1, $lcolor);
127             $theta = $theta - $thetac;
128         }
129     }
130
131     function image($word)
132     {
133         $width =& $this->width;
134         $height =& $this->height;
135
136         // Create the Image
137         $jpg = ImageCreate($width, $height);
138         $bg = ImageColorAllocate($jpg, 255, 255, 255);
139         $tx = ImageColorAllocate($jpg, 185, 140, 140);
140         ImageFilledRectangle($jpg, 0, 0, $width, $height, $bg);
141
142         $x = rand(0, $width);
143         $y = rand(0, $height);
144         $this->spiral($jpg, $x, $y, $width - 25, 190, 190);
145
146         $x = rand(10, 30);
147         $y = rand(50, $height - 20); //50-60
148
149         // randomize the chars
150         $angle = 0;
151         for ($i = 0; $i < strlen($word); $i++) {
152             $angle += rand(-5, 5);
153             if ($angle > 25) $angle = 15;
154             elseif ($angle < -25) $angle = -15;
155             $size = rand(14, 20);
156             $y += rand(-10, 10);
157             if ($y < 10) $y = 11;
158             elseif ($y > $height - 10) $y = $height - 11;
159             $x += rand($size, $size * 2);
160             imagettftext($jpg, $size, $angle, $x, $y, $tx,
161                 realpath(findfile("lib/captcha/Vera.ttf")),
162                 $word[$i]);
163         }
164
165         $x = rand(0, $width + 30);
166         $y = rand(0, $height + 35); // 115
167         $this->spiral($jpg, $x, $y, 255, 190, 190);
168
169         imageline($jpg, 0, 0, $width - 1, 0, $tx);
170         imageline($jpg, 0, 0, 0, $height - 1, $tx);
171         imageline($jpg, 0, $height - 1, $width - 1, $height - 1, $tx);
172         imageline($jpg, $width - 1, 0, $width - 1, $height - 1, $tx);
173
174         if (function_exists("ImageJpeg")) {
175             header("Content-type: image/jpeg");
176             ImageJpeg($jpg);
177         } elseif (function_exists("ImagePNG")) {
178             header("Content-type: image/png");
179             ImagePNG($jpg);
180         } elseif (function_exists("ImageGIF")) {
181             header("Content-type: image/gif");
182             ImageGIF($jpg);
183         } else {
184             trigger_error("missing GD bitmap support", E_USER_WARNING);
185         }
186     }
187
188 }
189
190 // Local Variables:
191 // mode: php
192 // tab-width: 8
193 // c-basic-offset: 4
194 // c-hanging-comment-ender-p: nil
195 // indent-tabs-mode: nil
196 // End: