001    /**
002     * Copyright (c) 2000-2013 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.CaptchaException;
019    import com.liferay.portal.kernel.captcha.CaptchaTextException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.Http;
025    import com.liferay.portal.kernel.util.HttpUtil;
026    import com.liferay.portal.kernel.util.ParamUtil;
027    import com.liferay.portal.kernel.util.PropsKeys;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portal.util.PrefsPropsUtil;
030    import com.liferay.portal.util.PropsValues;
031    
032    import java.io.IOException;
033    
034    import javax.portlet.PortletRequest;
035    import javax.portlet.PortletResponse;
036    
037    import javax.servlet.http.HttpServletRequest;
038    import javax.servlet.http.HttpServletResponse;
039    
040    /**
041     * @author Tagnaouti Boubker
042     * @author Jorge Ferrer
043     * @author Brian Wing Shun Chan
044     * @author Daniel Sanz
045     */
046    public class ReCaptchaImpl extends SimpleCaptchaImpl {
047    
048            @Override
049            public String getTaglibPath() {
050                    return _TAGLIB_PATH;
051            }
052    
053            @Override
054            public void serveImage(
055                    HttpServletRequest request, HttpServletResponse response) {
056    
057                    throw new UnsupportedOperationException();
058            }
059    
060            @Override
061            public void serveImage(
062                    PortletRequest portletRequest, PortletResponse portletResponse) {
063    
064                    throw new UnsupportedOperationException();
065            }
066    
067            @Override
068            protected boolean validateChallenge(HttpServletRequest request)
069                    throws CaptchaException {
070    
071                    String reCaptchaChallenge = ParamUtil.getString(
072                            request, "recaptcha_challenge_field");
073                    String reCaptchaResponse = ParamUtil.getString(
074                            request, "recaptcha_response_field");
075    
076                    Http.Options options = new Http.Options();
077    
078                    options.addPart("challenge", reCaptchaChallenge);
079    
080                    try {
081                            options.addPart(
082                                    "privatekey",
083                                    PrefsPropsUtil.getString(
084                                            PropsKeys.CAPTCHA_ENGINE_RECAPTCHA_KEY_PRIVATE,
085                                            PropsValues.CAPTCHA_ENGINE_RECAPTCHA_KEY_PRIVATE));
086                    }
087                    catch (SystemException se) {
088                            _log.error(se, se);
089                    }
090    
091                    options.addPart("remoteip", request.getRemoteAddr());
092                    options.addPart("response", reCaptchaResponse);
093                    options.setLocation(
094                            HttpUtil.protocolize(
095                                    PropsValues.CAPTCHA_ENGINE_RECAPTCHA_URL_VERIFY,
096                                    request.isSecure()));
097                    options.setPost(true);
098    
099                    String content = null;
100    
101                    try {
102                            content = HttpUtil.URLtoString(options);
103                    }
104                    catch (IOException ioe) {
105                            _log.error(ioe, ioe);
106    
107                            throw new CaptchaTextException();
108                    }
109    
110                    if (content == null) {
111                            _log.error("reCAPTCHA did not return a result");
112    
113                            throw new CaptchaTextException();
114                    }
115    
116                    String[] messages = content.split("\r?\n");
117    
118                    if (messages.length < 1) {
119                            _log.error("reCAPTCHA did not return a valid result: " + content);
120    
121                            throw new CaptchaTextException();
122                    }
123    
124                    return GetterUtil.getBoolean(messages[0]);
125            }
126    
127            @Override
128            protected boolean validateChallenge(PortletRequest portletRequest)
129                    throws CaptchaException {
130    
131                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
132                            portletRequest);
133    
134                    return validateChallenge(request);
135            }
136    
137            private static final String _TAGLIB_PATH =
138                    "/html/taglib/ui/captcha/recaptcha.jsp";
139    
140            private static Log _log = LogFactoryUtil.getLog(ReCaptchaImpl.class);
141    
142    }