1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69:
<?php
namespace Coast\Validator\Rule;
use Coast\Validator\Rule;
use Coast\Http;
use Coast\Url;
class Recaptcha extends Rule
{
const CONNECT = 'connect';
const INVALID = 'invalid';
protected $_config = [];
protected static $_configStatic = [];
public static function configStatic(array $configStatic = null)
{
if (func_num_args() > 0) {
self::$_configStatic = $configStatic;
}
return self::$_configStatic;
}
public function __construct(array $config = null)
{
$this->config(isset($config) ? $config : self::$_configStatic);
}
public function config(array $config = null)
{
if (func_num_args() > 0) {
$this->_config = $config;
return $this;
}
return $this->_config;
}
protected function _validate($value)
{
return;
$http = new Http();
$req = new Http\Request([
'url' => (new Url('https://www.google.com/recaptcha/api/siteverify'))->queryParams([
'secret' => isset($this->_config['secretKey'])
? $this->_config['secretKey']
: null,
'response' => $value,
'remoteip' => isset($this->_config['remoteIp'])
? $this->_config['remoteIp']
: null,
]),
]);
$res = $http->execute($req);
if (!$res->isSuccess()) {
$this->error(self::CONNECT);
}
$data = $res->json();
if (!isset($data->success) || $data->success == false) {
$this->error(self::INVALID);
}
}
}