1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.captcha;
24  
25  import com.liferay.portal.kernel.util.ParamUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.util.PropsValues;
28  import com.liferay.portal.util.WebKeys;
29  
30  import javax.portlet.PortletRequest;
31  import javax.portlet.PortletSession;
32  
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpSession;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  /**
40   * <a href="CaptchaUtil.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class CaptchaUtil {
46  
47      public static void check(HttpServletRequest req)
48          throws CaptchaTextException {
49  
50          if (isEnabled(req)) {
51              HttpSession ses = req.getSession();
52  
53              String captchaText = (String)ses.getAttribute(WebKeys.CAPTCHA_TEXT);
54  
55              // Captcha should never be null, but on the rare occasion it is,
56              // just let people register.
57  
58              if (captchaText != null) {
59                  if (!captchaText.equals(
60                          ParamUtil.getString(req, "captchaText"))) {
61  
62                      throw new CaptchaTextException();
63                  }
64                  else {
65                      if (_log.isDebugEnabled()) {
66                          _log.debug("Captcha text is valid");
67                      }
68  
69                      if ((PropsValues.CAPTCHA_MAX_CHALLENGES > 0) &&
70                          (Validator.isNotNull(req.getRemoteUser()))) {
71  
72                          Integer count = (Integer)ses.getAttribute(
73                              WebKeys.CAPTCHA_COUNT);
74  
75                          if (count == null) {
76                              count = new Integer(1);
77                          }
78                          else {
79                              count = new Integer(count.intValue() + 1);
80                          }
81  
82                          ses.setAttribute(WebKeys.CAPTCHA_COUNT, count);
83                      }
84                  }
85              }
86              else {
87                  if (_log.isErrorEnabled()) {
88                      _log.error("Captcha text is null");
89                  }
90              }
91          }
92      }
93  
94      public static void check(PortletRequest req) throws CaptchaTextException {
95          if (isEnabled(req)) {
96              PortletSession ses = req.getPortletSession();
97  
98              String captchaText = (String)ses.getAttribute(WebKeys.CAPTCHA_TEXT);
99  
100             // Captcha should never be null, but on the rare occasion it is,
101             // just let people register.
102 
103             if (captchaText != null) {
104                 if (!captchaText.equals(
105                         ParamUtil.getString(req, "captchaText"))) {
106 
107                     throw new CaptchaTextException();
108                 }
109                 else {
110                     if (_log.isDebugEnabled()) {
111                         _log.debug("Captcha text is valid");
112                     }
113 
114                     if ((PropsValues.CAPTCHA_MAX_CHALLENGES > 0) &&
115                         (Validator.isNotNull(req.getRemoteUser()))) {
116 
117                         Integer count = (Integer)ses.getAttribute(
118                             WebKeys.CAPTCHA_COUNT);
119 
120                         if (count == null) {
121                             count = new Integer(1);
122                         }
123                         else {
124                             count = new Integer(count.intValue() + 1);
125                         }
126 
127                         ses.setAttribute(WebKeys.CAPTCHA_COUNT, count);
128                     }
129                 }
130             }
131             else {
132                 if (_log.isErrorEnabled()) {
133                     _log.error("Captcha text is null");
134                 }
135             }
136         }
137     }
138 
139     public static boolean isEnabled(HttpServletRequest req) {
140         if (PropsValues.CAPTCHA_MAX_CHALLENGES > 0) {
141             HttpSession ses = req.getSession();
142 
143             Integer count = (Integer)ses.getAttribute(WebKeys.CAPTCHA_COUNT);
144 
145             if ((count != null) &&
146                 (PropsValues.CAPTCHA_MAX_CHALLENGES <= count.intValue())) {
147 
148                 return false;
149             }
150             else {
151                 return true;
152             }
153         }
154         else if (PropsValues.CAPTCHA_MAX_CHALLENGES < 0) {
155             return false;
156         }
157         else {
158             return true;
159         }
160     }
161 
162     public static boolean isEnabled(PortletRequest req) {
163         if (PropsValues.CAPTCHA_MAX_CHALLENGES > 0) {
164             PortletSession ses = req.getPortletSession();
165 
166             Integer count = (Integer)ses.getAttribute(WebKeys.CAPTCHA_COUNT);
167 
168             if ((count != null) &&
169                 (PropsValues.CAPTCHA_MAX_CHALLENGES <= count.intValue())) {
170 
171                 return false;
172             }
173             else {
174                 return true;
175             }
176         }
177         else if (PropsValues.CAPTCHA_MAX_CHALLENGES < 0) {
178             return false;
179         }
180         else {
181             return true;
182         }
183     }
184 
185     private static Log _log = LogFactory.getLog(CaptchaUtil.class);
186 
187 }