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.
33 * @var WikiRequest $request
37 function __construct($meta = array(), $width = 250, $height = 80)
40 * @var WikiRequest $request
45 $this->width = $width;
46 $this->height = $height;
48 $this->failed_msg = _("Typed in verification word mismatch ... are you a bot?");
49 $this->request =& $request;
52 function captchaword()
54 if (!$this->request->getSessionVar('captchaword')) {
55 $this->request->setSessionVar('captchaword', $this->get_word());
57 return $this->request->getSessionVar('captchaword');
62 if ($this->request->getSessionVar('captcha_ok') == true)
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']))
71 $this->request->setSessionVar('captcha_ok', true);
75 function getFormElements()
78 if (!$this->request->getSessionVar('captcha_ok')) {
80 = HTML::input(array('type' => 'text',
81 'class' => 'wikitext',
82 'id' => 'edit-captcha_input',
83 'name' => 'edit[captcha_input]',
84 'size' => $this->length + 2,
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:"));
96 if (defined('USE_CAPTCHA_RANDOM_WORD') and USE_CAPTCHA_RANDOM_WORD)
97 return $this->get_dictionary_word();
99 return rand_ascii_readable($this->length); // lib/stdlib.php
102 function get_dictionary_word()
104 // Load In the Word List
105 $fp = fopen(findfile("lib/captcha/dictionary"), "r");
108 $text[] = trim(fgets($fp, 1024));
113 while (strlen(trim($word)) == 0) {
114 $x = mt_rand(0, count($text));
121 function spiral(&$im, $origin_x = 100, $origin_y = 100, $r = 0, $g = 0, $b = 0)
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;
143 function image($word)
145 $width =& $this->width;
146 $height =& $this->height;
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);
154 $x = rand(0, $width);
155 $y = rand(0, $height);
156 $this->spiral($jpg, $x, $y, $width - 25, 190, 190);
159 $y = rand(50, $height - 20); //50-60
161 // randomize the chars
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);
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")),
177 $x = rand(0, $width + 30);
178 $y = rand(0, $height + 35); // 115
179 $this->spiral($jpg, $x, $y, 255, 190, 190);
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);
186 if (function_exists("ImageJpeg")) {
187 header("Content-type: image/jpeg");
189 } elseif (function_exists("ImagePNG")) {
190 header("Content-type: image/png");
192 } elseif (function_exists("ImageGIF")) {
193 header("Content-type: image/gif");
196 trigger_error("missing GD bitmap support", E_USER_WARNING);
206 // c-hanging-comment-ender-p: nil
207 // indent-tabs-mode: nil