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();
161 if( $info['viewed'] ) {
162 wfHttpError( 403, 'Access Forbidden', "Can't view captcha image a second time." );
166 $info['viewed'] = wfTimestamp();
167 $this->storeCaptcha( $info );
169 $salt = $info['salt'];
170 $hash = $info['hash'];
171 $file = $this->imagePath( $salt, $hash );
173 if( file_exists( $file ) ) {
175 require_once "$IP/includes/StreamFile.php";
176 wfStreamFile( $file );
180 wfHttpError( 500, 'Internal Error', 'Requested bogus captcha image' );
184 function imagePath( $salt, $hash ) {
185 global $wgCaptchaDirectory, $wgCaptchaDirectoryLevels;
186 $file = $wgCaptchaDirectory;
187 $file .= DIRECTORY_SEPARATOR;
188 for( $i = 0; $i < $wgCaptchaDirectoryLevels; $i++ ) {
190 $file .= DIRECTORY_SEPARATOR;
192 $file .= "image_{$salt}_{$hash}.png";
197 * Show a message asking the user to enter a captcha on edit
198 * The result will be treated as wiki text
200 * @param $action Action being performed
203 function getMessage( $action ) {
204 $name = 'fancycaptcha-' . $action;
205 $text = wfMsg( $name );
206 # Obtain a more tailored message, if possible, otherwise, fall back to
207 # the default for edits
208 return wfEmptyMsg( $name, $text ) ? wfMsg( 'fancycaptcha-edit' ) : $text;