2005-10-14 05:07:24 +00:00
|
|
|
<?php
|
2006-01-27 08:00:32 +00:00
|
|
|
/**
|
|
|
|
* Experimental image-based captcha plugin, using images generated by an
|
|
|
|
* external tool.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005, 2006 Brion Vibber <brion@pobox.com>
|
|
|
|
* http://www.mediawiki.org/
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
2007-01-20 15:10:35 +00:00
|
|
|
* @addtogroup Extensions
|
2006-01-27 08:00:32 +00:00
|
|
|
*/
|
2005-10-14 05:07:24 +00:00
|
|
|
|
|
|
|
if ( defined( 'MEDIAWIKI' ) ) {
|
|
|
|
|
|
|
|
global $wgCaptchaDirectory;
|
|
|
|
$wgCaptchaDirectory = "$wgUploadDirectory/captcha"; // bad default :D
|
|
|
|
|
2007-02-19 20:09:03 +00:00
|
|
|
global $wgCaptchaDirectoryLevels;
|
|
|
|
$wgCaptchaDirectoryLevels = 0; // To break into subdirectories
|
|
|
|
|
2005-10-14 05:07:24 +00:00
|
|
|
global $wgCaptchaSecret;
|
|
|
|
$wgCaptchaSecret = "CHANGE_THIS_SECRET!";
|
|
|
|
|
2007-01-04 16:48:53 +00:00
|
|
|
$wgExtensionFunctions[] = 'efFancyCaptcha';
|
|
|
|
|
|
|
|
function efFancyCaptcha() {
|
|
|
|
global $wgMessageCache;
|
|
|
|
require_once( dirname( __FILE__ ) . '/FancyCaptcha.i18n.php' );
|
|
|
|
foreach( efFancyCaptchaMessages() as $lang => $messages )
|
|
|
|
$wgMessageCache->addMessages( $messages, $lang );
|
|
|
|
}
|
2005-10-14 05:07:24 +00:00
|
|
|
|
|
|
|
class FancyCaptcha extends SimpleCaptcha {
|
2006-01-27 08:00:32 +00:00
|
|
|
/**
|
|
|
|
* Check if the submitted form matches the captcha session data provided
|
|
|
|
* by the plugin when the form was generated.
|
|
|
|
*
|
|
|
|
* @param WebRequest $request
|
|
|
|
* @param array $info
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function keyMatch( $request, $info ) {
|
|
|
|
global $wgCaptchaSecret;
|
2007-01-14 00:51:11 +00:00
|
|
|
|
2006-01-27 08:00:32 +00:00
|
|
|
$answer = $request->getVal( 'wpCaptchaWord' );
|
|
|
|
$digest = $wgCaptchaSecret . $info['salt'] . $answer . $wgCaptchaSecret . $info['salt'];
|
2005-10-14 05:07:24 +00:00
|
|
|
$answerHash = substr( md5( $digest ), 0, 16 );
|
2007-01-14 00:51:11 +00:00
|
|
|
|
2006-01-27 08:00:32 +00:00
|
|
|
if( $answerHash == $info['hash'] ) {
|
2006-01-29 07:37:33 +00:00
|
|
|
wfDebug( "FancyCaptcha: answer hash matches expected {$info['hash']}\n" );
|
2005-10-14 05:07:24 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2006-01-27 08:00:32 +00:00
|
|
|
wfDebug( "FancyCaptcha: answer hashes to $answerHash, expected {$info['hash']}\n" );
|
2005-10-14 05:07:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2007-01-14 00:51:11 +00:00
|
|
|
|
2006-01-27 01:53:10 +00:00
|
|
|
/**
|
|
|
|
* Insert the captcha prompt into the edit form.
|
|
|
|
*/
|
2006-01-29 07:37:33 +00:00
|
|
|
function getForm() {
|
2006-01-27 08:00:32 +00:00
|
|
|
$info = $this->pickImage();
|
|
|
|
if( !$info ) {
|
2006-01-27 01:53:10 +00:00
|
|
|
die( "out of captcha images; this shouldn't happen" );
|
2005-10-14 05:07:24 +00:00
|
|
|
}
|
2007-01-14 00:51:11 +00:00
|
|
|
|
2006-01-27 08:00:32 +00:00
|
|
|
// Generate a random key for use of this captcha image in this session.
|
|
|
|
// This is needed so multiple edits in separate tabs or windows can
|
|
|
|
// go through without extra pain.
|
|
|
|
$index = $this->storeCaptcha( $info );
|
2007-01-14 00:51:11 +00:00
|
|
|
|
2006-01-27 08:00:32 +00:00
|
|
|
wfDebug( "Captcha id $index using hash ${info['hash']}, salt ${info['salt']}.\n" );
|
2007-01-14 00:51:11 +00:00
|
|
|
|
2005-10-14 05:07:24 +00:00
|
|
|
$title = Title::makeTitle( NS_SPECIAL, 'Captcha/image' );
|
2007-01-14 00:51:11 +00:00
|
|
|
|
|
|
|
return "<p>" .
|
2006-01-27 01:53:10 +00:00
|
|
|
wfElement( 'img', array(
|
2006-01-27 08:00:32 +00:00
|
|
|
'src' => $title->getLocalUrl( 'wpCaptchaId=' . urlencode( $index ) ),
|
|
|
|
'width' => $info['width'],
|
|
|
|
'height' => $info['height'],
|
2006-01-27 01:53:10 +00:00
|
|
|
'alt' => '' ) ) .
|
|
|
|
"</p>\n" .
|
2006-01-27 08:00:32 +00:00
|
|
|
wfElement( 'input', array(
|
|
|
|
'type' => 'hidden',
|
|
|
|
'name' => 'wpCaptchaId',
|
|
|
|
'id' => 'wpCaptchaId',
|
|
|
|
'value' => $index ) ) .
|
|
|
|
"<p>" .
|
|
|
|
wfElement( 'input', array(
|
|
|
|
'name' => 'wpCaptchaWord',
|
|
|
|
'id' => 'wpCaptchaWord',
|
|
|
|
'tabindex' => 1 ) ) . // tab in before the edit textarea
|
2006-01-29 07:37:33 +00:00
|
|
|
"</p>\n";
|
2005-10-14 05:07:24 +00:00
|
|
|
}
|
2007-01-14 00:51:11 +00:00
|
|
|
|
2006-01-27 01:53:10 +00:00
|
|
|
/**
|
|
|
|
* Select a previously generated captcha image from the queue.
|
|
|
|
* @fixme subject to race conditions if lots of files vanish
|
|
|
|
* @return mixed tuple of (salt key, text hash) or false if no image to find
|
|
|
|
*/
|
2005-10-14 05:07:24 +00:00
|
|
|
function pickImage() {
|
2007-02-19 20:09:03 +00:00
|
|
|
global $wgCaptchaDirectory, $wgCaptchaDirectoryLevels;
|
|
|
|
return $this->pickImageDir(
|
|
|
|
$wgCaptchaDirectory,
|
|
|
|
$wgCaptchaDirectoryLevels );
|
|
|
|
}
|
|
|
|
|
|
|
|
function pickImageDir( $directory, $levels ) {
|
|
|
|
if( $levels ) {
|
|
|
|
$dirs = array();
|
|
|
|
|
|
|
|
// Check which subdirs are actually present...
|
|
|
|
$dir = opendir( $directory );
|
|
|
|
while( false !== ($entry = readdir( $dir ) ) ) {
|
|
|
|
if( ctype_xdigit( $entry ) && strlen( $entry ) == 1 ) {
|
|
|
|
$dirs[] = $entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir( $dir );
|
|
|
|
|
|
|
|
$place = mt_rand( 0, count( $dirs ) - 1 );
|
|
|
|
// In case all dirs are not filled,
|
|
|
|
// cycle through next digits...
|
|
|
|
for( $j = 0; $j < count( $dirs ); $j++ ) {
|
|
|
|
$char = $dirs[($place + $j) % count( $dirs )];
|
|
|
|
$return = $this->pickImageDir( "$directory/$char", $levels - 1 );
|
|
|
|
if( $return ) {
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Didn't find any images in this directory... empty?
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return $this->pickImageFromDir( $directory );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function pickImageFromDir( $directory ) {
|
|
|
|
if( !is_dir( $directory ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-29 18:38:46 +00:00
|
|
|
$n = mt_rand( 0, $this->countFiles( $directory ) - 1 );
|
2007-02-19 20:09:03 +00:00
|
|
|
$dir = opendir( $directory );
|
2007-01-14 00:51:11 +00:00
|
|
|
|
2005-10-14 05:07:24 +00:00
|
|
|
$count = 0;
|
2007-01-14 00:51:11 +00:00
|
|
|
|
2005-10-14 05:07:24 +00:00
|
|
|
$entry = readdir( $dir );
|
2006-01-27 01:53:10 +00:00
|
|
|
$pick = false;
|
2005-10-14 05:07:24 +00:00
|
|
|
while( false !== $entry ) {
|
|
|
|
$entry = readdir( $dir );
|
|
|
|
if( preg_match( '/^image_([0-9a-f]+)_([0-9a-f]+)\\.png$/', $entry, $matches ) ) {
|
2007-02-19 20:09:03 +00:00
|
|
|
$size = getimagesize( "$directory/$entry" );
|
2006-01-27 01:53:10 +00:00
|
|
|
$pick = array(
|
|
|
|
'salt' => $matches[1],
|
|
|
|
'hash' => $matches[2],
|
|
|
|
'width' => $size[0],
|
2006-01-27 08:00:32 +00:00
|
|
|
'height' => $size[1],
|
|
|
|
'viewed' => false,
|
2006-01-27 01:53:10 +00:00
|
|
|
);
|
|
|
|
if( $count++ == $n ) {
|
|
|
|
break;
|
2005-10-14 05:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-01-27 01:53:10 +00:00
|
|
|
closedir( $dir );
|
|
|
|
return $pick;
|
|
|
|
}
|
2007-01-14 00:51:11 +00:00
|
|
|
|
2006-01-27 01:53:10 +00:00
|
|
|
/**
|
|
|
|
* Count the number of files in a directory.
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function countFiles( $dirname ) {
|
|
|
|
$dir = opendir( $dirname );
|
|
|
|
$count = 0;
|
|
|
|
while( false !== ($entry = readdir( $dir ) ) ) {
|
2007-03-29 18:38:46 +00:00
|
|
|
if( $entry != '.' && $entry != '..' ) {
|
2006-01-27 01:53:10 +00:00
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir( $dir );
|
|
|
|
return $count;
|
2005-10-14 05:07:24 +00:00
|
|
|
}
|
2007-01-14 00:51:11 +00:00
|
|
|
|
2005-10-14 05:07:24 +00:00
|
|
|
function showImage() {
|
2006-01-27 08:00:32 +00:00
|
|
|
global $wgOut, $wgRequest;
|
2007-01-14 00:51:11 +00:00
|
|
|
|
2005-10-14 05:07:24 +00:00
|
|
|
$wgOut->disable();
|
2007-01-14 00:51:11 +00:00
|
|
|
|
2006-01-27 08:00:32 +00:00
|
|
|
$info = $this->retrieveCaptcha();
|
|
|
|
if( $info ) {
|
|
|
|
if( $info['viewed'] ) {
|
|
|
|
wfHttpError( 403, 'Access Forbidden', "Can't view captcha image a second time." );
|
|
|
|
return false;
|
|
|
|
}
|
2007-01-14 00:51:11 +00:00
|
|
|
|
2006-01-27 08:00:32 +00:00
|
|
|
$info['viewed'] = wfTimestamp();
|
2006-01-29 02:25:04 +00:00
|
|
|
$this->storeCaptcha( $info );
|
2007-01-14 00:51:11 +00:00
|
|
|
|
2006-01-27 08:00:32 +00:00
|
|
|
$salt = $info['salt'];
|
|
|
|
$hash = $info['hash'];
|
2007-02-19 20:09:03 +00:00
|
|
|
$file = $this->imagePath( $salt, $hash );
|
2007-01-14 00:51:11 +00:00
|
|
|
|
2006-01-27 08:00:32 +00:00
|
|
|
if( file_exists( $file ) ) {
|
2007-02-20 08:30:14 +00:00
|
|
|
global $IP;
|
|
|
|
require_once "$IP/includes/StreamFile.php";
|
|
|
|
wfStreamFile( $file );
|
2006-01-27 08:00:32 +00:00
|
|
|
return true;
|
2005-10-14 05:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
2006-01-27 08:00:32 +00:00
|
|
|
wfHttpError( 500, 'Internal Error', 'Requested bogus captcha image' );
|
|
|
|
return false;
|
2005-10-14 05:07:24 +00:00
|
|
|
}
|
2007-02-19 20:09:03 +00:00
|
|
|
|
|
|
|
function imagePath( $salt, $hash ) {
|
|
|
|
global $wgCaptchaDirectory, $wgCaptchaDirectoryLevels;
|
|
|
|
$file = $wgCaptchaDirectory;
|
|
|
|
$file .= DIRECTORY_SEPARATOR;
|
|
|
|
for( $i = 0; $i < $wgCaptchaDirectoryLevels; $i++ ) {
|
|
|
|
$file .= $hash{$i};
|
|
|
|
$file .= DIRECTORY_SEPARATOR;
|
|
|
|
}
|
|
|
|
$file .= "image_{$salt}_{$hash}.png";
|
|
|
|
return $file;
|
|
|
|
}
|
2007-01-14 00:51:11 +00:00
|
|
|
|
2007-01-04 16:48:53 +00:00
|
|
|
/**
|
|
|
|
* Show a message asking the user to enter a captcha on edit
|
|
|
|
* The result will be treated as wiki text
|
|
|
|
*
|
|
|
|
* @param $action Action being performed
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function getMessage( $action ) {
|
|
|
|
$name = 'fancycaptcha-' . $action;
|
|
|
|
$text = wfMsg( $name );
|
|
|
|
# Obtain a more tailored message, if possible, otherwise, fall back to
|
|
|
|
# the default for edits
|
|
|
|
return wfEmptyMsg( $name, $text ) ? wfMsg( 'fancycaptcha-edit' ) : $text;
|
|
|
|
}
|
2007-01-14 00:51:11 +00:00
|
|
|
|
2005-10-14 05:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} # End invocation guard
|
|
|
|
|
2007-06-29 01:36:09 +00:00
|
|
|
|