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
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.
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.
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.
37 function Captcha($meta = array(), $width = 250, $height = 80)
40 $this->width = $width;
41 $this->height = $height;
43 $this->failed_msg = _("Typed in verification word mismatch ... are you a bot?");
44 $this->request =& $GLOBALS['request'];
47 function captchaword()
49 if (!$this->request->getSessionVar('captchaword')) {
50 $this->request->setSessionVar('captchaword', $this->get_word());
52 return $this->request->getSessionVar('captchaword');
57 if ($this->request->getSessionVar('captcha_ok') == true)
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']))
66 $this->request->setSessionVar('captcha_ok', true);
70 function getFormElements()
73 if (!$this->request->getSessionVar('captcha_ok')) {
75 = HTML::input(array('type' => 'text',
76 'class' => 'wikitext',
77 'id' => 'edit-captcha_input',
78 'name' => 'edit[captcha_input]',
79 'size' => $this->length + 2,
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:"));
91 if (defined('USE_CAPTCHA_RANDOM_WORD') and USE_CAPTCHA_RANDOM_WORD)
92 return $this->get_dictionary_word();
94 return rand_ascii_readable($this->length); // lib/stdlib.php
97 function get_dictionary_word()
99 // Load In the Word List
100 $fp = fopen(findfile("lib/captcha/dictionary"), "r");
103 $text[] = trim(fgets($fp, 1024));
108 while (strlen(trim($word)) == 0) {
109 $x = mt_rand(0, count($text));
116 function spiral(&$im, $origin_x = 100, $origin_y = 100, $r = 0, $g = 0, $b = 0)
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;
138 function image($word)
140 $width =& $this->width;
141 $height =& $this->height;
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);
149 $x = rand(0, $width);
150 $y = rand(0, $height);
151 $this->spiral($jpg, $x, $y, $width - 25, 190, 190);
154 $y = rand(50, $height - 20); //50-60
156 // randomize the chars
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);
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")),
172 $x = rand(0, $width + 30);
173 $y = rand(0, $height + 35); // 115
174 $this->spiral($jpg, $x, $y, 255, 190, 190);
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);
181 if (function_exists("ImageJpeg")) {
182 header("Content-type: image/jpeg");
184 } elseif (function_exists("ImagePNG")) {
185 header("Content-type: image/png");
187 } elseif (function_exists("ImageGIF")) {
188 header("Content-type: image/gif");
191 trigger_error("missing GD bitmap support", E_USER_WARNING);
201 // c-hanging-comment-ender-p: nil
202 // indent-tabs-mode: nil