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