1
14
15 package com.liferay.portal.captcha.recaptcha;
16
17 import com.liferay.portal.captcha.simplecaptcha.SimpleCaptchaImpl;
18 import com.liferay.portal.kernel.captcha.CaptchaException;
19 import com.liferay.portal.kernel.captcha.CaptchaTextException;
20 import com.liferay.portal.kernel.log.Log;
21 import com.liferay.portal.kernel.log.LogFactoryUtil;
22 import com.liferay.portal.kernel.util.GetterUtil;
23 import com.liferay.portal.kernel.util.Http;
24 import com.liferay.portal.kernel.util.HttpUtil;
25 import com.liferay.portal.kernel.util.ParamUtil;
26 import com.liferay.portal.kernel.util.Validator;
27 import com.liferay.portal.util.PortalUtil;
28 import com.liferay.portal.util.PropsValues;
29 import com.liferay.portal.util.WebKeys;
30
31 import java.io.IOException;
32
33 import javax.portlet.PortletRequest;
34 import javax.portlet.PortletResponse;
35
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38 import javax.servlet.http.HttpSession;
39
40
47 public class ReCaptchaImpl extends SimpleCaptchaImpl {
48
49 public void check(HttpServletRequest request) throws CaptchaException {
50 if (!isEnabled(request)) {
51 return;
52 }
53
54 String reCaptchaChallenge = ParamUtil.getString(
55 request, "recaptcha_challenge_field");
56 String reCaptchaResponse = ParamUtil.getString(
57 request, "recaptcha_response_field");
58
59 Http.Options options = new Http.Options();
60
61 options.addPart("challenge", reCaptchaChallenge);
62 options.addPart(
63 "privatekey", PropsValues.CAPTCHA_ENGINE_RECAPTCHA_KEY_PRIVATE);
64 options.addPart("remoteip", request.getRemoteAddr());
65 options.addPart("response", reCaptchaResponse);
66 options.setLocation(PropsValues.CAPTCHA_ENGINE_RECAPTCHA_URL_VERIFY);
67 options.setPost(true);
68
69 String content = null;
70
71 try {
72 content = HttpUtil.URLtoString(options);
73 }
74 catch (IOException ioe) {
75 _log.error(ioe, ioe);
76
77 throw new CaptchaTextException();
78 }
79
80 if (content == null) {
81 _log.error("reCAPTCHA did not return a result");
82
83 throw new CaptchaTextException();
84 }
85
86 String[] messages = content.split("\r?\n");
87
88 if (messages.length < 1) {
89 _log.error("reCAPTCHA did not return a valid result: " + content);
90
91 throw new CaptchaTextException();
92 }
93
94 if (!GetterUtil.getBoolean(messages[0])) {
95 if ((PropsValues.CAPTCHA_MAX_CHALLENGES > 0) &&
96 (Validator.isNotNull(request.getRemoteUser()))) {
97
98 HttpSession session = request.getSession();
99
100 Integer count = (Integer)session.getAttribute(
101 WebKeys.CAPTCHA_COUNT);
102
103 if (count == null) {
104 count = new Integer(1);
105 }
106 else {
107 count = new Integer(count.intValue() + 1);
108 }
109
110 session.setAttribute(WebKeys.CAPTCHA_COUNT, count);
111 }
112
113 throw new CaptchaTextException();
114 }
115 }
116
117 public void check(PortletRequest portletRequest) throws CaptchaException {
118 if (!isEnabled(portletRequest)) {
119 return;
120 }
121
122 HttpServletRequest request = PortalUtil.getHttpServletRequest(
123 portletRequest);
124
125 check(request);
126 }
127
128 public String getTaglibPath() {
129 return _TAGLIB_PATH;
130 }
131
132 public void serveImage(
133 HttpServletRequest request, HttpServletResponse response) {
134
135 throw new UnsupportedOperationException();
136 }
137
138 public void serveImage(
139 PortletRequest portletRequest, PortletResponse portletResponse) {
140
141 throw new UnsupportedOperationException();
142 }
143
144 private static final String _TAGLIB_PATH =
145 "/html/taglib/ui/captcha/recaptcha.jsp";
146
147 private static Log _log = LogFactoryUtil.getLog(ReCaptchaImpl.class);
148
149 }