Merge branch '5.3'
authorjoshuaruesweg <ruesweg@woltlab.com>
Sat, 20 Mar 2021 16:40:54 +0000 (17:40 +0100)
committerjoshuaruesweg <ruesweg@woltlab.com>
Sat, 20 Mar 2021 16:40:54 +0000 (17:40 +0100)
1  2 
wcfsetup/install/files/lib/system/recaptcha/RecaptchaHandlerV2.class.php

index b16b75fca1bef6f7d596355d255ff3cdcd0cefc3,cb2a4c9240a62cb596644fc3321511786be03ac3..dd3b430193b7b65751c5d3d4a0f4bc3280fce498
@@@ -11,66 -9,58 +11,67 @@@ use wcf\util\UserUtil
  
  /**
   * Handles reCAPTCHA V2 support.
 - * 
 - * @author    Tim Duesterhus
 - * @copyright 2001-2019 WoltLab GmbH
 - * @license   GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 - * @package   WoltLabSuite\Core\System\Recaptcha
 + *
 + * @author  Tim Duesterhus
 + * @copyright   2001-2019 WoltLab GmbH
 + * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 + * @package WoltLabSuite\Core\System\Recaptcha
 + * @deprecated  5.4 - This was an implementation detail of wcf\system\captcha\RecaptchaHandler.
   */
 -class RecaptchaHandlerV2 extends SingletonFactory {
 -      /**
 -       * Validates response.
 -       * 
 -       * @param       string          $response
 -       * @param       string          $type
 -       * @throws      UserInputException
 -       */
 -      public function validate($response, $type = 'v2') {
 -              // fail if response is empty to avoid sending api requests
 -              if (empty($response)) {
 -                      throw new UserInputException('recaptchaString', 'false');
 -              }
 -              
 -              if ($type === 'v2') {
 -                      $key = RECAPTCHA_PRIVATEKEY;
 -              }
 -              else if ($type === 'invisible') {
 -                      $key = RECAPTCHA_PRIVATEKEY_INVISIBLE;
 -              }
 -              else {
 -                      // The bot modified the `recaptcha-type` form field.
 -                      throw new UserInputException('recaptchaString', 'false');
 -              }
 -              
 -              $request = new HTTPRequest('https://www.google.com/recaptcha/api/siteverify?secret='.rawurlencode($key).'&response='.rawurlencode($response).'&remoteip='.rawurlencode(UserUtil::getIpAddress()), ['timeout' => 10]);
 -              
 -              try {
 -                      $request->execute();
 -                      $reply = $request->getReply();
 -                      $data = JSON::decode($reply['body']);
 -                      
 -                      if ($data['success']) {
 -                              // yeah
 -                      }
 -                      else {
 -                              throw new UserInputException('recaptchaString', 'false');
 -                      }
 -              }
 -              catch (\Exception $e) {
 -                      if ($e instanceof UserInputException) throw $e;
 -                      
 -                      // log error, but accept captcha
 -                      \wcf\functions\exception\logThrowable($e);
 -              }
 -              
 -              WCF::getSession()->register('recaptchaDone', true);
 -      }
 +class RecaptchaHandlerV2 extends SingletonFactory
 +{
 +    /**
 +     * Validates response.
 +     *
 +     * @param string $response
 +     * @param string $type
 +     * @throws  UserInputException
 +     */
 +    public function validate($response, $type = 'v2')
 +    {
 +        // fail if response is empty to avoid sending api requests
 +        if (empty($response)) {
 +            throw new UserInputException('recaptchaString', 'false');
 +        }
 +
 +        if ($type === 'v2') {
 +            $key = RECAPTCHA_PRIVATEKEY;
 +        } elseif ($type === 'invisible') {
 +            $key = RECAPTCHA_PRIVATEKEY_INVISIBLE;
 +        } else {
-             throw new \InvalidArgumentException('$type must be either v2 or invisible.');
++            // The bot modified the `recaptcha-type` form field.
++            throw new UserInputException('recaptchaString', 'false');
 +        }
 +
 +        $request = new HTTPRequest(
 +            \sprintf(
 +                'https://www.google.com/recaptcha/api/siteverify?secret=%s&response=%s&remoteip=%s',
 +                \rawurlencode($key),
 +                \rawurlencode($response),
 +                \rawurlencode(UserUtil::getIpAddress())
 +            ),
 +            ['timeout' => 10]
 +        );
 +
 +        try {
 +            $request->execute();
 +            $reply = $request->getReply();
 +            $data = JSON::decode($reply['body']);
 +
 +            if ($data['success']) {
 +                // yeah
 +            } else {
 +                throw new UserInputException('recaptchaString', 'false');
 +            }
 +        } catch (\Exception $e) {
 +            if ($e instanceof UserInputException) {
 +                throw $e;
 +            }
 +
 +            // log error, but accept captcha
 +            \wcf\functions\exception\logThrowable($e);
 +        }
 +
 +        WCF::getSession()->register('recaptchaDone', true);
 +    }
  }