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 WebRequest $request
12 function keyMatch( $request, $info ) {
13 global $wgCaptchaSecret;
15 $answer = $request->getVal( 'wpCaptchaWord' );
16 $digest = $wgCaptchaSecret . $info['salt'] . $answer . $wgCaptchaSecret . $info['salt'];
17 $answerHash = substr( md5( $digest ), 0, 16 );
19 if( $answerHash == $info['hash'] ) {
20 wfDebug( "FancyCaptcha: answer hash matches expected {$info['hash']}\n" );
23 wfDebug( "FancyCaptcha: answer hashes to $answerHash, expected {$info['hash']}\n" );
29 * Insert the captcha prompt into the edit form.
32 $info = $this->pickImage();
34 die( "out of captcha images; this shouldn't happen" );
37 // Generate a random key for use of this captcha image in this session.
38 // This is needed so multiple edits in separate tabs or windows can
39 // go through without extra pain.
40 $index = $this->storeCaptcha( $info );
42 wfDebug( "Captcha id $index using hash ${info['hash']}, salt ${info['salt']}.\n" );
44 $title = Title::makeTitle( NS_SPECIAL, 'Captcha/image' );
47 wfElement( 'img', array(
48 'src' => $title->getLocalUrl( 'wpCaptchaId=' . urlencode( $index ) ),
49 'width' => $info['width'],
50 'height' => $info['height'],
53 wfElement( 'input', array(
55 'name' => 'wpCaptchaId',
56 'id' => 'wpCaptchaId',
57 'value' => $index ) ) .
59 wfElement( 'input', array(
60 'name' => 'wpCaptchaWord',
61 'id' => 'wpCaptchaWord',
62 'tabindex' => 1 ) ) . // tab in before the edit textarea
67 * Select a previously generated captcha image from the queue.
68 * @fixme subject to race conditions if lots of files vanish
69 * @return mixed tuple of (salt key, text hash) or false if no image to find
71 function pickImage() {
72 global $wgCaptchaDirectory, $wgCaptchaDirectoryLevels;
73 return $this->pickImageDir(
75 $wgCaptchaDirectoryLevels );
78 function pickImageDir( $directory, $levels ) {
82 // Check which subdirs are actually present...
83 $dir = opendir( $directory );
84 while( false !== ($entry = readdir( $dir ) ) ) {
85 if( ctype_xdigit( $entry ) && strlen( $entry ) == 1 ) {
91 $place = mt_rand( 0, count( $dirs ) - 1 );
92 // In case all dirs are not filled,
93 // cycle through next digits...
94 for( $j = 0; $j < count( $dirs ); $j++ ) {
95 $char = $dirs[($place + $j) % count( $dirs )];
96 $return = $this->pickImageDir( "$directory/$char", $levels - 1 );
101 // Didn't find any images in this directory... empty?
104 return $this->pickImageFromDir( $directory );
108 function pickImageFromDir( $directory ) {
109 if( !is_dir( $directory ) ) {
112 $n = mt_rand( 0, $this->countFiles( $directory ) - 1 );
113 $dir = opendir( $directory );
117 $entry = readdir( $dir );
119 while( false !== $entry ) {
120 $entry = readdir( $dir );
121 if( preg_match( '/^image_([0-9a-f]+)_([0-9a-f]+)\\.png$/', $entry, $matches ) ) {
122 $size = getimagesize( "$directory/$entry" );
124 'salt' => $matches[1],
125 'hash' => $matches[2],
127 'height' => $size[1],
130 if( $count++ == $n ) {
140 * Count the number of files in a directory.
143 function countFiles( $dirname ) {
144 $dir = opendir( $dirname );
146 while( false !== ($entry = readdir( $dir ) ) ) {
147 if( $entry != '.' && $entry != '..' ) {
155 function showImage() {
156 global $wgOut, $wgRequest;
160 $info = $this->retrieveCaptcha();
162 if( $info['viewed'] ) {
163 wfHttpError( 403, 'Access Forbidden', "Can't view captcha image a second time." );
167 $info['viewed'] = wfTimestamp();
168 $this->storeCaptcha( $info );
170 $salt = $info['salt'];
171 $hash = $info['hash'];
172 $file = $this->imagePath( $salt, $hash );
174 if( file_exists( $file ) ) {
176 require_once "$IP/includes/StreamFile.php";
177 wfStreamFile( $file );
181 wfHttpError( 500, 'Internal Error', 'Requested bogus captcha image' );
185 function imagePath( $salt, $hash ) {
186 global $wgCaptchaDirectory, $wgCaptchaDirectoryLevels;
187 $file = $wgCaptchaDirectory;
188 $file .= DIRECTORY_SEPARATOR;
189 for( $i = 0; $i < $wgCaptchaDirectoryLevels; $i++ ) {
191 $file .= DIRECTORY_SEPARATOR;
193 $file .= "image_{$salt}_{$hash}.png";
198 * Show a message asking the user to enter a captcha on edit
199 * The result will be treated as wiki text
201 * @param $action Action being performed
204 function getMessage( $action ) {
205 $name = 'fancycaptcha-' . $action;
206 $text = wfMsg( $name );
207 # Obtain a more tailored message, if possible, otherwise, fall back to
208 # the default for edits
209 return wfEmptyMsg( $name, $text ) ? wfMsg( 'fancycaptcha-edit' ) : $text;