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" );
27 function addCaptchaAPI( &$resultArr ) {
28 $info = $this->pickImage();
30 $resultArr['captcha']['error'] = 'Out of images';
33 $index = $this->storeCaptcha( $info );
34 $title = SpecialPage::getTitleFor( 'Captcha', 'image' );
35 $resultArr['captcha']['type'] = 'image';
36 $resultArr['captcha']['mime'] = 'image/png';
37 $resultArr['captcha']['id'] = $index;
38 $resultArr['captcha']['url'] = $title->getLocalUrl( 'wpCaptchaId=' . urlencode( $index ) );
42 * Insert the captcha prompt into the edit form.
45 $info = $this->pickImage();
47 throw new MWException( "Ran out of captcha images" );
50 // Generate a random key for use of this captcha image in this session.
51 // This is needed so multiple edits in separate tabs or windows can
52 // go through without extra pain.
53 $index = $this->storeCaptcha( $info );
55 wfDebug( "Captcha id $index using hash ${info['hash']}, salt ${info['salt']}.\n" );
57 $title = SpecialPage::getTitleFor( 'Captcha', 'image' );
60 Html::element( 'img', array(
61 'src' => $title->getLocalUrl( 'wpCaptchaId=' . urlencode( $index ) ),
62 'width' => $info['width'],
63 'height' => $info['height'],
66 Html::element( 'input', array(
68 'name' => 'wpCaptchaId',
69 'id' => 'wpCaptchaId',
70 'value' => $index ) ) .
72 Html::element( 'label', array(
73 'for' => 'wpCaptchaWord',
74 ), parent::getMessage( 'label' ) . wfMsg( 'colon-separator' ) ) .
75 Html::element( 'input', array(
76 'name' => 'wpCaptchaWord',
77 'id' => 'wpCaptchaWord',
79 'autocorrect' => 'off',
80 'autocapitalize' => 'off',
81 'required' => 'required',
82 'tabindex' => 1 ) ) . // tab in before the edit textarea
87 * Select a previously generated captcha image from the queue.
88 * @fixme subject to race conditions if lots of files vanish
89 * @return mixed tuple of (salt key, text hash) or false if no image to find
91 function pickImage() {
92 global $wgCaptchaDirectory, $wgCaptchaDirectoryLevels;
93 return $this->pickImageDir(
95 $wgCaptchaDirectoryLevels );
98 function pickImageDir( $directory, $levels ) {
102 // Check which subdirs are actually present...
103 $dir = opendir( $directory );
107 while ( false !== ( $entry = readdir( $dir ) ) ) {
108 if ( ctype_xdigit( $entry ) && strlen( $entry ) == 1 ) {
114 $place = mt_rand( 0, count( $dirs ) - 1 );
115 // In case all dirs are not filled,
116 // cycle through next digits...
117 for ( $j = 0; $j < count( $dirs ); $j++ ) {
118 $char = $dirs[( $place + $j ) % count( $dirs )];
119 $return = $this->pickImageDir( "$directory/$char", $levels - 1 );
124 // Didn't find any images in this directory... empty?
127 return $this->pickImageFromDir( $directory );
131 function pickImageFromDir( $directory ) {
132 if ( !is_dir( $directory ) ) {
135 $n = mt_rand( 0, $this->countFiles( $directory ) - 1 );
136 $dir = opendir( $directory );
140 $entry = readdir( $dir );
142 while ( false !== $entry ) {
143 $entry = readdir( $dir );
144 if ( preg_match( '/^image_([0-9a-f]+)_([0-9a-f]+)\\.png$/', $entry, $matches ) ) {
145 $size = getimagesize( "$directory/$entry" );
147 'salt' => $matches[1],
148 'hash' => $matches[2],
150 'height' => $size[1],
153 if ( $count++ == $n ) {
163 * Count the number of files in a directory.
166 function countFiles( $dirname ) {
167 $dir = opendir( $dirname );
169 while ( false !== ( $entry = readdir( $dir ) ) ) {
170 if ( $entry != '.' && $entry != '..' ) {
178 function showImage() {
183 $info = $this->retrieveCaptcha();
186 // Be a little less restrictive for now; in at least some circumstances,
187 // Konqueror tries to reload the image even if you haven't navigated
188 // away from the page.
189 if( $info['viewed'] ) {
190 wfHttpError( 403, 'Access Forbidden', "Can't view captcha image a second time." );
195 $info['viewed'] = wfTimestamp();
196 $this->storeCaptcha( $info );
198 $salt = $info['salt'];
199 $hash = $info['hash'];
200 $file = $this->imagePath( $salt, $hash );
202 if ( file_exists( $file ) ) {
204 require_once "$IP/includes/StreamFile.php";
205 header( "Cache-Control: private, s-maxage=0, max-age=3600" );
206 wfStreamFile( $file );
210 wfHttpError( 500, 'Internal Error', 'Requested bogus captcha image' );
214 function imagePath( $salt, $hash ) {
215 global $wgCaptchaDirectory, $wgCaptchaDirectoryLevels;
216 $file = $wgCaptchaDirectory;
217 $file .= DIRECTORY_SEPARATOR;
218 for ( $i = 0; $i < $wgCaptchaDirectoryLevels; $i++ ) {
219 $file .= $hash { $i } ;
220 $file .= DIRECTORY_SEPARATOR;
222 $file .= "image_{$salt}_{$hash}.png";
227 * Show a message asking the user to enter a captcha on edit
228 * The result will be treated as wiki text
230 * @param $action Action being performed
233 function getMessage( $action ) {
234 $name = 'fancycaptcha-' . $action;
235 $text = wfMsg( $name );
236 # Obtain a more tailored message, if possible, otherwise, fall back to
237 # the default for edits
238 return wfEmptyMsg( $name, $text ) ? wfMsg( 'fancycaptcha-edit' ) : $text;
242 * Delete a solved captcha image, if $wgCaptchaDeleteOnSolve is true.
244 function passCaptcha() {
245 global $wgCaptchaDeleteOnSolve;
247 $info = $this->retrieveCaptcha(); // get the captcha info before it gets deleted
248 $pass = parent::passCaptcha();
250 if ( $pass && $wgCaptchaDeleteOnSolve ) {
251 $filename = $this->imagePath( $info['salt'], $info['hash'] );
252 if ( file_exists( $filename ) ) {