<?php
    class Recaptcha {
        protected $site_key;
        protected $secret_key;

        function __construct() {
            $this->site_key = RECAPTCHA_SITE_KEY;
            $this->secret_key = RECAPTCHA_SECRET_KEY;
        }

        public function verify($token, $action) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,"https://www.google.com/recaptcha/api/siteverify");
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('secret' => $this->secret_key, 'response' => $token)));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $response = curl_exec($ch);
            curl_close($ch);
            $arrResponse = json_decode($response, true);
            
            if($arrResponse["success"] == '1' && $arrResponse["action"] == $action && $arrResponse["score"] >= 0.5) {
                return true;
            }
            return false;
        }

        public function getScripts() {
            return array(
                    array('link' => 'https://www.google.com/recaptcha/api.js?render=' . $this->site_key, 'position' => 'footer'),
                    array('link' => '/public/assets/js/recaptcha.js', 'position' => 'footer'));
        }
    }