Zend Framework 2: How to replace Figlet with reCaptcha with zfcUser correctly

I am trying to replace Figlet with reCaptcha in the zfcUser registration form. Partial instructions on how to do this can be found at https://github.com/ZF-Commons/ZfcUser#changing-registration-captcha-element , but the full instruction does not exist.

Validating the README.md file has two-step instructions on how to do this, but CAPTCHA uses Figlet when rendering on the form.

Has anyone successfully implemented this? I really need a hand on this.

Thanks in advance.


EDIT: Here is a proven working solution that I developed:

1. Add to the composer .json

// Add the lines below under the "require" element: "require": { "php": ">=5.3.3", "zendframework/zendframework": ">2.2.0rc1", "zendframework/zendservice-recaptcha": "2.*" 

}

2. Change to your ZF2 installation directory and run the following command:

 php composer.phar update 

3. Replace or create config / autoload / database.global.php with

 <?php $config = array( 'dbdriver' => 'pdo', 'dbhost' => 'localhost', 'dbport' => '3306', 'dbname' => 'CHANGEME', 'dbuser' => 'CHANGEME', 'dbpass' => 'CHANGEME', ); return array( 'service_manager' => array( 'factories' => array( 'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory', ), ), 'db' => array( 'driver' => 'pdo', 'dsn' => 'mysql:dbname='.$config['dbname'].';host='.$config['dbhost'], 'username' => $config['dbuser'], 'password' => $config['dbpass'], ), ); 

4: run this on your mySQL server:

 CREATE TABLE `user` ( `user_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, `username` VARCHAR(255) DEFAULT NULL UNIQUE, `email` VARCHAR(255) DEFAULT NULL UNIQUE, `display_name` VARCHAR(50) DEFAULT NULL, `password` VARCHAR(128) NOT NULL, `state` SMALLINT UNSIGNED ) ENGINE=InnoDB CHARSET="utf8"; 

5. Create / Replace config / autoload / recaptcha.global.php with

 <?php define('RECAPTCHA_PRIVATE_KEY','CHANGEME'); define('RECAPTCHA_PUBLIC_KEY','CHANGEME'); return array( 'zfcuser' => array( 'form_captcha_options' => array( 'class' => 'Zend\Captcha\ReCaptcha', 'options' => array( 'privkey' => RECAPTCHA_PRIVATE_KEY, 'pubkey' => RECAPTCHA_PUBLIC_KEY, ), ), ), 'di'=> array( 'instance'=>array( 'alias'=>array( 'recaptcha_element' => 'Zend\Form\Element\Captcha', ), 'ZfcUser\Form\Register' => array( 'parameters' => array( 'captcha_element'=>'recaptcha_element', ), ), ), ), ); 

6. Create / Replace config / autoload / zfcuser.global.php with

 <?php $settings = array( 'enable_registration' => true, 'enable_username' => true, 'auth_adapters' => array( 100 => 'ZfcUser\Authentication\Adapter\Db' ), 'enable_display_name' => false, 'auth_identity_fields' => array( 'email' ), 'use_registration_form_captcha' => true, 'user_login_widget_view_template' => 'zfc-user/user/login.phtml', ); return array( 'zfcuser' => $settings, 'service_manager' => array( 'aliases' => array( 'zfcuser_zend_db_adapter' => (isset($settings['zend_db_adapter'])) ? $settings['zend_db_adapter']: 'Zend\Db\Adapter\Adapter', ), ), ); 

7. Go to http://yourdomain.com/user

8. Enjoy! :)

+4
source share
1 answer

Here is how I did it, it may not be the best or the right way, but it worked for me:

Add the recaptcha service to your composer.json file:

 "require": { "Zendframework/zendservice-recaptcha": "2.*" } 

Launch the composer to get the service. Then you need to specify the configuration of ReCaptcha. I created a separate configuration file for storing ReCaptcha keys:

 //zfcuser.local.php return array( 'zfcuser' => array( 'form_captcha_options' => array( 'options' => array( 'privkey' => RECAPTCHA_PRIVATE_KEY, 'pubkey' => RECAPTCHA_PUBLIC_KEY, ), ), ), ); 

Then the ZfcUser captcha config looks like this, telling him to use the ReCaptcha service:

 //zfcuser.global.php 'form_captcha_options' => array( 'class' => 'Zend\Captcha\ReCaptcha', 'options' => array( 'wordLen' => 6, 'expiration' => 300, 'timeout' => 300, ), ), 

Edit:

You do not need recaptcha.global.php . You can invoke the configuration file as you like, as it ends with .global.php or .local.php. Usually you name things .local.php when you don't want them to be in version control.

In this case, I named the file zfcuser.local.php because all it does is store the ReCaptcha keys, and I did not want them to be in version control.

All configuration files are combined into one array when the application starts. So basically ignore the ZfcUser documentation. Or maybe someone else can explain how to make it work that way.

The third block of code is zfcuser.global.php.

+3
source

Source: https://habr.com/ru/post/1482087/


All Articles