1
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
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 }