2

I am developing a simple web form to collect feedback from visitors of my website. Since the visitor is not authenticated, I would like to implement an image verification step for the users. I have searched in atk4 documentation and couldn't find any reference for a similar implementation. I have also checked at4-addons source and found two resources - View_ReCaptcha and Form_Field_Verification.

I'm not sure which one should I follow. Is there any example code or tutorial which I can refer to implement a image verification step for my web form?

libregeek
  • 1,264
  • 11
  • 23
  • There are no tutorials at this time. It's not working perfectly either, was donated from a project. I'll add the usage into the answer and if you think you can improve this component, I'd greatly appreciate that. – romaninsh Oct 07 '11 at 14:21

1 Answers1

2

KCapcha in Agile Toolkit

This implements a very hard-core implementation of a captcha, without use of any controllers or views. Of course it's discouraged to code like that, but if you have your deadline, this is the last resort you can use:

$sec_image_field = $f->addField('line', 'sec_image', 'Security code')
    ->setNotNull()
    ->setNoSave();


$captcha_src = '/lib/kcaptcha/?' . $session_name . '=' . session_id();
$kaptcha_img = $sec_image_field->getTag('img',array('src' => $captcha_src, 'id' => 'kpt' ));
$kaptcha_img .= ' <a href="#" onclick="d=new Date(); (jQuery(\'#kpt\')'.
   '.attr(\'src\', \'' . $captcha_src . '&t=\' + d.getTime()));return false;">';
$kaptcha_img .= '<i class="atk-icon atk-icons-nobg atk-icon-arrows-left3"></i>';
$kaptcha_img .= ' reload</a>';
$sec_image_field->template->set('after_field', '<ins>Enther the code you see below</ins> <span>' . $kaptcha_img . '</span>'

Note: you'll need to download and install kcapcha into /lib/kcapcha/ from their website.

Example: http://agiletech.ie/contact

ReCapcha in Agile Toolkit

ReCapcha implementation you have found is much more decent, but not Ideal. It manually looks into the POST data and simply sets flag telling you if it was typed properly or not. Here is example usage:

$rc = $form->add('View_ReCaptcha');
....
if($form->isSubmitted()){
    ...

    if(!$rc->isValid()){

        $js=$this->js->univ();

        if($r->getError()){
            $js->alert($rc->getError());
        }else{
            $js->alert('Error in capcha');
        }
        $js->execute();
    }
    ....
}

Note: You would need recapcha lib installed. See source.

Making better Capcha

Probably the best way would be to introduce a new field type, (Form_Field_Capcha) which does the whole thing completely automatically. It shouldn't bundle any PHP libraries but would rely on 3rd party service for image generation. It also must use the standard form validation.

Community
  • 1
  • 1
romaninsh
  • 10,606
  • 4
  • 50
  • 70
  • It seems I have to go with the kcaptcha solution for now, since the requirement is very urgent. I will let you know the progress of implementation. – libregeek Oct 07 '11 at 15:27
  • I'll surely blog on http://agiletoolkit.org/blog/ once something better will appear and possibly edit my post here. – romaninsh Oct 07 '11 at 15:51