]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/Captcha.php
USE_CAPTCHA_RANDOM_WORD by Dan Frankowski
[SourceForge/phpwiki.git] / lib / Captcha.php
1 <?php
2 rcs_id('$Id: Captcha.php,v 1.3 2005-10-29 07:37:56 rurban Exp $');
3 /*
4   Session Captcha v1.0
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
9   This File is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13   
14   This File is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   General Public License for more details.
18
19   You should have received a copy of the GNU General Public License
20   along with This File; if not, write to the Free Software Foundation,
21   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24
25 function get_captcha_word () { 
26     if (USE_CAPTCHA_RANDOM_WORD)
27         return get_captcha_dictionary_word(); 
28     else
29         return get_captcha_random_word(); 
30 }
31
32 function get_captcha_dictionary_word () {
33     // Load In the Word List
34     $fp = fopen(FindFile("lib/captcha/dictionary"), "r");
35     while ( !feof($fp) )
36         $text[] = Trim(fgets($fp, 1024));
37     fclose($fp);
38
39     // Pick a Word
40     $word = "";
41     while ( strlen(Trim($word)) == 0 ) {
42         $x = rand(0, Count($text));
43         return $text[$x];
44     }
45 }
46
47 /* by Dan Frankowski.
48  */
49 function get_captcha_random_word () {
50     // Pick a few random letters or numbers
51     $word = "";
52     // Don't use 1 <=> l or 0 (for o), because they're hard to read
53     $letters = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKMNOPQRSTUVWXYZ23456789";
54     $letter_len = strlen($letters);
55     for ($i=0; $i<4; $i++) {
56         $word .= $letters[mt_rand(0, $letter_len-1)];
57     }
58     return $word;
59 }
60
61 // Draw the Spiral
62 function spiral( &$im, $origin_x = 100, $origin_y = 100, $r = 0, $g = 0, $b = 0 ) {
63     $theta = 1;
64     $thetac = 6;  
65     $radius = 15;  
66     $circles = 10;  
67     $points = 35;  
68     $lcolor = imagecolorallocate( $im, $r, $g, $b );
69     for( $i = 0; $i < ( $circles * $points ) - 1; $i++ ) {
70         $theta = $theta + $thetac;
71         $rad = $radius * ( $i / $points );
72         $x = ( $rad * cos( $theta ) ) + $origin_x;
73         $y = ( $rad * sin( $theta ) ) + $origin_y;
74         $theta = $theta + $thetac;
75         $rad1 = $radius * ( ( $i + 1 ) / $points );
76         $x1 = ( $rad1 * cos( $theta ) ) + $origin_x;
77         $y1 = ( $rad1 * sin( $theta ) ) + $origin_y;
78         imageline( $im, $x, $y, $x1, $y1, $lcolor );
79         $theta = $theta - $thetac;
80     }
81 }
82
83 function captcha_image ( $word ) {
84     $width = 250;
85     $height = 80;
86     
87     // Create the Image
88     $jpg = ImageCreate($width,$height);
89     $bg = ImageColorAllocate($jpg,255,255,255);
90     $tx = ImageColorAllocate($jpg,185,140,140);
91     ImageFilledRectangle($jpg,0,0,$width,$height,$bg);
92
93     $x = rand(0, $width);
94     $y = rand(0, $height);
95     spiral($jpg, $x, $y, 225, 190, 190);
96
97     $angle = rand(-25, 25);
98     $size = rand(14,20);
99     if ( $angle >= 0 )
100         $y = rand(50,$height-20);
101     else 
102         $y = rand(25, 50);
103     $x = rand(10, $width-100);
104
105     imagettftext($jpg, $size, $angle, $x, $y, $tx, 
106                  realpath(FindFile("lib/captcha/Vera.ttf")), 
107                  $word);
108
109     $x = rand(0, 280);
110     $y = rand(0, 115);
111     spiral($jpg, $x, $y, 255,190,190);
112
113     imageline($jpg, 0,0,$width-1,0,$tx);
114     imageline($jpg, 0,0,0,$height-1,$tx);
115     imageline($jpg, 0,$height-1,$width-1,$height-1,$tx);
116     imageline($jpg, $width-1,0,$width-1,$height-1,$tx);
117
118     header("Content-type: image/jpeg");
119     ImageJpeg($jpg);
120 }
121
122 // $Log: not supported by cvs2svn $
123
124 // Local Variables:
125 // mode: php
126 // tab-width: 8
127 // c-basic-offset: 4
128 // c-hanging-comment-ender-p: nil
129 // indent-tabs-mode: nil
130 // End:   
131 ?>