Skip to content

Instantly share code, notes, and snippets.

@cfalzone
Last active August 29, 2015 14:11
Show Gist options
  • Save cfalzone/30f823ca581c57da4178 to your computer and use it in GitHub Desktop.
Save cfalzone/30f823ca581c57da4178 to your computer and use it in GitHub Desktop.
dotCMS Contentlet-Based Spam Prevention
// HoneyPot Protection - if this field is filled in then this is probably a bot
if(UtilMethods.isSet(honeypot)) {
response.sendRedirect(errorPage);
Logger.error(this, "Honey Pot detected, ignoring the request. Params: "+params);
return;
}
// Some Simple Spam Filtering
if(firstName == null) firstName = "";
if(lastName == null) lastName = "";
if(firstName.equalsIgnoreCase(lastName) || (!UtilMethods.isSet(agentId))) {
Logger.error(this, "Case Considered spam, ignoring the request. Params: "+params);
response.sendRedirect(errorPage);
return;
}
// Check Spam Filter Rules
List<Contentlet> rules = new ArrayList<Contentlet>();
try {
rules = conAPI.search("+structureName:SpamFilters +tool:leadForm +live:true +deleted:false", 1000, 0, "modDate desc", sysUser, false);
} catch (Exception e) {
Logger.error(this, "Unable to get spam filters", e);
}
boolean isSpam = false;
String ruleFailed = "";
for(Contentlet rule : rules) {
String comparator = rule.getStringProperty("comparator").trim();
String targetValue = rule.getStringProperty("string").trim();
String fieldsval = rule.getStringProperty("applyToFields").trim();
List<String> fields = new ArrayList<String>();
if(fieldsval != null && fieldsval.length() > 0) {
if(fieldsval.contains(",")) fields.add(fieldsval);
else for(String v : fieldsval.split(",")) fields.add(v.trim());
} else {
Logger.error(this, "SpamFilter with id="+rule.getIdentifier()+" has no selected fields.");
break;
}
for(String field : fields) {
String valueToCompare = "";
if(field.equalsIgnoreCase("firstName")) valueToCompare = UtilMethods.isSet(firstName) ? firstName : "";
else if(field.equalsIgnoreCase("lastName")) valueToCompare = UtilMethods.isSet(lastName) ? lastName : "";
else if(field.equalsIgnoreCase("email")) valueToCompare = UtilMethods.isSet(email) ? email : "";
else if(field.equalsIgnoreCase("phone")) valueToCompare = UtilMethods.isSet(phoneNumber) ? phoneNumber : "";
else if(field.equalsIgnoreCase("company")) valueToCompare = UtilMethods.isSet(company) ? company : "";
else if(field.equalsIgnoreCase("phoneNumberSanitized")) {
String pns = phoneNumber.replaceAll("[^0-9]","");
valueToCompare = UtilMethods.isSet(pns) ? pns : "";
} else {
Logger.error(this, "SpamFilter with id="+rule.getIdentifier()+" has an unhandled field.");
break;
}
if(comparator.equalsIgnoreCase("equals")) {
if(!UtilMethods.isSet(targetValue) || targetValue.equalsIgnoreCase("null")) {
if(!UtilMethods.isSet(valueToCompare)) isSpam = true;
} else {
if(valueToCompare.equalsIgnoreCase(targetValue)) isSpam = true;
}
} else if(comparator.equalsIgnoreCase("contains")) {
if(valueToCompare.contains(targetValue)) isSpam = true;
} else if(comparator.equalsIgnoreCase("notcontains")) {
if(! valueToCompare.contains(targetValue)) isSpam = true;
} else if(comparator.equalsIgnoreCase("starts")) {
if(valueToCompare.toLowerCase().startsWith(targetValue.toLowerCase())) isSpam = true;
} else if(comparator.equalsIgnoreCase("ends")) {
if(valueToCompare.toLowerCase().endsWith(targetValue.toLowerCase())) isSpam = true;
} else if(comparator.equalsIgnoreCase("regex")) {
if(valueToCompare.matches(targetValue)) isSpam = true;
} else {
Logger.error(this, "SpamFilter with id="+rule.getIdentifier()+" has an unhandled comparator.");
break;
}
if(isSpam) break;
}
if(isSpam) {
ruleFailed = rule.getIdentifier();
break;
}
}
if(isSpam) {
Logger.error(this, "Failed SpamFilter with id="+ruleFailed+", ignoring the request. Params: "+params);
response.sendRedirect(errorPage);
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment