001    /**
002     * Copyright (c) 2000-2012 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(PropsValues.CAPTCHA_ENGINE_RECAPTCHA_URL_VERIFY);
094                    options.setPost(true);
095    
096                    String content = null;
097    
098                    try {
099                            content = HttpUtil.URLtoString(options);
100                    }
101                    catch (IOException ioe) {
102                            _log.error(ioe, ioe);
103    
104                            throw new CaptchaTextException();
105                    }
106    
107                    if (content == null) {
108                            _log.error("reCAPTCHA did not return a result");
109    
110                            throw new CaptchaTextException();
111                    }
112    
113                    String[] messages = content.split("\r?\n");
114    
115                    if (messages.length < 1) {
116                            _log.error("reCAPTCHA did not return a valid result: " + content);
117    
118                            throw new CaptchaTextException();
119                    }
120    
121                    return GetterUtil.getBoolean(messages[0]);
122            }
123    
124            @Override
125            protected boolean validateChallenge(PortletRequest portletRequest)
126                    throws CaptchaException {
127    
128                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
129                            portletRequest);
130    
131                    return validateChallenge(request);
132            }
133    
134            private static final String _TAGLIB_PATH =
135                    "/html/taglib/ui/captcha/recaptcha.jsp";
136    
137            private static Log _log = LogFactoryUtil.getLog(ReCaptchaImpl.class);
138    
139    }