0

captcha code

asked 2011-07-01 09:06:58 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

Hi all,

does anyone have integrate a non zk captcha component while working with ce edition and can post the codes for this.

thanks
Stephan

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2011-07-01 09:18:12 +0800

mjablonski gravatar image mjablonski
1284 3 5
http://www.jease.org/

updated 2011-07-01 09:19:33 +0800

Hi Stephan,

I'm using a non-ZK-Captcha based on SimpleCaptcha for my JeaseCMS. You can see it in use here:

http://www.jease.org/blog/quotation_marks/

Although it is not integrated as "native" ZK component, the usage within ZK is very straighforward. Building a server side wrapper for this should be a job of less than an hour:

<zk> 
 <vlayout id="input" width="100%"> 
   <image src="/site/service/Captcha.jsp?id=myCaptcha" /> 
   Please enter the code: 
   <textbox id="code" hflex="1" /> 
   <button label="Submit"> 
    <attribute name="onClick"><![CDATA[ 
      if (code.getValue().equals( 
          Sessions.getCurrent().getNativeSession().getAttribute("myCaptcha"))) { 
        alert("Code is correct"); 
      } else { 
        alert("Code is wrong"); 
      } 
    ]]></attribute> 
   </button> 
 </vlayout> 
</zk> 

All you need is this JSP... you can transform it easily into a servlet as well:

http://code.google.com/p/jease/source/browse/trunk/Jease/site/service/Captcha.jsp

And the corresponding jar-file:

http://jease.googlecode.com/svn/trunk/Jease/WEB-INF/lib/simplecaptcha.jar

Cheers, Maik

link publish delete flag offensive edit

answered 2011-07-01 09:27:55 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2011-07-01 09:28:42 +0800

Thank you Maik. I will study this.

Many thanks
Stephan

PS: I'm wondering why such an global used comp is not in the ce version ?????

link publish delete flag offensive edit

answered 2011-07-01 09:58:50 +0800

TonyQ gravatar image TonyQ
642
https://www.masterbranch....

Here's also an captcha component that base on google reCaptcha service.

https://github.com/tony1223/zk-recaptcha

link publish delete flag offensive edit

answered 2011-07-01 11:12:21 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

Maik. What version you use?

I have problems with GradiatedBackgroundProducer( Color.WHITE, Color.LIGHT_GRAY ) .
In my loaded version (v. 1.1.1) it accepts no arguments.
I need a version which is available from a maven repo.

best
Stephan

link publish delete flag offensive edit

answered 2011-07-01 12:09:35 +0800

mjablonski gravatar image mjablonski
1284 3 5
http://www.jease.org/

I'm using 1.1.1 which is available via SourceForge:

http://sourceforge.net/projects/simplecaptcha/files/

I just compared this jar-file with the one available via Maven (and declared as 1.1.1). There are differences between this two jars, as for example the GradiatedBackgroundProducer does take no arguments for the Maven version. If you still want to go with Maven, just leave out the parameters and use the default constructor.

Cheers, Maik

link publish delete flag offensive edit

answered 2011-07-01 14:57:52 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2011-07-27 04:29:22 +0800

Great Maik,
thanks TonyQ too,

here is the pic from the 'forgotten password' dialog.
I will try to integrate the codes for all in the Zksample2 too.

thanks
Stephan

CaptchaUtils.java:

import nl.captcha.Captcha;
import nl.captcha.backgrounds.GradiatedBackgroundProducer;
import nl.captcha.text.renderer.ColoredEdgesWordRenderer;

/**
 * EN: Utility class for creating a CAPTCHA.<br>
 * Captcha can be direct loaded into a org.zkoss.zul.Image<br>
 * DE: Hilfsklasse zur Erzeugung eines CAPTCHA.<br>
 * Captcha kann direkt in ein org.zkoss.zul.Image eingelesen werden.
 * 
 * <pre>
 * Image img = new org.zkoss.zul.Image();
 * img.setContent(CaptchaUtils.getCaptcha().getImage());
 * 
 * String verifyStr = captcha.getAnswer();
 * </pre>
 * 
 * @author Stephan Gerth
 */
public class FDCaptchaUtils {

	public FDCaptchaUtils() {
	}

