mediawiki-extensions-Confir.../includes/CaptchaStore.php

118 lines
2.5 KiB
PHP
Raw Normal View History

2011-04-23 11:44:47 +00:00
<?php
abstract class CaptchaStore {
/**
* Store the correct answer for a given captcha
* @param $index String
* @param $info String the captcha result
*/
abstract public function store( $index, $info );
/**
* Retrieve the answer for a given captcha
* @param $index String
* @return String
*/
abstract public function retrieve( $index );
/**
* Delete a result once the captcha has been used, so it cannot be reused
* @param $index
*/
abstract public function clear( $index );
/**
* Whether this type of CaptchaStore needs cookies
* @return Bool
*/
abstract public function cookiesNeeded();
/**
* The singleton instance
* @var CaptchaStore
*/
private static $instance;
/**
* Get somewhere to store captcha data that will persist between requests
*
* @throws Exception
* @return CaptchaStore
*/
final public static function get() {
if ( !self::$instance instanceof self ) {
global $wgCaptchaStorageClass;
if ( in_array( 'CaptchaStore', class_parents( $wgCaptchaStorageClass ) ) ) {
self::$instance = new $wgCaptchaStorageClass;
} else {
throw new Exception( "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 {
protected function __construct() {
// Make sure the session is started
if ( session_id() === '' ) {
wfSetupSession();
}
}
2011-04-23 11:44:47 +00:00
function store( $index, $info ) {
$_SESSION['captcha' . $info['index']] = $info;
}
function retrieve( $index ) {
if ( isset( $_SESSION['captcha' . $index] ) ) {
return $_SESSION['captcha' . $index];
} else {
return false;
}
}
function clear( $index ) {
unset( $_SESSION['captcha' . $index] );
}
function cookiesNeeded() {
return true;
}
}
class CaptchaCacheStore extends CaptchaStore {
2011-04-23 11:44:47 +00:00
function store( $index, $info ) {
global $wgCaptchaSessionExpiration;
ObjectCache::getMainStashInstance()->set(
wfMemcKey( 'captcha', $index ),
$info,
$wgCaptchaSessionExpiration
);
2011-04-23 11:44:47 +00:00
}
function retrieve( $index ) {
$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 ) {
ObjectCache::getMainStashInstance()->delete( wfMemcKey( 'captcha', $index ) );
2011-04-23 11:44:47 +00:00
}
function cookiesNeeded() {
return false;
}
}