3 abstract class CaptchaStore {
5 * Store the correct answer for a given captcha
7 * @param $info String the captcha result
9 public abstract function store( $index, $info );
12 * Retrieve the answer for a given captcha
13 * @param $index String
16 public abstract function retrieve( $index );
19 * Delete a result once the captcha has been used, so it cannot be reused
22 public abstract function clear( $index );
25 * Whether this type of CaptchaStore needs cookies
28 public abstract function cookiesNeeded();
31 * The singleton instance
34 private static $instance;
37 * Get somewhere to store captcha data that will persist between requests
40 * @return CaptchaStore
42 public final static function get() {
43 if ( !self::$instance instanceof self ) {
44 global $wgCaptchaStorageClass;
45 if ( in_array( 'CaptchaStore', class_parents( $wgCaptchaStorageClass ) ) ) {
46 self::$instance = new $wgCaptchaStorageClass;
48 throw new MWException( "Invalid CaptchaStore class $wgCaptchaStorageClass" );
51 return self::$instance;
55 * Protected constructor: no creating instances except through the factory method above
57 protected function __construct() {}
60 class CaptchaSessionStore extends CaptchaStore {
62 protected function __construct() {
63 // Make sure the session is started
64 if ( session_id() === '' ) {
69 function store( $index, $info ) {
70 $_SESSION['captcha' . $info['index']] = $info;
73 function retrieve( $index ) {
74 if ( isset( $_SESSION['captcha' . $index] ) ) {
75 return $_SESSION['captcha' . $index];
81 function clear( $index ) {
82 unset( $_SESSION['captcha' . $index] );
85 function cookiesNeeded() {
90 class CaptchaCacheStore extends CaptchaStore {
92 function store( $index, $info ) {
93 global $wgMemc, $wgCaptchaSessionExpiration;
94 $wgMemc->set( wfMemcKey( 'captcha', $index ), $info,
95 $wgCaptchaSessionExpiration );
98 function retrieve( $index ) {
100 $info = $wgMemc->get( wfMemcKey( 'captcha', $index ) );
108 function clear( $index ) {
110 $wgMemc->delete( wfMemcKey( 'captcha', $index ) );
113 function cookiesNeeded() {