Falundrim
Falundrim

Reputation: 63

How tu use recaptcha google with phalcon framework

I'm still trying to add a recaptcha to my website, I want try the recaptcha from Google but I can't use it properly. Checked or not, my email is still sent.

I tried to understand the code of How to validate Google reCaptcha v2 using phalcon/volt forms?.

But i don't understand where are my problems and more over how can you create an element like

 $recaptcha = new Check('recaptcha');

My controller implementation :

    <?php

/**
 * ContactController
 *
 * Allows to contact the staff using a contact form
 */
class ContactController extends ControllerBase
{
    public function initialize()
    {
        $this->tag->setTitle('Contact');
        parent::initialize();
    }

public function indexAction()
{
    $this->view->form = new ContactForm;
}

/**
 * Saves the contact information in the database
 */
public function sendAction()
{
    if ($this->request->isPost() != true) {
        return $this->forward('contact/index');
    }

    $form = new ContactForm;
    $contact = new Contact();

    // Validate the form
    $data = $this->request->getPost();
    if (!$form->isValid($data, $contact)) {
        foreach ($form->getMessages() as $message) {
            $this->flash->error($message);
        }
        return $this->forward('contact/index');
    }

    if ($contact->save() == false) {
        foreach ($contact->getMessages() as $message) {
            $this->flash->error($message);
        }
        return $this->forward('contact/index');
    }

    $this->flash->success('Merci, nous vous contacterons très rapidement');
    return $this->forward('index/index');
}

}

In my view i added :

<div class="g-recaptcha" data-sitekey="mypublickey0123456789"></div>
{{ form.messages('recaptcha') }}

But my problem is after : i create a new validator for the recaptcha like in How to validate Google reCaptcha v2 using phalcon/volt forms? :

use \Phalcon\Validation\Validator;
use \Phalcon\Validation\ValidatorInterface;
use \Phalcon\Validation\Message;

class RecaptchaValidator extends Validator implements ValidatorInterface
{
    public function validate(\Phalcon\Validation $validation, $attribute) 
    {
        if (!$this->isValid($validation)) {
            $message = $this->getOption('message');
            if ($message) { 
                $validation->appendMessage(new Message($message, $attribute, 'Recaptcha'));
            }
            return false;
        }
        return true;
    }


    public function isValid($validation) 
    {
        try {

            $value =  $validation->getValue('g-recaptcha-response');
            $ip    =  $validation->request->getClientAddress();

            $url = $config->'https://www.google.com/recaptcha/api/siteverify'
            $data = ['secret'   => $config->mysecretkey123456789
                     'response' => $value,
                     'remoteip' => $ip,
                    ];

            // Prepare POST request
            $options = [
                'http' => [
                    'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                    'method'  => 'POST',
                    'content' => http_build_query($data),
                ],
            ];

            // Make POST request and evaluate the response
            $context  = stream_context_create($options);
            $result = file_get_contents($url, false, $context);
            return json_decode($result)->success;
        }
        catch (Exception $e) {
            return null;
        }
    }
}

So i don't know if tjis code is correct anyway, i have a problem too after that : how to create an object "recaptcha" in my form add

$recaptcha = new ?????('recaptcha');
        $recaptcha->addValidator(new RecaptchaValidator([
            'message' => 'Please confirm that you are human'
        ]));
        $this->add($recaptcha);

PS: I apologize because i'm a noob here and my mother tongue is not english, so if you don't understand me or want give me some advices to create a proper question, don't hesitate ^^

Upvotes: 3

Views: 936

Answers (1)

Nikolay Mihaylov
Nikolay Mihaylov

Reputation: 3876

I've made a custom form element for recaptcha. Used it for many projects so far.

The form element class:

class Recaptcha extends \Phalcon\Forms\Element
{
    public function render($attributes = null)
    {
        $html = '<script src="https://www.google.com/recaptcha/api.js?hl=en"></script>';
        $html.= '<div class="g-recaptcha" data-sitekey="YOUR_PUBLIC_KEY"></div>';
        return $html;
    }
}

The recaptcha validator class:

use Phalcon\Validation\Validator;
use Phalcon\Validation\ValidatorInterface;
use Phalcon\Validation\Message;

class RecaptchaValidator extends Validator implements ValidatorInterface
{
    public function validate(\Phalcon\Validation $validation, $attribute)
    {
        $value = $validation->getValue('g-recaptcha-response');
        $ip = $validation->request->getClientAddress();

        if (!$this->verify($value, $ip)) {
            $validation->appendMessage(new Message($this->getOption('message'), $attribute, 'Recaptcha'));
            return false;
        }
        return true;
    }

    protected function verify($value, $ip)
    {
        $params = [
            'secret' => 'YOUR_PRIVATE_KEY',
            'response' => $value,
            'remoteip' => $ip
        ];
        $response = json_decode(file_get_contents('https://www.google.com/recaptcha/api/siteverify?' . http_build_query($params)));

        return (bool)$response->success;
    }
}

Using in your form class:

$recaptcha = new Recaptcha($name);
$recaptcha->addValidator(new RecaptchaValidator([
    'message' => 'YOUR_RECAPTCHA_ERROR_MESSAGE'
]));

Note 1: You were almost there, you just missed to create custom form element (the first and last code piece from my example);

Note 2: Also there is a library in Github: https://github.com/fizzka/phalcon-recaptcha I have not used it, but few peeps at phalcon forum recommended it.

Upvotes: 3

Related Questions