registry = $registry; $this->DB = $this->registry->DB(); $this->settings =& $this->registry->fetchSettings(); $this->request =& $this->registry->fetchRequest(); $this->lang = $this->registry->getClass('class_localization'); $this->member = $this->registry->member(); $this->memberData =& $this->registry->member()->fetchMemberData(); /* Settings */ $this->public_key = trim( $this->settings['recaptcha_public_key'] ); $this->private_key = trim( $this->settings['recaptcha_private_key'] ); $this->useSSL = $this->settings['logins_over_https']; } /** * Gets the challenge HTML (javascript and non-javascript version). * This is called from the browser, and the resulting reCAPTCHA HTML widget * is embedded within the HTML form it was called from. * * @access public * @return string Form HTML to display */ public function getTemplate() { if ( ! $this->public_key ) { return ''; } if ($this->useSSL) { $server = RECAPTCHA_API_SECURE_SERVER; } else { $server = RECAPTCHA_API_SERVER; } $html = ''; $html .= ""; $html .= ' '; //----------------------------------------- // Return Template Bit //----------------------------------------- return $this->registry->output->getTemplate('global_other')->captchaRecaptcha( $html ); } /** * Validate the input code * * @access public * @return boolean Validation successful * @since 1.0 */ public function validate() { if ( !$this->private_key ) { return ''; } $captcha_unique_id = $_REQUEST['recaptcha_challenge_field']; $captcha_input = $_REQUEST['recaptcha_response_field']; if ( $captcha_input == null || strlen($captcha_input) == 0 || $captcha_unique_id == null || strlen($captcha_unique_id) == 0) { return false; } $response = $this->_recaptchaPost( RECAPTCHA_VERIFY_SERVER, "/verify", array ( 'privatekey' => $this->private_key, 'remoteip' => $this->member->ip_address, 'challenge' => $captcha_unique_id, 'response' => $captcha_input ) ); $answers = explode( "\n", $response [1] ); if ( trim($answers[0]) == 'true' ) { return TRUE; } else { /** * It's an error */ $this->error = $answers[1]; return FALSE; } } /** * Gets a URL where the user can sign up for reCAPTCHA. If your application * has a configuration page where you enter a key, you should provide a link * using this function. * * @access public * @param string $domain The domain where the page is hosted * @param string $appname The name of your application */ public function getSignupUrl( $domain = null, $appname = null ) { return "http://recaptcha.net/api/getkey?" . $this->_encodeQueryString( array( 'domain' => $domain, 'app' => $appname ) ); } /** * Submits an HTTP POST to a reCAPTCHA server * * @access private * @param string $host * @param string $path * @param array $data * @param int port * @return array response */ private function _recaptchaPost( $host, $path, $data, $port = 80 ) { $req = $this->_encodeQueryString( $data ); $http_request = "POST $path HTTP/1.0\r\n"; $http_request .= "Host: $host\r\n"; $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n"; $http_request .= "Content-Length: " . strlen($req) . "\r\n"; $http_request .= "User-Agent: reCAPTCHA/PHP\r\n"; $http_request .= "\r\n"; $http_request .= $req; $response = ''; if( false == ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) { return false; } fwrite($fs, $http_request); while ( !feof($fs) ) { $response .= fgets($fs, 1160); // One TCP-IP packet } fclose($fs); $response = explode( "\r\n\r\n", $response, 2 ); return $response; } /** * Encode array of data into a query string * * @access private * @param array $data * @return string query string */ private function _encodeQueryString( $data ) { $req = ""; foreach ( $data as $key => $value ) { $req .= $key . '=' . urlencode( stripslashes($value) ) . '&'; } // Cut the last '&' $req = substr( $req, 0, strlen($req)-1 ); return $req; } }