1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.captcha.recaptcha;
16  
17  import com.liferay.portal.captcha.simplecaptcha.SimpleCaptchaImpl;
18  import com.liferay.portal.kernel.captcha.CaptchaTextException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.GetterUtil;
22  import com.liferay.portal.kernel.util.Http;
23  import com.liferay.portal.kernel.util.HttpUtil;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.util.PortalUtil;
26  import com.liferay.portal.util.PropsValues;
27  
28  import java.io.IOException;
29  
30  import javax.portlet.PortletRequest;
31  import javax.portlet.PortletResponse;
32  
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  /**
37   * <a href="ReCaptchaImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Tagnaouti Boubker
40   * @author Jorge Ferrer
41   * @author Brian Wing Shun Chan
42   */
43  public class ReCaptchaImpl extends SimpleCaptchaImpl {
44  
45      public void check(HttpServletRequest request) throws CaptchaTextException {
46          if (!isEnabled(request)) {
47              return;
48          }
49  
50          String reCaptchaChallenge = ParamUtil.getString(
51              request, "recaptcha_challenge_field");
52          String reCaptchaResponse = ParamUtil.getString(
53              request, "recaptcha_response_field");
54  
55          Http.Options options = new Http.Options();
56  
57          options.addPart("challenge", reCaptchaChallenge);
58          options.addPart(
59              "privatekey", PropsValues.CAPTCHA_ENGINE_RECAPTCHA_KEY_PRIVATE);
60          options.addPart("remoteip", request.getRemoteAddr());
61          options.addPart("response", reCaptchaResponse);
62          options.setLocation(PropsValues.CAPTCHA_ENGINE_RECAPTCHA_URL_VERIFY);
63          options.setPost(true);
64  
65          String content = null;
66  
67          try {
68              content = HttpUtil.URLtoString(options);
69          }
70          catch (IOException ioe) {
71              _log.error(ioe, ioe);
72  
73              throw new CaptchaTextException();
74          }
75  
76          if (content == null) {
77              _log.error("reCAPTCHA did not return a result");
78  
79              throw new CaptchaTextException();
80          }
81  
82          String[] messages = content.split("\r?\n");
83  
84          if (messages.length < 1) {
85              _log.error("reCAPTCHA did not return a valid result: " + content);
86  
87              throw new CaptchaTextException();
88          }
89  
90          if (!GetterUtil.getBoolean(messages[0])) {
91              throw new CaptchaTextException();
92          }
93      }
94  
95      public void check(PortletRequest portletRequest)
96          throws CaptchaTextException {
97  
98          if (!isEnabled(portletRequest)) {
99              return;
100         }
101 
102         HttpServletRequest request = PortalUtil.getHttpServletRequest(
103             portletRequest);
104 
105         check(request);
106     }
107 
108     public String getTaglibPath() {
109         return _TAGLIB_PATH;
110     }
111 
112     public void serveImage(
113         HttpServletRequest request, HttpServletResponse response) {
114 
115         throw new UnsupportedOperationException();
116     }
117 
118     public void serveImage(
119         PortletRequest portletRequest, PortletResponse portletResponse) {
120 
121         throw new UnsupportedOperationException();
122     }
123 
124     private static final String _TAGLIB_PATH =
125         "/html/taglib/ui/captcha/recaptcha.jsp";
126 
127     private static Log _log = LogFactoryUtil.getLog(ReCaptchaImpl.class);
128 
129 }