<?php
/*
 * This is a PHP library that handles calling reCAPTCHA.
 *    - Documentation and latest version
 *          http://recaptcha.net/plugins/php/
 *    - Get a reCAPTCHA API Key
 *          https://www.google.com/recaptcha/admin/create
 *    - Discussion group
 *          http://groups.google.com/group/recaptcha
 *
 * Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net
 * AUTHORS:
 *   Mike Crawford
 *   Ben Maurer
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
 * The reCAPTCHA server URL's
 */
define("RECAPTCHA_API_SERVER", "http://www.google.com/recaptcha/");
define("RECAPTCHA_API_SECURE_SERVER", "https://www.google.com/recaptcha/");
define("RECAPTCHA_VERIFY_SERVER", "www.google.com");

/**
 * Encodes the given data into a query string format
 * @param $data - array of string elements to be encoded
 * @return string - encoded request
 */
if(!function_exists('_recaptcha_qsencode')){
function _recaptcha_qsencode ($data) {
        $req = "";
        foreach ( $data as $key => $value )
                $req .= $key . '=' . urlencode( stripslashes($value) ) . '&';

        // Cut the last '&'
        $req=substr($req,0,strlen($req)-1);
        return $req;
}
}


/**
 * Submits an HTTP POST to a reCAPTCHA server
 * @param string $host
 * @param string $path
 * @param array $data
 * @param int port
 * @return array response
 */
