$value) {
$req .= $key . '=' . urlencode(stripslashes($value)) . '&';
}
// Cut the last '&'
$req=substr($req, 0, strlen($req)-1);
return $req;
}
/**
* Submits an HTTP GET to a reCAPTCHA server
* @param string $path
* @param array $data
*/
function _wppb_submitHTTPGet($path, $data)
{
$req = _wppb_encodeQS($data);
$response = wp_remote_get($path . $req);
if ( ! is_wp_error( $response ))
return $response["body"];
}
/**
* Gets the challenge HTML (javascript and non-javascript version).
* This is called from the browser, and the resulting reCAPTCHA HTML widget
* is embedded within the HTML form it was called from.
* @param string $pubkey A public key for reCAPTCHA
* @param string $error The error given by reCAPTCHA (optional, default is null)
* @param boolean $use_ssl Should the request be made over ssl? (optional, default is false)
* @return string - The HTML to be embedded in the user's form.
*/
function wppb_recaptcha_get_html ( $pubkey, $form_name='' ){
global $wppb_recaptcha_forms; // is the counter for the number of forms that have recaptcha so we always have unique ids on the element
if( is_null( $wppb_recaptcha_forms ) )
$wppb_recaptcha_forms = 0;
$wppb_recaptcha_forms++;
$field = wppb_get_recaptcha_field();
if ( empty($pubkey) )
echo ''. esc_html__("To use reCAPTCHA you must get an API key from", "profile-builder"). " https://www.google.com/recaptcha/admin/create
";
// extra class needed for Invisible reCAPTCHA html
$invisible_class = '';
if ( isset($field['recaptcha-type']) && ($field['recaptcha-type'] == 'invisible') ) {
$invisible_class = 'wppb-invisible-recaptcha';
}
// reCAPTCHA html for all forms and we make sure we have a unique id for v2
return '
';
}
/**
* Add reCAPTCHA scripts to both front-end PB forms (with support for multiple forms) as well as Default WP forms
*/
function wppb_recaptcha_script_footer(){
$field = wppb_get_recaptcha_field();
/* if we do not have a recaptcha field don't do nothing */
if( empty( $field ) )
return;
//do not add script if there is no shortcode
global $wppb_shortcode_on_front;
if( current_filter() == 'wp_footer' && ( !isset( $wppb_shortcode_on_front ) || $wppb_shortcode_on_front === false ) )
return;
//do not add script if the html for the field has not been added
global $wppb_recaptcha_present;
if( !isset( $wppb_recaptcha_present ) || $wppb_recaptcha_present === false )
return;
//we don't have jquery on the backend
if( current_filter() != 'wp_footer' ) {
wp_print_scripts('jquery');
}else if(!wp_script_is('jquery')){
wp_print_scripts('jquery');
}
//get site key
$pubkey = '';
if( isset( $field['public-key'] ) ) {
$pubkey = sanitize_text_field( $field['public-key'] );
}
// Check if we have a reCAPTCHA type
if ( !isset($field['recaptcha-type']) )
$field['recaptcha-type'] = 'v2' ;
/*for invisible recaptcha we have extra parameters and the selector is different. v2 is initialized on the id of the div
that must be unique and invisible is on the submit button of the forms that have the div */
if( $field['recaptcha-type'] === 'invisible' ) {
$callback_conditions = 'jQuery("input[type=\'submit\']", jQuery( ".wppb-recaptcha-element" ).closest("form") )';
$invisible_parameters = '"callback" : wppbInvisibleRecaptchaOnSubmit,"size": "invisible"';
}else {
$callback_conditions = 'jQuery(".wppb-recaptcha-element")';
$invisible_parameters = '';
}
//the section below is properly escaped or the variables contain static strings
// phpcs:disable
echo '
';
// phpcs:enable
if( $field['recaptcha-type'] === 'invisible' ) {
echo '';
}
$lang = '&hl=en';
$locale = get_locale();
if(!empty($locale)) {
$locale_parts = explode('_',$locale);
$lang = '&hl='.urlencode($locale_parts[0]);
}
$source = apply_filters( 'wppb_recaptcha_custom_field_source', 'www.google.com' );
echo '';
}
add_action('wp_footer', 'wppb_recaptcha_script_footer', 9999);
add_action('login_footer', 'wppb_recaptcha_script_footer');
add_action('register_form', 'wppb_recaptcha_script_footer');
add_action('lost_password', 'wppb_recaptcha_script_footer');
/**
* A wppb_ReCaptchaResponse is returned from wppb_recaptcha_check_answer()
*/
class wppb_ReCaptchaResponse {
var $is_valid;
}
/**
* Calls an HTTP POST function to verify if the user's answer was correct
* @param string $privkey
* @param string $remoteip
* @param string $response
* @return wppb_ReCaptchaResponse
*/
function wppb_recaptcha_check_answer ( $privkey, $remoteip, $response ){
if ( $remoteip == null || $remoteip == '' )
echo ''. esc_html__("For security reasons, you must pass the remote ip to reCAPTCHA!", "profile-builder") .'
';
// Discard empty solution submissions
if ($response == null || strlen($response) == 0) {
$recaptchaResponse = new wppb_ReCaptchaResponse();
if( isset( $_POST['wppb_recaptcha_load_error'] ) && wp_verify_nonce( sanitize_text_field( $_POST['wppb_recaptcha_load_error'] ), 'wppb_recaptcha_init_error' ) )
$recaptchaResponse->is_valid = true;
else
$recaptchaResponse->is_valid = false;
return $recaptchaResponse;
}
$source = apply_filters( 'wppb_recaptcha_custom_field_source', 'www.google.com' );
$getResponse = _wppb_submitHTTPGet(
"https://".$source."/recaptcha/api/siteverify?",
array (
'secret' => $privkey,
'remoteip' => $remoteip,
'response' => $response
)
);
$answers = json_decode($getResponse, true);
$recaptchaResponse = new wppb_ReCaptchaResponse();
if (trim($answers ['success']) == true) {
$recaptchaResponse->is_valid = true;
} else {
$recaptchaResponse->is_valid = false;
}
return $recaptchaResponse;
}
/* the function to display error message on the registration page */
function wppb_validate_captcha_response( $publickey, $privatekey ){
if (isset($_POST['g-recaptcha-response'])){
$recaptcha_response_field = sanitize_textarea_field( $_POST['g-recaptcha-response'] );
}
else {
$recaptcha_response_field = '';
}
if( isset( $_SERVER["REMOTE_ADDR"] ) )
$resp = wppb_recaptcha_check_answer($privatekey, sanitize_text_field( $_SERVER["REMOTE_ADDR"] ), $recaptcha_response_field );
if ( !empty( $_POST ) && isset( $resp ) )
return ( ( !$resp->is_valid ) ? false : true );
}
/* the function to add reCAPTCHA to the registration form of PB */
function wppb_recaptcha_handler ( $output, $form_location, $field, $user_id, $field_check_errors, $request_data ){
if ( $field['field'] == 'reCAPTCHA' ){
$item_title = apply_filters( 'wppb_'.$form_location.'_recaptcha_custom_field_'.$field['id'].'_item_title', wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_title_translation', $field['field-title'], true ) );
$item_description = wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_description_translation', $field['description'], true );
wppb_recaptcha_set_default_values();
if ( ($form_location == 'register') && ( isset($field['captcha-pb-forms']) ) && (strpos($field['captcha-pb-forms'],'pb_register') !== false) ) {
$error_mark = ( ( $field['required'] == 'Yes' ) ? '*' : '' );
global $wppb_recaptcha_present;
$wppb_recaptcha_present = true;
if ( array_key_exists( $field['id'], $field_check_errors ) )
$error_mark = '';
$publickey = trim( $field['public-key'] );
$privatekey = trim( $field['private-key'] );
if ( empty( $publickey ) || empty( $privatekey ) )
return ''.apply_filters( 'wppb_'.$form_location.'_recaptcha_custom_field_'.$field['id'].'_error_message', __("To use reCAPTCHA you must get an API public key from:", "profile-builder"). 'https://www.google.com/recaptcha/admin/create' ).'';
if ( empty($field['recaptcha-type']) || ($field['recaptcha-type'] == 'v2') ) {
$output = '' . wppb_recaptcha_get_html($publickey, 'pb_register');
if (!empty($item_description))
$output .= '' . $item_description . '';
}
else {
// html for Invisible reCAPTCHA
$output = wppb_recaptcha_get_html($publickey, 'pb_register');
}
return $output;
}
}
}
add_filter( 'wppb_output_form_field_recaptcha', 'wppb_recaptcha_handler', 10, 6 );
/* handle reCAPTCHA field validation on PB Register form */
function wppb_check_recaptcha_value( $message, $field, $request_data, $form_location ){
if( $field['field'] == 'reCAPTCHA' ){
if ( ( $form_location == 'register' ) && ( isset($field['captcha-pb-forms']) ) && (strpos($field['captcha-pb-forms'],'pb_register') !== false) ) {
/* theme my login plugin executes the register_errors hook on the frontend on all pages so on our register forms we might have already a recaptcha response
so do not verify it again or it will fail */
global $wppb_recaptcha_response;
if (!isset($wppb_recaptcha_response)){
$wppb_recaptcha_response = wppb_validate_captcha_response( trim( $field['public-key'] ), trim( $field['private-key'] ) );
}
if ( ( $wppb_recaptcha_response == false ) && ( $field['required'] == 'Yes' ) ){
return wppb_required_field_error($field["field-title"]);
}
}
}
return $message;
}
add_filter( 'wppb_check_form_field_recaptcha', 'wppb_check_recaptcha_value', 10, 4 );
// Get the reCAPTCHA field information
function wppb_get_recaptcha_field(){
$wppb_manage_fields = get_option( 'wppb_manage_fields', 'not_found' );
$field = '';
if ( $wppb_manage_fields != 'not_found' ) {
foreach ($wppb_manage_fields as $value) {
if ($value['field'] == 'reCAPTCHA')
$field = $value;
}
}
return $field;
}
/* Display reCAPTCHA on PB Recover Password form */
function wppb_display_recaptcha_recover_password( $output ){
$field = wppb_get_recaptcha_field();
if ( !empty($field) ) {
$publickey = trim($field['public-key']);
$item_title = apply_filters('wppb_recover_password_recaptcha_custom_field_' . $field['id'] . '_item_title', wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_title_translation', $field['field-title'], true));
$item_description = wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_description_translation', $field['description'], true);
// check where reCAPTCHA should display and add reCAPTCHA html
if ( isset($field['captcha-pb-forms']) && ( strpos( $field['captcha-pb-forms'],'pb_recover_password' ) !== false ) ) {
global $wppb_recaptcha_present;
$wppb_recaptcha_present = true;
if ( empty($field['recaptcha-type']) || ($field['recaptcha-type'] == 'v2') ) {
$recaptcha_output = '' . wppb_recaptcha_get_html($publickey, 'pb_recover_password');
if (!empty($item_description))
$recaptcha_output .= '' . $item_description . '';
$output = str_replace('', '