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