Permalink
Browse files

Clean up form validation code.

  • Loading branch information...
1 parent c214dfd commit 3f5ad7d77a4b877220cd410afbb286dce5b15b75 @bharat bharat committed Jan 29, 2010
Showing with 25 additions and 17 deletions.
  1. +1 −1 modules/recaptcha/controllers/admin_recaptcha.php
  2. +24 −16 modules/recaptcha/helpers/recaptcha.php
@@ -42,7 +42,7 @@ public function index() {
} 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");
}
@@ -24,12 +24,16 @@ static function get_configure_form() {
->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 @@ static function check_config() {
* @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 @@ static function is_recaptcha_valid($challenge, $response, $private_key) {
$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") {

0 comments on commit 3f5ad7d

Please sign in to comment.