001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.captcha.recaptcha;
016    
017    import com.liferay.portal.captcha.simplecaptcha.SimpleCaptchaImpl;
018    import com.liferay.portal.kernel.captcha.CaptchaTextException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.Http;
023    import com.liferay.portal.kernel.util.HttpUtil;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.util.PortalUtil;
026    import com.liferay.portal.util.PropsValues;
027    
028    import java.io.IOException;
029    
030    import javax.portlet.PortletRequest;
031    import javax.portlet.PortletResponse;
032    
033    import javax.servlet.http.HttpServletRequest;
034    import javax.servlet.http.HttpServletResponse;
035    
036    /**
037     * @author Tagnaouti Boubker
038     * @author Jorge Ferrer
039     * @author Brian Wing Shun Chan
040     */
041    public class ReCaptchaImpl extends SimpleCaptchaImpl {
042    
043            public void check(HttpServletRequest request) throws CaptchaTextException {
044                    if (!isEnabled(request)) {
045                            return;
046                    }
047    
048                    String reCaptchaChallenge = ParamUtil.getString(
049                            request, "recaptcha_challenge_field");
050                    String reCaptchaResponse = ParamUtil.getString(
051                            request, "recaptcha_response_field");
052    
053                    Http.Options options = new Http.Options();
054    
055                    options.addPart("challenge", reCaptchaChallenge);
056                    options.addPart(
057                            "privatekey", PropsValues.CAPTCHA_ENGINE_RECAPTCHA_KEY_PRIVATE);
058                    options.addPart("remoteip", request.getRemoteAddr());
059                    options.addPart("response", reCaptchaResponse);
060                    options.setLocation(PropsValues.CAPTCHA_ENGINE_RECAPTCHA_URL_VERIFY);
061                    options.setPost(true);
062    
063                    String content = null;
064    
065                    try {
066                            content = HttpUtil.URLtoString(options);
067                    }
068                    catch (IOException ioe) {
069                            _log.error(ioe, ioe);
070    
071                            throw new CaptchaTextException();
072                    }
073    
074                    if (content == null) {
075                            _log.error("reCAPTCHA did not return a result");
076    
077                            throw new CaptchaTextException();
078                    }
079    
080                    String[] messages = content.split("\r?\n");
081    
082                    if (messages.length < 1) {
083                            _log.error("reCAPTCHA did not return a valid result: " + content);
084    
085                            throw new CaptchaTextException();
086                    }
087    
088                    if (!GetterUtil.getBoolean(messages[0])) {
089                            throw new CaptchaTextException();
090                    }
091            }
092    
093            public void check(PortletRequest portletRequest)
094                    throws CaptchaTextException {
095    
096                    if (!isEnabled(portletRequest)) {
097                            return;
098                    }
099    
100                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
101                            portletRequest);
102    
103                    check(request);
104            }
105    
106            public String getTaglibPath() {
107                    return _TAGLIB_PATH;
108            }
109    
110            public void serveImage(
111                    HttpServletRequest request, HttpServletResponse response) {
112    
113                    throw new UnsupportedOperationException();
114            }
115    
116            public void serveImage(
117                    PortletRequest portletRequest, PortletResponse portletResponse) {
118    
119                    throw new UnsupportedOperationException();
120            }
121    
122            private static final String _TAGLIB_PATH =
123                    "/html/taglib/ui/captcha/recaptcha.jsp";
124    
125            private static Log _log = LogFactoryUtil.getLog(ReCaptchaImpl.class);
126    
127    }