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