r1286 - in xwiki/trunk: lib src/main/java/com/xpn/xwiki/plugin src/main/java/com/xpn/xwiki/plugin/captcha src/test/cactus/com/xpn/xwiki/test

Phung Hai Nam namphunghai at users.forge.objectweb.org
Wed Sep 6 10:41:09 CEST 2006


Author: namphunghai
Date: 2006-09-06 10:41:06 +0200 (Wed, 06 Sep 2006)
New Revision: 1286

Added:
   xwiki/trunk/lib/jcaptcha-all-1.0.jar
   xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/captcha/
   xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/captcha/CaptchaParams.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/captcha/CaptchaPlugin.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/captcha/CaptchaPluginApi.java
   xwiki/trunk/src/test/cactus/com/xpn/xwiki/test/ServletCaptchaTest.java
Modified:
   xwiki/trunk/src/test/cactus/com/xpn/xwiki/test/ServletSectionEditTest.java
Log:
XWIKI-118 .Add captcha plugin and jcaptcha-all-1.0.jar , and simplest test cactus for captcha

Added: xwiki/trunk/lib/jcaptcha-all-1.0.jar
===================================================================
(Binary files differ)


Property changes on: xwiki/trunk/lib/jcaptcha-all-1.0.jar
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/captcha/CaptchaParams.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/captcha/CaptchaParams.java	2006-09-05 17:07:22 UTC (rev 1285)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/captcha/CaptchaParams.java	2006-09-06 08:41:06 UTC (rev 1286)
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2006, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * @author Phung Hai Nam (phunghainam at xwiki.com)
+ * @version 5 Sep 2006
+ */
+
+package com.xpn.xwiki.plugin.captcha;
+
+import java.util.Map;
+import java.util.HashMap;
+
+public class CaptchaParams {
+
+    private Map map = new HashMap();
+
+    public CaptchaParams() { }
+
+    public CaptchaParams(Map map) {
+        this.map = map;
+    }
+
+    public Object get(Object key) {
+        return map.get(key);
+    }
+
+    public void put(Object key, Object value) {
+        map.put(key, value);
+    }
+}
\ No newline at end of file

