arthur-eudeline
4/14/2019 - 10:55 AM

reCaptcha

<?php

/**
 * Class reCaptcha
 */
class reCaptcha {
	
	/**
	 * Clé secrète du site
	 * @var string
	 */
	private $secret_key;
	
	/**
	 * Clé du site
	 * @var string
	 */
	private $site_key;
	
	/**
	 * reCaptcha constructor.
	 *
	 * @param string $site_key   La clé du site
	 * @param string $secret_key La clé secrète
	 */
	public function __construct( string $site_key, string $secret_key ) {
		$this->site_key   = $site_key;
		$this->secret_key = $secret_key;
	}
	
	/**
	 * Génère le HTML de la balise reCaptcha
	 * @param array $args
	 */
	public function render( array $args = [] ) {
		echo $this->get_render( $args );
	}
	
	/**
	 * Retourne le HTML de la balise reCaptcha
	 * @param array $args
	 *
	 * @return string
	 */
	public function get_render( array $args = [] ) {
		$output = "";
		
		$output .= '<div class="g-recaptcha" data-sitekey="' . $this->site_key . '"></div>';
		
		return $output;
	}
	
	/**
	 * Vérifie si le reCaptcha est valide
	 * @param string $code Le code donné par le captcha (par défaut dans $_POST['g-recaptcha-response'])
	 *
	 * @return bool
	 */
	public function check( string $code ) {
		if ( is_null( $code ) || $code === '' ) {
			return false;
		}
		
		$url    = "https://www.google.com/recaptcha/api/siteverify";
		$params = array(
			"secret"   => $this->secret_key,
			"response" => $code
		);
		
		$params = http_build_query( $params );
		$url    .= "?" . $params;
		
		if ( function_exists( 'curl_version' ) ) {
			$curl = curl_init( $url );
			curl_setopt( $curl, CURLOPT_HEADER, false );
			curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
			curl_setopt( $curl, CURLOPT_TIMEOUT, 5 );
			curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
			$response = curl_exec( $curl );
		} else {
			$reponse = file_get_contents( $url );
		}
		
		if ( is_null( $response ) || empty( $response ) ) {
			return false;
		}
		
		$response = json_decode( $response );
		
		if ( boolval( $response->success ) !== true ) {
			return false;
		} else {
			return true;
		}
		
	}
	
	/**
	 * Génére le code HTML de la balise script de reCaptcha
	 */
	public function the_script() {
		echo $this->get_the_script();
	}
	
	/**
	 * Retourne le code HTML de la balise script de reCaptcha
	 * @return string
	 */
	public function get_the_script() {
		return '<script src="https://www.google.com/recaptcha/api.js" async defer></script>';
	}
}
<?php 
  require_once(__DIR__ . "/includes/class/recaptcha.class.php");
  $recaptcha = new reCaptcha($site_key, $private_key);
?><!DOCTYPE html>
<html>
<head>
  <!-- ... -->
  <?php $recaptcha->the_script(); ?>
</head>
<body>
  <!-- ... -->
  
  <?php 
  if (isset($_POST['g-recaptcha-response'])) {
    if ( $recaptcha->check($_POST['g-recaptcha-response']) ) { 
      echo 'Captcha Valide !';
    } else {
      echo 'Captcha invalide';
    }
  }
  ?>
  
  <form>
    <input type="text">
    <?php $recaptcha->render(); ?>
    <button type="submit">Envoyer</button>
  </form>
</body>
</html>