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 {$info['hash']}\n" );
57 wfDebug( "FancyCaptcha: answer hashes to $answerHash, expected {$info['hash']}\n" );
63 * Insert the captcha prompt into the edit form.
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' );
81 wfElement( 'img', array(
82 'src' => $title->getLocalUrl( 'wpCaptchaId=' . urlencode( $index ) ),
83 'width' => $info['width'],
84 'height' => $info['height'],
87 wfElement( 'input', array(
89 'name' => 'wpCaptchaId',
90 'id' => 'wpCaptchaId',
91 'value' => $index ) ) .
93 wfElement( 'input', array(
94 'name' => 'wpCaptchaWord',
95 'id' => 'wpCaptchaWord',
96 'tabindex' => 1 ) ) . // tab in before the edit textarea
101 * Select a previously generated captcha image from the queue.
102 * @fixme subject to race conditions if lots of files vanish
103 * @return mixed tuple of (salt key, text hash) or false if no image to find
105 function pickImage() {
106 global $wgCaptchaDirectory;
107 $n = mt_rand( 0, $this->countFiles( $wgCaptchaDirectory ) );
108 $dir = opendir( $wgCaptchaDirectory );
112 $entry = readdir( $dir );
114 while( false !== $entry ) {
115 $entry = readdir( $dir );
116 if( preg_match( '/^image_([0-9a-f]+)_([0-9a-f]+)\\.png$/', $entry, $matches ) ) {
117 $size = getimagesize( "$wgCaptchaDirectory/$entry" );
119 'salt' => $matches[1],
120 'hash' => $matches[2],
122 'height' => $size[1],
125 if( $count++ == $n ) {
135 * Count the number of files in a directory.
138 function countFiles( $dirname ) {
139 $dir = opendir( $dirname );
141 while( false !== ($entry = readdir( $dir ) ) ) {
142 if( $dir != '.' && $dir != '..' ) {
150 function showImage() {
151 global $wgOut, $wgRequest;
152 global $wgCaptchaDirectory;
156 $info = $this->retrieveCaptcha();
158 if( $info['viewed'] ) {
159 wfHttpError( 403, 'Access Forbidden', "Can't view captcha image a second time." );
163 $info['viewed'] = wfTimestamp();
164 $this->storeCaptcha( $info );
166 $salt = $info['salt'];
167 $hash = $info['hash'];
168 $file = $wgCaptchaDirectory . DIRECTORY_SEPARATOR . "image_{$salt}_{$hash}.png";
170 if( file_exists( $file ) ) {
171 header( 'Content-type: image/png' );
176 wfHttpError( 500, 'Internal Error', 'Requested bogus captcha image' );
181 } # End invocation guard