3 class FancyCaptcha extends SimpleCaptcha {
5 * Check if the submitted form matches the captcha session data provided
6 * by the plugin when the form was generated.
8 * @param string $answer
12 function keyMatch( $answer, $info ) {
13 global $wgCaptchaSecret;
15 $digest = $wgCaptchaSecret . $info['salt'] . $answer . $wgCaptchaSecret . $info['salt'];
16 $answerHash = substr( md5( $digest ), 0, 16 );
18 if( $answerHash == $info['hash'] ) {
19 wfDebug( "FancyCaptcha: answer hash matches expected {$info['hash']}\n" );
22 wfDebug( "FancyCaptcha: answer hashes to $answerHash, expected {$info['hash']}\n" );
28 * Insert the captcha prompt into the edit form.
31 $info = $this->pickImage();
33 die( "out of captcha images; this shouldn't happen" );
36 // Generate a random key for use of this captcha image in this session.
37 // This is needed so multiple edits in separate tabs or windows can
38 // go through without extra pain.
39 $index = $this->storeCaptcha( $info );
41 wfDebug( "Captcha id $index using hash ${info['hash']}, salt ${info['salt']}.\n" );
43 $title = Title::makeTitle( NS_SPECIAL, 'Captcha/image' );
46 wfElement( 'img', array(
47 'src' => $title->getLocalUrl( 'wpCaptchaId=' . urlencode( $index ) ),
48 'width' => $info['width'],
49 'height' => $info['height'],
52 wfElement( 'input', array(
54 'name' => 'wpCaptchaId',
55 'id' => 'wpCaptchaId',
56 'value' => $index ) ) .
58 wfElement( 'input', array(
59 'name' => 'wpCaptchaWord',
60 'id' => 'wpCaptchaWord',
61 'tabindex' => 1 ) ) . // tab in before the edit textarea
66 * Select a previously generated captcha image from the queue.
67 * @fixme subject to race conditions if lots of files vanish
68 * @return mixed tuple of (salt key, text hash) or false if no image to find
70 function pickImage() {
71 global $wgCaptchaDirectory, $wgCaptchaDirectoryLevels;
72 return $this->pickImageDir(
74 $wgCaptchaDirectoryLevels );
77 function pickImageDir( $directory, $levels ) {
81 // Check which subdirs are actually present...
82 $dir = opendir( $directory );
83 while( false !== ($entry = readdir( $dir ) ) ) {
84 if( ctype_xdigit( $entry ) && strlen( $entry ) == 1 ) {
90 $place = mt_rand( 0, count( $dirs ) - 1 );
91 // In case all dirs are not filled,
92 // cycle through next digits...
93 for( $j = 0; $j < count( $dirs ); $j++ ) {
94 $char = $dirs[($place + $j) % count( $dirs )];
95 $return = $this->pickImageDir( "$directory/$char", $levels - 1 );
100 // Didn't find any images in this directory... empty?
103 return $this->pickImageFromDir( $directory );
107 function pickImageFromDir( $directory ) {
108 if( !is_dir( $directory ) ) {
111 $n = mt_rand( 0, $this->countFiles( $directory ) - 1 );
112 $dir = opendir( $directory );
116 $entry = readdir( $dir );
118 while( false !== $entry ) {
119 $entry = readdir( $dir );
120 if( preg_match( '/^image_([0-9a-f]+)_([0-9a-f]+)\\.png$/', $entry, $matches ) ) {
121 $size = getimagesize( "$directory/$entry" );
123 'salt' => $matches[1],
124 'hash' => $matches[2],
126 'height' => $size[1],
129 if( $count++ == $n ) {
139 * Count the number of files in a directory.
142 function countFiles( $dirname ) {
143 $dir = opendir( $dirname );
145 while( false !== ($entry = readdir( $dir ) ) ) {
146 if( $entry != '.' && $entry != '..' ) {
154 function showImage() {
155 global $wgOut, $wgRequest;
159 $info = $this->retrieveCaptcha();
162 // Be a little less restrictive for now; in at least some circumstances,
163 // Konqueror tries to reload the image even if you haven't navigated
164 // away from the page.
165 if( $info['viewed'] ) {
166 wfHttpError( 403, 'Access Forbidden', "Can't view captcha image a second time." );
171 $info['viewed'] = wfTimestamp();
172 $this->storeCaptcha( $info );
174 $salt = $info['salt'];
175 $hash = $info['hash'];
176 $file = $this->imagePath( $salt, $hash );
178 if( file_exists( $file ) ) {
180 require_once "$IP/includes/StreamFile.php";
181 header( "Cache-Control: private, s-maxage=0, max-age=3600" );
182 wfStreamFile( $file );
186 wfHttpError( 500, 'Internal Error', 'Requested bogus captcha image' );
190 function imagePath( $salt, $hash ) {
191 global $wgCaptchaDirectory, $wgCaptchaDirectoryLevels;
192 $file = $wgCaptchaDirectory;
193 $file .= DIRECTORY_SEPARATOR;
194 for( $i = 0; $i < $wgCaptchaDirectoryLevels; $i++ ) {
196 $file .= DIRECTORY_SEPARATOR;
198 $file .= "image_{$salt}_{$hash}.png";
203 * Show a message asking the user to enter a captcha on edit
204 * The result will be treated as wiki text
206 * @param $action Action being performed
209 function getMessage( $action ) {
210 $name = 'fancycaptcha-' . $action;
211 $text = wfMsg( $name );
212 # Obtain a more tailored message, if possible, otherwise, fall back to
213 # the default for edits
214 return wfEmptyMsg( $name, $text ) ? wfMsg( 'fancycaptcha-edit' ) : $text;