Author: michiel
Date: 2010-02-09 09:50:00 +0100 (Tue, 09 Feb 2010)
New Revision: 40917

Modified:
   
mmbase/branches/MMBase-1_9/applications/resources/src/main/resources/META-INF/tags/mm/resources/captcha.tagx
Log:


Modified: 
mmbase/branches/MMBase-1_9/applications/resources/src/main/resources/META-INF/tags/mm/resources/captcha.tagx
===================================================================
--- 
mmbase/branches/MMBase-1_9/applications/resources/src/main/resources/META-INF/tags/mm/resources/captcha.tagx
        2010-02-08 17:51:01 UTC (rev 40916)
+++ 
mmbase/branches/MMBase-1_9/applications/resources/src/main/resources/META-INF/tags/mm/resources/captcha.tagx
        2010-02-09 08:50:00 UTC (rev 40917)
@@ -3,7 +3,7 @@
     xmlns:mm="http://www.mmbase.org/mmbase-taglib-2.0";
     xmlns:jsp="http://java.sun.com/JSP/Page";
     version="2.0">
-  <jsp:directive.tag 
import="java.io.*,java.util.*,org.mmbase.util.images.*,org.mmbase.servlet.FileServlet"
 />
+  <jsp:directive.tag import="java.io.*,java.util.*,org.mmbase.datatypes.*" />
 
   <!-- Length of key to  produce. The generated key will be stored in the 
session attribute
        'captchakey'
@@ -11,6 +11,8 @@
   -->
   <jsp:directive.attribute name="length"   type="java.lang.Integer" />
 
+  <jsp:directive.attribute name="swirl"   type="java.lang.Integer" />
+
   <!-- If template attribute is used, current node is an image, and temlate is 
used to generate the
        background of the captcha image. Defaults to a white back-ground. -->
   <jsp:directive.attribute name="template" type="java.lang.String" />
@@ -22,17 +24,11 @@
 
   <!-- If the key is stated explicitely, no new key is generated, but this 
given one is shown. -->
   <jsp:directive.attribute name="key"      type="java.lang.String" />
-  <jsp:declaration>
-
-    static final String[] charset = {"3","4","5","6","7","8", 
"a","b","c","d","e","f","h","i","j","k","m","n","p","q","r","s","t","u","v","w","x","y"};
    // 29 elements
-    static final Random rand = new Random();
-    static Timer deleter = new Timer(true);
-
-  </jsp:declaration>
   <jsp:scriptlet>
     InputStream input = null;
-    StringBuilder sb = new StringBuilder();
+    String text = null;;
     int length = 5;
+    int swirl = 30;
   </jsp:scriptlet>
   <c:if test="${! empty template}">
     <mm:node>
@@ -48,70 +44,30 @@
   </c:if>
   <c:if test="${! empty key}">
     <mm:write referid="key" jspvar="k" vartype="string" write="false">
-      <jsp:scriptlet>sb.append(k);</jsp:scriptlet>
+      <jsp:scriptlet>text = k;</jsp:scriptlet>
     </mm:write>
   </c:if>
   <mm:import jspvar="fillColor" from="this" externid="fill">000000</mm:import>
   <mm:import jspvar="backgroundColor" from="this" 
externid="background">white</mm:import>
   <jsp:scriptlet>
-    String base = "temporary_images";
-  </jsp:scriptlet>
-  <mm:import 
id="baseUrl"><jsp:expression>org.mmbase.servlet.MMBaseServlet.getBasePath("files")
 + base</jsp:expression></mm:import>
-
-  <jsp:scriptlet>
-    if (input == null) {
-       input = new ByteArrayInputStream(gifBytes);
+    if (text == null) {
+       text = CaptchaDataType.createString(length);
     }
-    if (sb.length() == 0) {
-      for(int n = 0; n &lt; length; n++) {
-        sb = sb.append(charset[rand.nextInt(28)]);
-      }
-    }
-    final File directory =  FileServlet.getFile(base, response);
-    directory.mkdir();
-    final File file = File.createTempFile("captcha", ".png", directory);
-    file.deleteOnExit();
-    String captchaKey = sb.toString();
-   // captchaKey = " ";
-   session.setAttribute("captchakey", captchaKey);
-   session.setAttribute("captchafilename", file + file.getName());
-   FileReceiver receiver = new FileReceiver(file);
-   //white pixel
+    CaptchaDataType.CaptchaImage image = new 
CaptchaDataType.CaptchaImage(text);
+    image.swirl = swirl;
+    image.fillColor = fillColor;
+    image.background = backgroundColor;
+    CaptchaDataType.createCaptchaImage(input, image);
 
-   List&lt;String&gt; commands = new ArrayList&lt;String&gt;();
-   if (backgroundColor != null &amp;&amp; !backgroundColor.equals("white") 
&amp;&amp; !backgroundColor.equals("#fff")) {
-     if ("transparent".equals(backgroundColor)) {
-       commands.add("transparent(white)");
-     } else {
-       commands.add("fill(" + backgroundColor + ")");
-       commands.add("draw(color 0,0 reset)");
-      }
-   }
-   commands.add("gravity(west)");
-   commands.add("s(80x22!)");
-   commands.add("fill(" + fillColor + ")");
-   commands.add("pointsize(20)");
-   commands.add("text(0,0,\'" + captchaKey + "')");
-   commands.add("f(png)");
-   commands.add("swirl(30)");
-   ImageConversionRequest req =
-   Factory.getImageConversionRequest(input, "gif", receiver, commands);
-
-   // should also be possible to use 'text', but that is broken with newer 
image-magicks.
-
-   req.waitForConversion();
-   deleter.schedule(new TimerTask() { public void run() {file.delete();} }, 
60000);
-   Dimension dim = receiver.getDimension();
-   int width = dim.getWidth();
-   int height = dim.getHeight();
+    session.setAttribute("captchakey", image.text); // deprecated
+    session.setAttribute(CaptchaDataType.KEY, image.text);
+    jspContext.setAttribute("image", image);
+    session.setAttribute("captchafilename", image.file.getName());
   </jsp:scriptlet>
-  <mm:import 
id="filename"><jsp:expression>file.getName()</jsp:expression></mm:import>
-  <mm:import id="width" vartype="integer" 
><jsp:expression>width</jsp:expression></mm:import>
-  <mm:import id="height" 
vartype="integer"><jsp:expression>height</jsp:expression></mm:import>
-  <mm:link page="${baseUrl}/${filename}">
+  <mm:link page="${image.path}">
     <c:choose>
-      <c:when test="${width > 0}">
-        <img src="${_}" alt="captcha" width="${width}" height="${height}" />
+      <c:when test="${image.width > 0}">
+        <img src="${_}" alt="captcha" width="${image.width}" 
height="${image.height}" />
       </c:when>
       <c:otherwise>
         <img src="${_}" alt="captcha" />

_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to