Simplify the recaptcha admin page. It's not guaranteed to verify your

recaptcha private key properly anymore, but it's more intuitive to
configure in the admin UI.
This commit is contained in:
Bharat Mediratta
2009-01-28 08:15:56 +00:00
parent 66fae63558
commit 6220db47b3
4 changed files with 73 additions and 139 deletions

View File

@@ -20,90 +20,45 @@
class Admin_Recaptcha_Controller extends Admin_Controller {
public function index() {
$form = recaptcha::get_configure_form();
if (request::method() == "post") {
$old_public_key = module::get_var("recaptcha", "public_key");
$old_private_key = module::get_var("recaptcha", "private_key");
if (request::method() == "post") {
if ($form->validate()) {
$public_key = $form->configure_recaptcha->public_key->value;
$private_key = $form->configure_recaptcha->private_key->value;
$valid_key = $form->validate();
if ($valid_key) {
$input = Input::instance();
$challenge = $input->post("recaptcha_challenge_field", "", true);
$response = $input->post("recaptcha_response_field", "", true);
$valid_key = recaptcha::is_recaptcha_valid($challenge, $response,
$form->configure_recaptcha->private_key->value);
if (empty($valid_key) && $form->captcha_error == "invalid-site-private-key") {
$form->configure_recaptcha->private_key->add_error("invalid", 1);
unset($form->captcha_error);
if ($public_key && $private_key) {
module::set_var("recaptcha", "public_key", $public_key);
module::set_var("recaptcha", "private_key", $private_key);
message::success(t("Recaptcha configured!"));
log::success(t("Recaptcha public and private keys set"));
url::redirect("admin/recaptcha");
} else if ($public_key && !$private_key) {
$form->configure_recaptcha->private_key->add_error("invalid");
} else if ($private_key && !$public_key) {
$form->configure_recaptcha->public_key->add_error("invalid");
} else {
module::set_var("recaptcha", "public_key", "");
module::set_var("recaptcha", "private_key", "");
message::success(t("Recaptcha disabled!"));
log::success(t("Recaptcha public and private keys cleared"));
url::redirect("admin/recaptcha");
}
}
if ($valid_key) {
$new_public_key = $form->configure_recaptcha->public_key->value;
$new_private_key = $form->configure_recaptcha->private_key->value;
$update = $this->_update_key("public_key", $old_public_key, $new_public_key);
$update |= $this->_update_key("private_key", $old_private_key, $new_private_key);
if ($update) {
message::success(t("Recaptcha Configured"));
}
recaptcha::check_config();
}
} else {
$valid_key = !empty($old_public_key) && !empty($old_private_key);
}
recaptcha::check_config();
$view = new Admin_View("admin.html");
$view->content = new View("admin_recaptcha.html");
$view->content->valid_key = $valid_key;
$view->content->public_key = module::get_var("recaptcha", "public_key");
$view->content->private_key = module::get_var("recaptcha", "private_key");
$view->content->form = $form;
print $view;
}
private function _update_key($type, $old_key, $new_key) {
$changed = true;
if ($old_key && !$new_key) {
log::success(sprintf(t("Your Recaptcha %s has been cleared."), strtr($type, "_", " ")));
} else if ($old_key && $new_key && $old_key != $new_key) {
log::success(sprintf(t("Your Recaptcha %s has been changed."), strtr($type, "_", " ")));
} else if (!$old_key && $new_key) {
log::success(sprintf(t("Your Recaptcha %s has been saved."), strtr($type, "_", " ")));
} else {
$changed = false;
}
if ($changed) {
module::set_var("recaptcha", $type, $new_key);
}
return $changed;
}
public function gethtml($public_key, $error=null) {
$http_request = "GET /challenge?k=$public_key HTTP/1.0\r\n";
$response = "";
if( false == ( $fs = @fsockopen("api.recaptcha.net", 80, $errno, $errstr, 10) ) ) {
throw new Exception("@todo COULD NOT OPEN SOCKET");
}
$errorpart = empty($error) ? "" : "&error=$error";
fputs($fs, "GET /challenge?k=$public_key&ajax=1$errorpart HTTP/1.0\r\n");
fputs($fs, "Host: api.recaptcha.net\r\n");
fputs($fs, "Connection: Close\r\n\r\n");
while (!feof($fs)) {
$response .= fgets($fs, 1160); // One TCP-IP packet
}
fclose($fs);
$response = explode("\r\n\r\n", $response, 2);
if (strpos($response[1], "document.write") === 0) {
header("HTTP/1.1 400 BAD REQUEST");
if (preg_match("#.*\'(.*)\'#", $response[1], $matches)) {
$msg = $matches[1];
} else {
$msg = _t("Unable to determine error message");
}
print $msg;
} else {
header("HTTP/1.1 200 OK");
print json_encode(array("result" => "success", "script" => $response[1]));
}
public function test() {
$view = new View("admin_recaptcha_test.html");
$view->public_key = module::get_var("recaptcha", "public_key");
print $view;
}
}