validate(); if ($valid_key) { $valid_key = recaptcha::is_recaptcha_valid($form, $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 ($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->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])); } } }