3 * Experimental image-based captcha plugin, using images generated by an
6 * Copyright (C) 2005, 2006 Brion Vibber <brion@pobox.com>
7 * http://www.mediawiki.org/
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * http://www.gnu.org/copyleft/gpl.html
25 * @subpackage Extensions
28 if ( defined( 'MEDIAWIKI' ) ) {
30 global $wgCaptchaDirectory;
31 $wgCaptchaDirectory = "$wgUploadDirectory/captcha"; // bad default :D
33 global $wgCaptchaSecret;
34 $wgCaptchaSecret = "CHANGE_THIS_SECRET!";
36 $wgExtensionFunctions[] = 'efFancyCaptcha';
38 function efFancyCaptcha() {
39 global $wgMessageCache;
40 require_once( dirname( __FILE__ ) . '/FancyCaptcha.i18n.php' );
41 foreach( efFancyCaptchaMessages() as $lang => $messages )
42 $wgMessageCache->addMessages( $messages, $lang );
45 class FancyCaptcha extends SimpleCaptcha {
47 * Check if the submitted form matches the captcha session data provided
48 * by the plugin when the form was generated.
50 * @param WebRequest $request
54 function keyMatch( $request, $info ) {
55 global $wgCaptchaSecret;
57 $answer = $request->getVal( 'wpCaptchaWord' );
58 $digest = $wgCaptchaSecret . $info['salt'] . $answer . $wgCaptchaSecret . $info['salt'];
59 $answerHash = substr( md5( $digest ), 0, 16 );
61 if( $answerHash == $info['hash'] ) {
62 wfDebug( "FancyCaptcha: answer hash matches expected {$info['hash']}\n" );
65 wfDebug( "FancyCaptcha: answer hashes to $answerHash, expected {$info['hash']}\n" );
71 * Insert the captcha prompt into the edit form.
74 $info = $this->pickImage();
76 die( "out of captcha images; this shouldn't happen" );
79 // Generate a random key for use of this captcha image in this session.
80 // This is needed so multiple edits in separate tabs or windows can
81 // go through without extra pain.
82 $index = $this->storeCaptcha( $info );
84 wfDebug( "Captcha id $index using hash ${info['hash']}, salt ${info['salt']}.\n" );
86 $title = Title::makeTitle( NS_SPECIAL, 'Captcha/image' );
89 wfElement( 'img', array(
90 'src' => $title->getLocalUrl( 'wpCaptchaId=' . urlencode( $index ) ),
91 'width' => $info['width'],
92 'height' => $info['height'],
95 wfElement( 'input', array(
97 'name' => 'wpCaptchaId',
98 'id' => 'wpCaptchaId',
99 'value' => $index ) ) .
101 wfElement( 'input', array(
102 'name' => 'wpCaptchaWord',
103 'id' => 'wpCaptchaWord',
104 'tabindex' => 1 ) ) . // tab in before the edit textarea
109 * Select a previously generated captcha image from the queue.
110 * @fixme subject to race conditions if lots of files vanish
111 * @return mixed tuple of (salt key, text hash) or false if no image to find
113 function pickImage() {
114 global $wgCaptchaDirectory;
115 $n = mt_rand( 0, $this->countFiles( $wgCaptchaDirectory ) );
116 $dir = opendir( $wgCaptchaDirectory );
120 $entry = readdir( $dir );
122 while( false !== $entry ) {
123 $entry = readdir( $dir );
124 if( preg_match( '/^image_([0-9a-f]+)_([0-9a-f]+)\\.png$/', $entry, $matches ) ) {
125 $size = getimagesize( "$wgCaptchaDirectory/$entry" );
127 'salt' => $matches[1],
128 'hash' => $matches[2],
130 'height' => $size[1],
133 if( $count++ == $n ) {
143 * Count the number of files in a directory.
146 function countFiles( $dirname ) {
147 $dir = opendir( $dirname );
149 while( false !== ($entry = readdir( $dir ) ) ) {
150 if( $dir != '.' && $dir != '..' ) {
158 function showImage() {
159 global $wgOut, $wgRequest;
160 global $wgCaptchaDirectory;
164 $info = $this->retrieveCaptcha();
166 if( $info['viewed'] ) {
167 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 = $wgCaptchaDirectory . DIRECTORY_SEPARATOR . "image_{$salt}_{$hash}.png";
178 if( file_exists( $file ) ) {
179 header( 'Content-type: image/png' );
184 wfHttpError( 500, 'Internal Error', 'Requested bogus captcha image' );
189 * Show a message asking the user to enter a captcha on edit
190 * The result will be treated as wiki text
192 * @param $action Action being performed
195 function getMessage( $action ) {
196 $name = 'fancycaptcha-' . $action;
197 $text = wfMsg( $name );
198 # Obtain a more tailored message, if possible, otherwise, fall back to
199 # the default for edits
200 return wfEmptyMsg( $name, $text ) ? wfMsg( 'fancycaptcha-edit' ) : $text;
205 } # End invocation guard