2 rcs_id('$Id: Captcha.php,v 1.6 2007-01-07 18:42:10 rurban Exp $');
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
21 along with This File; if not, write to the Free Software Foundation,
22 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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'] = "<img src=\"$url\" alt=\"captcha\" />";
68 $el['CAPTCHA_LABEL'] = '<label for="edit-captcha_input">'._("Type word above:").' </label>';
73 function get_word () {
74 if (USE_CAPTCHA_RANDOM_WORD)
75 return get_dictionary_word();
77 return rand_ascii_readable($this->length); // lib/stdlib.php
80 function get_dictionary_word () {
81 // Load In the Word List
82 $fp = fopen(findfile("lib/captcha/dictionary"), "r");
84 $text[] = trim(fgets($fp, 1024));
90 while ( strlen(trim($word)) == 0 ) {
91 if (function_exists('mt_rand'))
92 $x = mt_rand(0, count($text));
94 $x = rand(0, count($text));
100 function spiral( &$im, $origin_x = 100, $origin_y = 100, $r = 0, $g = 0, $b = 0 ) {
106 $lcolor = imagecolorallocate( $im, $r, $g, $b );
107 for( $i = 0; $i < ( $circles * $points ) - 1; $i++ ) {
108 $theta = $theta + $thetac;
109 $rad = $radius * ( $i / $points );
110 $x = ( $rad * cos( $theta ) ) + $origin_x;
111 $y = ( $rad * sin( $theta ) ) + $origin_y;
112 $theta = $theta + $thetac;
113 $rad1 = $radius * ( ( $i + 1 ) / $points );
114 $x1 = ( $rad1 * cos( $theta ) ) + $origin_x;
115 $y1 = ( $rad1 * sin( $theta ) ) + $origin_y;
116 imageline( $im, $x, $y, $x1, $y1, $lcolor );
117 $theta = $theta - $thetac;
121 function image ( $word ) {
122 $width =& $this->width;
123 $height =& $this->height;
126 $jpg = ImageCreate($width,$height);
127 $bg = ImageColorAllocate($jpg,255,255,255);
128 $tx = ImageColorAllocate($jpg,185,140,140);
129 ImageFilledRectangle($jpg,0,0,$width,$height,$bg);
131 $x = rand(0, $width);
132 $y = rand(0, $height);
133 $this->spiral($jpg, $x, $y, $width-25, 190, 190);
136 $y = rand(50, $height-20); //50-60
138 // randomize the chars
139 for ($i=0; $i < strlen($word); $i++) {
140 $angle += rand(-5, 5);
141 if ( $angle > 25 ) $angle = 15;
142 elseif ( $angle < -25 ) $angle = -15;
143 $size = rand(14, 20);
145 if ( $y < 10 ) $y = 11;
146 elseif ( $y > $height-10 ) $y = $height-11;
147 $x += rand($size, $size*2);
148 imagettftext($jpg, $size, $angle, $x, $y, $tx,
149 realpath(findfile("lib/captcha/Vera.ttf")),
153 $x = rand(0, $width+30);
154 $y = rand(0, $height+35); // 115
155 $this->spiral($jpg, $x, $y, 255,190,190);
157 imageline($jpg, 0,0,$width-1,0,$tx);
158 imageline($jpg, 0,0,0,$height-1,$tx);
159 imageline($jpg, 0,$height-1,$width-1,$height-1,$tx);
160 imageline($jpg, $width-1,0,$width-1,$height-1,$tx);
162 if (function_exists("ImageJpeg")) {
163 header("Content-type: image/jpeg");
165 } elseif (function_exists("ImagePNG")) {
166 header("Content-type: image/png");
168 } elseif (function_exists("ImageGIF")) {
169 header("Content-type: image/gif");
172 trigger_error("missing GD bitmap support", E_USER_WARNING);
178 // $Log: not supported by cvs2svn $
179 // Revision 1.5 2005/10/31 16:44:13 rurban
180 // fallback to PNG or GIF if JPEG support does not exist
182 // Revision 1.4 2005/10/30 14:20:42 rurban
183 // move Captcha specific vars and methods into a Captcha object
184 // randomize Captcha chars positions and angles (smoothly)
186 // Revision 1.3 2005/10/29 07:37:56 rurban
187 // USE_CAPTCHA_RANDOM_WORD by Dan Frankowski
194 // c-hanging-comment-ender-p: nil
195 // indent-tabs-mode: nil