<commit_before><commit_msg>Google Recaptcha validator.<commit_after><?php
/*
 * Copyright 2015 <NAME> <http://jacksleight.com/>
 * This source file is subject to the MIT license that is bundled with this package in the file LICENCE. 
 */

namespace Coast\Validator\Rule;

use Coast\Validator\Rule;

class Recaptcha extends Rule
{
	const CONNECT = 'connect';
	const INVALID = 'invalid';

	protected $_secretKey = null;
	protected $_remoteIp = null;

	public function __construct($secretKey, $remoteIp = null)
	{
		$this->secretKey($secretKey);
		$this->remoteIp($remoteIp);
	}

    public function secretKey($secretKey = null)
    {
        if (func_num_args() > 0) {
            $this->_secretKey = $secretKey;
            return $this;
        }
        return $this->_secretKey;
    }

    public function remoteIp($remoteIp = null)
    {
        if (func_num_args() > 0) {
            $this->_remoteIp = $remoteIp;
            return $this;
        }
        return $this->_remoteIp;
    }

	protected function _validate($value)
	{
		$http = new \Coast\Http();
		$res = $http->get((new \Coast\Url('https://www.google.com/recaptcha/api/siteverify'))
		 	->queryParams([
		 		'secret'   => $this->_secretKey,
		 		'response' => $value,
		 		'remoteip' => $this->_remoteIp,
		 	]));
		if (!$res->isSuccess()) {
			$this->error(self::CONNECT);
		}
		$data = $res->json(); 
		if (!isset($data['success']) || $data['success'] == false) {
			$this->error(self::INVALID);
		}	
	}
}
{'fraction_non_alphanumeric': 0.16124661246612465, 'fraction_numerical': 0.0040650406504065045, 'mean_word_length': 2.9177718832891246, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 24, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '796984', 'n_tokens_mistral': 521, 'n_tokens_neox': 483, 'n_words': 105}