2017-12-16 12:32:46 +00:00
|
|
|
<?php
|
|
|
|
|
2022-04-08 16:40:15 +00:00
|
|
|
namespace MediaWiki\Extension\ConfirmEdit\Store;
|
|
|
|
|
2023-06-07 15:21:54 +00:00
|
|
|
use BadMethodCallException;
|
|
|
|
use ConfigException;
|
2022-04-08 16:40:15 +00:00
|
|
|
|
2017-12-16 12:32:46 +00:00
|
|
|
abstract class CaptchaStore {
|
|
|
|
/**
|
|
|
|
* Store the correct answer for a given captcha
|
|
|
|
* @param string $index
|
2019-05-26 20:19:03 +00:00
|
|
|
* @param array $info the captcha result
|
2017-12-16 12:32:46 +00:00
|
|
|
*/
|
|
|
|
abstract public function store( $index, $info );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the answer for a given captcha
|
|
|
|
* @param string $index
|
2019-05-26 20:19:03 +00:00
|
|
|
* @return array|false
|
2017-12-16 12:32:46 +00:00
|
|
|
*/
|
|
|
|
abstract public function retrieve( $index );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a result once the captcha has been used, so it cannot be reused
|
|
|
|
* @param string $index
|
|
|
|
*/
|
|
|
|
abstract public function clear( $index );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this type of CaptchaStore needs cookies
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
abstract public function cookiesNeeded();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The singleton instance
|
2019-12-21 02:46:59 +00:00
|
|
|
* @var CaptchaStore|null
|
2017-12-16 12:32:46 +00:00
|
|
|
*/
|
|
|
|
private static $instance;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get somewhere to store captcha data that will persist between requests
|
|
|
|
*
|
|
|
|
* @return CaptchaStore
|
|
|
|
*/
|
|
|
|
final public static function get() {
|
2021-09-08 18:12:24 +00:00
|
|
|
global $wgCaptchaStorageClass;
|
2017-12-16 12:32:46 +00:00
|
|
|
if ( !self::$instance instanceof self ) {
|
2022-04-08 16:40:15 +00:00
|
|
|
if ( in_array( self::class, class_parents( $wgCaptchaStorageClass ) ) ) {
|
2017-12-16 12:32:46 +00:00
|
|
|
self::$instance = new $wgCaptchaStorageClass;
|
|
|
|
} else {
|
2023-06-07 15:21:54 +00:00
|
|
|
throw new ConfigException( "Invalid CaptchaStore class $wgCaptchaStorageClass" );
|
2017-12-16 12:32:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
final public static function unsetInstanceForTests() {
|
|
|
|
if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
|
2023-06-07 15:21:54 +00:00
|
|
|
throw new BadMethodCallException( 'Cannot unset ' . __CLASS__ . ' instance in operation.' );
|
2017-12-16 12:32:46 +00:00
|
|
|
}
|
|
|
|
self::$instance = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Protected constructor: no creating instances except through the factory method above
|
|
|
|
*/
|
|
|
|
protected function __construct() {
|
|
|
|
}
|
|
|
|
}
|