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!";
37 class FancyCaptcha extends SimpleCaptcha {
39 * Check if the submitted form matches the captcha session data provided
40 * by the plugin when the form was generated.
42 * @param WebRequest $request
46 function keyMatch( $request, $info ) {
47 global $wgCaptchaSecret;
49 $answer = $request->getVal( 'wpCaptchaWord' );
50 $digest = $wgCaptchaSecret . $info['salt'] . $answer . $wgCaptchaSecret . $info['salt'];
51 $answerHash = substr( md5( $digest ), 0, 16 );
53 if( $answerHash == $info['hash'] ) {
54 wfDebug( "FancyCaptcha: answer hash matches expected $hash\n" );
57 wfDebug( "FancyCaptcha: answer hashes to $answerHash, expected {$info['hash']}\n" );
63 * Insert the captcha prompt into the edit form.
65 function formCallback( &$out ) {
66 $info = $this->pickImage();
68 die( "out of captcha images; this shouldn't happen" );
71 // Generate a random key for use of this captcha image in this session.
72 // This is needed so multiple edits in separate tabs or windows can
73 // go through without extra pain.
74 $index = $this->storeCaptcha( $info );
76 wfDebug( "Captcha id $index using hash ${info['hash']}, salt ${info['salt']}.\n" );
78 $title = Title::makeTitle( NS_SPECIAL, 'Captcha/image' );
80 $out->addWikiText( wfMsg( "captcha-short" ) );
82 $out->addHTML( "<p>" .
83 wfElement( 'img', array(
84 'src' => $title->getLocalUrl( 'wpCaptchaId=' . urlencode( $index ) ),
85 'width' => $info['width'],
86 'height' => $info['height'],
89 wfElement( 'input', array(
91 'name' => 'wpCaptchaId',
92 'id' => 'wpCaptchaId',
93 'value' => $index ) ) .
95 wfElement( 'input', array(
96 'name' => 'wpCaptchaWord',
97 'id' => 'wpCaptchaWord',
98 'tabindex' => 1 ) ) . // tab in before the edit textarea
103 * Select a previously generated captcha image from the queue.
104 * @fixme subject to race conditions if lots of files vanish
105 * @return mixed tuple of (salt key, text hash) or false if no image to find
107 function pickImage() {
108 global $wgCaptchaDirectory;
109 $n = mt_rand( 0, $this->countFiles( $wgCaptchaDirectory ) );
110 $dir = opendir( $wgCaptchaDirectory );
114 $entry = readdir( $dir );
116 while( false !== $entry ) {
117 $entry = readdir( $dir );
118 if( preg_match( '/^image_([0-9a-f]+)_([0-9a-f]+)\\.png$/', $entry, $matches ) ) {
119 $size = getimagesize( "$wgCaptchaDirectory/$entry" );
121 'salt' => $matches[1],
122 'hash' => $matches[2],
124 'height' => $size[1],
127 if( $count++ == $n ) {
137 * Count the number of files in a directory.
140 function countFiles( $dirname ) {
141 $dir = opendir( $dirname );
143 while( false !== ($entry = readdir( $dir ) ) ) {
144 if( $dir != '.' && $dir != '..' ) {
152 function showImage() {
153 global $wgOut, $wgRequest;
154 global $wgCaptchaDirectory;
158 $info = $this->retrieveCaptcha();
160 if( $info['viewed'] ) {
161 wfHttpError( 403, 'Access Forbidden', "Can't view captcha image a second time." );
165 $info['viewed'] = wfTimestamp();
166 $this->storeCaptcha( $info, $info['index'] );
168 $salt = $info['salt'];
169 $hash = $info['hash'];
170 $file = $wgCaptchaDirectory . DIRECTORY_SEPARATOR . "image_{$salt}_{$hash}.png";
172 if( file_exists( $file ) ) {
173 header( 'Content-type: image/png' );
178 wfHttpError( 500, 'Internal Error', 'Requested bogus captcha image' );
183 } # End invocation guard