	/**
	 * Create a 5 digits captcha.
	 * 
	 * @return
	 */
	public static Captcha getCaptcha() {

		Captcha captcha = new Captcha.Builder(140, 50)
                        .addText(new ColoredEdgesWordRenderer()).addNoise()
                        .addBackground(new GradiatedBackgroundProducer())
                        .addBorder()
                        .build();

		return captcha;
	}

}

Pieces of the zul view:

    . . .
						<grid sclass="GridPlain"
							style="padding-right: 15px;">
							<columns>
								<column align="right" width="180px" />
								<column />
							</columns>
							<rows>

								<!-- Captcha / Captcha -->
								<row>
									<label
										value="${c:l('common.Captcha')}" />
									<hbox>



====>>>									<image id="img_Captcha" />



										<toolbarbutton id="btnReCaptcha"
											image="/images/icons/refresh_green_16x16.gif"
											tooltiptext="${c:l('common.ReCaptcha')}" />
									</hbox>
								</row>
								<!-- Verify Captcha / Verifiziere Captcha -->
								<row>
									<label
										value="${c:l('common.Repeat')}" />
									<hbox>
										<textbox id="txtb_VerifyCaptcha"
											maxlength="6" width="140px" />
										<toolbarbutton
											id="btnVerifyCaptcha" image="/images/icons/start_16x16.gif"
											tooltiptext="${c:l('common.Check.Input')}" />
									</hbox>
								</row>
							</rows>
						</grid>

	. . .

Pieces of the Controller:

public class ForgottenPasswordCtrl extends GenericForwardComposer implements Serializable {

   . . .
	protected Window windowForgottenPassword; // autowired

	protected Image img_Captcha; // autowired
	protected Textbox txtb_EmailAddress; // autowired
	protected Textbox txtb_VerifyCaptcha; // autowired
	protected Button btnSendPassword; // autowired

	private Captcha captcha;

   . . .

	public void onCreate$windowForgottenPassword(Event event) throws Exception {

		// Create the captcha and set it as image
		doReCaptcha(event);

		windowForgottenPassword.doModal();
	}

	public void onClick$btnClose(Event event) throws InterruptedException {...
	public void onClick$btnSendPassword(Event event) {...
	public void onClick$btnReCaptcha(Event event) {...
	public void onClick$btnVerifyCaptcha(Event event) {...

    . . .

	/**
	 * Do a re-Captcha.<br>
	 * 
	 * @param event
	 */
	private void doReCaptcha(Event event) {
		// init: disable the send password button
		btnSendPassword.setDisabled(true);

		setCaptcha(FDCaptchaUtils.getCaptcha());
		img_Captcha.setContent(getCaptcha().getImage());
	}

	/**
	 * 1. Checks if the email address is entered.<br>
	 * 2. Verify the created captcha digits against the users input from a textbox.<br>
	 * 3. Enable/disable the 'reset password' button.<br>
	 * 
	 * @param event
	 */
	private void doVerifyCaptcha(Event event) {

		// init: disable the send password button
		btnSendPassword.setDisabled(true);

		// check if the email address is entered
		if (StringUtils.isEmpty(txtb_EmailAddress.getValue())) {
			throw new WrongValueException(txtb_EmailAddress, Labels.getLabel("message.Error.CannotBeEmpty"));
		}

		// check if the captcha is verified correctly
		if (StringUtils.equals(txtb_VerifyCaptcha.getValue(), getCaptcha().getAnswer())) {
			btnSendPassword.setDisabled(false);
		}
	}

    . . .

        // +++++++++++++++++++++++++++++++++++++++++++++++++ //
	// ++++++++++++++++ Setter/Getter ++++++++++++++++++ //
	// +++++++++++++++++++++++++++++++++++++++++++++++++ //

	public void setCaptcha(Captcha captcha) {
		this.captcha = captcha;
	}

	public Captcha getCaptcha() {
		return captcha;
	}

    . . . 

link publish delete flag offensive edit

answered 2012-03-12 08:26:19 +0800

mayurk gravatar image mayurk
21

Hey Guys,

It really works fine.

Thanks to all of you

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-07-01 09:06:58 +0800

Seen: 1,091 times

Last updated: Mar 12 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More