$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; // will contain the name of all PB forms containing reCAPTCHA which are loaded on a certain page
if ( empty($pubkey) )
echo $errorMessage = ''. __("To use reCAPTCHA you must get an API key from", "profile-builder"). " https://www.google.com/recaptcha/admin/create
";
// we add this number to the form name to differentiate between 2 different forms of the same type (e.g. PB Login widget + PB Login page form) which both have reCAPTCHA
$length = strval(strlen($wppb_recaptcha_forms));
if ($form_name != '') {
if ($wppb_recaptcha_forms != '') $wppb_recaptcha_forms .= ','.trim($form_name).$length;
else $wppb_recaptcha_forms = trim($form_name).$length;
// reCAPTCHA html for PB forms
return '
';
}
else { //reCAPTCHA html and scripts for default WP forms (backend)
return '
';
}
}
// For PB forms (not default WP ones) containing reCAPTCHA we'll make sure to add the script in the page's header and footer, in order to allow multiple reCAPTCHAs on the same page.
function wppb_recaptcha_script_header() {
global $wppb_recaptcha_forms;
//initialize the $wppb_recaptcha_forms global var
$wppb_recaptcha_forms = '';
}
add_action('wp_head', 'wppb_recaptcha_script_header');
function wppb_recaptcha_script_footer(){
global $wppb_recaptcha_forms;
if( !empty( $wppb_recaptcha_forms ) ) {
echo '';
$forms = explode(',',$wppb_recaptcha_forms);
$field = wppb_get_recaptcha_field();
$pubkey = '';
if( isset( $field['public-key'] ) ) {
$pubkey = trim( $field['public-key'] );
}
echo '';
}
}
add_action('wp_footer','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 ''. __("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();
$recaptchaResponse->is_valid = false;
return $recaptchaResponse;
}
$getResponse = _wppb_submitHTTPGet(
"https://www.google.com/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 = $_POST['g-recaptcha-response'];
}
else {
$recaptcha_response_field = '';
}
$resp = wppb_recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $recaptcha_response_field );
if ( !empty( $_POST ) )
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'] ) );
$item_description = wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_description_translation', $field['description'] );
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' ) ? '*' : '' );
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' ).'';
$output = ''.wppb_recaptcha_get_html( $publickey , 'pb_register');
if( !empty( $item_description ) )
$output .= '' . $item_description . '';
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) ) {
if ( ( wppb_validate_captcha_response( trim( $field['public-key'] ), trim( $field['private-key'] ) ) == 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']));
$item_description = wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_description_translation', $field['description']);
// check where reCAPCHA should display and add reCAPTCHA html
if ( isset($field['captcha-pb-forms']) && ( strpos( $field['captcha-pb-forms'],'pb_recover_password' ) !== false ) ) {
$recaptcha_output = '' . wppb_recaptcha_get_html($publickey, 'pb_recover_password');
if (!empty($item_description))
$recaptcha_output .= '' . $item_description . '';
$output = str_replace('', '
' . $recaptcha_output . '
' . '', $output);
}
}
return $output;
}
add_filter('wppb_recover_password_generate_password_input','wppb_display_recaptcha_recover_password');
/* Function that changes the messageNo from the Recover Password form */
function wppb_recaptcha_change_recover_password_message_no($messageNo) {
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'recover_password') {
$field = wppb_get_recaptcha_field();
if (!empty($field)) {
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 ( isset($field['captcha-pb-forms']) && (strpos($field['captcha-pb-forms'], 'pb_recover_password') !== false) ) {
if ( ($wppb_recaptcha_response == false ) && ( $field['required'] == 'Yes' ) )
$messageNo = '';
}
}
}
return $messageNo;
}
add_filter('wppb_recover_password_message_no', 'wppb_recaptcha_change_recover_password_message_no');
/* Function that adds the reCAPTCHA error message on the Recover Password form */
function wppb_recaptcha_recover_password_displayed_message1( $message ) {
$field = wppb_get_recaptcha_field();
if ( !empty($field) ){
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 ( isset($field['captcha-pb-forms']) && ( strpos( $field['captcha-pb-forms'],'pb_recover_password' ) !== false ) && ( $wppb_recaptcha_response == false )) {
// This message is also altered by the plugin-compatibilities.php file, in regards to Captcha plugin ( function wppb_captcha_recover_password_displayed_message1 )
if (($message == '