]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - extensions/ConfirmEdit/includes/CaptchaStore.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / extensions / ConfirmEdit / includes / CaptchaStore.php
1 <?php
2
3 use MediaWiki\Session\SessionManager;
4
5 abstract class CaptchaStore {
6         /**
7          * Store the correct answer for a given captcha
8          * @param  $index String
9          * @param  $info String the captcha result
10          */
11         abstract public function store( $index, $info );
12
13         /**
14          * Retrieve the answer for a given captcha
15          * @param  $index String
16          * @return String|false
17          */
18         abstract public function retrieve( $index );
19
20         /**
21          * Delete a result once the captcha has been used, so it cannot be reused
22          * @param  $index
23          */
24         abstract public function clear( $index );
25
26         /**
27          * Whether this type of CaptchaStore needs cookies
28          * @return Bool
29          */
30         abstract public function cookiesNeeded();
31
32         /**
33          * The singleton instance
34          * @var CaptchaStore
35          */
36         private static $instance;
37
38         /**
39          * Get somewhere to store captcha data that will persist between requests
40          *
41          * @throws Exception
42          * @return CaptchaStore
43          */
44         final public static function get() {
45                 if ( !self::$instance instanceof self ) {
46                         global $wgCaptchaStorageClass;
47                         if ( in_array( 'CaptchaStore', class_parents( $wgCaptchaStorageClass ) ) ) {
48                                 self::$instance = new $wgCaptchaStorageClass;
49                         } else {
50                                 throw new Exception( "Invalid CaptchaStore class $wgCaptchaStorageClass" );
51                         }
52                 }
53                 return self::$instance;
54         }
55
56         final public static function unsetInstanceForTests() {
57                 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
58                         throw new MWException( 'Cannot unset ' . __CLASS__ . ' instance in operation.' );
59                 }
60                 self::$instance = null;
61         }
62
63         /**
64          * Protected constructor: no creating instances except through the factory method above
65          */
66         protected function __construct() {
67         }
68 }
69
70 class CaptchaSessionStore extends CaptchaStore {
71         protected function __construct() {
72                 // Make sure the session is started
73                 SessionManager::getGlobalSession()->persist();
74         }
75
76         function store( $index, $info ) {
77                 SessionManager::getGlobalSession()->set( 'captcha' . $index, $info );
78         }
79
80         function retrieve( $index ) {
81                 return SessionManager::getGlobalSession()->get( 'captcha' . $index, false );
82         }
83
84         function clear( $index ) {
85                 SessionManager::getGlobalSession()->remove( 'captcha' . $index );
86         }
87
88         function cookiesNeeded() {
89                 return true;
90         }
91 }
92
93 class CaptchaCacheStore extends CaptchaStore {
94         function store( $index, $info ) {
95                 global $wgCaptchaSessionExpiration;
96
97                 ObjectCache::getMainStashInstance()->set(
98                         wfMemcKey( 'captcha', $index ),
99                         $info,
100                         $wgCaptchaSessionExpiration
101                 );
102         }
103
104         function retrieve( $index ) {
105                 $info = ObjectCache::getMainStashInstance()->get( wfMemcKey( 'captcha', $index ) );
106                 if ( $info ) {
107                         return $info;
108                 } else {
109                         return false;
110                 }
111         }
112
113         function clear( $index ) {
114                 ObjectCache::getMainStashInstance()->delete( wfMemcKey( 'captcha', $index ) );
115         }
116
117         function cookiesNeeded() {
118                 return false;
119         }
120 }
121
122 class CaptchaHashStore extends CaptchaStore {
123         protected $data = [];
124
125         public function store( $index, $info ) {
126                 $this->data[$index] = $info;
127         }
128
129         public function retrieve( $index ) {
130                 if ( array_key_exists( $index, $this->data ) ) {
131                         return $this->data[$index];
132                 }
133                 return false;
134         }
135
136         public function clear( $index ) {
137                 unset( $this->data[$index] );
138         }
139
140         public function cookiesNeeded() {
141                 return false;
142         }
143
144         public function clearAll() {
145                 $this->data = [];
146         }
147 }