diff options
author | Tim Almdal <tnalmdal@shaw.ca> | 2009-01-26 16:12:57 +0000 |
---|---|---|
committer | Tim Almdal <tnalmdal@shaw.ca> | 2009-01-26 16:12:57 +0000 |
commit | bfb5c42124db39605c64f08837eb667be04979cc (patch) | |
tree | 99581379399368a1ed7d53176a904fdc12b14c69 /modules/recaptcha/helpers/recaptcha.php | |
parent | 57f5cdeb300448e1b5d811d1d319353d61698597 (diff) |
Adding Recaptcha to the comment module. Recaptcha integration consists of a Form_Recaptcha class derived from Form_Input that can be added to any class that requires Recaptcha verfication.
Diffstat (limited to 'modules/recaptcha/helpers/recaptcha.php')
-rw-r--r-- | modules/recaptcha/helpers/recaptcha.php | 59 |
1 files changed, 14 insertions, 45 deletions
diff --git a/modules/recaptcha/helpers/recaptcha.php b/modules/recaptcha/helpers/recaptcha.php index 1d40eb09..662b98d7 100644 --- a/modules/recaptcha/helpers/recaptcha.php +++ b/modules/recaptcha/helpers/recaptcha.php @@ -18,18 +18,6 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class recaptcha_Core { - /** - * The reCAPTCHA server URL"s - */ - const API_SERVER = "http://api.recaptcha.net"; - const API_SECURE_SERVER = "https://api-secure.recaptcha.net"; - const VERIFY_SERVER = "api-verify.recaptcha.net"; - - /** - * RecaptchaOptions - */ - private $options = array(); - static function get_configure_form() { $form = new Forge("admin/recaptcha", "", "post", array("id" => "gConfigureRecaptchaForm")); $group = $form->group("configure_recaptcha") @@ -49,8 +37,6 @@ class recaptcha_Core { $group->submit("")->value(t("Save")); $site_domain = urlencode(stripslashes($_SERVER["HTTP_HOST"])); - $form->recaptcha_site = self::API_SERVER; - $form->recaptcha_ssl_site = self::API_SECURE_SERVER; $form->get_key_url = "http://recaptcha.net/api/getkey?domain=$site_domain&app=Gallery3"; return $form; } @@ -72,63 +58,46 @@ class recaptcha_Core { * 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. - * @param string $pubkey The public key to use in the challenge * @param string $error The error given by reCAPTCHA (optional, default is null) - * @param boolean $use_ssl Should the request be made over ssl? (optional, default is false) - * @param string $lang Any supported language code + * @param string $pubkey The public key to use in the challenge (optional, default is null) * @return string - The HTML to be embedded in the user"s form. */ - static function get_challenge_html($pubkey, $error = NULL, $use_ssl = false) { - if (empty($pubkey)) { - throw new Exception("@todo NEED KEY <a href=\"http://recaptcha.net/api/getkey\">" . - "http://recaptcha.net/api/getkey</a>"); - } - - $lang = Kohana::config("locale.root_locale"); - $server = $use_ssl ? self::API_SECURE_SERVER : self::API_SERVER; - $errorpart = ""; - if ($error) { - $errorpart = "&error=". $error; - } - return (count(self::$options) > 0 ? "<script type=\"text/javascript\">" . - "var RecaptchaOptions = {lang:'$lang'};</script>" : "") . - "<script type=\"text/javascript\" src=\"$server/challenge?k=" . - "{$pubkey}$errorpart \"></script>"; + static function get_challenge_html($id, $error=null, $public_key=null ) { } /** * Form validation call back for captcha validation * @param string $form - * @return true if valid, false if not + * @return string error message or null */ - static function is_recaptcha_valid($form, $private_key=null) { + static function is_recaptcha_valid($challenge, $response, $private_key=null) { + if (!module::installed("recaptcha")) { + return null; + } $input = Input::instance(); if (empty($private_key)) { $private_key = module::get_var("recaptcha", "private_key"); } - $remoteip = $_SERVER["REMOTE_ADDR"] ; - $challenge = $input->post("recaptcha_challenge_field", "", true); - $response = $input->post("recaptcha_response_field", "", true); + $remoteip = $input->server("REMOTE_ADDR"); //discard spam submissions if (empty($challenge) || empty($response)) { - $form->captcha_error = "incorrect-captcha-sol"; - return false; + return "incorrect-captcha-sol"; } - $response = self::_http_post(self::VERIFY_SERVER, "/verify", + $response = self::_http_post("api-verify.recaptcha.net", "/verify", array ("privatekey" => $private_key, "remoteip" => $remoteip, "challenge" => $challenge, "response" => $response)); + Kohana::log("debug", print_r($response, 1)); + Kohana::log("debug", print_r(debug_backtrace(), 1)); $answers = explode ("\n", $response [1]); if (trim ($answers [0]) == "true") { - return true; + return null; } else { - $form->captcha_error = $answers[1]; - Kohana::log("debug", print_r($answers, 1)); - return false; + return $answers[1]; } } |