001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.kernel.util.Validator;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.PrefsPropsUtil;
031    import com.liferay.portal.util.PropsValues;
032    import com.liferay.portal.util.WebKeys;
033    
034    import java.io.IOException;
035    
036    import javax.portlet.PortletRequest;
037    import javax.portlet.PortletResponse;
038    
039    import javax.servlet.http.HttpServletRequest;
040    import javax.servlet.http.HttpServletResponse;
041    import javax.servlet.http.HttpSession;
042    
043    /**
044     * @author Tagnaouti Boubker
045     * @author Jorge Ferrer
046     * @author Brian Wing Shun Chan
047     */
048    public class ReCaptchaImpl extends SimpleCaptchaImpl {
049    
050            public void check(HttpServletRequest request) throws CaptchaException {
051                    if (!isEnabled(request)) {
052                            return;
053                    }
054    
055                    String reCaptchaChallenge = ParamUtil.getString(
056                            request, "recaptcha_challenge_field");
057                    String reCaptchaResponse = ParamUtil.getString(
058                            request, "recaptcha_response_field");
059    
060                    Http.Options options = new Http.Options();
061    
062                    options.addPart("challenge", reCaptchaChallenge);
063    
064                    try {
065                            options.addPart(
066                                    "privatekey",
067                                    PrefsPropsUtil.getString(
068                                            PropsKeys.CAPTCHA_ENGINE_RECAPTCHA_KEY_PRIVATE,
069                                            PropsValues.CAPTCHA_ENGINE_RECAPTCHA_KEY_PRIVATE));
070                    }
071                    catch (SystemException se) {
072                            _log.error(se, se);
073                    }
074    
075                    options.addPart("remoteip", request.getRemoteAddr());
076                    options.addPart("response", reCaptchaResponse);
077                    options.setLocation(PropsValues.CAPTCHA_ENGINE_RECAPTCHA_URL_VERIFY);
078                    options.setPost(true);
079    
080                    String content = null;
081    
082                    try {
083                            content = HttpUtil.URLtoString(options);
084                    }
085                    catch (IOException ioe) {
086                            _log.error(ioe, ioe);
087    
088                            throw new CaptchaTextException();
089                    }
090    
091                    if (content == null) {
092                            _log.error("reCAPTCHA did not return a result");
093    
094                            throw new CaptchaTextException();
095                    }
096    
097                    String[] messages = content.split("\r?\n");
098    
099                    if (messages.length < 1) {
100                            _log.error("reCAPTCHA did not return a valid result: " + content);
101    
102                            throw new CaptchaTextException();
103                    }
104    
105                    if (!GetterUtil.getBoolean(messages[0])) {
106                            if ((PropsValues.CAPTCHA_MAX_CHALLENGES > 0) &&
107                                    (Validator.isNotNull(request.getRemoteUser()))) {
108    
109                                    HttpSession session = request.getSession();
110    
111                                    Integer count = (Integer)session.getAttribute(
112                                            WebKeys.CAPTCHA_COUNT);
113    
114                                    if (count == null) {
115                                            count = new Integer(1);
116                                    }
117                                    else {
118                                            count = new Integer(count.intValue() + 1);
119                                    }
120    
121                                    session.setAttribute(WebKeys.CAPTCHA_COUNT, count);
122                            }
123    
124                            throw new CaptchaTextException();
125                    }
126            }
127    
128            public void check(PortletRequest portletRequest) throws CaptchaException {
129                    if (!isEnabled(portletRequest)) {
130                            return;
131                    }
132    
133                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
134                            portletRequest);
135    
136                    check(request);
137            }
138    
139            public String getTaglibPath() {
140                    return _TAGLIB_PATH;
141            }
142    
143            public void serveImage(
144                    HttpServletRequest request, HttpServletResponse response) {
145    
146                    throw new UnsupportedOperationException();
147            }
148    
149            public void serveImage(
150                    PortletRequest portletRequest, PortletResponse portletResponse) {
151    
152                    throw new UnsupportedOperationException();
153            }
154    
155            private static final String _TAGLIB_PATH =
156                    "/html/taglib/ui/captcha/recaptcha.jsp";
157    
158            private static Log _log = LogFactoryUtil.getLog(ReCaptchaImpl.class);
159    
160    }