committing progress so i don't forget to save changes this month
[toast/cookiecaptcha.git] / FancyCaptcha.php
1 <?php
2
3 if ( defined( 'MEDIAWIKI' ) ) {
4
5 global $wgCaptchaDirectory;
6 $wgCaptchaDirectory = "$wgUploadDirectory/captcha"; // bad default :D
7
8 global $wgCaptchaSecret;
9 $wgCaptchaSecret = "CHANGE_THIS_SECRET!";
10
11
12 class FancyCaptcha extends SimpleCaptcha {
13         function keyMatch() {
14                 global $wgRequest, $wgCaptchaSecret;
15                 
16                 if( !isset( $_SESSION['ceAnswerVar'] ) ) {
17                         wfDebug( "FancyCaptcha: no session captcha key set, this is new visitor.\n" );
18                         return false;
19                 }
20                 
21                 $var  = @$_SESSION['ceAnswerVar'];
22                 $salt = @$_SESSION['captchaSalt'];
23                 $hash = @$_SESSION['captchaHash'];
24                 
25                 $answer = $wgRequest->getVal( $var );
26                 $digest = $wgCaptchaSecret . $salt . $answer . $wgCaptchaSecret . $salt;
27                 $answerHash = substr( md5( $digest ), 0, 16 );
28                 
29                 if( $answerHash == $hash ) {
30                         wfDebug( "FancyCaptcha: answer hash matches expected $hash\n" );
31                         return true;
32                 } else {
33                         wfDebug( "FancyCaptcha: answer hashes to $answerHash, expected $hash\n" );
34                         return false;
35                 }
36         }
37         
38         /**
39          * Insert the captcha prompt into the edit form.
40          */
41         function formCallback( &$out ) {
42                 $dest = 'wpCaptchaWord' . mt_rand();
43                 
44                 $img = $this->pickImage();
45                 if( !$img ) {
46                         die( "out of captcha images; this shouldn't happen" );
47                 }
48                 
49                 $_SESSION['ceAnswerVar'] = $dest;
50                 $_SESSION['captchaHash'] = $img['hash'];
51                 $_SESSION['captchaSalt'] = $img['salt'];
52                 $_SESSION['captchaViewed'] = false;
53                 wfDebug( "Picked captcha with hash ${img['hash']}, salt ${img['salt']}.\n" );
54                 
55                 $title = Title::makeTitle( NS_SPECIAL, 'Captcha/image' );
56                 
57                 $out->addWikiText( wfMsg( "captcha-short" ) );  
58                 $out->addHTML( "<p>" . 
59                         wfElement( 'img', array(
60                                 'src'    => $title->getLocalUrl(),
61                                 'width'  => $img['width'],
62                                 'height' => $img['height'],
63                                 'alt'    => '' ) ) .
64                         "</p>\n" .
65                         "<p><input name=\"$dest\" id=\"$dest\" tabindex=\"1\" /></p>" );
66         }
67         
68         /**
69          * Select a previously generated captcha image from the queue.
70          * @fixme subject to race conditions if lots of files vanish
71          * @return mixed tuple of (salt key, text hash) or false if no image to find
72          */
73         function pickImage() {
74                 global $wgCaptchaDirectory;
75                 $pick = mt_rand( 0, $this->countFiles( $wgCaptchaDirectory ) );
76                 $dir = opendir( $wgCaptchaDirectory );
77                 
78                 $n = mt_rand( 0, 16 );
79                 $count = 0;
80                 
81                 $entry = readdir( $dir );
82                 $pick = false;
83                 while( false !== $entry ) {
84                         $entry = readdir( $dir );
85                         if( preg_match( '/^image_([0-9a-f]+)_([0-9a-f]+)\\.png$/', $entry, $matches ) ) {
86                                 $size = getimagesize( "$wgCaptchaDirectory/$entry" );
87                                 $pick = array(
88                                         'salt' => $matches[1],
89                                         'hash' => $matches[2],
90                                         'width' => $size[0],
91                                         'height' => $size[1]
92                                 );
93                                 if( $count++ == $n ) {
94                                         break;
95                                 }
96                         }
97                 }
98                 closedir( $dir );
99                 return $pick;
100         }
101         
102         /**
103          * Count the number of files in a directory.
104          * @return int
105          */
106         function countFiles( $dirname ) {
107                 $dir = opendir( $dirname );
108                 $count = 0;
109                 while( false !== ($entry = readdir( $dir ) ) ) {
110                         if( $dir != '.' && $dir != '..' ) {
111                                 $count++;
112                         }
113                 }
114                 closedir( $dir );
115                 return $count;
116         }
117         
118         function showImage() {
119                 global $wgOut;
120                 $wgOut->disable();
121                 if( !empty( $_SESSION['captchaViewed'] ) ) {
122                         wfHttpError( 403, 'Access Forbidden', "Can't view captcha image a second time." );
123                         return false;
124                 }
125                 $_SESSION['captchaViewed'] = wfTimestamp();
126                 
127                 if( isset( $_SESSION['captchaSalt'] ) ) {
128                         $salt = $_SESSION['captchaSalt'];
129                         if( isset( $_SESSION['captchaHash'] ) ) {
130                                 $hash = $_SESSION['captchaHash'];
131                                 
132                                 global $wgCaptchaDirectory;
133                                 $file = $wgCaptchaDirectory . DIRECTORY_SEPARATOR . "image_{$salt}_{$hash}.png";
134                                 if( file_exists( $file ) ) {
135                                         header( 'Content-type: image/png' );
136                                         readfile( $file );
137                                 }
138                         }
139                 } else {
140                         wfHttpError( 500, 'Internal Error', 'Requested bogus captcha image' );
141                 }
142         }
143 }
144
145 } # End invocation guard
146
147 ?>