2007-11-12 07:42:25 +00:00
|
|
|
<?php
|
|
|
|
|
2016-04-25 20:58:18 +00:00
|
|
|
use MediaWiki\Auth\AuthenticationRequest;
|
|
|
|
use MediaWiki\Auth\AuthManager;
|
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
class FancyCaptcha extends SimpleCaptcha {
|
2016-04-25 20:58:18 +00:00
|
|
|
// used for fancycaptcha-edit, fancycaptcha-addurl, fancycaptcha-badlogin,
|
2016-06-16 14:06:35 +00:00
|
|
|
// fancycaptcha-accountcreate, fancycaptcha-create, fancycaptcha-sendemail via getMessage()
|
2016-04-25 20:58:18 +00:00
|
|
|
protected static $messagePrefix = 'fancycaptcha-';
|
|
|
|
|
2012-09-16 17:31:47 +00:00
|
|
|
/**
|
|
|
|
* @return FileBackend
|
|
|
|
*/
|
|
|
|
public function getBackend() {
|
|
|
|
global $wgCaptchaFileBackend, $wgCaptchaDirectory;
|
|
|
|
|
|
|
|
if ( $wgCaptchaFileBackend ) {
|
|
|
|
return FileBackendGroup::singleton()->get( $wgCaptchaFileBackend );
|
|
|
|
} else {
|
|
|
|
static $backend = null;
|
|
|
|
if ( !$backend ) {
|
2016-05-09 23:41:17 +00:00
|
|
|
$backend = new FSFileBackend( [
|
2012-09-16 17:31:47 +00:00
|
|
|
'name' => 'captcha-backend',
|
2015-06-28 02:33:28 +00:00
|
|
|
'wikiId' => wfWikiId(),
|
2016-05-09 23:41:17 +00:00
|
|
|
'lockManager' => new NullLockManager( [] ),
|
|
|
|
'containerPaths' => [ 'captcha-render' => $wgCaptchaDirectory ],
|
2012-09-16 17:31:47 +00:00
|
|
|
'fileMode' => 777
|
2016-05-09 23:41:17 +00:00
|
|
|
] );
|
2012-09-16 17:31:47 +00:00
|
|
|
}
|
|
|
|
return $backend;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return integer Estimate of the number of captchas files
|
|
|
|
*/
|
|
|
|
public function estimateCaptchaCount() {
|
|
|
|
global $wgCaptchaDirectoryLevels;
|
|
|
|
|
|
|
|
$factor = 1;
|
|
|
|
$sampleDir = $this->getBackend()->getRootStoragePath() . '/captcha-render';
|
|
|
|
if ( $wgCaptchaDirectoryLevels >= 1 ) { // 1/16 sample if 16 shards
|
|
|
|
$sampleDir .= '/' . dechex( mt_rand( 0, 15 ) );
|
|
|
|
$factor = 16;
|
|
|
|
}
|
|
|
|
if ( $wgCaptchaDirectoryLevels >= 3 ) { // 1/256 sample if 4096 shards
|
|
|
|
$sampleDir .= '/' . dechex( mt_rand( 0, 15 ) );
|
|
|
|
$factor = 256;
|
|
|
|
}
|
|
|
|
|
|
|
|
$count = 0;
|
2016-05-09 23:41:17 +00:00
|
|
|
foreach ( $this->getBackend()->getFileList( [ 'dir' => $sampleDir ] ) as $file ) {
|
2012-09-16 17:31:47 +00:00
|
|
|
++$count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ( $count * $factor );
|
|
|
|
}
|
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
/**
|
|
|
|
* 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' );
|
2016-04-25 20:58:18 +00:00
|
|
|
$resultArr['captcha'] = $this->describeCaptchaType();
|
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
|
|
|
}
|
|
|
|
|
2016-04-25 20:58:18 +00:00
|
|
|
public function describeCaptchaType() {
|
|
|
|
return [
|
|
|
|
'type' => 'image',
|
|
|
|
'mime' => 'image/png',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
Remove getForm() and replace by getFormInformation()
This commit removes SimpleCaptcha::getForm() and replaces it by its more informative
counterpart getFormInformation(), which returns an array, which provides some
more information about the form than only the html.
The information included in the array is:
* html: The HTML of the CAPTCHA form (this is the same as what you expected from
getForm() previously)
* modules: ResourceLoader modules, if any, that should be added to the output of the
page
* modulestyles: ResourceLoader style modules, if any, that should be added to th
output of the page
* headitems: Head items that should be added to the output (see OutputPage::addHeadItems)
Mostly you shouldn't need to handle the response of getFormInformation() anymore, as there's
a new function, addFormToOutput(), which takes an instance of OutputPage as a first parameter
and handles the response of getFormInformation for you (adds all information to the given
OutputPage instance, if they're provided).
Bug: T141300
Depends-On: I433afd124b57526caa13a540cda48ba2b99a9bde
Change-Id: I25f344538052fc18993c43185fbd97804a7cfc81
2016-07-26 16:08:42 +00:00
|
|
|
function getFormInformation( $tabIndex = 1 ) {
|
2015-10-28 15:52:04 +00:00
|
|
|
global $wgEnableAPI;
|
Remove getForm() and replace by getFormInformation()
This commit removes SimpleCaptcha::getForm() and replaces it by its more informative
counterpart getFormInformation(), which returns an array, which provides some
more information about the form than only the html.
The information included in the array is:
* html: The HTML of the CAPTCHA form (this is the same as what you expected from
getForm() previously)
* modules: ResourceLoader modules, if any, that should be added to the output of the
page
* modulestyles: ResourceLoader style modules, if any, that should be added to th
output of the page
* headitems: Head items that should be added to the output (see OutputPage::addHeadItems)
Mostly you shouldn't need to handle the response of getFormInformation() anymore, as there's
a new function, addFormToOutput(), which takes an instance of OutputPage as a first parameter
and handles the response of getFormInformation for you (adds all information to the given
OutputPage instance, if they're provided).
Bug: T141300
Depends-On: I433afd124b57526caa13a540cda48ba2b99a9bde
Change-Id: I25f344538052fc18993c43185fbd97804a7cfc81
2016-07-26 16:08:42 +00:00
|
|
|
$modules = [];
|
2007-11-12 07:42:25 +00:00
|
|
|
|
2010-06-08 19:30:48 +00:00
|
|
|
$title = SpecialPage::getTitleFor( 'Captcha', 'image' );
|
2016-04-25 20:58:18 +00:00
|
|
|
$info = $this->getCaptcha();
|
|
|
|
$index = $this->storeCaptcha( $info );
|
2013-01-17 02:00:09 +00:00
|
|
|
|
|
|
|
if ( $wgEnableAPI ) {
|
|
|
|
// Loaded only if JS is enabled
|
Remove getForm() and replace by getFormInformation()
This commit removes SimpleCaptcha::getForm() and replaces it by its more informative
counterpart getFormInformation(), which returns an array, which provides some
more information about the form than only the html.
The information included in the array is:
* html: The HTML of the CAPTCHA form (this is the same as what you expected from
getForm() previously)
* modules: ResourceLoader modules, if any, that should be added to the output of the
page
* modulestyles: ResourceLoader style modules, if any, that should be added to th
output of the page
* headitems: Head items that should be added to the output (see OutputPage::addHeadItems)
Mostly you shouldn't need to handle the response of getFormInformation() anymore, as there's
a new function, addFormToOutput(), which takes an instance of OutputPage as a first parameter
and handles the response of getFormInformation for you (adds all information to the given
OutputPage instance, if they're provided).
Bug: T141300
Depends-On: I433afd124b57526caa13a540cda48ba2b99a9bde
Change-Id: I25f344538052fc18993c43185fbd97804a7cfc81
2016-07-26 16:08:42 +00:00
|
|
|
$modules[] = 'ext.confirmEdit.fancyCaptcha';
|
2007-11-12 07:42:25 +00:00
|
|
|
|
2013-04-26 23:34:30 +00:00
|
|
|
$captchaReload = Html::element(
|
|
|
|
'small',
|
2016-05-09 23:41:17 +00:00
|
|
|
[
|
2013-04-26 23:34:30 +00:00
|
|
|
'class' => 'confirmedit-captcha-reload fancycaptcha-reload'
|
2016-05-09 23:41:17 +00:00
|
|
|
],
|
2013-04-26 23:34:30 +00:00
|
|
|
wfMessage( 'fancycaptcha-reload-text' )->text()
|
|
|
|
);
|
2013-01-17 02:00:09 +00:00
|
|
|
} else {
|
|
|
|
$captchaReload = '';
|
|
|
|
}
|
|
|
|
|
2015-01-14 16:34:23 +00:00
|
|
|
$form = Html::openElement( 'div' ) .
|
2016-05-09 23:41:17 +00:00
|
|
|
Html::element( 'label', [
|
2015-01-14 16:34:23 +00:00
|
|
|
'for' => 'wpCaptchaWord',
|
2016-05-09 23:41:17 +00:00
|
|
|
],
|
2016-04-25 20:58:18 +00:00
|
|
|
wfMessage( 'captcha-label' )->text() . ' ' . wfMessage( 'fancycaptcha-captcha' )->text()
|
2015-01-14 16:34:23 +00:00
|
|
|
) .
|
2016-05-09 23:41:17 +00:00
|
|
|
Html::openElement( 'div', [ 'class' => 'fancycaptcha-captcha-container' ] ) .
|
|
|
|
Html::openElement( 'div', [ 'class' => 'fancycaptcha-captcha-and-reload' ] ) .
|
|
|
|
Html::openElement( 'div', [ 'class' => 'fancycaptcha-image-container' ] ) .
|
|
|
|
Html::element( 'img', [
|
2013-04-23 19:28:44 +00:00
|
|
|
'class' => 'fancycaptcha-image',
|
|
|
|
'src' => $title->getLocalUrl( 'wpCaptchaId=' . urlencode( $index ) ),
|
|
|
|
'alt' => ''
|
2016-05-09 23:41:17 +00:00
|
|
|
]
|
2015-01-14 16:34:23 +00:00
|
|
|
) . $captchaReload . Html::closeElement( 'div' ) . Html::closeElement( 'div' ) . "\n" .
|
2016-05-09 23:41:17 +00:00
|
|
|
Html::element( 'input', [
|
2013-04-23 19:28:44 +00:00
|
|
|
'name' => 'wpCaptchaWord',
|
2014-07-30 01:39:22 +00:00
|
|
|
'class' => 'mw-ui-input',
|
2013-04-23 19:28:44 +00:00
|
|
|
'id' => 'wpCaptchaWord',
|
|
|
|
'type' => 'text',
|
2013-04-27 01:26:53 +00:00
|
|
|
'size' => '12', // max_length in captcha.py plus fudge factor
|
2013-05-03 01:09:51 +00:00
|
|
|
'autocomplete' => 'off',
|
2013-04-23 19:28:44 +00:00
|
|
|
'autocorrect' => 'off',
|
|
|
|
'autocapitalize' => 'off',
|
|
|
|
'required' => 'required',
|
2015-09-23 06:05:28 +00:00
|
|
|
'tabindex' => $tabIndex,
|
2015-10-22 14:46:13 +00:00
|
|
|
'placeholder' => wfMessage( 'fancycaptcha-imgcaptcha-ph' )
|
2016-05-09 23:41:17 +00:00
|
|
|
]
|
2015-01-14 16:34:23 +00:00
|
|
|
); // tab in before the edit textarea
|
2016-04-25 20:58:18 +00:00
|
|
|
if ( $this->action == 'createaccount' ) {
|
2015-06-09 16:27:41 +00:00
|
|
|
// use raw element, because the message can contain links or some other html
|
2016-05-09 23:41:17 +00:00
|
|
|
$form .= Html::rawElement( 'small', [
|
2015-01-14 16:34:23 +00:00
|
|
|
'class' => 'mw-createacct-captcha-assisted'
|
2016-05-09 23:41:17 +00:00
|
|
|
], wfMessage( 'createacct-imgcaptcha-help' )->parse()
|
2015-01-14 16:34:23 +00:00
|
|
|
);
|
|
|
|
}
|
2016-05-09 23:41:17 +00:00
|
|
|
$form .= Html::element( 'input', [
|
2013-04-23 19:28:44 +00:00
|
|
|
'type' => 'hidden',
|
|
|
|
'name' => 'wpCaptchaId',
|
|
|
|
'id' => 'wpCaptchaId',
|
|
|
|
'value' => $index
|
2016-05-09 23:41:17 +00:00
|
|
|
]
|
2015-01-14 16:34:23 +00:00
|
|
|
) . Html::closeElement( 'div' ) . Html::closeElement( 'div' ) . "\n";
|
|
|
|
|
Remove getForm() and replace by getFormInformation()
This commit removes SimpleCaptcha::getForm() and replaces it by its more informative
counterpart getFormInformation(), which returns an array, which provides some
more information about the form than only the html.
The information included in the array is:
* html: The HTML of the CAPTCHA form (this is the same as what you expected from
getForm() previously)
* modules: ResourceLoader modules, if any, that should be added to the output of the
page
* modulestyles: ResourceLoader style modules, if any, that should be added to th
output of the page
* headitems: Head items that should be added to the output (see OutputPage::addHeadItems)
Mostly you shouldn't need to handle the response of getFormInformation() anymore, as there's
a new function, addFormToOutput(), which takes an instance of OutputPage as a first parameter
and handles the response of getFormInformation for you (adds all information to the given
OutputPage instance, if they're provided).
Bug: T141300
Depends-On: I433afd124b57526caa13a540cda48ba2b99a9bde
Change-Id: I25f344538052fc18993c43185fbd97804a7cfc81
2016-07-26 16:08:42 +00:00
|
|
|
return [
|
|
|
|
'html' => $form,
|
|
|
|
'modules' => $modules,
|
|
|
|
// Uses addModuleStyles so it is loaded when JS is disabled.
|
|
|
|
'modulestyles' => [ 'ext.confirmEdit.fancyCaptcha.styles' ],
|
|
|
|
];
|
2013-01-17 02:00:09 +00:00
|
|
|
}
|
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
/**
|
|
|
|
* Select a previously generated captcha image from the queue.
|
|
|
|
* @return mixed tuple of (salt key, text hash) or false if no image to find
|
|
|
|
*/
|
2012-09-16 17:31:47 +00:00
|
|
|
protected function pickImage() {
|
|
|
|
global $wgCaptchaDirectoryLevels;
|
|
|
|
|
|
|
|
$lockouts = 0; // number of times another process claimed a file before this one
|
|
|
|
$baseDir = $this->getBackend()->getRootStoragePath() . '/captcha-render';
|
|
|
|
return $this->pickImageDir( $baseDir, $wgCaptchaDirectoryLevels, $lockouts );
|
2007-11-12 07:42:25 +00:00
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
2012-09-16 17:31:47 +00:00
|
|
|
/**
|
|
|
|
* @param $directory string
|
|
|
|
* @param $levels integer
|
|
|
|
* @param $lockouts integer
|
|
|
|
* @return Array|bool
|
|
|
|
*/
|
|
|
|
protected function pickImageDir( $directory, $levels, &$lockouts ) {
|
|
|
|
global $wgMemc;
|
2009-07-19 15:13:01 +00:00
|
|
|
|
2012-09-16 17:31:47 +00:00
|
|
|
if ( $levels <= 0 ) { // $directory has regular files
|
|
|
|
return $this->pickImageFromDir( $directory, $lockouts );
|
|
|
|
}
|
|
|
|
|
|
|
|
$backend = $this->getBackend();
|
|
|
|
|
|
|
|
$key = "fancycaptcha:dirlist:{$backend->getWikiId()}:" . sha1( $directory );
|
|
|
|
$dirs = $wgMemc->get( $key ); // check cache
|
2013-01-01 00:36:02 +00:00
|
|
|
if ( !is_array( $dirs ) || !count( $dirs ) ) { // cache miss
|
2016-05-09 23:41:17 +00:00
|
|
|
$dirs = []; // subdirs actually present...
|
|
|
|
foreach ( $backend->getTopDirectoryList( [ 'dir' => $directory ] ) as $entry ) {
|
2009-07-19 15:13:01 +00:00
|
|
|
if ( ctype_xdigit( $entry ) && strlen( $entry ) == 1 ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
$dirs[] = $entry;
|
|
|
|
}
|
|
|
|
}
|
2012-09-16 17:31:47 +00:00
|
|
|
wfDebug( "Cache miss for $directory subdirectory listing.\n" );
|
2013-01-01 00:36:02 +00:00
|
|
|
if ( count( $dirs ) ) {
|
|
|
|
$wgMemc->set( $key, $dirs, 86400 );
|
|
|
|
}
|
2007-11-12 07:42:25 +00:00
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
2012-09-16 17:31:47 +00:00
|
|
|
if ( !count( $dirs ) ) {
|
|
|
|
// Remove this directory if empty so callers don't keep looking here
|
2016-05-09 23:41:17 +00:00
|
|
|
$backend->clean( [ 'dir' => $directory ] );
|
2012-09-16 17:31:47 +00:00
|
|
|
return false; // none found
|
2007-11-12 07:42:25 +00:00
|
|
|
}
|
2012-09-16 17:31:47 +00:00
|
|
|
|
|
|
|
$place = mt_rand( 0, count( $dirs ) - 1 ); // pick a random subdir
|
|
|
|
// In case all dirs are not filled, cycle through next digits...
|
2015-10-28 15:52:04 +00:00
|
|
|
$fancyCount = count( $dirs );
|
|
|
|
for ( $j = 0; $j < $fancyCount; $j++ ) {
|
2012-09-16 17:31:47 +00:00
|
|
|
$char = $dirs[( $place + $j ) % count( $dirs )];
|
|
|
|
$info = $this->pickImageDir( "$directory/$char", $levels - 1, $lockouts );
|
|
|
|
if ( $info ) {
|
|
|
|
return $info; // found a captcha
|
|
|
|
} else {
|
|
|
|
wfDebug( "Could not find captcha in $directory.\n" );
|
|
|
|
$wgMemc->delete( $key ); // files changed on disk?
|
|
|
|
}
|
2012-08-30 15:49:16 +00:00
|
|
|
}
|
2007-11-12 07:42:25 +00:00
|
|
|
|
2012-09-16 17:31:47 +00:00
|
|
|
return false; // didn't find any images in this directory... empty?
|
|
|
|
}
|
2007-11-12 07:42:25 +00:00
|
|
|
|
2012-09-16 17:31:47 +00:00
|
|
|
/**
|
|
|
|
* @param $directory string
|
|
|
|
* @param $lockouts integer
|
|
|
|
* @return Array|bool
|
|
|
|
*/
|
|
|
|
protected function pickImageFromDir( $directory, &$lockouts ) {
|
|
|
|
global $wgMemc;
|
|
|
|
|
|
|
|
$backend = $this->getBackend();
|
|
|
|
|
|
|
|
$key = "fancycaptcha:filelist:{$backend->getWikiId()}:" . sha1( $directory );
|
|
|
|
$files = $wgMemc->get( $key ); // check cache
|
2013-01-01 00:36:02 +00:00
|
|
|
if ( !is_array( $files ) || !count( $files ) ) { // cache miss
|
2016-05-09 23:41:17 +00:00
|
|
|
$files = []; // captcha files
|
|
|
|
foreach ( $backend->getTopFileList( [ 'dir' => $directory ] ) as $entry ) {
|
2012-09-16 17:31:47 +00:00
|
|
|
$files[] = $entry;
|
|
|
|
if ( count( $files ) >= 500 ) { // sanity
|
|
|
|
wfDebug( 'Skipping some captchas; $wgCaptchaDirectoryLevels set too low?.' );
|
2007-11-12 07:42:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-01-01 00:36:02 +00:00
|
|
|
if ( count( $files ) ) {
|
|
|
|
$wgMemc->set( $key, $files, 86400 );
|
|
|
|
}
|
2012-09-16 17:31:47 +00:00
|
|
|
wfDebug( "Cache miss for $directory captcha listing.\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !count( $files ) ) {
|
|
|
|
// Remove this directory if empty so callers don't keep looking here
|
2016-05-09 23:41:17 +00:00
|
|
|
$backend->clean( [ 'dir' => $directory ] );
|
2012-09-16 17:31:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$info = $this->pickImageFromList( $directory, $files, $lockouts );
|
|
|
|
if ( !$info ) {
|
|
|
|
wfDebug( "Could not find captcha in $directory.\n" );
|
|
|
|
$wgMemc->delete( $key ); // files changed on disk?
|
2007-11-12 07:42:25 +00:00
|
|
|
}
|
2012-09-16 17:31:47 +00:00
|
|
|
|
|
|
|
return $info;
|
2007-11-12 07:42:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-09-16 17:31:47 +00:00
|
|
|
* @param $directory string
|
|
|
|
* @param $files array
|
|
|
|
* @param $lockouts integer
|
|
|
|
* @return boolean
|
2007-11-12 07:42:25 +00:00
|
|
|
*/
|
2012-09-16 17:31:47 +00:00
|
|
|
protected function pickImageFromList( $directory, array $files, &$lockouts ) {
|
|
|
|
global $wgMemc, $wgCaptchaDeleteOnSolve;
|
|
|
|
|
|
|
|
if ( !count( $files ) ) {
|
|
|
|
return false; // none found
|
|
|
|
}
|
|
|
|
|
|
|
|
$backend = $this->getBackend();
|
|
|
|
$place = mt_rand( 0, count( $files ) - 1 ); // pick a random file
|
|
|
|
$misses = 0; // number of files in listing that don't actually exist
|
2015-10-28 15:52:04 +00:00
|
|
|
$fancyImageCount = count( $files );
|
|
|
|
for ( $j = 0; $j < $fancyImageCount; $j++ ) {
|
2012-09-16 17:31:47 +00:00
|
|
|
$entry = $files[( $place + $j ) % count( $files )];
|
|
|
|
if ( preg_match( '/^image_([0-9a-f]+)_([0-9a-f]+)\\.png$/', $entry, $matches ) ) {
|
|
|
|
if ( $wgCaptchaDeleteOnSolve ) { // captcha will be deleted when solved
|
|
|
|
$key = "fancycaptcha:filelock:{$backend->getWikiId()}:" . sha1( $entry );
|
|
|
|
// Try to claim this captcha for 10 minutes (for the user to solve)...
|
|
|
|
if ( ++$lockouts <= 10 && !$wgMemc->add( $key, '1', 600 ) ) {
|
|
|
|
continue; // could not acquire (skip it to avoid race conditions)
|
|
|
|
}
|
|
|
|
}
|
2016-05-09 23:41:17 +00:00
|
|
|
if ( !$backend->fileExists( [ 'src' => "$directory/$entry" ] ) ) {
|
2012-09-16 17:31:47 +00:00
|
|
|
if ( ++$misses >= 5 ) { // too many files in the listing don't exist
|
|
|
|
break; // listing cache too stale? break out so it will be cleared
|
|
|
|
}
|
|
|
|
continue; // try next file
|
|
|
|
}
|
2016-05-09 23:41:17 +00:00
|
|
|
return [
|
2012-09-16 17:31:47 +00:00
|
|
|
'salt' => $matches[1],
|
|
|
|
'hash' => $matches[2],
|
|
|
|
'viewed' => false,
|
2016-05-09 23:41:17 +00:00
|
|
|
];
|
2007-11-12 07:42:25 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-16 17:31:47 +00:00
|
|
|
|
|
|
|
return false; // none found
|
2007-11-12 07:42:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function showImage() {
|
2016-04-25 20:58:18 +00:00
|
|
|
global $wgOut, $wgRequest;
|
2007-11-12 07:42:25 +00:00
|
|
|
|
|
|
|
$wgOut->disable();
|
|
|
|
|
2016-04-25 20:58:18 +00:00
|
|
|
$index = $wgRequest->getVal( 'wpCaptchaId' );
|
|
|
|
$info = $this->retrieveCaptcha( $index );
|
2009-07-19 15:13:01 +00:00
|
|
|
if ( $info ) {
|
2012-09-02 12:26:25 +00:00
|
|
|
$timestamp = new MWTimestamp();
|
|
|
|
$info['viewed'] = $timestamp->getTimestamp();
|
2007-11-12 07:42:25 +00:00
|
|
|
$this->storeCaptcha( $info );
|
|
|
|
|
|
|
|
$salt = $info['salt'];
|
|
|
|
$hash = $info['hash'];
|
2012-09-16 17:31:47 +00:00
|
|
|
|
2016-05-09 23:41:17 +00:00
|
|
|
return $this->getBackend()->streamFile( [
|
2012-09-16 17:31:47 +00:00
|
|
|
'src' => $this->imagePath( $salt, $hash ),
|
2016-05-09 23:41:17 +00:00
|
|
|
'headers' => [ "Cache-Control: private, s-maxage=0, max-age=3600" ]
|
|
|
|
] )->isOK();
|
2007-11-12 07:42:25 +00:00
|
|
|
}
|
2012-09-16 17:31:47 +00:00
|
|
|
|
2015-03-18 06:54:12 +00:00
|
|
|
wfHttpError( 400, 'Request Error', 'Requested bogus captcha image' );
|
2007-11-12 07:42:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
2012-09-16 17:31:47 +00:00
|
|
|
/**
|
|
|
|
* @param $salt string
|
|
|
|
* @param $hash string
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function imagePath( $salt, $hash ) {
|
|
|
|
global $wgCaptchaDirectoryLevels;
|
|
|
|
|
|
|
|
$file = $this->getBackend()->getRootStoragePath() . '/captcha-render/';
|
2009-07-19 15:13:01 +00:00
|
|
|
for ( $i = 0; $i < $wgCaptchaDirectoryLevels; $i++ ) {
|
2012-09-16 17:31:47 +00:00
|
|
|
$file .= $hash{ $i } . '/';
|
2007-11-12 07:42:25 +00:00
|
|
|
}
|
|
|
|
$file .= "image_{$salt}_{$hash}.png";
|
2012-09-16 17:31:47 +00:00
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
2012-09-16 17:31:47 +00:00
|
|
|
/**
|
|
|
|
* @param $basename string
|
|
|
|
* @return Array (salt, hash)
|
2015-01-10 01:48:35 +00:00
|
|
|
* @throws Exception
|
2012-09-16 17:31:47 +00:00
|
|
|
*/
|
|
|
|
public function hashFromImageName( $basename ) {
|
|
|
|
if ( preg_match( '/^image_([0-9a-f]+)_([0-9a-f]+)\\.png$/', $basename, $matches ) ) {
|
2016-05-09 23:41:17 +00:00
|
|
|
return [ $matches[1], $matches[2] ];
|
2012-09-16 17:31:47 +00:00
|
|
|
} else {
|
2015-01-10 01:48:35 +00:00
|
|
|
throw new Exception( "Invalid filename '$basename'.\n" );
|
2012-09-16 17:31:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-29 13:31:31 +00:00
|
|
|
/**
|
|
|
|
* Delete a solved captcha image, if $wgCaptchaDeleteOnSolve is true.
|
2016-04-25 20:58:18 +00:00
|
|
|
* @inheritdoc
|
2010-08-29 13:31:31 +00:00
|
|
|
*/
|
2016-04-25 20:58:18 +00:00
|
|
|
protected function passCaptcha( $index, $word ) {
|
2015-04-02 22:56:48 +00:00
|
|
|
global $wgCaptchaDeleteOnSolve;
|
2010-08-29 13:31:31 +00:00
|
|
|
|
2016-04-25 20:58:18 +00:00
|
|
|
$info = $this->retrieveCaptcha( $index ); // get the captcha info before it gets deleted
|
|
|
|
$pass = parent::passCaptcha( $index, $word );
|
2010-08-29 13:31:31 +00:00
|
|
|
|
|
|
|
if ( $pass && $wgCaptchaDeleteOnSolve ) {
|
2016-05-09 23:41:17 +00:00
|
|
|
$this->getBackend()->quickDelete( [
|
2012-09-16 17:31:47 +00:00
|
|
|
'src' => $this->imagePath( $info['salt'], $info['hash'] )
|
2016-05-09 23:41:17 +00:00
|
|
|
] );
|
2010-08-29 13:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $pass;
|
|
|
|
}
|
2016-04-25 20:58:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array with 'salt' and 'hash' keys. Hash is
|
|
|
|
* md5( $wgCaptchaSecret . $salt . $answer . $wgCaptchaSecret . $salt )[0..15]
|
|
|
|
* @return array
|
|
|
|
* @throws Exception When a captcha image cannot be produced.
|
|
|
|
*/
|
|
|
|
public function getCaptcha() {
|
|
|
|
$info = $this->pickImage();
|
|
|
|
if ( !$info ) {
|
|
|
|
throw new UnderflowException( 'Ran out of captcha images' );
|
|
|
|
}
|
|
|
|
return $info;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCaptchaInfo( $captchaData, $id ) {
|
|
|
|
$title = SpecialPage::getTitleFor( 'Captcha', 'image' );
|
|
|
|
return $title->getLocalURL( 'wpCaptchaId=' . urlencode( $id ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onAuthChangeFormFields(
|
|
|
|
array $requests, array $fieldInfo, array &$formDescriptor, $action
|
|
|
|
) {
|
|
|
|
/** @var CaptchaAuthenticationRequest $req */
|
|
|
|
$req =
|
|
|
|
AuthenticationRequest::getRequestByClass( $requests,
|
2016-05-03 16:42:00 +00:00
|
|
|
CaptchaAuthenticationRequest::class, true );
|
2016-04-25 20:58:18 +00:00
|
|
|
if ( !$req ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTMLFancyCaptchaField will include this
|
|
|
|
unset( $formDescriptor['captchaInfo' ] );
|
|
|
|
|
|
|
|
$formDescriptor['captchaWord'] = [
|
|
|
|
'class' => HTMLFancyCaptchaField::class,
|
|
|
|
'imageUrl' => $this->getCaptchaInfo( $req->captchaData, $req->captchaId ),
|
|
|
|
'label-message' => $this->getMessage( $this->action ),
|
|
|
|
'showCreateHelp' => in_array( $action, [
|
|
|
|
AuthManager::ACTION_CREATE,
|
|
|
|
AuthManager::ACTION_CREATE_CONTINUE
|
|
|
|
], true ),
|
|
|
|
] + $formDescriptor['captchaWord'];
|
|
|
|
}
|
2007-11-12 07:42:25 +00:00
|
|
|
}
|