From f19f204534c64fea06d66d3308a59e8194cf0e65 Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Thu, 16 Jul 2015 22:04:42 -0400 Subject: [PATCH 1/2] #284 Add recaptchalib to repo --- .gitignore | 1 - inc/recaptcha/recaptchalib_v2.php | 140 ++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+), 1 deletion(-) create mode 100755 inc/recaptcha/recaptchalib_v2.php diff --git a/.gitignore b/.gitignore index 39da1146..7b12609a 100644 --- a/.gitignore +++ b/.gitignore @@ -269,7 +269,6 @@ attachments img/ban.png img/banned.png img/ico_tools.png -inc/recaptcha/recaptchalib_v2.php ip_whois.php language/en/emails/reset_password.txt language/en/help_files/ticket_list.html diff --git a/inc/recaptcha/recaptchalib_v2.php b/inc/recaptcha/recaptchalib_v2.php new file mode 100755 index 00000000..ae467a28 --- /dev/null +++ b/inc/recaptcha/recaptchalib_v2.php @@ -0,0 +1,140 @@ +" . self::$_signupUrl . ""); + } + $this->_secret=$secret; + } + + /** + * Encodes the given data into a query string format. + * + * @param array $data array of string elements to be encoded. + * + * @return string - encoded request. + */ + private function _encodeQS($data) + { + $req = ""; + foreach ($data as $key => $value) { + $req .= $key . '=' . urlencode(stripslashes($value)) . '&'; + } + + // Cut the last '&' + $req=substr($req, 0, strlen($req)-1); + return $req; + } + + /** + * Submits an HTTP GET to a reCAPTCHA server. + * + * @param string $path url path to recaptcha server. + * @param array $data array of parameters to be sent. + * + * @return array response + */ + private function _submitHTTPGet($path, $data) + { + $req = $this->_encodeQS($data); + $response = file_get_contents($path . $req); + return $response; + } + + /** + * Calls the reCAPTCHA siteverify API to verify whether the user passes + * CAPTCHA test. + * + * @param string $remoteIp IP address of end user. + * @param string $response response string from recaptcha verification. + * + * @return ReCaptchaResponse + */ + public function verifyResponse($remoteIp, $response) + { + // Discard empty solution submissions + if ($response == null || strlen($response) == 0) { + $recaptchaResponse = new ReCaptchaResponse(); + $recaptchaResponse->success = false; + $recaptchaResponse->errorCodes = 'missing-input'; + return $recaptchaResponse; + } + + $getResponse = $this->_submitHttpGet( + self::$_siteVerifyUrl, + array ( + 'secret' => $this->_secret, + 'remoteip' => $remoteIp, + 'v' => self::$_version, + 'response' => $response + ) + ); + $answers = json_decode($getResponse, true); + $recaptchaResponse = new ReCaptchaResponse(); + + if (trim($answers ['success']) == true) { + $recaptchaResponse->success = true; + } else { + $recaptchaResponse->success = false; + $recaptchaResponse->errorCodes = $answers [error-codes]; + } + + return $recaptchaResponse; + } +} + +?> From bab50e059f5c8816deafb97860dacec790700fde Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Thu, 16 Jul 2015 22:04:59 -0400 Subject: [PATCH 2/2] #284 Add cURL support for recaptchalib_v2 --- inc/recaptcha/recaptchalib_v2.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/inc/recaptcha/recaptchalib_v2.php b/inc/recaptcha/recaptchalib_v2.php index ae467a28..9b035ada 100755 --- a/inc/recaptcha/recaptchalib_v2.php +++ b/inc/recaptcha/recaptchalib_v2.php @@ -91,6 +91,27 @@ class ReCaptcha private function _submitHTTPGet($path, $data) { $req = $this->_encodeQS($data); + // Try using cURL first. If that fails, fallback to file_get_contents + if (function_exists('curl_init')) { + $handle = curl_init($path); + $queryString = http_build_query($data, '', '&'); + $options = array( + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => $queryString, + CURLOPT_HTTPHEADER => array( + 'Content-Type: application/x-www-form-urlencoded' + ), + CURLINFO_HEADER_OUT => false, + CURLOPT_HEADER => false, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_SSL_VERIFYPEER => true + ); + curl_setopt_array($handle, $options); + $response = curl_exec($handle); + curl_close($handle); + return $response; + } + $response = file_get_contents($path . $req); return $response; }