Introduce a CaptchaStore abstract class that CaptchaSessionStore and CaptchaCacheStore can extend. Some tidying, documentation and type hinting.

This commit is contained in:
Happy-melon 2011-04-24 11:41:49 +00:00
parent 4d62315053
commit 54fa154b40
5 changed files with 84 additions and 7 deletions

View file

@ -1,9 +1,19 @@
<?php
class SimpleCaptcha {
/**
* @var CaptchaStore
*/
protected $storage;
function __construct() {
global $wgCaptchaStorageClass;
$this->storage = new $wgCaptchaStorageClass;
if( in_array( 'CaptchaStore', class_implements( $wgCaptchaStorageClass ) ) ) {
$this->storage = new $wgCaptchaStorageClass;
} else {
throw new MWException( "Invalid CaptchaStore class $wgCaptchaStorageClass" );
}
}
function getCaptcha() {
@ -81,7 +91,7 @@ class SimpleCaptcha {
/**
* Inject whazawhoo
* @fixme if multiple thingies insert a header, could break
* @param HTMLForm
* @param $form HTMLForm
* @return bool true to keep running callbacks
*/
function injectEmailUser( &$form ) {
@ -103,7 +113,7 @@ class SimpleCaptcha {
/**
* Inject whazawhoo
* @fixme if multiple thingies insert a header, could break
* @param SimpleTemplate $template
* @param QuickTemplate $template
* @return bool true to keep running callbacks
*/
function injectUserCreate( &$template ) {
@ -126,7 +136,7 @@ class SimpleCaptcha {
* Inject a captcha into the user login form after a failed
* password attempt as a speedbump for mass attacks.
* @fixme if multiple thingies insert a header, could break
* @param SimpleTemplate $template
* @param $template QuickTemplate
* @return bool true to keep running callbacks
*/
function injectUserLogin( &$template ) {
@ -410,6 +420,8 @@ class SimpleCaptcha {
/**
* Load external links from the externallinks table
* @param $title Title
* @return Array
*/
function getLinksFromTracker( $title ) {
$dbr = wfGetDB( DB_SLAVE );

View file

@ -1,5 +1,63 @@
<?php
class CaptchaSessionStore {
abstract class CaptchaStore {
/**
* Store the correct answer for a given captcha
* @param $index String
* @param $info String the captcha result
*/
public abstract function store( $index, $info );
/**
* Retrieve the answer for a given captcha
* @param $index String
* @return String
*/
public abstract function retrieve( $index );
/**
* Delete a result once the captcha has been used, so it cannot be reused
* @param $index
*/
public abstract function clear( $index );
/**
* Whether this type of CaptchaStore needs cookies
* @return Bool
*/
public abstract function cookiesNeeded();
/**
* The singleton instance
* @var CaptchaStore
*/
private static $instance;
/**
* Get somewhere to store captcha data that will persist between requests
*
* @throws MWException
* @return CaptchaStore
*/
public final static function get() {
if( !self::$instance instanceof self ){
global $wgCaptchaStorageClass;
if( in_array( 'CaptchaStore', class_implements( $wgCaptchaStorageClass ) ) ) {
self::$instance = new $wgCaptchaStorageClass;
} else {
throw new MWException( "Invalid CaptchaStore class $wgCaptchaStorageClass" );
}
}
return self::$instance;
}
/**
* Protected constructor: no creating instances except through the factory method above
*/
protected function __construct(){}
}
class CaptchaSessionStore extends CaptchaStore {
function store( $index, $info ) {
$_SESSION['captcha' . $info['index']] = $info;
@ -22,7 +80,7 @@ class CaptchaSessionStore {
}
}
class CaptchaCacheStore {
class CaptchaCacheStore extends CaptchaStore {
function store( $index, $info ) {
global $wgMemc, $wgCaptchaSessionExpiration;

View file

@ -202,6 +202,7 @@ $wgHooks['APIEditBeforeSave'][] = 'ConfirmEditHooks::confirmEditAPI';
$wgAutoloadClasses['ConfirmEditHooks'] = "$wgConfirmEditIP/ConfirmEditHooks.php";
$wgAutoloadClasses['SimpleCaptcha']= "$wgConfirmEditIP/Captcha.php";
$wgAutoloadClasses['CaptchaStore']= "$wgConfirmEditIP/CaptchaStore.php";
$wgAutoloadClasses['CaptchaSessionStore']= "$wgConfirmEditIP/CaptchaStore.php";
$wgAutoloadClasses['CaptchaCacheStore']= "$wgConfirmEditIP/CaptchaStore.php";
$wgAutoloadClasses['CaptchaSpecialPage'] = "$wgConfirmEditIP/ConfirmEditHooks.php";

View file

@ -1,6 +1,12 @@
<?php
class ConfirmEditHooks {
/**
* Get the global Captcha instance
*
* @return Captcha
*/
static function getInstance() {
global $wgCaptcha, $wgCaptchaClass;
static $done = false;

View file

@ -44,7 +44,7 @@ class FancyCaptcha extends SimpleCaptcha {
function getForm() {
$info = $this->pickImage();
if ( !$info ) {
die( "out of captcha images; this shouldn't happen" );
throw new MWException( "Ran out of captcha images" );
}
// Generate a random key for use of this captcha image in this session.