[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/extensions/ConfirmEdit/ -> ReCaptcha.class.php (source)

   1  <?php
   2  
   3  class ReCaptcha extends SimpleCaptcha {
   4      // reCAPTHCA error code returned from recaptcha_check_answer
   5      private $recaptcha_error = null;
   6  
   7      /**
   8       * Displays the reCAPTCHA widget.
   9       * If $this->recaptcha_error is set, it will display an error in the widget.
  10       *
  11       */
  12  	function getForm() {
  13          global $wgReCaptchaPublicKey, $wgReCaptchaTheme;
  14  
  15          $useHttps = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' );
  16          $js = 'var RecaptchaOptions = ' . Xml::encodeJsVar( array( 'theme' => $wgReCaptchaTheme, 'tabindex' => 1  ) );
  17  
  18          return Html::inlineScript( $js ) . recaptcha_get_html( $wgReCaptchaPublicKey, $this->recaptcha_error, $useHttps );
  19      }
  20  
  21      /**
  22       * Calls the library function recaptcha_check_answer to verify the users input.
  23       * Sets $this->recaptcha_error if the user is incorrect.
  24       * @return boolean
  25       *
  26       */
  27  	function passCaptcha() {
  28          global $wgReCaptchaPrivateKey, $wgRequest;
  29  
  30          // API is hardwired to return wpCaptchaId and wpCaptchaWord, so use that if the standard two are empty
  31          $challenge = $wgRequest->getVal( 'recaptcha_challenge_field', $wgRequest->getVal( 'wpCaptchaId' ) );
  32          $response = $wgRequest->getVal( 'recaptcha_response_field', $wgRequest->getVal( 'wpCaptchaWord' ) );
  33  
  34          if ( $response === null ) {
  35              // new captcha session
  36              return false;
  37          }
  38  
  39          $ip = $wgRequest->getIP();
  40  
  41          $recaptcha_response = recaptcha_check_answer(
  42              $wgReCaptchaPrivateKey,
  43              $ip,
  44              $challenge,
  45              $response
  46          );
  47  
  48          if ( !$recaptcha_response->is_valid ) {
  49              $this->recaptcha_error = $recaptcha_response->error;
  50              return false;
  51          }
  52  
  53          $recaptcha_error = null;
  54  
  55          return true;
  56  
  57      }
  58  
  59  	function addCaptchaAPI( &$resultArr ) {
  60          global $wgReCaptchaPublicKey;
  61  
  62          $resultArr['captcha']['type'] = 'recaptcha';
  63          $resultArr['captcha']['mime'] = 'image/png';
  64          $resultArr['captcha']['key'] = $wgReCaptchaPublicKey;
  65          $resultArr['captcha']['error'] = $this->recaptcha_error;
  66      }
  67  
  68      /**
  69       * Show a message asking the user to enter a captcha on edit
  70       * The result will be treated as wiki text
  71       *
  72       * @param $action string Action being performed
  73       * @return string
  74       */
  75  	function getMessage( $action ) {
  76          $name = 'recaptcha-' . $action;
  77          $text = wfMessage( $name )->text();
  78  
  79          # Obtain a more tailored message, if possible, otherwise, fall back to
  80          # the default for edits
  81          return wfMessage( $name, $text )->isDisabled() ? wfMessage( 'recaptcha-edit' )->text() : $text;
  82      }
  83  
  84  	public function APIGetAllowedParams( &$module, &$params, $flags ) {
  85          if ( $flags && $this->isAPICaptchaModule( $module ) ) {
  86              $params['recaptcha_challenge_field'] = null;
  87              $params['recaptcha_response_field'] = null;
  88          }
  89  
  90          return true;
  91      }
  92  
  93  	public function APIGetParamDescription( &$module, &$desc ) {
  94          if ( $this->isAPICaptchaModule( $module ) ) {
  95              $desc['recaptcha_challenge_field'] = 'Field from the ReCaptcha widget';
  96              $desc['recaptcha_response_field'] = 'Field from the ReCaptcha widget';
  97          }
  98  
  99          return true;
 100      }
 101  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1