2011-04-23 11:44:47 +00:00
|
|
|
<?php
|
2011-04-24 11:41:49 +00:00
|
|
|
|
2016-04-25 20:58:18 +00:00
|
|
|
use MediaWiki\Session\SessionManager;
|
|
|
|
|
2011-04-24 11:41:49 +00:00
|
|
|
abstract class CaptchaStore {
|
|
|
|
/**
|
|
|
|
* Store the correct answer for a given captcha
|
2017-09-09 18:10:12 +00:00
|
|
|
* @param string $index
|
|
|
|
* @param string $info the captcha result
|
2011-04-24 11:41:49 +00:00
|
|
|
*/
|
2015-10-28 15:52:04 +00:00
|
|
|
abstract public function store( $index, $info );
|
2011-04-24 11:41:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the answer for a given captcha
|
2017-09-09 18:10:12 +00:00
|
|
|
* @param string $index
|
|
|
|
* @return string|false
|
2011-04-24 11:41:49 +00:00
|
|
|
*/
|
2015-10-28 15:52:04 +00:00
|
|
|
abstract public function retrieve( $index );
|
2011-04-24 11:41:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a result once the captcha has been used, so it cannot be reused
|
2017-09-09 18:10:12 +00:00
|
|
|
* @param string $index
|
2011-04-24 11:41:49 +00:00
|
|
|
*/
|
2015-10-28 15:52:04 +00:00
|
|
|
abstract public function clear( $index );
|
2011-04-24 11:41:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this type of CaptchaStore needs cookies
|
2017-09-09 18:10:12 +00:00
|
|
|
* @return bool
|
2011-04-24 11:41:49 +00:00
|
|
|
*/
|
2015-10-28 15:52:04 +00:00
|
|
|
abstract public function cookiesNeeded();
|
2011-04-24 11:41:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The singleton instance
|
|
|
|
* @var CaptchaStore
|
|
|
|
*/
|
|
|
|
private static $instance;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get somewhere to store captcha data that will persist between requests
|
|
|
|
*
|
2015-01-10 01:48:35 +00:00
|
|
|
* @throws Exception
|
2011-04-24 11:41:49 +00:00
|
|
|
* @return CaptchaStore
|
|
|
|
*/
|
2015-10-28 15:52:04 +00:00
|
|
|
final public static function get() {
|
2012-01-12 08:58:40 +00:00
|
|
|
if ( !self::$instance instanceof self ) {
|
2011-04-24 11:41:49 +00:00
|
|
|
global $wgCaptchaStorageClass;
|
2012-01-12 08:58:40 +00:00
|
|
|
if ( in_array( 'CaptchaStore', class_parents( $wgCaptchaStorageClass ) ) ) {
|
2011-04-24 11:41:49 +00:00
|
|
|
self::$instance = new $wgCaptchaStorageClass;
|
|
|
|
} else {
|
2015-01-10 01:48:35 +00:00
|
|
|
throw new Exception( "Invalid CaptchaStore class $wgCaptchaStorageClass" );
|
2011-04-24 11:41:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
|
2016-07-28 13:51:51 +00:00
|
|
|
final public static function unsetInstanceForTests() {
|
|
|
|
if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
|
|
|
|
throw new MWException( 'Cannot unset ' . __CLASS__ . ' instance in operation.' );
|
|
|
|
}
|
|
|
|
self::$instance = null;
|
|
|
|
}
|
|
|
|
|
2011-04-24 11:41:49 +00:00
|
|
|
/**
|
|
|
|
* Protected constructor: no creating instances except through the factory method above
|
|
|
|
*/
|
2015-10-28 15:52:04 +00:00
|
|
|
protected function __construct() {
|
|
|
|
}
|
2011-04-24 11:41:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class CaptchaSessionStore extends CaptchaStore {
|
2012-06-16 02:24:36 +00:00
|
|
|
protected function __construct() {
|
|
|
|
// Make sure the session is started
|
2016-04-25 20:58:18 +00:00
|
|
|
SessionManager::getGlobalSession()->persist();
|
2012-06-16 02:24:36 +00:00
|
|
|
}
|
|
|
|
|
2011-04-23 11:44:47 +00:00
|
|
|
function store( $index, $info ) {
|
2016-04-25 20:58:18 +00:00
|
|
|
SessionManager::getGlobalSession()->set( 'captcha' . $index, $info );
|
2011-04-23 11:44:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function retrieve( $index ) {
|
2016-04-25 20:58:18 +00:00
|
|
|
return SessionManager::getGlobalSession()->get( 'captcha' . $index, false );
|
2011-04-23 11:44:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function clear( $index ) {
|
2016-04-25 20:58:18 +00:00
|
|
|
SessionManager::getGlobalSession()->remove( 'captcha' . $index );
|
2011-04-23 11:44:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function cookiesNeeded() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-24 11:41:49 +00:00
|
|
|
class CaptchaCacheStore extends CaptchaStore {
|
2011-04-23 11:44:47 +00:00
|
|
|
function store( $index, $info ) {
|
2015-05-25 22:38:27 +00:00
|
|
|
global $wgCaptchaSessionExpiration;
|
|
|
|
|
|
|
|
ObjectCache::getMainStashInstance()->set(
|
|
|
|
wfMemcKey( 'captcha', $index ),
|
|
|
|
$info,
|
|
|
|
$wgCaptchaSessionExpiration
|
|
|
|
);
|
2011-04-23 11:44:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function retrieve( $index ) {
|
2015-05-25 22:38:27 +00:00
|
|
|
$info = ObjectCache::getMainStashInstance()->get( wfMemcKey( 'captcha', $index ) );
|
2011-04-23 11:44:47 +00:00
|
|
|
if ( $info ) {
|
|
|
|
return $info;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function clear( $index ) {
|
2015-05-25 22:38:27 +00:00
|
|
|
ObjectCache::getMainStashInstance()->delete( wfMemcKey( 'captcha', $index ) );
|
2011-04-23 11:44:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function cookiesNeeded() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-04-25 20:58:18 +00:00
|
|
|
|
|
|
|
class CaptchaHashStore extends CaptchaStore {
|
|
|
|
protected $data = [];
|
|
|
|
|
|
|
|
public function store( $index, $info ) {
|
|
|
|
$this->data[$index] = $info;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function retrieve( $index ) {
|
|
|
|
if ( array_key_exists( $index, $this->data ) ) {
|
|
|
|
return $this->data[$index];
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function clear( $index ) {
|
|
|
|
unset( $this->data[$index] );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function cookiesNeeded() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function clearAll() {
|
|
|
|
$this->data = [];
|
|
|
|
}
|
|
|
|
}
|