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.
28 function Captcha($meta = array(), $width = 250, $height = 80)
31 $this->width = $width;
32 $this->height = $height;
34 $this->failed_msg = _("Typed in verification word mismatch ... are you a bot?");
35 $this->request =& $GLOBALS['request'];
38 function captchaword()
40 if (!$this->request->getSessionVar('captchaword')) {
41 $this->request->setSessionVar('captchaword', $this->get_word());
43 return $this->request->getSessionVar('captchaword');
48 if ($this->request->getSessionVar('captcha_ok') == true)
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']))
57 $this->request->setSessionVar('captcha_ok', true);
61 function getFormElements()
64 if (!$this->request->getSessionVar('captcha_ok')) {
66 = HTML::input(array('type' => 'text',
67 'class' => 'wikitext',
68 'id' => 'edit-captcha_input',
69 'name' => 'edit[captcha_input]',
70 'size' => $this->length + 2,
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:"));
82 if (USE_CAPTCHA_RANDOM_WORD)
83 return get_dictionary_word();
85 return rand_ascii_readable($this->length); // lib/stdlib.php
88 function get_dictionary_word()
90 // Load In the Word List
91 $fp = fopen(findfile("lib/captcha/dictionary"), "r");
93 $text[] = trim(fgets($fp, 1024));
99 while (strlen(trim($word)) == 0) {
100 if (function_exists('mt_rand'))
101 $x = mt_rand(0, count($text));
103 $x = rand(0, count($text));
109 function spiral(&$im, $origin_x = 100, $origin_y = 100, $r = 0, $g = 0, $b = 0)
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;
131 function image($word)
133 $width =& $this->width;
134 $height =& $this->height;
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);
142 $x = rand(0, $width);
143 $y = rand(0, $height);
144 $this->spiral($jpg, $x, $y, $width - 25, 190, 190);
147 $y = rand(50, $height - 20); //50-60
149 // randomize the chars
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);
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")),
165 $x = rand(0, $width + 30);
166 $y = rand(0, $height + 35); // 115
167 $this->spiral($jpg, $x, $y, 255, 190, 190);
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);
174 if (function_exists("ImageJpeg")) {
175 header("Content-type: image/jpeg");
177 } elseif (function_exists("ImagePNG")) {
178 header("Content-type: image/png");
180 } elseif (function_exists("ImageGIF")) {
181 header("Content-type: image/gif");
184 trigger_error("missing GD bitmap support", E_USER_WARNING);
194 // c-hanging-comment-ender-p: nil
195 // indent-tabs-mode: nil