* (bug 32609) API: Move captchaid/captchaword of action=edit from core to Captcha...
[toast/cookiecaptcha.git] / Captcha.php
index 2382845364da8995ca3216d7099dcffabe38586e..a67870c8e61e444cc834753bb415bff98b53d961 100644 (file)
@@ -44,7 +44,7 @@ abstract class Captcha {
 
        /**
         * Instantiate a new Captcha object for a given Id
-        * 
+        *
         * @param  $id Int
         * @return Captcha
         */
@@ -218,20 +218,6 @@ abstract class Captcha {
 
 class SimpleCaptcha {
 
-       /**
-        * @var CaptchaStore
-        */
-       protected $storage;
-
-       function __construct() {
-               global $wgCaptchaStorageClass;
-               if( in_array( 'CaptchaStore', class_implements( $wgCaptchaStorageClass ) ) ) {
-                       $this->storage = new $wgCaptchaStorageClass;
-               } else {
-                       throw new MWException( "Invalid CaptchaStore class $wgCaptchaStorageClass" );
-               }
-       }
-
        function getCaptcha() {
                $a = mt_rand( 0, 100 );
                $b = mt_rand( 0, 10 );
@@ -267,7 +253,9 @@ class SimpleCaptcha {
                $captcha = $this->getCaptcha();
                $index = $this->storeCaptcha( $captcha );
 
-               return "<p><label for=\"wpCaptchaWord\">{$captcha['question']}</label> = " .
+               // dir="ltr" is needed because otherwise it may say
+               // "5 - 20" instead of "20 - 5" and that would be wrong.
+               return "<p><label dir=\"ltr\" for=\"wpCaptchaWord\">{$captcha['question']}</label> = " .
                        Xml::element( 'input', array(
                                'name' => 'wpCaptchaWord',
                                'id'   => 'wpCaptchaWord',
@@ -317,7 +305,7 @@ class SimpleCaptcha {
                                wfDebug( "ConfirmEdit: user group allows skipping captcha on email sending\n" );
                                return true;
                        }
-                       $form->addFooterText( 
+                       $form->addFooterText(
                                "<div class='captcha'>" .
                                $wgOut->parse( $this->getMessage( 'sendemail' ) ) .
                                $this->getForm() .
@@ -704,6 +692,7 @@ class SimpleCaptcha {
                        $this->addCaptchaAPI( $resultArr );
                        return false;
                }
+
                return true;
        }
 
@@ -754,7 +743,7 @@ class SimpleCaptcha {
        }
 
        /**
-        * Check the captcha on Special:EmailUser 
+        * Check the captcha on Special:EmailUser
         * @param $from MailAddress
         * @param $to MailAddress
         * @param $subject String
@@ -771,7 +760,7 @@ class SimpleCaptcha {
                        }
                        if ( $this->isIPWhitelisted() )
                                return true;
-               
+
                        if ( defined( 'MW_API' ) ) {
                                # API mode
                                # Asking for captchas in the API is really silly
@@ -787,6 +776,36 @@ class SimpleCaptcha {
                return true;
        }
 
+       /**
+        * @param $module ApiBase
+        * @param $params array
+        * @return bool
+        */
+       public function APIGetAllowedParams( &$module, &$params ) {
+               if ( !$module instanceof ApiEditPage ) {
+                       return true;
+               }
+               $params['captchaword'] = null;
+               $params['captchaid'] = null;
+
+               return true;
+       }
+
+       /**
+        * @param $module ApiBae
+        * @param $desc array
+        * @return bool
+        */
+       public function APIGetParamDescription( &$module, &$desc ) {
+               if ( !$module instanceof ApiEditPage ) {
+                       return true;
+               }
+               $desc['captchaid'] = 'CAPTCHA ID from previous request';
+               $desc['captchaword'] = 'Answer to the CAPTCHA';
+
+               return true;
+       }
+
        /**
         * Given a required captcha run, test form input for correct
         * input on the open session.
@@ -835,7 +854,7 @@ class SimpleCaptcha {
                        // Assign random index if we're not udpating
                        $info['index'] = strval( mt_rand() );
                }
-               $this->storage->store( $info['index'], $info );
+               CaptchaStore::get()->store( $info['index'], $info );
                return $info['index'];
        }
 
@@ -846,7 +865,7 @@ class SimpleCaptcha {
        function retrieveCaptcha() {
                global $wgRequest;
                $index = $wgRequest->getVal( 'wpCaptchaId' );
-               return $this->storage->retrieve( $index );
+               return CaptchaStore::get()->retrieve( $index );
        }
 
        /**
@@ -854,7 +873,7 @@ class SimpleCaptcha {
         * it can't be reused.
         */
        function clearCaptcha( $info ) {
-               $this->storage->clear( $info['index'] );
+               CaptchaStore::get()->clear( $info['index'] );
        }
 
        /**
@@ -901,7 +920,7 @@ class SimpleCaptcha {
                global $wgOut;
                $wgOut->setPageTitle( wfMsg( 'captchahelp-title' ) );
                $wgOut->addWikiText( wfMsg( 'captchahelp-text' ) );
-               if ( $this->storage->cookiesNeeded() ) {
+               if ( CaptchaStore::get()->cookiesNeeded() ) {
                        $wgOut->addWikiText( wfMsg( 'captchahelp-cookies-needed' ) );
                }
        }