001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * This file creation date: Nov 13, 2004 / 17:17:09
040: * The JForum Project
041: * http://www.jforum.net
042: */
043:
044: package net.jforum.util;
045:
046: import java.awt.Color;
047: import java.awt.image.BufferedImage;
048: import java.io.IOException;
049: import java.io.OutputStream;
050: import java.util.ArrayList;
051: import java.util.Iterator;
052: import java.util.List;
053:
054: import javax.imageio.ImageIO;
055:
056: import net.jforum.JForumExecutionContext;
057: import net.jforum.SessionFacade;
058: import net.jforum.util.preferences.ConfigKeys;
059: import net.jforum.util.preferences.SystemGlobals;
060:
061: import org.apache.log4j.Logger;
062:
063: import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
064: import com.octo.captcha.component.image.backgroundgenerator.GradientBackgroundGenerator;
065: import com.octo.captcha.component.image.fontgenerator.FontGenerator;
066: import com.octo.captcha.component.image.fontgenerator.TwistedAndShearedRandomFontGenerator;
067: import com.octo.captcha.component.image.textpaster.RandomTextPaster;
068: import com.octo.captcha.component.image.textpaster.TextPaster;
069: import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;
070: import com.octo.captcha.component.image.wordtoimage.WordToImage;
071: import com.octo.captcha.component.wordgenerator.RandomWordGenerator;
072: import com.octo.captcha.component.wordgenerator.WordGenerator;
073: import com.octo.captcha.engine.image.ListImageCaptchaEngine;
074: import com.octo.captcha.image.ImageCaptchaFactory;
075: import com.octo.captcha.image.gimpy.GimpyFactory;
076:
077: /**
078: * @author James Yong
079: * @version $Id: Captcha.java,v 1.13 2007/08/06 15:38:01 rafaelsteil Exp $
080: */
081: public class Captcha extends ListImageCaptchaEngine {
082: private static final Logger logger = Logger
083: .getLogger(Captcha.class);
084:
085: private static Captcha classInstance = new Captcha();
086: private List backgroundGeneratorList;
087: private List textPasterList;
088: private List fontGeneratorList;
089:
090: private static String charsInUse;
091:
092: private void initializeChars() {
093: if (SystemGlobals.getBoolValue(ConfigKeys.CAPTCHA_IGNORE_CASE)) {
094: charsInUse = "123456789abcdefghijlmnopkrstuvxzyk@#%^";
095: } else {
096: charsInUse = "123456789ABCDEFGHJLKMNPRSTWXYZabcdefghijlmnopkrstuvxzyk@#%^";
097: }
098: }
099:
100: /**
101: * Gets the singleton
102: *
103: * @return Instance of Captcha class
104: */
105: public static Captcha getInstance() {
106: return classInstance;
107: }
108:
109: protected void buildInitialFactories() {
110: this .initializeChars();
111:
112: this .backgroundGeneratorList = new ArrayList();
113: this .textPasterList = new ArrayList();
114: this .fontGeneratorList = new ArrayList();
115:
116: int width = SystemGlobals.getIntValue(ConfigKeys.CAPTCHA_WIDTH);
117: int height = SystemGlobals
118: .getIntValue(ConfigKeys.CAPTCHA_HEIGHT);
119: int minWords = SystemGlobals
120: .getIntValue(ConfigKeys.CAPTCHA_MIN_WORDS);
121: int maxWords = SystemGlobals
122: .getIntValue(ConfigKeys.CAPTCHA_MAX_WORDS);
123: int minFontSize = SystemGlobals
124: .getIntValue(ConfigKeys.CAPTCHA_MIN_FONT_SIZE);
125: int maxFontSize = SystemGlobals
126: .getIntValue(ConfigKeys.CAPTCHA_MAX_FONT_SIZE);
127:
128: this .backgroundGeneratorList
129: .add(new GradientBackgroundGenerator(
130: new Integer(width), new Integer(height),
131: Color.PINK, Color.LIGHT_GRAY));
132: this .backgroundGeneratorList
133: .add(new GradientBackgroundGenerator(
134: new Integer(width), new Integer(height),
135: Color.WHITE, Color.RED));
136: this .backgroundGeneratorList
137: .add(new GradientBackgroundGenerator(
138: new Integer(width), new Integer(height),
139: Color.ORANGE, Color.LIGHT_GRAY));
140: this .backgroundGeneratorList
141: .add(new GradientBackgroundGenerator(
142: new Integer(width), new Integer(height),
143: Color.CYAN, Color.LIGHT_GRAY));
144: //this.backgroundGeneratorList.add(new FunkyBackgroundGenerator(new Integer(250), new Integer(50)));
145:
146: this .textPasterList.add(new RandomTextPaster(new Integer(
147: minWords), new Integer(maxWords), Color.DARK_GRAY));
148: this .textPasterList.add(new RandomTextPaster(new Integer(
149: minWords), new Integer(maxWords), Color.BLUE));
150: this .textPasterList.add(new RandomTextPaster(new Integer(
151: minWords), new Integer(maxWords), Color.GREEN));
152: this .textPasterList.add(new RandomTextPaster(new Integer(
153: minWords), new Integer(maxWords), Color.MAGENTA));
154: this .textPasterList.add(new RandomTextPaster(new Integer(
155: minWords), new Integer(maxWords), Color.BLACK));
156: this .textPasterList.add(new RandomTextPaster(new Integer(
157: minWords), new Integer(maxWords), Color.WHITE));
158:
159: this .fontGeneratorList
160: .add(new TwistedAndShearedRandomFontGenerator(
161: new Integer(minFontSize), new Integer(
162: maxFontSize)));
163:
164: // Create a random word generator
165: WordGenerator words = new RandomWordGenerator(charsInUse);
166:
167: for (Iterator fontIter = this .fontGeneratorList.iterator(); fontIter
168: .hasNext();) {
169: FontGenerator fontGeny = (FontGenerator) fontIter.next();
170:
171: for (Iterator backIter = this .backgroundGeneratorList
172: .iterator(); backIter.hasNext();) {
173: BackgroundGenerator bkgdGeny = (BackgroundGenerator) backIter
174: .next();
175:
176: for (Iterator textIter = this .textPasterList.iterator(); textIter
177: .hasNext();) {
178: TextPaster textPaster = (TextPaster) textIter
179: .next();
180:
181: WordToImage word2image = new ComposedWordToImage(
182: fontGeny, bkgdGeny, textPaster);
183:
184: // Creates a ImageCaptcha Factory
185: ImageCaptchaFactory factory = new GimpyFactory(
186: words, word2image);
187:
188: // Add a factory to the gimpy list (A Gimpy is a ImagCaptcha)
189: addFactory(factory);
190: }
191: }
192: }
193: }
194:
195: public void writeCaptchaImage() {
196: BufferedImage image = SessionFacade.getUserSession()
197: .getCaptchaImage();
198:
199: if (image == null) {
200: return;
201: }
202:
203: OutputStream outputStream = null;
204:
205: try {
206: outputStream = JForumExecutionContext.getResponse()
207: .getOutputStream();
208: ImageIO.write(image, "jpg", outputStream);
209: } catch (IOException ex) {
210: logger.error(ex);
211: } finally {
212: if (outputStream != null) {
213: try {
214: outputStream.close();
215: } catch (IOException ex) {
216: }
217: }
218: }
219: }
220: }
|