recaptcha.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Class that handles reCAPTCHA.
  4. */
  5. class Jetpack_ReCaptcha {
  6. /**
  7. * URL to which requests are POSTed.
  8. *
  9. * @const string
  10. */
  11. const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';
  12. /**
  13. * Site key to use in HTML code.
  14. *
  15. * @var string
  16. */
  17. private $site_key;
  18. /**
  19. * Shared secret for the site.
  20. *
  21. * @var string
  22. */
  23. private $secret_key;
  24. /**
  25. * Config for reCAPTCHA instance.
  26. *
  27. * @var array
  28. */
  29. private $config;
  30. /**
  31. * Error codes returned from reCAPTCHA API.
  32. *
  33. * @see https://developers.google.com/recaptcha/docs/verify
  34. *
  35. * @var array
  36. */
  37. private $error_codes;
  38. /**
  39. * Create a configured instance to use the reCAPTCHA service.
  40. *
  41. * @param string $site_key Site key to use in HTML code.
  42. * @param string $secret_key Shared secret between site and reCAPTCHA server.
  43. * @param array $config Config array to optionally configure reCAPTCHA instance.
  44. */
  45. public function __construct( $site_key, $secret_key, $config = array() ) {
  46. $this->site_key = $site_key;
  47. $this->secret_key = $secret_key;
  48. $this->config = wp_parse_args( $config, $this->get_default_config() );
  49. $this->error_codes = array(
  50. 'missing-input-secret' => __( 'The secret parameter is missing', 'jetpack' ),
  51. 'invalid-input-secret' => __( 'The secret parameter is invalid or malformed', 'jetpack' ),
  52. 'missing-input-response' => __( 'The response parameter is missing', 'jetpack' ),
  53. 'invalid-input-response' => __( 'The response parameter is invalid or malformed', 'jetpack' ),
  54. 'invalid-json' => __( 'Invalid JSON', 'jetpack' ),
  55. 'unexpected-response' => __( 'Unexpected response', 'jetpack' ),
  56. 'unexpected-hostname' => __( 'Unexpected hostname', 'jetpack' ),
  57. );
  58. }
  59. /**
  60. * Get default config for this reCAPTCHA instance.
  61. *
  62. * @return array Default config
  63. */
  64. public function get_default_config() {
  65. return array(
  66. 'language' => get_locale(),
  67. 'script_async' => true,
  68. 'tag_class' => 'g-recaptcha',
  69. 'tag_attributes' => array(
  70. 'theme' => 'light',
  71. 'type' => 'image',
  72. 'tabindex' => 0,
  73. ),
  74. );
  75. }
  76. /**
  77. * Calls the reCAPTCHA siteverify API to verify whether the user passes
  78. * CAPTCHA test.
  79. *
  80. * @param string $response The value of 'g-recaptcha-response' in the submitted
  81. * form.
  82. * @param string $remote_ip The end user's IP address.
  83. *
  84. * @return bool|WP_Error Returns true if verified. Otherwise WP_Error is returned.
  85. */
  86. public function verify( $response, $remote_ip ) {
  87. // No need make a request if response is empty.
  88. if ( empty( $response ) ) {
  89. return new WP_Error( 'missing-input-response', $this->error_codes['missing-input-response'], 400 );
  90. }
  91. $resp = wp_remote_post( self::VERIFY_URL, $this->get_verify_request_params( $response, $remote_ip ) );
  92. if ( is_wp_error( $resp ) ) {
  93. return $resp;
  94. }
  95. $resp_decoded = json_decode( wp_remote_retrieve_body( $resp ), true );
  96. if ( ! $resp_decoded ) {
  97. return new WP_Error( 'invalid-json', $this->error_codes['invalid-json'], 400 );
  98. }
  99. // Default error code and message.
  100. $error_code = 'unexpected-response';
  101. $error_message = $this->error_codes['unexpected-response'];
  102. // Use the first error code if exists.
  103. if ( isset( $resp_decoded['error-codes'] ) && is_array( $resp_decoded['error-codes'] ) ) {
  104. if ( isset( $resp_decoded['error-codes'][0] ) && isset( $this->error_codes[ $resp_decoded['error-codes'][0] ] ) ) {
  105. $error_message = $this->error_codes[ $resp_decoded['error-codes'][0] ];
  106. $error_code = $resp_decoded['error-codes'][0];
  107. }
  108. }
  109. if ( ! isset( $resp_decoded['success'] ) ) {
  110. return new WP_Error( $error_code, $error_message );
  111. }
  112. if ( true !== $resp_decoded['success'] ) {
  113. return new WP_Error( $error_code, $error_message );
  114. }
  115. // Validate the hostname matches expected source
  116. if ( isset( $resp_decoded['hostname'] ) ) {
  117. $url = wp_parse_url( get_home_url() );
  118. if ( $url['host'] !== $resp_decoded['hostname'] ) {
  119. return new WP_Error( 'unexpected-host', $this->error_codes['unexpected-hostname'] );
  120. }
  121. }
  122. return true;
  123. }
  124. /**
  125. * Get siteverify request parameters.
  126. *
  127. * @param string $response The value of 'g-recaptcha-response' in the submitted
  128. * form.
  129. * @param string $remote_ip The end user's IP address.
  130. *
  131. * @return array
  132. */
  133. public function get_verify_request_params( $response, $remote_ip ) {
  134. return array(
  135. 'body' => array(
  136. 'secret' => $this->secret_key,
  137. 'response' => $response,
  138. 'remoteip' => $remote_ip,
  139. ),
  140. 'sslverify' => true,
  141. );
  142. }
  143. /**
  144. * Get reCAPTCHA HTML to render.
  145. *
  146. * @return string
  147. */
  148. public function get_recaptcha_html() {
  149. return sprintf(
  150. '
  151. <div
  152. class="%s"
  153. data-sitekey="%s"
  154. data-theme="%s"
  155. data-type="%s"
  156. data-tabindex="%s"></div>
  157. <script type="text/javascript" src="https://www.google.com/recaptcha/api.js?hl=%s"%s></script>
  158. ',
  159. esc_attr( $this->config['tag_class'] ),
  160. esc_attr( $this->site_key ),
  161. esc_attr( $this->config['tag_attributes']['theme'] ),
  162. esc_attr( $this->config['tag_attributes']['type'] ),
  163. esc_attr( $this->config['tag_attributes']['tabindex'] ),
  164. rawurlencode( $this->config['language'] ),
  165. $this->config['script_async'] ? ' async' : ''
  166. );
  167. }
  168. }