if ( ! function_exists( '_recaptcha_http_post' ) ) {
	function _recaptcha_http_post( $host, $path, $data, $port = 80 ) {
		$scheme = $port === 443 ? 'https' : 'http';
		$url = $scheme . '://' . $host . ':' . $port . $path;

		$response = wp_remote_post( $url, array(
			'body'    => _recaptcha_qsencode( $data ),
			'timeout' => 10,
			'headers' => array(
				'Content-Type' => 'application/x-www-form-urlencoded',
				'User-Agent'   => 'reCAPTCHA/PHP',
			),
		) );

		if ( is_wp_error( $response ) ) {
			return array( '', 'Connection error: ' . $response->get_error_message() );
		}

		$body    = wp_remote_retrieve_body( $response );
		$headers = wp_remote_retrieve_headers( $response );

		return array( $headers, $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.
 */

if (!function_exists('recaptcha_get_html')) {
    function recaptcha_get_html($pubkey, $error = null, $use_ssl = false) {
        if (empty($pubkey)) {
            return esc_html__("To use reCAPTCHA you must get an API key from https://www.google.com/recaptcha/admin/create", 'apptivo-business-site');
        }

        $server = $use_ssl ? RECAPTCHA_API_SECURE_SERVER : RECAPTCHA_API_SERVER;

        $iframe_src = $server . '/api/fallback?k=' . urlencode($pubkey);
        ob_start();
        ?>
        <div class="g-recaptcha" data-sitekey="<?php echo esc_attr($pubkey); ?>"></div>
        <input type="hidden" class="hiddenRecaptcha" name="hiddenRecaptcha" id="hiddenRecaptcha">
        <noscript>
            <div>
                <div style="width: 302px; height: 422px; position: relative;">
                    <div style="width: 302px; height: 422px; position: absolute;">
                        <iframe src="<?php echo esc_url($iframe_src); ?>"
                                frameborder="0" scrolling="no"
                                style="width: 302px; height:422px; border-style: none;">
                        </iframe>
                    </div>
                </div>
                <div style="width: 300px; height: 60px; border-style: none;
                            bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px;
                            background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px;">
                    <textarea id="g-recaptcha-response" name="g-recaptcha-response"
                              class="g-recaptcha-response"
                              style="width: 250px; height: 40px; border: 1px solid #c1c1c1;
                                     margin: 10px 25px; padding: 0px; resize: none;" >
                    </textarea>
                </div>
            </div>
        </noscript>
        <?php
        return ob_get_clean();
    }
}

/**
 * A ReCaptchaResponse is returned from recaptcha_check_answer()
 */
if(!class_exists('ReCaptchaResponse')){
    class ReCaptchaResponse {
        var $is_valid;
        var $error;
    }
}



/**
  * Calls an HTTP POST function to verify if the user's guess was correct
  * @param string $privkey
  * @param string $remoteip
  * @param string $challenge
  * @param string $response
  * @param array $extra_params an array of extra variables to post to the server
  * @return ReCaptchaResponse
  */
if(!function_exists('recaptcha_check_answers')){
	function recaptcha_check_answers($privkey, $remoteip, $response){
		if(empty($privkey)){
			die("To use reCAPTCHA you must get an API key from <a href='https://www.google.com/recaptcha/admin/create'>Google reCAPTCHA Admin</a>");
		}

		if(empty($response)){
			die("reCAPTCHA validation failed. Please try again.");
		}

		//$recaptcha_response = new ReCaptchaResponse();
		$responseData = [];
		$responseData = check_recaptcha_version($privkey, $response);

		// Determine the reCAPTCHA version
		// Check if 'success' and 'score' are part of the response
		if(isset($responseData->success) && $responseData->success == true){
			//return isset($responseData->score) ? 'v3' : 'v2';
			if(isset($responseData->score)){
				$key_version = 'v3';
			}else{
				$key_version = 'v2';
			}
		}else{
			return 'Invalid key or other error';
		}

		//$recaptcha_response = new ReCaptchaResponse();
		$recaptcha_response = new stdClass(); // Use stdClass to create a generic object

		// Block v2 keys
		if ($key_version === 'v2'){
			die("reCAPTCHA v2 is not allowed. Please use reCAPTCHA v3.");
		}

		// Check the response based on reCAPTCHA version
		if($key_version === 'v3'){
			// v3 specific validation
			if($responseData->success && isset($responseData->score) && $responseData->score >= 0.5){
				$recaptcha_response->is_valid = true;
			}else{
				$recaptcha_response->is_valid = false;
				$recaptcha_response->error = "reCAPTCHA validation failed. You might be a robot.";
			}
		}else{
			// Handle invalid or unknown keys
			$recaptcha_response->is_valid = false;
			$recaptcha_response->error = "Invalid reCAPTCHA key. Please check your configuration.";
		}
		return $recaptcha_response;
	}
}

/**
 * Check Type of Google reCAPTCHA keys, Whether v2 keys or v3 keys
 */
function check_recaptcha_version($secret_key, $token){
	$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=" . $secret_key . "&response=" . $token);
	// Check if the response is false
    if ($response === false) {
        return ['success' => false, 'error' => 'Unable to reach reCAPTCHA server.'];
    }
	
	$responseData = json_decode($response);
	// Check if decoding was successful
    if (json_last_error() !== JSON_ERROR_NONE) {
        return ['success' => false, 'error' => 'Invalid JSON response.'];
    }

	return $responseData;
}

/**
 * gets a URL where the user can sign up for reCAPTCHA. If your application
 * has a configuration page where you enter a key, you should provide a link
 * using this function.
 * @param string $domain The domain where the page is hosted
 * @param string $appname The name of your application
 */
if(!function_exists('recaptcha_get_signup_url')){
function recaptcha_get_signup_url ($domain = null, $appname = null) {
	return "https://www.google.com/recaptcha/admin/create?" .  _recaptcha_qsencode (array ('domains' => $domain, 'app' => $appname));
}
}
if(!function_exists('_recaptcha_aes_pad')){
function _recaptcha_aes_pad($val) {
	$block_size = 16;
	$numpad = $block_size - (strlen ($val) % $block_size);
	return str_pad($val, strlen ($val) + $numpad, chr($numpad));
}
}

/* Mailhide related code */
if(!function_exists('_recaptcha_aes_encrypt')){
function _recaptcha_aes_encrypt($val,$ky) {
	if (! function_exists ("mcrypt_encrypt")) {
		die ("To use reCAPTCHA Mailhide, you need to have the mcrypt php module installed.");
	}
	$mode=MCRYPT_MODE_CBC;   
	$enc=MCRYPT_RIJNDAEL_128;
	$val=_recaptcha_aes_pad($val);
	return mcrypt_encrypt($enc, $ky, $val, $mode, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
}
}

if(!function_exists('_recaptcha_mailhide_urlbase64')){
function _recaptcha_mailhide_urlbase64 ($x) {
	return strtr(base64_encode ($x), '+/', '-_');
}
}
/* gets the reCAPTCHA Mailhide url for a given email, public key and private key */
function recaptcha_mailhide_url($pubkey, $privkey, $email) {
	if ($pubkey == '' || $pubkey == null || $privkey == "" || $privkey == null) {
		die ("To use reCAPTCHA Mailhide, you have to sign up for a public and private key, " .
		     "you can do so at <a href='http://www.google.com/recaptcha/mailhide/apikey'>http://www.google.com/recaptcha/mailhide/apikey</a>");
	}
	

	$ky = pack('H*', $privkey);
	$cryptmail = _recaptcha_aes_encrypt ($email, $ky);
	
	return "http://www.google.com/recaptcha/mailhide/d?k=" . $pubkey . "&c=" . _recaptcha_mailhide_urlbase64 ($cryptmail);
}

/**
 * gets the parts of the email to expose to the user.
 * eg, given johndoe@example,com return ["john", "example.com"].
 * the email is then displayed as john...@example.com
 */
function _recaptcha_mailhide_email_parts ($email) {
	$arr = preg_split("/@/", $email );

	if (strlen ($arr[0]) <= 4) {
		$arr[0] = substr ($arr[0], 0, 1);
	} else if (strlen ($arr[0]) <= 6) {
		$arr[0] = substr ($arr[0], 0, 3);
	} else {
		$arr[0] = substr ($arr[0], 0, 4);
	}
	return $arr;
}

/**
 * Gets html to display an email address given a public an private key.
 * to get a key, go to:
 *
 * http://www.google.com/recaptcha/mailhide/apikey
 */
function recaptcha_mailhide_html($pubkey, $privkey, $email) {
	$emailparts = _recaptcha_mailhide_email_parts ($email);
	$url = recaptcha_mailhide_url ($pubkey, $privkey, $email);
	
	return htmlentities($emailparts[0]) . "<a href='" . htmlentities ($url) .
		"' onclick=\"window.open('" . htmlentities ($url) . "', '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=300'); return false;\" title=\"Reveal this e-mail address\">...</a>@" . htmlentities ($emailparts [1]);

}
?>
