]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/Captcha.php
captcha feature by Benjamin Drieu. Patch #1110699
[SourceForge/phpwiki.git] / lib / Captcha.php
1 <?php
2 /*
3   Session Captcha v1.0
4   by Gavin M. Roy <gmr@bteg.net>
5   Modified by Benjamin Drieu <bdrieu@april.org> - 2005 for PhpWiki
6
7   This File is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or
10   (at your option) any later version.
11   
12   This File is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with This File; if not, write to the Free Software Foundation,
19   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22
23 function get_captcha_word () {
24     // Load In the Word List
25     $fp = fopen(FindFile("lib/captcha/dictionary"), "r");
26     while ( !feof($fp) )
27         $text[] = Trim(fgets($fp, 1024));
28     fclose($fp);
29
30     // Pick a Word
31     $word = "";
32     while ( strlen(Trim($word)) == 0 ) {
33         $x = rand(0, Count($text));
34         return $text[$x];
35     }
36 }
37
38 // Draw the Spiral
39 function spiral( &$im, $origin_x = 100, $origin_y = 100, $r = 0, $g = 0, $b = 0 ) {
40     $theta = 1;
41     $thetac = 6;  
42     $radius = 15;  
43     $circles = 10;  
44     $points = 35;  
45     $lcolor = imagecolorallocate( $im, $r, $g, $b );
46     for( $i = 0; $i < ( $circles * $points ) - 1; $i++ ) {
47         $theta = $theta + $thetac;
48         $rad = $radius * ( $i / $points );
49         $x = ( $rad * cos( $theta ) ) + $origin_x;
50         $y = ( $rad * sin( $theta ) ) + $origin_y;
51         $theta = $theta + $thetac;
52         $rad1 = $radius * ( ( $i + 1 ) / $points );
53         $x1 = ( $rad1 * cos( $theta ) ) + $origin_x;
54         $y1 = ( $rad1 * sin( $theta ) ) + $origin_y;
55         imageline( $im, $x, $y, $x1, $y1, $lcolor );
56         $theta = $theta - $thetac;
57     }
58 }
59
60 function captcha_image ( $word ) {
61     $width = 250;
62     $height = 80;
63     
64     // Create the Image
65     $jpg = ImageCreate($width,$height);
66     $bg = ImageColorAllocate($jpg,255,255,255);
67     $tx = ImageColorAllocate($jpg,185,140,140);
68     ImageFilledRectangle($jpg,0,0,$width,$height,$bg);
69
70     $x = rand(0, $width);
71     $y = rand(0, $height);
72     spiral($jpg, $x, $y, 225, 190, 190);
73
74     $angle = rand(-25, 25);
75     $size = rand(14,20);
76     if ( $angle >= 0 )
77         $y = rand(50,$height-20);
78     else 
79         $y = rand(25, 50);
80     $x = rand(10, $width-100);
81
82     imagettftext($jpg, $size, $angle, $x, $y, $tx, FindFile("lib/captcha/Vera.ttf"), $word);
83
84     $x = rand(0, 280);
85     $y = rand(0, 115);
86     spiral($jpg, $x, $y, 255,190,190);
87
88     imageline($jpg, 0,0,$width-1,0,$tx);
89     imageline($jpg, 0,0,0,$height-1,$tx);
90     imageline($jpg, 0,$height-1,$width-1,$height-1,$tx);
91     imageline($jpg, $width-1,0,$width-1,$height-1,$tx);
92
93     header("Content-type: image/jpeg");
94     ImageJpeg($jpg);
95 }
96 ?>