| 1 31 32 package org.opencms.frontend.templateone.form; 33 34 import org.opencms.file.CmsFile; 35 import org.opencms.file.CmsObject; 36 import org.opencms.i18n.CmsEncoder; 37 import org.opencms.jsp.CmsJspActionElement; 38 import org.opencms.main.CmsLog; 39 import org.opencms.util.CmsStringUtil; 40 import org.opencms.xml.content.CmsXmlContent; 41 import org.opencms.xml.content.CmsXmlContentFactory; 42 43 import java.awt.Color ; 44 import java.util.Locale ; 45 46 import javax.servlet.http.HttpServletRequest ; 47 48 import org.apache.commons.logging.Log; 49 50 60 public final class CmsCaptchaSettings implements Cloneable { 61 62 63 public static final String C_PARAM_BACKGROUND_COLOR = "bgcol"; 64 65 66 public static final String C_PARAM_CHARACTERS = "crs"; 67 68 69 public static final String C_PARAM_FILTER_AMPLITUDE = "famplit"; 70 71 72 public static final String C_PARAM_FILTER_WAVE_LENGTH = "fwavlen"; 73 74 75 public static final String C_PARAM_FONT_COLOR = "fcol"; 76 77 78 public static final String C_PARAM_HOLES_PER_GLYPH = "holes"; 79 80 81 public static final String C_PARAM_IMAGE_HEIGHT = "h"; 82 83 84 public static final String C_PARAM_IMAGE_WIDTH = "w"; 85 86 87 public static final String C_PARAM_MAX_FONT_SIZE = "maxfs"; 88 89 90 public static final String C_PARAM_MAX_PHRASE_LENGTH = "maxpl"; 91 92 93 public static final String C_PARAM_MIN_FONT_SIZE = "minfs"; 94 95 96 public static final String C_PARAM_MIN_PHRASE_LENGTH = "minpl"; 97 98 99 public static final String C_PARAM_PRESET = "prst"; 100 101 102 public static final String C_PARAM_USE_BACKGROUND_IMAGE = "bgimg"; 103 104 105 public static final String NODE_CAPTCHAPRESET_BACKGROUNDCOLOR = "BackgroundColor"; 106 107 108 public static final String NODE_CAPTCHAPRESET_FILTER_AMPLITUDE = "FilterAmplitude"; 109 110 111 public static final String NODE_CAPTCHAPRESET_FILTER_WAVELENGTH = "FilterWaveLength"; 112 113 114 public static final String NODE_CAPTCHAPRESET_FONTCOLOR = "FontColor"; 115 116 117 public static final String NODE_CAPTCHAPRESET_HOLESPERGLYPH = "HolesPerGlyph"; 118 119 120 public static final String NODE_CAPTCHAPRESET_IMAGEHEIGHT = "ImageHeight"; 121 122 123 public static final String NODE_CAPTCHAPRESET_IMAGEWIDTH = "ImageWidth"; 124 125 126 public static final String NODE_CAPTCHAPRESET_MAX_FONT_SIZE = "MaxFontSize"; 127 128 129 public static final String NODE_CAPTCHAPRESET_MAX_PHRASE_LENGTH = "MaxPhraseLength"; 130 131 132 public static final String NODE_CAPTCHAPRESET_MIN_FONT_SIZE = "MinFontSize"; 133 134 135 public static final String NODE_CAPTCHAPRESET_MIN_PHRASE_LENGTH = "MinPhraseLength"; 136 137 138 private static final Log LOG = CmsLog.getLog(CmsCaptchaSettings.class); 139 140 141 private Color m_backgroundColor = Color.WHITE; 142 143 144 private String m_characterPool = "abcdefghiklmnoprstuvwxyz"; 145 146 147 private int m_filterAmplitude = 2; 148 149 150 private int m_filterWaveLength = 100; 151 152 153 private Color m_fontColor = Color.BLACK; 154 155 156 private Integer m_holesPerGlyp = new Integer (0); 157 158 159 private int m_imageHeight = 50; 160 161 162 private int m_imageWidth = 150; 163 164 165 private int m_maxFontSize = 40; 166 167 168 private int m_maxPhraseLength = 5; 169 170 171 private int m_minFontSize = 30; 172 173 174 private int m_minPhraseLength = 5; 175 176 181 private String m_presetPath = "factory defaults (classfile)"; 182 183 184 private boolean m_useBackgroundImage = true; 185 186 194 private CmsCaptchaSettings() { 195 196 } 198 199 203 private CmsCaptchaSettings(CmsJspActionElement jsp) { 204 205 init(jsp); 206 } 207 208 224 public static CmsCaptchaSettings getInstance(CmsJspActionElement jsp) { 225 226 CmsCaptchaSettings result = new CmsCaptchaSettings(jsp); 227 return (CmsCaptchaSettings)result.clone(); 228 } 229 230 236 public Color getBackgroundColor() { 237 238 return m_backgroundColor; 239 } 240 241 247 public String getBackgroundColorString() { 248 249 StringBuffer buf = new StringBuffer (); 250 251 buf.append("#"); 252 buf.append(toHexString(m_backgroundColor.getRed())); 253 buf.append(toHexString(m_backgroundColor.getGreen())); 254 buf.append(toHexString(m_backgroundColor.getBlue())); 255 256 return buf.toString(); 257 } 258 259 265 public int getFilterAmplitude() { 266 267 return m_filterAmplitude; 268 } 269 270 276 public int getFilterWaveLength() { 277 278 return m_filterWaveLength; 279 } 280 281 287 public Color getFontColor() { 288 289 return m_fontColor; 290 } 291 292 298 public String getFontColorString() { 299 300 StringBuffer buf = new StringBuffer (); 301 302 buf.append("#"); 303 buf.append(toHexString(m_fontColor.getRed())); 304 buf.append(toHexString(m_fontColor.getGreen())); 305 buf.append(toHexString(m_fontColor.getBlue())); 306 307 return buf.toString(); 308 } 309 310 315 public Integer getHolesPerGlyph() { 316 317 return m_holesPerGlyp; 318 } 319 320 326 public int getImageHeight() { 327 328 return m_imageHeight; 329 } 330 331 337 public int getImageWidth() { 338 339 return m_imageWidth; 340 } 341 342 348 public int getMaxFontSize() { 349 350 return m_maxFontSize; 351 } 352 353 359 public int getMaxPhraseLength() { 360 361 return m_maxPhraseLength; 362 } 363 364 370 public int getMinFontSize() { 371 372 return m_minFontSize; 373 } 374 375 381 public int getMinPhraseLength() { 382 383 return m_minPhraseLength; 384 } 385 386 397 public void init(CmsJspActionElement jsp) { 398 399 HttpServletRequest request = jsp.getRequest(); 400 401 String stringValue = request.getParameter(C_PARAM_IMAGE_WIDTH); 403 if (CmsStringUtil.isNotEmpty(stringValue)) { 404 m_imageWidth = Integer.parseInt(stringValue); 405 } 406 407 stringValue = request.getParameter(C_PARAM_IMAGE_HEIGHT); 409 if (CmsStringUtil.isNotEmpty(stringValue)) { 410 m_imageHeight = Integer.parseInt(stringValue); 411 } 412 413 stringValue = request.getParameter(C_PARAM_MIN_PHRASE_LENGTH); 415 if (CmsStringUtil.isNotEmpty(stringValue)) { 416 m_minPhraseLength = Integer.parseInt(stringValue); 417 } 418 419 stringValue = request.getParameter(C_PARAM_MAX_PHRASE_LENGTH); 421 if (CmsStringUtil.isNotEmpty(stringValue)) { 422 m_maxPhraseLength = Integer.parseInt(stringValue); 423 } 424 425 stringValue = request.getParameter(C_PARAM_MIN_FONT_SIZE); 427 if (CmsStringUtil.isNotEmpty(stringValue)) { 428 m_minFontSize = Integer.parseInt(stringValue); 429 } 430 431 stringValue = request.getParameter(C_PARAM_MAX_FONT_SIZE); 433 if (CmsStringUtil.isNotEmpty(stringValue)) { 434 m_maxFontSize = Integer.parseInt(stringValue); 435 } 436 437 stringValue = request.getParameter(C_PARAM_FONT_COLOR); 439 if (CmsStringUtil.isNotEmpty(stringValue)) { 440 stringValue = CmsEncoder.unescape(stringValue, jsp.getRequestContext().getEncoding()); 441 setFontColor(stringValue); 442 } 443 444 stringValue = request.getParameter(C_PARAM_BACKGROUND_COLOR); 446 if (CmsStringUtil.isNotEmpty(stringValue)) { 447 stringValue = CmsEncoder.unescape(stringValue, jsp.getRequestContext().getEncoding()); 448 } 449 setBackgroundColor(stringValue); 450 451 stringValue = request.getParameter(C_PARAM_HOLES_PER_GLYPH); 453 if (CmsStringUtil.isNotEmpty(stringValue)) { 454 setHolesPerGlyph(Integer.parseInt(stringValue)); 455 } 456 457 stringValue = request.getParameter(C_PARAM_FILTER_AMPLITUDE); 459 if (CmsStringUtil.isNotEmpty(stringValue)) { 460 setFilterAmplitude(Integer.parseInt(stringValue)); 461 } 462 463 stringValue = request.getParameter(C_PARAM_FILTER_WAVE_LENGTH); 465 if (CmsStringUtil.isNotEmpty(stringValue)) { 466 setFilterWaveLength(Integer.parseInt(stringValue)); 467 } 468 stringValue = request.getParameter(C_PARAM_USE_BACKGROUND_IMAGE); 470 if (CmsStringUtil.isNotEmpty(stringValue)) { 471 setUseBackgroundImage(Boolean.valueOf(stringValue).booleanValue()); 472 } 473 474 stringValue = request.getParameter(C_PARAM_CHARACTERS); 476 if (CmsStringUtil.isNotEmpty(stringValue)) { 477 setCharacterPool(stringValue); 478 } 479 stringValue = request.getParameter(C_PARAM_CHARACTERS); 481 if (CmsStringUtil.isNotEmpty(stringValue)) { 482 setCharacterPool(stringValue); 483 } 484 485 stringValue = request.getParameter(C_PARAM_PRESET); 487 if (CmsStringUtil.isNotEmpty(stringValue)) { 488 m_presetPath = stringValue; 489 } 490 491 } 492 493 519 public void init(CmsObject cms, CmsXmlContent content, Locale locale) { 520 521 try { 522 String captchaSettingsPath = content.getStringValue( 523 cms, 524 new StringBuffer (CmsForm.NODE_CAPTCHA).append("/").append(CmsForm.NODE_CAPTCHA_PRESET).toString(), 525 locale); 526 if (CmsStringUtil.isNotEmpty(captchaSettingsPath)) { 527 m_presetPath = captchaSettingsPath; 528 CmsFile captchaSettingsFile = cms.readFile(captchaSettingsPath); 529 CmsXmlContent preset = CmsXmlContentFactory.unmarshal(cms, captchaSettingsFile); 530 531 Locale captchaSettingsLocale = Locale.ENGLISH; 532 533 String stringValue = preset.getStringValue( 535 cms, 536 CmsCaptchaSettings.NODE_CAPTCHAPRESET_IMAGEWIDTH, 537 captchaSettingsLocale); 538 if (CmsStringUtil.isNotEmpty(stringValue)) { 539 m_imageWidth = Integer.parseInt(stringValue); 540 } 541 542 stringValue = preset.getStringValue( 544 cms, 545 CmsCaptchaSettings.NODE_CAPTCHAPRESET_IMAGEHEIGHT, 546 captchaSettingsLocale); 547 if (CmsStringUtil.isNotEmpty(stringValue)) { 548 m_imageHeight = Integer.parseInt(stringValue); 549 } 550 551 stringValue = preset.getStringValue( 553 cms, 554 CmsCaptchaSettings.NODE_CAPTCHAPRESET_MIN_PHRASE_LENGTH, 555 captchaSettingsLocale); 556 if (CmsStringUtil.isNotEmpty(stringValue)) { 557 m_minPhraseLength = Integer.parseInt(stringValue); 558 } 559 560 stringValue = preset.getStringValue( 562 cms, 563 CmsCaptchaSettings.NODE_CAPTCHAPRESET_MAX_PHRASE_LENGTH, 564 captchaSettingsLocale); 565 if (CmsStringUtil.isNotEmpty(stringValue)) { 566 m_maxPhraseLength = Integer.parseInt(stringValue); 567 } 568 569 stringValue = preset.getStringValue( 571 cms, 572 CmsCaptchaSettings.NODE_CAPTCHAPRESET_MIN_FONT_SIZE, 573 captchaSettingsLocale); 574 if (CmsStringUtil.isNotEmpty(stringValue)) { 575 m_minFontSize = Integer.parseInt(stringValue); 576 } 577 578 stringValue = preset.getStringValue( 580 cms, 581 CmsCaptchaSettings.NODE_CAPTCHAPRESET_MAX_FONT_SIZE, 582 captchaSettingsLocale); 583 if (CmsStringUtil.isNotEmpty(stringValue)) { 584 m_maxFontSize = Integer.parseInt(stringValue); 585 } 586 587 stringValue = preset.getStringValue( 589 cms, 590 CmsCaptchaSettings.NODE_CAPTCHAPRESET_FONTCOLOR, 591 captchaSettingsLocale); 592 if (CmsStringUtil.isNotEmpty(stringValue)) { 593 setFontColor(stringValue); 594 } 595 596 stringValue = preset.getStringValue( 600 cms, 601 CmsCaptchaSettings.NODE_CAPTCHAPRESET_BACKGROUNDCOLOR, 602 captchaSettingsLocale); 603 setBackgroundColor(stringValue); 604 605 stringValue = preset.getStringValue( 607 cms, 608 CmsCaptchaSettings.NODE_CAPTCHAPRESET_HOLESPERGLYPH, 609 captchaSettingsLocale); 610 if (CmsStringUtil.isNotEmpty(stringValue)) { 611 setHolesPerGlyph(Integer.parseInt(stringValue)); 612 } 613 614 stringValue = preset.getStringValue( 616 cms, 617 CmsCaptchaSettings.NODE_CAPTCHAPRESET_FILTER_AMPLITUDE, 618 captchaSettingsLocale); 619 if (CmsStringUtil.isNotEmpty(stringValue)) { 620 setFilterAmplitude(Integer.parseInt(stringValue)); 621 } 622 623 stringValue = preset.getStringValue( 625 cms, 626 CmsCaptchaSettings.NODE_CAPTCHAPRESET_FILTER_WAVELENGTH, 627 captchaSettingsLocale); 628 if (CmsStringUtil.isNotEmpty(stringValue)) { 629 setFilterWaveLength(Integer.parseInt(stringValue)); 630 } 631 632 stringValue = preset.getStringValue(cms, CmsForm.NODE_CAPTCHA_CHARACTERS, captchaSettingsLocale); 633 if (CmsStringUtil.isNotEmpty(stringValue)) { 634 setCharacterPool(stringValue); 635 } 636 637 if (CmsStringUtil.isNotEmpty(stringValue)) { 638 setCharacterPool(stringValue); 639 } 640 641 } else { 642 } 644 645 } catch (Exception ex) { 646 if (LOG.isErrorEnabled()) { 647 LOG.error(ex.getLocalizedMessage()); 648 } 649 650 } 651 652 } 653 654 660 public boolean isUseBackgroundImage() { 661 662 return m_useBackgroundImage; 663 } 664 665 671 public void setBackgroundColor(Color backgroundColor) { 672 673 m_backgroundColor = backgroundColor; 674 } 675 676 682 public void setBackgroundColor(String backgroundColor) { 683 684 if (CmsStringUtil.isNotEmpty(backgroundColor)) { 685 if (backgroundColor.startsWith("#")) { 686 backgroundColor = backgroundColor.substring(1); 687 } 688 689 m_backgroundColor = new Color (Integer.valueOf(backgroundColor, 16).intValue()); 690 m_useBackgroundImage = false; 691 } else if (backgroundColor != null) { 692 m_useBackgroundImage = false; 697 m_backgroundColor = Color.WHITE; 698 } else { 699 m_useBackgroundImage = true; 702 m_backgroundColor = Color.WHITE; 704 } 705 } 706 707 712 public void setFilterAmplitude(int i) { 713 714 m_filterAmplitude = i; 715 716 } 717 718 725 public void setFilterWaveLength(int filterWaveLength) { 726 727 m_filterWaveLength = filterWaveLength; 728 } 729 730 736 public void setFontColor(Color fontColor) { 737 738 m_fontColor = fontColor; 739 } 740 741 747 public void setFontColor(String fontColor) { 748 749 if (CmsStringUtil.isNotEmpty(fontColor)) { 750 if (fontColor.startsWith("#")) { 751 fontColor = fontColor.substring(1); 752 } 753 754 m_fontColor = new Color (Integer.valueOf(fontColor, 16).intValue()); 755 } else { 756 m_fontColor = Color.BLACK; 757 } 758 } 759 760 765 public void setHolesPerGlyph(int holes) { 766 767 m_holesPerGlyp = new Integer (holes); 768 } 769 770 776 public void setImageHeight(int imageHeight) { 777 778 m_imageHeight = imageHeight; 779 } 780 781 787 public void setImageWidth(int imageWidth) { 788 789 m_imageWidth = imageWidth; 790 } 791 792 798 public void setMaxFontSize(int maxFontSize) { 799 800 m_maxFontSize = maxFontSize; 801 } 802 803 809 public void setMaxPhraseLength(int maxPhraseLength) { 810 811 m_maxPhraseLength = maxPhraseLength; 812 } 813 814 820 public void setMinFontSize(int minFontSize) { 821 822 m_minFontSize = minFontSize; 823 } 824 825 831 public void setMinPhraseLength(int minPhraseLength) { 832 833 m_minPhraseLength = minPhraseLength; 834 } 835 836 843 public void setUseBackgroundImage(boolean useBackgroundImage) { 844 845 m_useBackgroundImage = useBackgroundImage; 846 } 847 848 855 public String toRequestParams(CmsObject cms) { 856 857 StringBuffer buf = new StringBuffer (); 858 859 buf.append(C_PARAM_IMAGE_WIDTH).append("=").append(m_imageWidth); 860 buf.append("&").append(C_PARAM_IMAGE_HEIGHT).append("=").append(m_imageHeight); 861 buf.append("&").append(C_PARAM_MIN_FONT_SIZE).append("=").append(m_minFontSize); 862 buf.append("&").append(C_PARAM_MAX_FONT_SIZE).append("=").append(m_maxFontSize); 863 buf.append("&").append(C_PARAM_MIN_PHRASE_LENGTH).append("=").append(m_minPhraseLength); 864 buf.append("&").append(C_PARAM_MAX_PHRASE_LENGTH).append("=").append(m_maxPhraseLength); 865 buf.append("&").append(C_PARAM_FONT_COLOR).append("=").append( 866 CmsEncoder.escape(getFontColorString(), cms.getRequestContext().getEncoding())); 867 buf.append("&").append(C_PARAM_BACKGROUND_COLOR).append("=").append( 868 CmsEncoder.escape(getBackgroundColorString(), cms.getRequestContext().getEncoding())); 869 buf.append("&").append(C_PARAM_HOLES_PER_GLYPH).append("=").append(m_holesPerGlyp); 870 buf.append("&").append(C_PARAM_FILTER_AMPLITUDE).append("=").append(m_filterAmplitude); 871 buf.append("&").append(C_PARAM_FILTER_WAVE_LENGTH).append("=").append(m_filterWaveLength); 872 buf.append("&").append(C_PARAM_CHARACTERS).append("=").append(m_characterPool); 873 buf.append("&").append(C_PARAM_PRESET).append("=").append(m_presetPath); 874 buf.append("&").append(C_PARAM_USE_BACKGROUND_IMAGE).append("=").append(Boolean.toString(m_useBackgroundImage)); 875 return buf.toString(); 876 } 877 878 881 protected Object clone() { 882 883 CmsCaptchaSettings result = new CmsCaptchaSettings(); 884 result.m_backgroundColor = m_backgroundColor; 886 result.m_filterAmplitude = m_filterAmplitude; 887 result.m_filterWaveLength = m_filterWaveLength; 888 result.m_fontColor = m_fontColor; 889 result.m_holesPerGlyp = m_holesPerGlyp; 890 result.m_imageHeight = m_imageHeight; 891 result.m_imageWidth = m_imageWidth; 892 result.m_maxFontSize = m_maxFontSize; 893 result.m_maxPhraseLength = m_maxPhraseLength; 894 result.m_minFontSize = m_minFontSize; 895 result.m_useBackgroundImage = m_useBackgroundImage; 896 result.m_minPhraseLength = m_minPhraseLength; 897 result.m_characterPool = m_characterPool; 898 result.m_presetPath = m_presetPath; 899 return result; 900 } 901 902 908 String getCharacterPool() { 909 910 return m_characterPool; 911 } 912 913 923 String getPresetPath() { 924 925 return m_presetPath; 926 } 927 928 934 void setCharacterPool(String characterPool) { 935 936 m_characterPool = characterPool; 937 } 938 939 946 private String toHexString(int colorRange) { 947 948 if (colorRange < 10) { 949 return "0" + Integer.toHexString(colorRange); 950 } else { 951 return Integer.toHexString(colorRange); 952 } 953 } 954 } 955 | Popular Tags |