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              if (captchaText != null) {
56                  if (!captchaText.equals(
57                          ParamUtil.getString(req, "captchaText"))) {
58  
59                      throw new CaptchaTextException();
60                  }
61                  else {
62                      if (_log.isDebugEnabled()) {
63                          _log.debug("Captcha text is valid");
64                      }
65  
66                      ses.removeAttribute(WebKeys.CAPTCHA_TEXT);
67  
68                      if ((PropsValues.CAPTCHA_MAX_CHALLENGES > 0) &&
69                          (Validator.isNotNull(req.getRemoteUser()))) {
70  
71                          Integer count = (Integer)ses.getAttribute(
72                              WebKeys.CAPTCHA_COUNT);
73  
74                          if (count == null) {
75                              count = new Integer(1);
76                          }
77                          else {
78                              count = new Integer(count.intValue() + 1);
79                          }
80  
81                          ses.setAttribute(WebKeys.CAPTCHA_COUNT, count);
82                      }
83                  }
84              }
85              else {
86                  if (_log.isErrorEnabled()) {
87                      _log.error(
88                          "Captcha text is null. User " + req.getRemoteUser() +
89                              " may be trying to circumvent the captcha.");
90                  }
91  
92                  throw new CaptchaTextException();
93              }
94          }
95      }
96  
97      public static void check(PortletRequest req) throws CaptchaTextException {
98          if (isEnabled(req)) {
99              PortletSession ses = req.getPortletSession();
100 
101             String captchaText = (String)ses.getAttribute(WebKeys.CAPTCHA_TEXT);
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                     ses.removeAttribute(WebKeys.CAPTCHA_TEXT);
115 
116                     if ((PropsValues.CAPTCHA_MAX_CHALLENGES > 0) &&
117                         (Validator.isNotNull(req.getRemoteUser()))) {
118 
119                         Integer count = (Integer)ses.getAttribute(
120                             WebKeys.CAPTCHA_COUNT);
121 
122                         if (count == null) {
123                             count = new Integer(1);
124                         }
125                         else {
126                             count = new Integer(count.intValue() + 1);
127                         }
128 
129                         ses.setAttribute(WebKeys.CAPTCHA_COUNT, count);
130                     }
131                 }
132             }
133             else {
134                 if (_log.isErrorEnabled()) {
135                     _log.error(
136                         "Captcha text is null. User " + req.getRemoteUser() +
137                             " may be trying to circumvent the captcha.");
138                 }
139 
140                 throw new CaptchaTextException();
141             }
142         }
143     }
144 
145     public static boolean isEnabled(HttpServletRequest req) {
146         if (PropsValues.CAPTCHA_MAX_CHALLENGES > 0) {
147             HttpSession ses = req.getSession();
148 
149             Integer count = (Integer)ses.getAttribute(WebKeys.CAPTCHA_COUNT);
150 
151             if ((count != null) &&
152                 (PropsValues.CAPTCHA_MAX_CHALLENGES <= count.intValue())) {
153 
154                 return false;
155             }
156             else {
157                 return true;
158             }
159         }
160         else if (PropsValues.CAPTCHA_MAX_CHALLENGES < 0) {
161             return false;
162         }
163         else {
164             return true;
165         }
166     }
167 
168     public static boolean isEnabled(PortletRequest req) {
169         if (PropsValues.CAPTCHA_MAX_CHALLENGES > 0) {
170             PortletSession ses = req.getPortletSession();
171 
172             Integer count = (Integer)ses.getAttribute(WebKeys.CAPTCHA_COUNT);
173 
174             if ((count != null) &&
175                 (PropsValues.CAPTCHA_MAX_CHALLENGES <= count.intValue())) {
176 
177                 return false;
178             }
179             else {
180                 return true;
181             }
182         }
183         else if (PropsValues.CAPTCHA_MAX_CHALLENGES < 0) {
184             return false;
185         }
186         else {
187             return true;
188         }
189     }
190 
191     private static Log _log = LogFactory.getLog(CaptchaUtil.class);
192 
193 }