" . 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); $url = $path . $req; // Use curl if possible because allow_url_fopen is off in many // environments, making file_get_contents fail. if (function_exists('curl_init')) { $response = $this->_curl($url); } else { $response = file_get_contents($url); } return $response; } private function _curl($url) { // Initiate curl. $c = curl_init(); // Set timeout. $timeout = 3; curl_setopt($c, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($c, CURLOPT_RETURNTRANSFER, TRUE); // Set url for call. curl_setopt($c, CURLOPT_URL, $url); // Execute curl call. $response = curl_exec($c); // Close curl. curl_close($c); 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 WPUF_ReCaptchaResponse */ public function verifyResponse($remoteIp, $response) { // Discard empty solution submissions if ($response == null || strlen($response) == 0) { $recaptchaResponse = new WPUF_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 WPUF_ReCaptchaResponse(); if (trim($answers ['success']) == true) { $recaptchaResponse->success = true; } else { $recaptchaResponse->success = false; $recaptchaResponse->errorCodes = $answers ['error-codes']; } return $recaptchaResponse; } } ?>