MOON
Server: Apache
System: Linux server1.studioinfinity.com.br 2.6.32-954.3.5.lve1.4.90.el6.x86_64 #1 SMP Tue Feb 21 12:26:30 UTC 2023 x86_64
User: artinside (517)
PHP: 7.4.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //home/artinside/sites.artinside.com.br/paliar/source/Support/RecaptchaV2.php
<?php
/**
 * Created by PhpStorm.
 * User: sergiohidalgojunior
 * Date: 2019-09-17
 * Time: 09:23
 */

namespace Source\Support;



/**
 * Class RecaptchaV2
 * @package Source\Support
 */
class RecaptchaV2
{
    /**
     * ReCAPTCHA URL verifying
     *
     * @var string
     */
    const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';


    /**
     * Private key
     *
     * @var string
     */
    private $secretKey;

    /** @var Message */
    private $message;


    /**
     * Initialize site and secret keys
     *
     * @param string $secretKey Secret key from ReCaptcha dashboard
     * @return void
     */
    public function __construct($secretKey = "6Letp7MlAAAAAAdb4sa8R1-Ijnzi6VK5DK7pz-mg")
    {
        $this->setSecretKey($secretKey);
        $this->message = new Message();
    }

    /**
     * Set secret key
     *
     * @param string $key
     * @return object
     */
    public function setSecretKey($key)
    {
        $this->secretKey = $key;
        return $this;
    }

    /**
     * Checks the code given by the captcha
     *
     * @param string $response Response code after submitting form (usually $_POST['g-recaptcha-response'])
     * @return bool
     */
    public function isValid($response)
    {
        if (empty($this->secretKey))
            return false;

        if (empty($response)) {

            $this->message->error("Não clicou em não sou um robo");
            return false;
        }

        $curl = curl_init();

        curl_setopt_array($curl,[
            CURLOPT_URL => self::VERIFY_URL,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => [
                'secret' => $this->secretKey,
                'response' => $response ?? ''
            ]
        ]);
        $response = curl_exec($curl);

        curl_close($curl);

        $responseObject = json_decode($response);

        return $responseObject->success;

    }



}