From 3f5ad7d77a4b877220cd410afbb286dce5b15b75 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Fri, 29 Jan 2010 11:20:35 -0800 Subject: Clean up form validation code. --- modules/recaptcha/controllers/admin_recaptcha.php | 2 +- modules/recaptcha/helpers/recaptcha.php | 40 ++++++++++++++--------- 2 files changed, 25 insertions(+), 17 deletions(-) (limited to 'modules/recaptcha') diff --git a/modules/recaptcha/controllers/admin_recaptcha.php b/modules/recaptcha/controllers/admin_recaptcha.php index 6874fce9..0ddb98c1 100644 --- a/modules/recaptcha/controllers/admin_recaptcha.php +++ b/modules/recaptcha/controllers/admin_recaptcha.php @@ -42,7 +42,7 @@ class Admin_Recaptcha_Controller extends Admin_Controller { } else { module::set_var("recaptcha", "public_key", ""); module::set_var("recaptcha", "private_key", ""); - message::success(t("reCAPTCHA disabled!")); + message::success(t("No keys provided. reCAPTCHA is disabled!")); log::success("recaptcha", t("reCAPTCHA public and private keys cleared")); url::redirect("admin/recaptcha"); } diff --git a/modules/recaptcha/helpers/recaptcha.php b/modules/recaptcha/helpers/recaptcha.php index 789bae85..2b3b869c 100644 --- a/modules/recaptcha/helpers/recaptcha.php +++ b/modules/recaptcha/helpers/recaptcha.php @@ -24,12 +24,16 @@ class recaptcha_Core { ->label(t("Configure reCAPTCHA")); $group->input("public_key") ->label(t("Public Key")) - ->value(module::get_var("recaptcha", "public_key")); - $group->public_key->error_messages("invalid", t("The public key you provided is invalid.")); + ->value(module::get_var("recaptcha", "public_key")) + ->rules("required") + ->error_messages("required", t("You must enter a public key")) + ->error_messages("invalid", t("This public key is invalid")); $group->input("private_key") ->label(t("Private Key")) - ->value(module::get_var("recaptcha", "private_key")); - $group->private_key->error_messages("invalid", t("The private key you provided is invalid.")); + ->value(module::get_var("recaptcha", "private_key")) + ->callback("recaptcha::verify_key") + ->error_messages("required", t("You must enter a private key")) + ->error_messages("invalid", t("This private key is invalid")); $group->submit("")->value(t("Save")); $site_domain = urlencode(stripslashes($_SERVER["HTTP_HOST"])); @@ -55,19 +59,23 @@ class recaptcha_Core { * @param string $private_key * @return boolean */ - static function verify_key($private_key) { + static function verify_key($private_key_input) { + if (empty($private_key_input->value)) { + $private_key_input->add_error("required", 1); + return; + } + $remote_ip = Input::instance()->server("REMOTE_ADDR"); $response = self::_http_post("api-verify.recaptcha.net", "/verify", - array("privatekey" => $private_key, + array("privatekey" => $private_key_input->value, "remoteip" => $remote_ip, "challenge" => "right", "response" => "wrong")); - $answers = explode("\n", $response[1]); - if (trim($answers[0]) == "true") { - return null; - } else { - return $answers[1]; + if ($response[1] == "false\ninvalid-site-private-key") { + // This is the only thing I can figure out how to verify. + // See http://recaptcha.net/apidocs/captcha for possible return values + $private_key_input->add_error("invalid", 1); } } @@ -80,16 +88,16 @@ class recaptcha_Core { $input = Input::instance(); $remote_ip = $input->server("REMOTE_ADDR"); - //discard spam submissions + // discard spam submissions if (empty($challenge) || empty($response)) { return "incorrect-captcha-sol"; } $response = self::_http_post("api-verify.recaptcha.net", "/verify", - array ("privatekey" => $private_key, - "remoteip" => $remote_ip, - "challenge" => $challenge, - "response" => $response)); + array("privatekey" => $private_key, + "remoteip" => $remote_ip, + "challenge" => $challenge, + "response" => $response)); $answers = explode ("\n", $response [1]); if (trim ($answers [0]) == "true") { -- cgit v1.2.3