captchaType ) {
case 'simple_captcha' :
if ( class_exists( 'ReallySimpleCaptcha' ) ) {
$prefix = $_POST[ "input_captcha_prefix_{$this->id}" ];
$captcha_obj = $this->get_simple_captcha();
if ( ! $captcha_obj->check( $prefix, str_replace( ' ', '', $value ) ) ) {
$this->set_failed_validation( esc_html__( "The CAPTCHA wasn't entered correctly. Go back and try it again.", 'gravityforms' ) );
}
//removes old files in captcha folder (older than 1 hour);
$captcha_obj->cleanup();
}
break;
case 'math' :
$prefixes = explode( ',', $_POST[ "input_captcha_prefix_{$this->id}" ] );
$captcha_obj = $this->get_simple_captcha();
//finding first number
for ( $first = 0; $first < 10; $first ++ ) {
if ( $captcha_obj->check( $prefixes[0], $first ) ) {
break;
}
}
//finding second number
for ( $second = 0; $second < 10; $second ++ ) {
if ( $captcha_obj->check( $prefixes[2], $second ) ) {
break;
}
}
//if it is a +, perform the sum
if ( $captcha_obj->check( $prefixes[1], '+' ) ) {
$result = $first + $second;
} else {
$result = $first - $second;
}
if ( intval( $result ) != intval( $value ) ) {
$this->set_failed_validation( esc_html__( "The CAPTCHA wasn't entered correctly. Go back and try it again.", 'gravityforms' ) );
}
//removes old files in captcha folder (older than 1 hour);
$captcha_obj->cleanup();
break;
default:
$this->validate_recaptcha( $form );
}
}
/**
* Validates the reCAPTCHA response.
*
* In our application flow, we create a decoded string out of the reCAPTCHA service response if the reCAPTCHA field
* is added to the form on a page other than the last page. We therefore first attempt to validate the decoded response,
* falling back to validating the reCAPTCHA with a request to Google.
*
* @see GF_Field_CAPTCHA::verify_decoded_response()
*
* @since unknown
*
* @param array $form The form data.
*
* @return bool
*/
public function validate_recaptcha( $form ) {
$response = $this->get_posted_recaptcha_response();
if ( ! ( $this->verify_decoded_response( $form, $response ) || $this->verify_recaptcha_response( $response ) ) ) {
$this->set_failed_validation( __( 'The reCAPTCHA was invalid. Go back and try it again.', 'gravityforms' ) );
return false;
}
return true;
}
/**
* Verifies that the decoded response meets the requirements for submitting the form.
*
* Returns false if the decoded response doesn't exist or the reCAPTCHA field is on the last page, as we'll want
* regular validation at that point instead.
*
* @since 2.4.24
*
* @param array $form The form data.
* @param string $response The encoded response to verify.
*
* @return bool
*/
private function verify_decoded_response( $form, $response ) {
$decoded_response = $this->get_decoded_recaptcha_response( $response );
// No decoded object.
if ( ! is_object( $decoded_response ) ) {
return false;
}
// Not a time that we need to verify the decoded object.
if ( ! GFFormDisplay::is_last_page( $form ) || $this->is_on_last_page( $form ) ) {
return false;
}
return (
$decoded_response->success === true
&& ! empty( $decoded_response->token )
&& gmdate( time() ) <= strtotime( '+1 day', strtotime( $decoded_response->challenge_ts ) )
);
}
/**
* Set validation failed on reCAPTCHA field.
*
* @since 2.4.24
*
* @param string $message The message to set if one does not already exist.
*/
private function set_failed_validation( $message ) {
$this->failed_validation = true;
$this->validation_message = empty( $this->errorMessage ) ? $message : $this->errorMessage;
}
/**
* Get the saved site key.
*
* @since 2.4.24
*
* @return string
*/
public function get_site_key() {
if ( ! $this->site_key ) {
$this->site_key = get_option( 'rg_gforms_captcha_public_key', '' );
}
return $this->site_key;
}
/**
* Get the saved secret key.
*
* @since 2.4.25
*
* @return string
*/
public function get_secret_key() {
if ( ! $this->secret_key ) {
$this->secret_key = get_option( 'rg_gforms_captcha_private_key', '' );
}
return $this->secret_key;
}
/**
* Get the value of the reCAPTCHA response input.
*
* When user clicks on the "I'm not a robot" box, the response token is populated into a hidden field by Google.
* If the current form is a multi-page form and the reCAPTCHA field is on a page other than the last page, this
* value will return an openssl encoded string with the Google reCAPTCHA validation data and some supplemental
* validation data instead.
*
* @see GF_Field_CAPTCHA::get_encoded_recaptcha_response()
*
* @since 2.4.24
*
* @return string
*/
private function get_posted_recaptcha_response() {
return sanitize_text_field( rgpost( 'g-recaptcha-response' ) );
}
/**
* Validate the reCAPTCHA token provided by Google.
*
* @since unknown
*
* @param string $response The token to verify.
* @param null $secret_key The secret key for reCAPTCHA verification.
*
* @return bool
*/
public function verify_recaptcha_response( $response, $secret_key = null ) {
$verify_url = 'https://www.google.com/recaptcha/api/siteverify';
if ( $secret_key == null ) {
$secret_key = $this->get_secret_key();
}
// pass secret key and token for verification of whether the response was valid
$response = wp_remote_post( $verify_url, array(
'method' => 'POST',
'body' => array(
'secret' => $secret_key,
'response' => $response
),
) );
if ( ! is_wp_error( $response ) ) {
$this->response = json_decode( wp_remote_retrieve_body( $response ) );
return $this->response->success == true;
} else {
GFCommon::log_debug( __METHOD__ . '(): Validating the reCAPTCHA response has failed due to the following: ' . $response->get_error_message() );
}
return false;
}
public function get_field_input( $form, $value = '', $entry = null ) {
$form_id = $form['id'];
$is_entry_detail = $this->is_entry_detail();
$is_form_editor = $this->is_form_editor();
$id = (int) $this->id;
$field_id = $is_entry_detail || $is_form_editor || $form_id == 0 ? "input_$id" : 'input_' . $form_id . "_$id";
switch ( $this->captchaType ) {
case 'simple_captcha' :
$size = empty($this->simpleCaptchaSize) ? 'medium' : esc_attr( $this->simpleCaptchaSize );
$captcha = $this->get_captcha();
$tabindex = $this->get_tabindex();
$dimensions = $is_entry_detail || $is_form_editor ? '' : "width='" . esc_attr( rgar( $captcha, 'width' ) ) . "' height='" . esc_attr( rgar( $captcha, 'height' ) ) . "'";
return "
2 - " . sprintf( __( 'Enter your reCAPTCHA site and secret keys in the reCAPTCHA Settings section of the %sSettings page%s', 'gravityforms' ), "", '' ) . '