Localisation updates for core and extension messages from translatewiki.net
[toast/cookiecaptcha.git] / CaptchaStore.php
1 <?php
2
3 abstract class CaptchaStore {
4         /**
5          * Store the correct answer for a given captcha
6          * @param  $index String
7          * @param  $info String the captcha result
8          */
9         public abstract function store( $index, $info );
10
11         /**
12          * Retrieve the answer for a given captcha
13          * @param  $index String
14          * @return String
15          */
16         public abstract function retrieve( $index );
17
18         /**
19          * Delete a result once the captcha has been used, so it cannot be reused
20          * @param  $index
21          */
22         public abstract function clear( $index );
23
24         /**
25          * Whether this type of CaptchaStore needs cookies
26          * @return Bool
27          */
28         public abstract function cookiesNeeded();
29
30         /**
31          * The singleton instance
32          * @var CaptchaStore
33          */
34         private static $instance;
35
36         /**
37          * Get somewhere to store captcha data that will persist between requests
38          *
39          * @throws MWException
40          * @return CaptchaStore
41          */
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;
47                         } else {
48                                 throw new MWException( "Invalid CaptchaStore class $wgCaptchaStorageClass" );
49                         }
50                 }
51                 return self::$instance;
52         }
53
54         /**
55          * Protected constructor: no creating instances except through the factory method above
56          */
57         protected function __construct(){}
58 }
59
60 class CaptchaSessionStore extends CaptchaStore {
61
62         function store( $index, $info ) {
63                 $_SESSION['captcha' . $info['index']] = $info;
64         }
65
66         function retrieve( $index ) {
67                 if ( isset( $_SESSION['captcha' . $index] ) ) {
68                         return $_SESSION['captcha' . $index];
69                 } else {
70                         return false;
71                 }
72         }
73
74         function clear( $index ) {
75                 unset( $_SESSION['captcha' . $index] );
76         }
77
78         function cookiesNeeded() {
79                 return true;
80         }
81 }
82
83 class CaptchaCacheStore extends CaptchaStore {
84
85         function store( $index, $info ) {
86                 global $wgMemc, $wgCaptchaSessionExpiration;
87                 $wgMemc->set( wfMemcKey( 'captcha', $index ), $info,
88                         $wgCaptchaSessionExpiration );
89         }
90
91         function retrieve( $index ) {
92                 global $wgMemc;
93                 $info = $wgMemc->get( wfMemcKey( 'captcha', $index ) );
94                 if ( $info ) {
95                         return $info;
96                 } else {
97                         return false;
98                 }
99         }
100
101         function clear( $index ) {
102                 global $wgMemc;
103                 $wgMemc->delete( wfMemcKey( 'captcha', $index ) );
104         }
105
106         function cookiesNeeded() {
107                 return false;
108         }
109 }