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.
27 function Captcha($meta = array(), $width = 250, $height = 80) {
29 $this->width = $width;
30 $this->height = $height;
32 $this->failed_msg = _("Typed in verification word mismatch ... are you a bot?");
33 $this->request =& $GLOBALS['request'];
36 function captchaword() {
37 if ( ! $this->request->getSessionVar('captchaword')) {
38 $this->request->setSessionVar('captchaword', $this->get_word());
40 return $this->request->getSessionVar('captchaword');
44 if ($this->request->getSessionVar('captcha_ok') == true)
47 if ( ! array_key_exists ( 'captcha_input', $this->meta )
48 or ($this->request->getSessionVar('captchaword')
49 and ($this->request->getSessionVar('captchaword') != $this->meta['captcha_input'])))
52 $this->request->setSessionVar('captcha_ok', true);
56 function getFormElements() {
58 if (! $this->request->getSessionVar('captcha_ok')) {
60 = HTML::input(array('type' => 'text',
61 'class' => 'wikitext',
62 'id' => 'edit-captcha_input',
63 'name' => 'edit[captcha_input]',
64 'size' => $this->length + 2,
66 $url = WikiURL("", array("action"=>"captcha","id"=>time()), false);
67 $el['CAPTCHA_IMAGE'] = HTML::img(array('src' => $url, 'alt' => 'captcha'));
68 $el['CAPTCHA_LABEL'] = HTML::label(array('for' => 'edit-captcha_input'),
69 _("Type word above:"));
74 function get_word () {
75 if (USE_CAPTCHA_RANDOM_WORD)
76 return get_dictionary_word();
78 return rand_ascii_readable($this->length); // lib/stdlib.php
81 function get_dictionary_word () {
82 // Load In the Word List
83 $fp = fopen(findfile("lib/captcha/dictionary"), "r");
85 $text[] = trim(fgets($fp, 1024));
91 while ( strlen(trim($word)) == 0 ) {
92 if (function_exists('mt_rand'))
93 $x = mt_rand(0, count($text));
95 $x = rand(0, count($text));
101 function spiral( &$im, $origin_x = 100, $origin_y = 100, $r = 0, $g = 0, $b = 0 ) {
107 $lcolor = imagecolorallocate( $im, $r, $g, $b );
108 for( $i = 0; $i < ( $circles * $points ) - 1; $i++ ) {
109 $theta = $theta + $thetac;
110 $rad = $radius * ( $i / $points );
111 $x = ( $rad * cos( $theta ) ) + $origin_x;
112 $y = ( $rad * sin( $theta ) ) + $origin_y;
113 $theta = $theta + $thetac;
114 $rad1 = $radius * ( ( $i + 1 ) / $points );
115 $x1 = ( $rad1 * cos( $theta ) ) + $origin_x;
116 $y1 = ( $rad1 * sin( $theta ) ) + $origin_y;
117 imageline( $im, $x, $y, $x1, $y1, $lcolor );
118 $theta = $theta - $thetac;
122 function image ( $word ) {
123 $width =& $this->width;
124 $height =& $this->height;
127 $jpg = ImageCreate($width,$height);
128 $bg = ImageColorAllocate($jpg,255,255,255);
129 $tx = ImageColorAllocate($jpg,185,140,140);
130 ImageFilledRectangle($jpg,0,0,$width,$height,$bg);
132 $x = rand(0, $width);
133 $y = rand(0, $height);
134 $this->spiral($jpg, $x, $y, $width-25, 190, 190);
137 $y = rand(50, $height-20); //50-60
139 // randomize the chars
140 for ($i=0; $i < strlen($word); $i++) {
141 $angle += rand(-5, 5);
142 if ( $angle > 25 ) $angle = 15;
143 elseif ( $angle < -25 ) $angle = -15;
144 $size = rand(14, 20);
146 if ( $y < 10 ) $y = 11;
147 elseif ( $y > $height-10 ) $y = $height-11;
148 $x += rand($size, $size*2);
149 imagettftext($jpg, $size, $angle, $x, $y, $tx,
150 realpath(findfile("lib/captcha/Vera.ttf")),
154 $x = rand(0, $width+30);
155 $y = rand(0, $height+35); // 115
156 $this->spiral($jpg, $x, $y, 255,190,190);
158 imageline($jpg, 0,0,$width-1,0,$tx);
159 imageline($jpg, 0,0,0,$height-1,$tx);
160 imageline($jpg, 0,$height-1,$width-1,$height-1,$tx);
161 imageline($jpg, $width-1,0,$width-1,$height-1,$tx);
163 if (function_exists("ImageJpeg")) {
164 header("Content-type: image/jpeg");
166 } elseif (function_exists("ImagePNG")) {
167 header("Content-type: image/png");
169 } elseif (function_exists("ImageGIF")) {
170 header("Content-type: image/gif");
173 trigger_error("missing GD bitmap support", E_USER_WARNING);
183 // c-hanging-comment-ender-p: nil
184 // indent-tabs-mode: nil