1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
45   * <a href="ReCaptchaImpl.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Tagnaouti Boubker
48   * @author Jorge Ferrer
49   * @author Brian Wing Shun Chan
50   */
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 }