2007-11-12 07:42:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class FancyCaptcha extends SimpleCaptcha {
|
|
|
|
/**
|
|
|
|
* Check if the submitted form matches the captcha session data provided
|
|
|
|
* by the plugin when the form was generated.
|
|
|
|
*
|
2008-01-24 21:42:21 +00:00
|
|
|
* @param string $answer
|
2007-11-12 07:42:25 +00:00
|
|
|
* @param array $info
|
|
|
|
* @return bool
|
|
|
|
*/
|
2008-01-24 21:42:21 +00:00
|
|
|
function keyMatch( $answer, $info ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
global $wgCaptchaSecret;
|
|
|
|
|
|
|
|
$digest = $wgCaptchaSecret . $info['salt'] . $answer . $wgCaptchaSecret . $info['salt'];
|
|
|
|
$answerHash = substr( md5( $digest ), 0, 16 );
|
|
|
|
|
2009-07-19 15:13:01 +00:00
|
|
|
if ( $answerHash == $info['hash'] ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
wfDebug( "FancyCaptcha: answer hash matches expected {$info['hash']}\n" );
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
wfDebug( "FancyCaptcha: answer hashes to $answerHash, expected {$info['hash']}\n" );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-19 15:13:01 +00:00
|
|
|
function addCaptchaAPI( &$resultArr ) {
|
2008-02-28 17:42:23 +00:00
|
|
|
$info = $this->pickImage();
|
2009-07-19 15:13:01 +00:00
|
|
|
if ( !$info ) {
|
2008-02-28 17:42:23 +00:00
|
|
|
$resultArr['captcha']['error'] = 'Out of images';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$index = $this->storeCaptcha( $info );
|
2010-06-08 19:30:48 +00:00
|
|
|
$title = SpecialPage::getTitleFor( 'Captcha', 'image' );
|
2008-02-28 17:42:23 +00:00
|
|
|
$resultArr['captcha']['type'] = 'image';
|
2008-02-29 21:44:21 +00:00
|
|
|
$resultArr['captcha']['mime'] = 'image/png';
|
2008-02-28 17:42:23 +00:00
|
|
|
$resultArr['captcha']['id'] = $index;
|
2009-07-19 15:13:01 +00:00
|
|
|
$resultArr['captcha']['url'] = $title->getLocalUrl( 'wpCaptchaId=' . urlencode( $index ) );
|
2008-02-28 17:42:23 +00:00
|
|
|
}
|
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
/**
|
|
|
|
* Insert the captcha prompt into the edit form.
|
|
|
|
*/
|
|
|
|
function getForm() {
|
|
|
|
$info = $this->pickImage();
|
2009-07-19 15:13:01 +00:00
|
|
|
if ( !$info ) {
|
2011-04-24 11:41:49 +00:00
|
|
|
throw new MWException( "Ran out of captcha images" );
|
2007-11-12 07:42:25 +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 );
|
|
|
|
|
|
|
|
wfDebug( "Captcha id $index using hash ${info['hash']}, salt ${info['salt']}.\n" );
|
|
|
|
|
2010-06-08 19:30:48 +00:00
|
|
|
$title = SpecialPage::getTitleFor( 'Captcha', 'image' );
|
2007-11-12 07:42:25 +00:00
|
|
|
|
|
|
|
return "<p>" .
|
2012-04-18 10:05:08 +00:00
|
|
|
Html::element( 'img', array(
|
2007-11-12 07:42:25 +00:00
|
|
|
'src' => $title->getLocalUrl( 'wpCaptchaId=' . urlencode( $index ) ),
|
|
|
|
'width' => $info['width'],
|
|
|
|
'height' => $info['height'],
|
|
|
|
'alt' => '' ) ) .
|
|
|
|
"</p>\n" .
|
2012-04-18 10:05:08 +00:00
|
|
|
Html::element( 'input', array(
|
2007-11-12 07:42:25 +00:00
|
|
|
'type' => 'hidden',
|
|
|
|
'name' => 'wpCaptchaId',
|
|
|
|
'id' => 'wpCaptchaId',
|
|
|
|
'value' => $index ) ) .
|
2012-04-18 10:05:08 +00:00
|
|
|
'<p>' .
|
|
|
|
Html::element( 'label', array(
|
|
|
|
'for' => 'wpCaptchaWord',
|
2012-06-07 16:31:11 +00:00
|
|
|
), parent::getMessage( 'label' ) . wfMsg( 'colon-separator' ) ) .
|
2011-10-21 14:59:16 +00:00
|
|
|
Html::element( 'input', array(
|
2007-11-12 07:42:25 +00:00
|
|
|
'name' => 'wpCaptchaWord',
|
|
|
|
'id' => 'wpCaptchaWord',
|
2012-04-18 10:05:08 +00:00
|
|
|
'type' => 'text',
|
2011-12-26 19:50:20 +00:00
|
|
|
'autocorrect' => 'off',
|
|
|
|
'autocapitalize' => 'off',
|
2012-04-18 10:05:08 +00:00
|
|
|
'required' => 'required',
|
2007-11-12 07:42:25 +00:00
|
|
|
'tabindex' => 1 ) ) . // tab in before the edit textarea
|
|
|
|
"</p>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
function pickImage() {
|
|
|
|
global $wgCaptchaDirectory, $wgCaptchaDirectoryLevels;
|
|
|
|
return $this->pickImageDir(
|
|
|
|
$wgCaptchaDirectory,
|
|
|
|
$wgCaptchaDirectoryLevels );
|
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
function pickImageDir( $directory, $levels ) {
|
2009-07-19 15:13:01 +00:00
|
|
|
if ( $levels ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
$dirs = array();
|
2009-07-19 15:13:01 +00:00
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
// Check which subdirs are actually present...
|
|
|
|
$dir = opendir( $directory );
|
2012-04-09 23:02:21 +00:00
|
|
|
if ( !$dir ) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
while ( false !== ( $entry = readdir( $dir ) ) ) {
|
|
|
|
if ( ctype_xdigit( $entry ) && strlen( $entry ) == 1 ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
$dirs[] = $entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir( $dir );
|
2009-07-19 15:13:01 +00:00
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
$place = mt_rand( 0, count( $dirs ) - 1 );
|
|
|
|
// In case all dirs are not filled,
|
|
|
|
// cycle through next digits...
|
2009-07-19 15:13:01 +00:00
|
|
|
for ( $j = 0; $j < count( $dirs ); $j++ ) {
|
|
|
|
$char = $dirs[( $place + $j ) % count( $dirs )];
|
2007-11-12 07:42:25 +00:00
|
|
|
$return = $this->pickImageDir( "$directory/$char", $levels - 1 );
|
2009-07-19 15:13:01 +00:00
|
|
|
if ( $return ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Didn't find any images in this directory... empty?
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return $this->pickImageFromDir( $directory );
|
|
|
|
}
|
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
function pickImageFromDir( $directory ) {
|
2009-07-19 15:13:01 +00:00
|
|
|
if ( !is_dir( $directory ) ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$n = mt_rand( 0, $this->countFiles( $directory ) - 1 );
|
|
|
|
$dir = opendir( $directory );
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
|
|
|
|
$entry = readdir( $dir );
|
|
|
|
$pick = false;
|
2009-07-19 15:13:01 +00:00
|
|
|
while ( false !== $entry ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
$entry = readdir( $dir );
|
2009-07-19 15:13:01 +00:00
|
|
|
if ( preg_match( '/^image_([0-9a-f]+)_([0-9a-f]+)\\.png$/', $entry, $matches ) ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
$size = getimagesize( "$directory/$entry" );
|
|
|
|
$pick = array(
|
|
|
|
'salt' => $matches[1],
|
|
|
|
'hash' => $matches[2],
|
|
|
|
'width' => $size[0],
|
|
|
|
'height' => $size[1],
|
|
|
|
'viewed' => false,
|
|
|
|
);
|
2009-07-19 15:13:01 +00:00
|
|
|
if ( $count++ == $n ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir( $dir );
|
|
|
|
return $pick;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Count the number of files in a directory.
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function countFiles( $dirname ) {
|
|
|
|
$dir = opendir( $dirname );
|
|
|
|
$count = 0;
|
2009-07-19 15:13:01 +00:00
|
|
|
while ( false !== ( $entry = readdir( $dir ) ) ) {
|
|
|
|
if ( $entry != '.' && $entry != '..' ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir( $dir );
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
|
|
|
function showImage() {
|
2010-07-26 21:42:17 +00:00
|
|
|
global $wgOut;
|
2007-11-12 07:42:25 +00:00
|
|
|
|
|
|
|
$wgOut->disable();
|
|
|
|
|
|
|
|
$info = $this->retrieveCaptcha();
|
2009-07-19 15:13:01 +00:00
|
|
|
if ( $info ) {
|
2008-02-13 06:44:23 +00:00
|
|
|
/*
|
|
|
|
// Be a little less restrictive for now; in at least some circumstances,
|
|
|
|
// Konqueror tries to reload the image even if you haven't navigated
|
|
|
|
// away from the page.
|
2007-11-12 07:42:25 +00:00
|
|
|
if( $info['viewed'] ) {
|
|
|
|
wfHttpError( 403, 'Access Forbidden', "Can't view captcha image a second time." );
|
|
|
|
return false;
|
|
|
|
}
|
2008-02-13 06:44:23 +00:00
|
|
|
*/
|
2007-11-12 07:42:25 +00:00
|
|
|
|
|
|
|
$info['viewed'] = wfTimestamp();
|
|
|
|
$this->storeCaptcha( $info );
|
|
|
|
|
|
|
|
$salt = $info['salt'];
|
|
|
|
$hash = $info['hash'];
|
|
|
|
$file = $this->imagePath( $salt, $hash );
|
|
|
|
|
2009-07-19 15:13:01 +00:00
|
|
|
if ( file_exists( $file ) ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
global $IP;
|
|
|
|
require_once "$IP/includes/StreamFile.php";
|
2008-02-13 06:44:23 +00:00
|
|
|
header( "Cache-Control: private, s-maxage=0, max-age=3600" );
|
2007-11-12 07:42:25 +00:00
|
|
|
wfStreamFile( $file );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wfHttpError( 500, 'Internal Error', 'Requested bogus captcha image' );
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
function imagePath( $salt, $hash ) {
|
|
|
|
global $wgCaptchaDirectory, $wgCaptchaDirectoryLevels;
|
|
|
|
$file = $wgCaptchaDirectory;
|
|
|
|
$file .= DIRECTORY_SEPARATOR;
|
2009-07-19 15:13:01 +00:00
|
|
|
for ( $i = 0; $i < $wgCaptchaDirectoryLevels; $i++ ) {
|
|
|
|
$file .= $hash { $i } ;
|
2007-11-12 07:42:25 +00:00
|
|
|
$file .= DIRECTORY_SEPARATOR;
|
|
|
|
}
|
|
|
|
$file .= "image_{$salt}_{$hash}.png";
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2010-08-29 13:31:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a solved captcha image, if $wgCaptchaDeleteOnSolve is true.
|
|
|
|
*/
|
|
|
|
function passCaptcha() {
|
|
|
|
global $wgCaptchaDeleteOnSolve;
|
|
|
|
|
|
|
|
$info = $this->retrieveCaptcha(); // get the captcha info before it gets deleted
|
|
|
|
$pass = parent::passCaptcha();
|
|
|
|
|
|
|
|
if ( $pass && $wgCaptchaDeleteOnSolve ) {
|
|
|
|
$filename = $this->imagePath( $info['salt'], $info['hash'] );
|
|
|
|
if ( file_exists( $filename ) ) {
|
|
|
|
unlink( $filename );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $pass;
|
|
|
|
}
|
2007-11-12 07:42:25 +00:00
|
|
|
}
|