Нет описания

recaptcha.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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' => false,
  68. 'script_defer' => true,
  69. 'script_lazy' => false,
  70. 'tag_class' => 'g-recaptcha',
  71. 'tag_attributes' => array(
  72. 'theme' => 'light',
  73. 'type' => 'image',
  74. 'tabindex' => 0,
  75. ),
  76. );
  77. }
  78. /**
  79. * Calls the reCAPTCHA siteverify API to verify whether the user passes
  80. * CAPTCHA test.
  81. *
  82. * @param string $response The value of 'g-recaptcha-response' in the submitted
  83. * form.
  84. * @param string $remote_ip The end user's IP address.
  85. *
  86. * @return bool|WP_Error Returns true if verified. Otherwise WP_Error is returned.
  87. */
  88. public function verify( $response, $remote_ip ) {
  89. // No need make a request if response is empty.
  90. if ( empty( $response ) ) {
  91. return new WP_Error( 'missing-input-response', $this->error_codes['missing-input-response'], 400 );
  92. }
  93. $resp = wp_remote_post( self::VERIFY_URL, $this->get_verify_request_params( $response, $remote_ip ) );
  94. if ( is_wp_error( $resp ) ) {
  95. return $resp;
  96. }
  97. $resp_decoded = json_decode( wp_remote_retrieve_body( $resp ), true );
  98. if ( ! $resp_decoded ) {
  99. return new WP_Error( 'invalid-json', $this->error_codes['invalid-json'], 400 );
  100. }
  101. // Default error code and message.
  102. $error_code = 'unexpected-response';
  103. $error_message = $this->error_codes['unexpected-response'];
  104. // Use the first error code if exists.
  105. if ( isset( $resp_decoded['error-codes'] ) && is_array( $resp_decoded['error-codes'] ) ) {
  106. if ( isset( $resp_decoded['error-codes'][0] ) && isset( $this->error_codes[ $resp_decoded['error-codes'][0] ] ) ) {
  107. $error_message = $this->error_codes[ $resp_decoded['error-codes'][0] ];
  108. $error_code = $resp_decoded['error-codes'][0];
  109. }
  110. }
  111. if ( ! isset( $resp_decoded['success'] ) ) {
  112. return new WP_Error( $error_code, $error_message );
  113. }
  114. if ( true !== $resp_decoded['success'] ) {
  115. return new WP_Error( $error_code, $error_message );
  116. }
  117. // Validate the hostname matches expected source
  118. if ( isset( $resp_decoded['hostname'] ) ) {
  119. $url = wp_parse_url( get_home_url() );
  120. /**
  121. * Allow other valid hostnames.
  122. *
  123. * This can be useful in cases where the token hostname is expected to be
  124. * different from the get_home_url (ex. AMP recaptcha token contains a different hostname)
  125. *
  126. * @module sharedaddy
  127. *
  128. * @since 9.1.0
  129. *
  130. * @param array [ $url['host'] ] List of the valid hostnames to check against.
  131. */
  132. $valid_hostnames = apply_filters( 'jetpack_recaptcha_valid_hostnames', array( $url['host'] ) );
  133. if ( ! in_array( $resp_decoded['hostname'], $valid_hostnames, true ) ) {
  134. return new WP_Error( 'unexpected-host', $this->error_codes['unexpected-hostname'] );
  135. }
  136. }
  137. return true;
  138. }
  139. /**
  140. * Get siteverify request parameters.
  141. *
  142. * @param string $response The value of 'g-recaptcha-response' in the submitted
  143. * form.
  144. * @param string $remote_ip The end user's IP address.
  145. *
  146. * @return array
  147. */
  148. public function get_verify_request_params( $response, $remote_ip ) {
  149. return array(
  150. 'body' => array(
  151. 'secret' => $this->secret_key,
  152. 'response' => $response,
  153. 'remoteip' => $remote_ip,
  154. ),
  155. 'sslverify' => true,
  156. );
  157. }
  158. /**
  159. * Get reCAPTCHA HTML to render.
  160. *
  161. * @return string
  162. */
  163. public function get_recaptcha_html() {
  164. $url = sprintf(
  165. 'https://www.google.com/recaptcha/api.js?hl=%s',
  166. rawurlencode( $this->config['language'] )
  167. );
  168. $html = sprintf(
  169. '
  170. <div
  171. class="%s"
  172. data-sitekey="%s"
  173. data-theme="%s"
  174. data-type="%s"
  175. data-tabindex="%s"
  176. data-lazy="%s"
  177. data-url="%s"></div>
  178. ',
  179. esc_attr( $this->config['tag_class'] ),
  180. esc_attr( $this->site_key ),
  181. esc_attr( $this->config['tag_attributes']['theme'] ),
  182. esc_attr( $this->config['tag_attributes']['type'] ),
  183. esc_attr( $this->config['tag_attributes']['tabindex'] ),
  184. $this->config['script_lazy'] ? 'true' : 'false',
  185. esc_attr( $url )
  186. );
  187. if ( ! $this->config['script_lazy'] ) {
  188. $html = $html . sprintf(
  189. // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
  190. '<script src="%s"%s%s></script>
  191. ',
  192. $url,
  193. $this->config['script_async'] && ! $this->config['script_defer'] ? ' async' : '',
  194. $this->config['script_defer'] ? ' defer' : ''
  195. );
  196. }
  197. return $html;
  198. }
  199. }