1 31 32 package org.opencms.frontend.templateone.form; 33 34 import org.opencms.i18n.CmsEncoder; 35 import org.opencms.i18n.CmsMessages; 36 import org.opencms.jsp.CmsJspActionElement; 37 import org.opencms.mail.CmsHtmlMail; 38 import org.opencms.mail.CmsSimpleMail; 39 import org.opencms.main.CmsLog; 40 import org.opencms.util.CmsStringUtil; 41 42 import java.util.ArrayList ; 43 import java.util.Collections ; 44 import java.util.HashMap ; 45 import java.util.Iterator ; 46 import java.util.List ; 47 import java.util.Map ; 48 import java.util.StringTokenizer ; 49 50 import javax.mail.internet.AddressException ; 51 import javax.mail.internet.InternetAddress ; 52 import javax.servlet.http.HttpServletRequest ; 53 import javax.servlet.http.HttpServletResponse ; 54 import javax.servlet.jsp.PageContext ; 55 56 import org.apache.commons.logging.Log; 57 58 72 public class CmsFormHandler extends CmsJspActionElement { 73 74 75 public static final String ACTION_CONFIRMED = "confirmed"; 76 77 78 public static final String ACTION_CORRECT_INPUT = "correct"; 79 80 81 public static final String ACTION_SUBMIT = "submit"; 82 83 84 public static final String ERROR_MANDATORY = "mandatory"; 85 86 87 public static final String ERROR_VALIDATION = "validation"; 88 89 90 public static final String PARAM_FORMACTION = "formaction"; 91 92 93 private static final Log LOG = CmsLog.getLog(CmsFormHandler.class); 94 95 96 private Map m_errors; 97 98 99 private CmsForm m_formConfiguration; 100 101 102 private String m_hiddenFields; 103 104 105 private boolean m_initial; 106 107 108 private CmsMessages m_messages; 109 110 private Boolean m_isValidatedCorrect; 111 112 120 public CmsFormHandler(PageContext context, HttpServletRequest req, HttpServletResponse res) 121 throws Exception { 122 123 super(context, req, res); 124 init(req, null); 125 } 126 127 136 public CmsFormHandler(PageContext context, HttpServletRequest req, HttpServletResponse res, String formConfigUri) 137 throws Exception { 138 139 super(context, req, res); 140 init(req, formConfigUri); 141 } 142 143 149 public String convertToHtmlValue(String value) { 150 151 return convertValue(value, "html"); 152 } 153 154 160 public String convertToPlainValue(String value) { 161 162 return convertValue(value, ""); 163 } 164 165 178 public String convertValue(String value, String outputType) { 179 180 if ("html".equalsIgnoreCase(outputType)) { 181 value = CmsStringUtil.escapeHtml(value); 183 } else { 184 value = CmsStringUtil.substitute(value, "<br>", "\n"); 186 } 187 return value; 188 } 189 190 195 public String createHiddenFields() { 196 197 if (CmsStringUtil.isEmpty(m_hiddenFields)) { 198 List fields = getFormConfiguration().getFields(); 199 StringBuffer result = new StringBuffer (fields.size() * 8); 200 for (int i = 0, n = fields.size(); i < n; i++) { 202 203 I_CmsField currentField = (I_CmsField)fields.get(i); 204 205 if (currentField == null) { 206 continue; 207 } else if (CmsCheckboxField.class.isAssignableFrom(currentField.getClass())) { 208 Iterator k = currentField.getItems().iterator(); 210 while (k.hasNext()) { 211 CmsFieldItem item = (CmsFieldItem)k.next(); 212 if (item.isSelected()) { 213 result.append("<input type=\"hidden\" name=\""); 214 result.append(currentField.getName()); 215 result.append("\" value=\""); 216 result.append(CmsEncoder.escapeXml(item.getValue())); 217 result.append("\">\n"); 218 } 219 } 220 } else if (CmsStringUtil.isNotEmpty(currentField.getValue())) { 221 result.append("<input type=\"hidden\" name=\""); 223 result.append(currentField.getName()); 224 result.append("\" value=\""); 225 result.append(CmsEncoder.escapeXml(currentField.getValue())); 226 result.append("\">\n"); 227 } 228 229 } 230 m_hiddenFields = result.toString(); 232 } 233 return m_hiddenFields; 235 } 236 237 242 public Map getErrors() { 243 244 return m_errors; 245 } 246 247 252 public CmsForm getFormConfiguration() { 253 254 return m_formConfiguration; 255 } 256 257 262 public CmsMessages getMessages() { 263 264 return m_messages; 265 } 266 267 272 public boolean hasValidationErrors() { 273 274 return (!isInitial() && getErrors().size() > 0); 275 } 276 277 284 public void init(HttpServletRequest req, String formConfigUri) throws Exception { 285 286 String formAction = req.getParameter(PARAM_FORMACTION); 287 288 setErrors(new HashMap ()); 289 m_isValidatedCorrect = null; 290 setInitial(CmsStringUtil.isEmpty(formAction)); 291 setMessages(new CmsMessages("/org/opencms/frontend/templateone/form/workplace", getRequestContext().getLocale())); 293 setFormConfiguration(new CmsForm(this, getMessages(), isInitial(), formConfigUri, formAction)); 295 } 296 297 302 public boolean isInitial() { 303 304 return m_initial; 305 } 306 307 312 public void sendConfirmationMail() throws Exception { 313 314 I_CmsField mailField = (I_CmsField)getFormConfiguration().getFields().get( 316 getFormConfiguration().getConfirmationMailField()); 317 String mailTo = mailField.getValue(); 318 319 if (getFormConfiguration().getMailType().equals(CmsForm.MAILTYPE_HTML)) { 321 CmsHtmlMail theMail = new CmsHtmlMail(); 323 theMail.setCharset(getCmsObject().getRequestContext().getEncoding()); 324 if (CmsStringUtil.isNotEmpty(getFormConfiguration().getMailFrom())) { 325 theMail.setFrom(getFormConfiguration().getMailFrom()); 326 } 327 theMail.setTo(createInternetAddresses(mailTo)); 328 theMail.setSubject(getFormConfiguration().getMailSubjectPrefix() 329 + getFormConfiguration().getConfirmationMailSubject()); 330 theMail.setHtmlMsg(createMailTextFromFields(true, true)); 331 theMail.setTextMsg(createMailTextFromFields(false, true)); 332 theMail.send(); 334 } else { 335 CmsSimpleMail theMail = new CmsSimpleMail(); 337 theMail.setCharset(getCmsObject().getRequestContext().getEncoding()); 338 if (CmsStringUtil.isNotEmpty(getFormConfiguration().getMailFrom())) { 339 theMail.setFrom(getFormConfiguration().getMailFrom()); 340 } 341 theMail.setTo(createInternetAddresses(mailTo)); 342 theMail.setSubject(getFormConfiguration().getMailSubjectPrefix() 343 + getFormConfiguration().getConfirmationMailSubject()); 344 theMail.setMsg(createMailTextFromFields(false, true)); 345 theMail.send(); 347 } 348 } 349 350 357 public boolean sendMail() { 358 359 try { 360 if (getFormConfiguration().isConfirmationMailEnabled()) { 362 if (!getFormConfiguration().isConfirmationMailOptional() 363 || Boolean.valueOf(getRequest().getParameter(CmsForm.PARAM_SENDCONFIRMATION)).booleanValue()) { 364 sendConfirmationMail(); 365 } 366 } 367 if (getFormConfiguration().getMailType().equals(CmsForm.MAILTYPE_HTML)) { 369 CmsHtmlMail theMail = new CmsHtmlMail(); 371 theMail.setCharset(getCmsObject().getRequestContext().getEncoding()); 372 if (CmsStringUtil.isNotEmpty(getFormConfiguration().getMailFrom())) { 373 theMail.setFrom(getFormConfiguration().getMailFrom()); 374 } 375 theMail.setTo(createInternetAddresses(getFormConfiguration().getMailTo())); 376 theMail.setCc(createInternetAddresses(getFormConfiguration().getMailCC())); 377 theMail.setBcc(createInternetAddresses(getFormConfiguration().getMailBCC())); 378 theMail.setSubject(getFormConfiguration().getMailSubjectPrefix() 379 + getFormConfiguration().getMailSubject()); 380 theMail.setHtmlMsg(createMailTextFromFields(true, false)); 381 theMail.setTextMsg(createMailTextFromFields(false, false)); 382 theMail.send(); 384 } else { 385 CmsSimpleMail theMail = new CmsSimpleMail(); 387 theMail.setCharset(getCmsObject().getRequestContext().getEncoding()); 388 if (CmsStringUtil.isNotEmpty(getFormConfiguration().getMailFrom())) { 389 theMail.setFrom(getFormConfiguration().getMailFrom()); 390 } 391 theMail.setTo(createInternetAddresses(getFormConfiguration().getMailTo())); 392 theMail.setCc(createInternetAddresses(getFormConfiguration().getMailCC())); 393 theMail.setBcc(createInternetAddresses(getFormConfiguration().getMailBCC())); 394 theMail.setSubject(getFormConfiguration().getMailSubjectPrefix() 395 + getFormConfiguration().getMailSubject()); 396 theMail.setMsg(createMailTextFromFields(false, false)); 397 theMail.send(); 399 } 400 } catch (Exception e) { 401 if (LOG.isErrorEnabled()) { 403 LOG.error(e); 404 } 405 m_errors.put("sendmail", e.getMessage()); 406 return false; 407 } 408 return true; 409 } 410 411 416 public boolean showCheck() { 417 418 boolean result = false; 419 420 if (getFormConfiguration().getShowCheck() && ACTION_SUBMIT.equals(getRequest().getParameter(PARAM_FORMACTION))) { 421 result = true; 422 } else if (getFormConfiguration().captchaFieldIsOnCheckPage() && ACTION_CONFIRMED.equals(getRequest().getParameter(PARAM_FORMACTION)) && !validate()) { 423 result = true; 424 } 425 426 return result; 427 } 428 429 434 public boolean showForm() { 435 436 boolean result = false; 437 438 if (isInitial()) { 439 result = true; 441 } else if (ACTION_CORRECT_INPUT.equalsIgnoreCase(getRequest().getParameter(PARAM_FORMACTION))) { 442 result = true; 444 } else if (ACTION_SUBMIT.equalsIgnoreCase(getRequest().getParameter(PARAM_FORMACTION)) && !validate()) { 445 result = true; 447 448 if (getFormConfiguration().hasCaptchaField() && getFormConfiguration().captchaFieldIsOnCheckPage()) { 449 getFormConfiguration().removeCaptchaField(); 454 } 455 } else if (ACTION_CONFIRMED.equalsIgnoreCase(getRequest().getParameter(PARAM_FORMACTION)) && getFormConfiguration().captchaFieldIsOnCheckPage() && !validate()) { 456 result = false; 458 } 459 460 return result; 461 } 462 463 471 public boolean validate() { 472 473 if (m_isValidatedCorrect != null) { 474 return m_isValidatedCorrect.booleanValue(); 475 } 476 477 boolean allOk = true; 478 List fields = getFormConfiguration().getFields(); 480 481 for (int i = 0, n = fields.size(); i < n; i++) { 483 484 I_CmsField currentField = (I_CmsField)fields.get(i); 485 486 if (currentField == null) { 487 continue; 488 } 489 490 if (CmsCaptchaField.class.isAssignableFrom(currentField.getClass())) { 491 continue; 493 } 494 495 String validationError = currentField.validate(this); 496 if (CmsStringUtil.isNotEmpty(validationError)) { 497 getErrors().put(currentField.getName(), validationError); 498 allOk = false; 499 } 500 } 501 502 CmsCaptchaField captchaField = m_formConfiguration.getCaptchaField(); 503 if (captchaField != null) { 504 505 boolean captchaFieldIsOnInputPage = getFormConfiguration().captchaFieldIsOnInputPage() && getFormConfiguration().isInputFormSubmitted(); 506 boolean captchaFieldIsOnCheckPage = getFormConfiguration().captchaFieldIsOnCheckPage() && getFormConfiguration().isCheckPageSubmitted(); 507 508 if (captchaFieldIsOnInputPage || captchaFieldIsOnCheckPage) { 509 if (!captchaField.validateCaptchaPhrase(this, captchaField.getValue())) { 510 getErrors().put(captchaField.getName(), ERROR_VALIDATION); 511 allOk = false; 512 } 513 } 514 } 515 516 m_isValidatedCorrect = new Boolean (allOk); 517 return allOk; 518 } 519 520 527 protected List createInternetAddresses(String mailAddresses) throws AddressException { 528 529 if (CmsStringUtil.isNotEmpty(mailAddresses)) { 530 StringTokenizer T = new StringTokenizer (mailAddresses, ";"); 532 List addresses = new ArrayList (T.countTokens()); 533 while (T.hasMoreTokens()) { 534 InternetAddress address = new InternetAddress (T.nextToken()); 535 addresses.add(address); 536 } 537 return addresses; 538 } else { 539 return Collections.EMPTY_LIST; 541 } 542 } 543 544 551 protected String createMailTextFromFields(boolean isHtmlMail, boolean isConfirmationMail) { 552 553 List fieldValues = getFormConfiguration().getFields(); 554 StringBuffer result = new StringBuffer (fieldValues.size() * 8); 555 if (isHtmlMail) { 556 result.append("<html><head>\n"); 558 result.append("<style type=\"text/css\"><!--\n"); 559 String style = getMessages().key("form.email.style.body"); 560 if (CmsStringUtil.isNotEmpty(style)) { 561 result.append("body,h1,p,td { "); 562 result.append(style); 563 result.append(" }\n"); 564 } 565 style = getMessages().key("form.email.style.h1"); 566 if (CmsStringUtil.isNotEmpty(style)) { 567 result.append("h1 { "); 568 result.append(style); 569 result.append(" }\n"); 570 } 571 style = getMessages().key("form.email.style.p"); 572 if (CmsStringUtil.isNotEmpty(style)) { 573 result.append("p { "); 574 result.append(style); 575 result.append(" }\n"); 576 } 577 style = getMessages().key("form.email.style.fields"); 578 if (CmsStringUtil.isNotEmpty(style)) { 579 result.append("table.fields { "); 580 result.append(style); 581 result.append(" }\n"); 582 } 583 style = getMessages().key("form.email.style.fieldlabel"); 584 if (CmsStringUtil.isNotEmpty(style)) { 585 result.append("td.fieldlabel { "); 586 result.append(style); 587 result.append(" }\n"); 588 } 589 style = getMessages().key("form.email.style.fieldvalue"); 590 if (CmsStringUtil.isNotEmpty(style)) { 591 result.append("td.fieldvalue { "); 592 result.append(style); 593 result.append(" }\n"); 594 } 595 style = getMessages().key("form.email.style.misc"); 596 if (CmsStringUtil.isNotEmpty(style)) { 597 result.append(getMessages().key("form.email.style.misc")); 598 } 599 result.append("//--></style>\n"); 600 result.append("</head><body>\n"); 601 if (isConfirmationMail) { 602 result.append(getFormConfiguration().getConfirmationMailText()); 604 } else { 605 result.append(getFormConfiguration().getMailText()); 607 } 608 result.append("<table border=\"0\" class=\"fields\">\n"); 609 } else { 610 if (isConfirmationMail) { 612 result.append(getFormConfiguration().getConfirmationMailTextPlain()); 614 } else { 615 result.append(getFormConfiguration().getMailTextPlain()); 617 } 618 result.append("\n\n"); 619 } 620 Iterator i = fieldValues.iterator(); 622 while (i.hasNext()) { 623 I_CmsField current = (I_CmsField)i.next(); 624 if (isHtmlMail) { 625 result.append("<tr><td class=\"fieldlabel\">"); 627 result.append(current.getLabel()); 628 result.append("</td><td class=\"fieldvalue\">"); 629 result.append(convertToHtmlValue(current.toString())); 630 result.append("</td></tr>\n"); 631 } else { 632 result.append(current.getLabel()); 634 result.append("\t"); 635 result.append(current.toString()); 636 result.append("\n"); 637 } 638 } 639 if (isHtmlMail) { 640 result.append("</table>\n"); 642 if (!isConfirmationMail && getFormConfiguration().hasConfigurationErrors()) { 643 result.append("<h1>"); 645 result.append(getMessages().key("form.configuration.error.headline")); 646 result.append("</h1>\n<p>"); 647 for (int k = 0; k < getFormConfiguration().getConfigurationErrors().size(); k++) { 648 result.append(getFormConfiguration().getConfigurationErrors().get(k)); 649 result.append("<br>"); 650 } 651 result.append("</p>\n"); 652 } 653 result.append("</body></html>"); 655 } else if (!isConfirmationMail && getFormConfiguration().hasConfigurationErrors()) { 656 result.append("\n"); 658 result.append(getMessages().key("form.configuration.error.headline")); 659 result.append("\n"); 660 for (int k = 0; k < getFormConfiguration().getConfigurationErrors().size(); k++) { 661 result.append(getFormConfiguration().getConfigurationErrors().get(k)); 662 result.append("\n"); 663 } 664 } 665 666 return result.toString(); 667 } 668 669 674 protected void setErrors(Map errors) { 675 676 m_errors = errors; 677 } 678 679 684 protected void setFormConfiguration(CmsForm configuration) { 685 686 m_formConfiguration = configuration; 687 } 688 689 693 protected void setInitial(boolean initial) { 694 695 m_initial = initial; 696 } 697 698 703 protected void setMessages(CmsMessages messages) { 704 705 m_messages = messages; 706 } 707 708 } 709 | Popular Tags |