Code Monkey home page Code Monkey logo

zf1-recaptcha-2's Introduction

ZF1 reCAPTCHA2 - Google reCAPTCHA integration

ZF1 ships with Google reCAPTCHA version 1. Now that version 2 is available you will probably want to use an up-to-date element for your Zend Framework installations.

Installation

Composer Install

"require": {
    "cgsmith/zf1-recaptcha-2": "~1.0"
}

Usage

Install the latest version (1.0.1) and setup your plugins and form abstract with the following information:

  • On your application.ini or where you want
recaptcha.sitekey = "YOUR SITE KEY GIVEN BY GOOGLE"
recaptcha.secretkey = "YOUR SECRET KEY GIVEN BY GOOGLE"
  • In your bootstrap:
public function _initView()
{
    $view = new Zend_View();

    $view->addHelperPath(
        APPLICATION_PATH . '/../vendor/cgsmith/zf1-recaptcha-2/src/Cgsmith/View/Helper',
        'Cgsmith\\View\\Helper\\'
    );

    Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_ViewRenderer($view));

    return $view;
}

public function _initRecaptcha()
{
    $config = \Zend_Registry::get('application');
    $params = $config->recaptcha->toArray();

    $params['messageTemplates'] = [
        \Cgsmith\Validate\Recaptcha::INVALID_CAPTCHA => 'The captcha was invalid', // set custom/translated message
        \Cgsmith\Validate\Recaptcha::CAPTCHA_EMPTY => 'The captcha must be completed'
    ];

    \Zend_Registry::set('recaptcha', $params);
}
  • Below is what you would setup in your form.
<?php

// create your element
$this->addElement(new \Cgsmith\Form\Element\Recaptcha());
  • On your controller, after "validate the post data"
if($form->isValid($_POST)) {
	$values = $form->getValues();
	unset($values['g-recaptcha-response']);
	// Your business logic must be here
}

About

Requirements

  • ZF1 reCAPTCHA2 works with PHP 5.5 or above.

Submitting bugs and feature requests

Bugs and feature request are tracked on GitHub

Author

Chris Smith - [email protected] - http://twitter.com/cgsmith105

License

ZF1 reCAPTCHA2 is licensed under the MIT License - see the LICENSE file for details

zf1-recaptcha-2's People

Contributors

cgsmith avatar chrif avatar joachimjusth avatar karlhorky avatar lukassteiner avatar solverat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

zf1-recaptcha-2's Issues

Site key and secret key must be specified.

I get this:

Site key and secret key must be specified.

Can you help me, please?

modules/default/forms/Registration.php
$this->addElement(new \Cgsmith\Form\Element\Recaptcha());

configs/application-local.php

return [
    'recaptcha' => [
        'sitekey' => 'my site key',
        'secretkey' => 'my secret key',
    ]
];

modules/default/Bootstrap.php

public function _initView()
    {
        $view = new Zend_View();

        $view->addHelperPath(
            APPLICATION_PATH . '/../vendor/cgsmith/zf1-recaptcha-2/src/Cgsmith/View/Helper',
            'Cgsmith\\View\\Helper\\'
        );

        Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_ViewRenderer($view));

        return $view;
    }

    public function _initRecaptcha()
    {
        $config = \Zend_Registry::get('appConfig');
        $params = $config->recaptcha->toArray();

        $params['messageTemplates'] = [
            \Cgsmith\Validate\Recaptcha::INVALID_CAPTCHA => 'The captcha was invalid', // set custom/translated message
            \Cgsmith\Validate\Recaptcha::CAPTCHA_EMPTY => 'The captcha must be completed'
        ];

        \Zend_Registry::set('recaptcha', $params);
    }

Doesn't work with sub-forms

Hi,

Really nice that you made this! But when you work with sub-forms in ZF, the script doesn't work anymore. That's because Google generates it's own code for the input field (textarea) and that's always the POST param "g-recaptcha-response".

So when working with sub-forms you never retrieve the value of that field. I made a work-around by retrieving the raw request params and than perform a verify with the API (the same code that you use in the _verify method).

But just wanted to let you know this.. if you would find a more suitable solution that would be great of course.. I'll try myself, if I find something, I'll let you know!

allow custom classes in viewHelper

it would be great if we could add some custom classes.

i added and tested the modified method in Cgsmith\View\Helper\FormReceptcha and it's working:

public function formRecaptcha($name, $value = null, $attribs = null, $options = null, $listsep = '')
{
    if (!isset($attribs['siteKey']) || !isset($attribs['secretKey'])) {
        throw new \Zend_Exception('Site key is not set in the view helper');
    }

    $customClasses = '';
    if( isset( $attribs['classes'] )) {
        if( is_array( $attribs['classes'] ) ) {
            $customClasses = implode(' ', $attribs['classes']);
        } else {
            $customClasses = $attribs['classes'];
        }
    }

    return '<div class="g-recaptcha ' . $customClasses . '" data-sitekey="' . $attribs['siteKey'] . '"></div>';
}

Naming problems

Thank You for Your work.

I found some errors inside code.
Class Cgsmith\Validate\ValidateRecaptcha should be named 'Recaptcha' or Zend will not load it.
Because You are using namespaces every Zend_Exception should be executed as \Zend_Exception.

That's alle I think.

Zend_Form_Element::filterName issue

Hi there

Im having trouble with hydration - isValid always returns false as it thinks the value is empty. Zend_Form_Element::filterName() filters out '-' characters when setting the element name, obviously this is an issue as Google renders the element on the page with the '-' characters (g-recaptcha-response becomes grecaptcharesponse). Is there a known work around for this short of extending the Zend_Form_Element class to include the '-' character in the list of allowed name characters?

Many thanks!
Jon

Receiving "missing-input-response" in google response

In the validate class, $this->_value is not set in public function isValid($value, $context = null).

protected function _verify() then uses $this->_value to build query string.

This results in an empty value being passed to the google api servers.

Zend_Form_Element not found

I get this:
Fatal error: Class 'Cgsmith\Form\Element\Zend_Form_Element' not found in /home/eakdafreak/domains/eakdafreak.com/vendor/cgsmith/zf1-recaptcha-2/src/Cgsmith/Form/Element/Recaptcha.php on line 13

Can you help me, please?

Merge to new ZF1 fork

Hi, I am reaching out to ask if we can merge your library directly into one of the currently running ZF1 forks. That is covered under GPL3 and your library is marked up GPL3.

Shardj/zf1-future#343

Fails to validate when PHP setting arg_separator.output = '&amp;'

Tracked down an issue in our PHP setup where the captcha failed to validate.
Google server response: "missing-input-response"

It is caused by our PHP INI that has the setting:
arg_separator.output = "&amp;"

This leads to the query string generated using http_build_query() to be concatenated by the separator &amp; which makes the google server fail to parse the post parameters.

My fix is to force it to use a single '&' in /Validate/Recaptcha.php#L81:

    $queryString = http_build_query([
        'secret'   => $this->_secretKey,
        'response' => $value,
        'remoteIp' => $_SERVER['REMOTE_ADDR']
    ], '', '&');

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.