Added: xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/captcha/CaptchaPlugin.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/captcha/CaptchaPlugin.java	2006-09-05 17:07:22 UTC (rev 1285)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/captcha/CaptchaPlugin.java	2006-09-06 08:41:06 UTC (rev 1286)
@@ -0,0 +1,154 @@
+/*
+ * Copyright 2006, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * @author Phung Hai Nam (phunghainam at xwiki.com)
+ * @version 5 Sep 2006
+ */
+
+package com.xpn.xwiki.plugin.captcha;
+
+import com.octo.captcha.module.config.CaptchaModuleConfigHelper;
+import com.octo.captcha.module.struts.CaptchaServicePlugin;
+import com.octo.captcha.service.CaptchaService;
+import com.octo.captcha.service.CaptchaServiceException;
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.api.Api;
+import com.xpn.xwiki.plugin.XWikiDefaultPlugin;
+import com.xpn.xwiki.plugin.XWikiPluginInterface;
+import com.xpn.xwiki.web.XWikiRequest;
+import org.apache.velocity.VelocityContext;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class CaptchaPlugin extends XWikiDefaultPlugin {
+
+    private static Log mLogger = LogFactory.getFactory().getInstance(com.xpn.xwiki.plugin.captcha.CaptchaPlugin.class);
+
+    public CaptchaPlugin(String name, String className, XWikiContext context) {
+        super(name, className, context);
+        init(context);
+    }
+
+    public String getName() {
+        return "jcaptcha";
+    }
+
+    public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context) {
+        return new CaptchaPluginApi((CaptchaPlugin) plugin, context);
+    }
+
+    public void flushCache() { }
+
+    public void init(XWikiContext context) {
+        super.init(context);
+    }
+
+    public CaptchaParams getCaptchaParams(String user, String action, XWikiContext context) throws XWikiException {
+        CaptchaParams cparams = new CaptchaParams();
+        cparams.put("user", user);
+        cparams.put("action", action);
+        return cparams;
+    }
+
+    public String displayCaptcha(CaptchaParams captchaParams, String classname, XWikiContext context) throws XWikiException {
+        if (context.getWiki().ParamAsLong("xwiki.plugin.captcha", 0) != 1) return "";
+        StringBuffer output = new StringBuffer();
+        String user = (String) captchaParams.get("user");
+        String action = (String) captchaParams.get("action");
+        String actionuser = "";
+        if(user.equals("XWiki.XWikiGuest")) actionuser = action + "_anonymous" ;
+        else if (user.equals("XWiki.Admin")) return ""; // not captcha with admin
+        else actionuser = action + "_registered"  ;
+
+        String typecaptcha = context.getWiki().getWebPreference(actionuser, context);
+
+        output.append("<div id=\"captcha\">");
+        if (typecaptcha.equalsIgnoreCase("image")) {
+            output.append("<table id=\"captchaimage\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">");
+            output.append("<tr><td align=\"left\" width=\"150\">Enter the code shown :</td>");
+            output.append("<td><input type=\"text\" name=\"jcaptcha_response\" size=\"29\"></td></tr>");
+            output.append("<tr><td nowrap=\"nowrap\" width=\"right\"><spacer type=\"horizontal\" width=\"150\"></td>");
+            output.append("<td><img class=\"");
+            output.append(classname);
+            output.append("\" ");
+            output.append("src=\"" + context.getDoc().getURL("jcaptcha",context) + "\">");
+            output.append("</td></tr>");
+            output.append("</table>");
+        } else if (typecaptcha.equalsIgnoreCase("text")) {
+            int term1 = (int) Math.floor(Math.random()*100);
+            int term2 = (int) Math.floor(Math.random()*100);
+            int sum = term1 + term2;
+            output.append("Please answer this simple math question: ");
+            output.append(term1 + " + " + term2 + " = ");
+            output.append("<input type='text' name='sum_answer' size='10' >");
+            output.append("<input type='hidden' name='sum_result' value='" + sum + "'>");
+        }
+        output.append("</div>");
+
+        return output.toString();
+    }
+
+    public Boolean verifyCaptcha(CaptchaParams captchaParams, XWikiContext context) throws XWikiException {
+        if (context.getWiki().ParamAsLong("xwiki.plugin.captcha", 0) != 1) return Boolean.TRUE;
+        String user = (String) captchaParams.get("user");
+        String action = (String) captchaParams.get("action");
+        String actionuser;
+        if(user.equals("XWiki.XWikiGuest")) actionuser = action + "_anonymous";
+        else if (user.equals("XWiki.Admin")) return Boolean.TRUE;  // if is admin then return TRUE for confirm captcha
+        else actionuser = action + "_registered";
+        String typecaptcha = context.getWiki().getWebPreference(actionuser, context);
+
+        XWikiRequest request = context.getRequest();
+        Boolean isResponseCorrect = Boolean.FALSE;
+        if (typecaptcha.equalsIgnoreCase("image")) {
+            CaptchaService service = CaptchaServicePlugin.getInstance().getService();
+            String responseKey = CaptchaServicePlugin.getInstance().getResponseKey();
+            String captchaID = CaptchaModuleConfigHelper.getId(request);
+            String challengeResponse = request.getParameter(responseKey);
+            request.removeAttribute(responseKey);
+            if (challengeResponse != null) {
+                try {
+                    isResponseCorrect = service.validateResponseForID(captchaID, challengeResponse);
+                } catch(CaptchaServiceException e) {
+                    request.setAttribute(CaptchaServicePlugin.getInstance().getMessageKey(), CaptchaModuleConfigHelper.getMessage(request));
+                }
+            } else {
+                isResponseCorrect = Boolean.TRUE;
+            }
+        } else if (typecaptcha.equalsIgnoreCase("text")) {
+            String sum = request.getParameter("sum_result");
+            String sum_answer = request.getParameter("sum_answer");
+            if((sum != null) && (sum_answer != null)) {
+                try {
+                    if (Integer.parseInt(sum) == Integer.parseInt(sum_answer))
+                        isResponseCorrect = Boolean.TRUE;
+                } catch (NumberFormatException e) {
+                    isResponseCorrect = Boolean.FALSE;
+                }
+            } else
+                isResponseCorrect = Boolean.TRUE;
+        } else
+            isResponseCorrect = Boolean.TRUE;
+        VelocityContext vcontext = (VelocityContext) context.get("vcontext");
+        vcontext.put("confirmresult", isResponseCorrect.toString());
+
+        return isResponseCorrect;
+    }
+}

