16 define(
'RECAPTCHA_RESPONSE_FIELD',
'g-recaptcha-response');
17 define(
'RECAPTCHA_HOST',
'https://www.recaptcha.net');
18 define(
"RECAPTCHA_PATH",
"/recaptcha/api/siteverify");
33 function __construct(&$form, $userIp, $message, $hostname =
'') {
34 parent::__construct($form, RECAPTCHA_RESPONSE_FIELD, FORM_VALIDATOR_REQUIRED_VALUE, $message);
35 $this->_userIp = $userIp;
36 $this->_hostname = $hostname;
51 if (is_null($privateKey) || empty($privateKey)) {
55 if (is_null($this->_userIp) || empty($this->_userIp)) {
62 $requestOptions = array(
64 'header' =>
"Content-Type: application/x-www-form-urlencoded;\r\n",
66 'content' => http_build_query(array(
67 'secret' => $privateKey,
68 'response' => $form->getData(RECAPTCHA_RESPONSE_FIELD),
69 'remoteip' => $this->_userIp,
74 $proxySettings = array(
80 if (!empty($proxySettings[
'host'])) {
81 $requestOptions[
'http'][
'proxy'] = $proxySettings[
'host'] . ((!empty($proxySettings[
'port'])) ?
':'.$proxySettings[
'port'] :
'');
82 $requestOptions[
'http'][
'request_fulluri'] =
true;
83 if (!empty($proxySettings[
'user'])) {
84 $requestOptions[
'http'][
'header'] .=
'Proxy-Authorization: Basic ' . base64_encode($proxySettings[
'user'].
':'.$proxySettings[
'pass']);
88 $requestContext = stream_context_create($requestOptions);
89 $response = file_get_contents(RECAPTCHA_HOST . RECAPTCHA_PATH,
false, $requestContext);
90 if ($response ===
false) {
97 if (isset($response[
'success']) && $response[
'success'] ===
true) {
98 if (
Config::getVar(
'captcha',
'recaptcha_enforce_hostname') && $response[
'hostname'] !== $this->_hostname) {
99 $this->_message =
'common.captcha.error.invalid-input-response';
104 if (isset($response[
'error-codes']) && is_array($response[
'error-codes'])) {
105 $this->_message =
'common.captcha.error.' . $response[
'error-codes'][0];