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