Added: xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/captcha/CaptchaPluginApi.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/captcha/CaptchaPluginApi.java	2006-09-05 17:07:22 UTC (rev 1285)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/captcha/CaptchaPluginApi.java	2006-09-06 08:41:06 UTC (rev 1286)
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2006, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * @author Phung Hai Nam (phunghainam at xwiki.com)
+ * @version 5 Sep 2006
+ */
+
+package com.xpn.xwiki.plugin.captcha;
+
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.api.Api;
+
+public class CaptchaPluginApi extends Api {
+
+    private CaptchaPlugin plugin;
+
+    public CaptchaPluginApi(CaptchaPlugin plugin, XWikiContext context) {
+        super(context);
+        setPlugin(plugin);
+    }
+
+    public CaptchaPlugin getPlugin() {
+        return plugin;
+    }
+
+    private void setPlugin(CaptchaPlugin plugin) {
+        this.plugin = plugin;
+    }
+
+    public String displayCaptcha(CaptchaParams captchaParams, String classname) throws XWikiException {
+        return getPlugin().displayCaptcha(captchaParams, classname, context);
+    }
+
+    public Boolean verifyCaptcha(CaptchaParams captchaParams) throws XWikiException {
+        return getPlugin().verifyCaptcha(captchaParams, context);
+    }
+
+    public CaptchaParams getCaptchaParams(String user, String typecaptcha) throws XWikiException {
+        return getPlugin().getCaptchaParams(user, typecaptcha, context);
+    }
+}

Added: xwiki/trunk/src/test/cactus/com/xpn/xwiki/test/ServletCaptchaTest.java
===================================================================
--- xwiki/trunk/src/test/cactus/com/xpn/xwiki/test/ServletCaptchaTest.java	2006-09-05 17:07:22 UTC (rev 1285)
+++ xwiki/trunk/src/test/cactus/com/xpn/xwiki/test/ServletCaptchaTest.java	2006-09-06 08:41:06 UTC (rev 1286)
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2006, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * @author Phung Hai Nam (phunghainam at xwiki.com)
+ * @version 5 Sep 2006
+ */
+
+package com.xpn.xwiki.test;
+
+import org.apache.cactus.WebRequest;
+import org.apache.cactus.WebResponse;
+import org.hibernate.HibernateException;
+import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.objects.classes.BaseClass;
+import com.xpn.xwiki.objects.BaseObject;
+import com.xpn.xwiki.doc.XWikiDocument;
+import com.xpn.xwiki.store.XWikiHibernateStore;
+
+public class ServletCaptchaTest extends ServletTest {
+
+    public void setUp() throws Exception {
+        super.setUp();
+    }
+
+    public void cleanUp() {
+        super.cleanUp();
+    }
+
+    // This test for show captcha when edit to avoid spam robost
+    public void beginShowCaptchaForEdit(WebRequest webRequest) throws HibernateException, XWikiException {
+        XWikiHibernateStore hibstore = new XWikiHibernateStore(getHibpath());
+        StoreHibernateTest.cleanUp(hibstore, context);
+        clientSetUp(hibstore);
+
+        XWikiDocument doc = new XWikiDocument();
+        Utils.prepareObject(doc, "Main.ShowCaptchaForEditTest");
+        BaseClass bclass = doc.getxWikiClass();
+        BaseObject bobject = doc.getObject(bclass.getName(), 0);
+
+        Utils.createDoc(xwiki.getStore(), "Main", "ShowCaptchaForEditTest", bobject, bclass, context);
+        setUrl(webRequest, "edit", "Main", "ShowCaptchaForEditTest", "");
+    }
+
+    public void endShowCaptchaForEdit(WebResponse webResponse) throws HibernateException, XWikiException {
+         try {
+            String result = webResponse.getText();
+            assertTrue("Content should have captcha for confirm edit to avoid spam robots : " + result, result.indexOf("<div id=\"captcha\">") != -1);
+        } finally {
+            clientTearDown();
+        }
+    }
+
+    public void testShowCaptchaForEdit() throws Throwable {
+        launchTest();
+    }
+}

Modified: xwiki/trunk/src/test/cactus/com/xpn/xwiki/test/ServletSectionEditTest.java
===================================================================
--- xwiki/trunk/src/test/cactus/com/xpn/xwiki/test/ServletSectionEditTest.java	2006-09-05 17:07:22 UTC (rev 1285)
+++ xwiki/trunk/src/test/cactus/com/xpn/xwiki/test/ServletSectionEditTest.java	2006-09-06 08:41:06 UTC (rev 1286)
@@ -16,6 +16,9 @@
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * @author Phung Hai Nam (phunghainam at xwiki.com)
+ * @version 5 Sep 2006
  */
 package com.xpn.xwiki.test;
 
@@ -29,10 +32,6 @@
 import org.apache.cactus.WebResponse;
 import org.hibernate.HibernateException;
 
-/**
- * @author Phung Hai Nam
- * @version 28 Aug 2006
- */
 public class ServletSectionEditTest extends ServletTest {
 
     public void setUp() throws Exception {





More information about the Xwiki-notifications mailing list