user1818439
user1818439

Reputation: 33

CakePHP 2.2.4 reCAPTCHA validation not working

Hello I have a signup system working in cakePHP 2.2.4 signup the reCAPTCHA I'm using from this link Jahdrien/ReCaptcha-Plugin is showing on my view but the validation of the codes is not working please can someone tell me how to do the reCAPTCHA validation?

UsersController.php // My Cotroller.

<?php
class UsersController extends AppController{
    public $components = array('Recaptcha.Recaptcha');
    public $helpers = array('Recaptcha.Recaptcha');
    public function signup(){

        $d = $this->request->data;
        $d['User']['id'] = null;
        if(!empty($d['User']['password'])){
                $d['User']['password'] = Security::hash($d['User']['password'],null,true);  
        }
        if($this->User->save($d,true,array('username','password','email'))){
            $link = array('controller'=>'Users','action'=>'activate',$this->User->id.'-'.md5($d['User']['password']));
            App::uses('CakeEmail','Network/Email');
            $mail = new CakeEmail();
            $mail->from('[email protected]')
                 ->to($d['User']['email'])
                 ->subject('CakePHP Test :: Registration on Ohyeahhh.com')
                 ->emailFormat('html')
                 ->template('signup')
                 ->viewVars(array('username'=>$d['User']['username'],'link'=>$link))
                 ->send();
                 $this->request->data = array();
            $this->Session->setFlash("Your account was successfully created.","notif",array('type'=>'Success'));
        }else{
            $this->Session->setFlash("Please correct the following errors.","notif");
        }

    }
?>

User.php // My Model.

<?php
class User extends AppModel{
    public $validate = array(
        'username'=>array(
            array(
                'rule'=>'alphaNumeric',
                'allowEmpty'=>false,
                'message'=>'Invalide Username!'
            ),
            array(
                'rule' => array('minLength', '4'),
                'message' => 'Username has to be more than 3 chars'
            ),
            array(
                'rule'=>'isUnique',
                'message'=>'Username already taken!'
            )
        ),
        'password' => array(
                array(
                    'rule' => 'alphaNumeric',
                    'allowEmpty'=>false,
                    'message' => 'Password must be AlphaNumeric!'
                ),
                array(
                    'rule' => array('minLength', '4'),
                    'message' => 'Username has to be more that 3 chars'
                ),
                array(
                    'rule' => array('confirmPassword', 'password'),
                    'message' => 'Passwords do not match'
                )), 
        'confirmpassword' => array(
                            'rule' => 'alphanumeric'
        ),
        'email'=>array(
            array(
                'rule'=>'email',
                'allowEmpty'=>false,
                'required'=>true,
                'message'=>'Invalide mail adress!'
            ),
            array(
                'rule'=>'isUnique',
                'message'=>'Mail adress already taken!'
            )
        )
    );
    function confirmPassword($data)
    {
        if ($data['password'] == Security::hash($this->data['User']['confirmpassword'],null,true)) {
            return true;
        }
            return false;
    }
}
?>

signup.ctp // My view.

<?php 

echo $this->Session->flash();
echo $this->Form->create('User');   
echo $this->Form->input('username' ,array('label'=>"Username :")); 
echo $this->Form->input('password' ,array('label'=>"Password :")); 
echo $this->Form->input('confirmpassword' ,array('label'=>"Password (type again to catch typos) :", 'type' => 'password')); 
echo $this->Form->input('email' ,array('label'=>"Email :"));  
echo $this->Recaptcha->show(array(
                'theme' => 'red',
                'lang' => 'en',
            ));
echo $this->Recaptcha->error();
echo $this->Form->end('Register'); 
?>

The Recaptcha folder is in my "root/plugin/" folder.

This is the ValidationBehavior.php that I don't know how to use to make the validation working.

<?php
class ValidationBehavior extends ModelBehavior {
    function beforeValidate(&$model) {
        die(' probando funcion de validacion ');
        $model->validate['recaptcha_response_field'] = array(
            'checkRecaptcha' => array(
                'rule' => array('checkRecaptcha', 'recaptcha_challenge_field'),
                'required' => true,
                'message' => 'You did not enter the words correctly. Please try again.',
            ),
        );
    }

    function checkRecaptcha(&$model, $data, $target) {
        App::import('Vendor', 'RecaptchaPlugin.recaptchalib');
        Configure::load('RecaptchaPlugin.key');
        $privatekey = Configure::read('Recaptcha.Private');
        $res = recaptcha_check_answer(
            $privatekey,                            $_SERVER['REMOTE_ADDR'],
            $model->data[$model->alias][$target],   $data['recaptcha_response_field']
        );
        return $res->is_valid;
    }
}
?>

Thanks.

Upvotes: 0

Views: 1787

Answers (2)

Mar Lu
Mar Lu

Reputation: 44

Using CakeDC recaptcha is a good solution. Just implemented it without issues. If you getting an error it is probably because the plugin is not being loaded. Make sure that all files are placed in the Plugin\Recaptcha directory and you have following line added to the bootstrap.php:

CakePlugin::load('Recaptcha');

Upvotes: 0

floriank
floriank

Reputation: 25698

Honestly try ours.

https://github.com/CakeDC/recaptcha

It's united tested and documented. The one you're using is actually not really well done.

Upvotes: 2

Related Questions