Пример #1
0
  public void takeValuesFromRequest(WORequest request, WOContext context) {
    if (context._wasFormSubmitted()) {
      super.takeValuesFromRequest(request, context);

      String apiKey = ERXProperties.stringForKey("er.captcha.akismet.apiKey");
      String url =
          ERXProperties.stringForKeyWithDefault(
              "er.captcha.akismet.url", "http://" + request._serverName());
      Akismet api = new Akismet(apiKey, url);
      if (ERXApplication.isDevelopmentModeSafe()) {
        if (!api.verifyAPIKey()) {
          throw new RuntimeException(
              "The API key you provided is invalid. Please set a valid api key in the property 'er.captcha.akismet.apiKey'.");
        }
      }

      String ipAddress = stringValueForBinding("remoteAddress", request._remoteAddress());
      String userAgent = stringValueForBinding("userAgent", request.headerForKey("user-agent"));
      String referrer = stringValueForBinding("referrer", request.headerForKey("referer"));
      String permalink = stringValueForBinding("permalink");
      String commentType = stringValueForBinding("commentType");
      String author = stringValueForBinding("author");
      String authorEmail = stringValueForBinding("authorEmail");
      String authorURL = stringValueForBinding("authorURL");
      String content = stringValueForBinding("content");
      Map other = null;

      boolean isSpam =
          api.commentCheck(
              ipAddress,
              userAgent,
              referrer,
              permalink,
              commentType,
              author,
              authorEmail,
              authorURL,
              content,
              other);
      if (isSpam) {
        validationFailedWithException(
            new NSValidation.ValidationException("Spam check failed."),
            this,
            ERXSimpleSpamCheck.SPAM_CHECK_KEY);
        setValueForBinding(Boolean.FALSE, "valid");
      } else {
        setValueForBinding(Boolean.TRUE, "valid");
      }
    }
  }