diff --git a/README.md b/README.md index 60944a0..a9b104c 100644 --- a/README.md +++ b/README.md @@ -7,12 +7,15 @@ Google reCAPTCHA for CakePHP 3 [![License](https://poser.pugx.org/cakephp-fr/recaptcha/license.png)](https://packagist.org/packages/cakephp-fr/recaptcha) [![Total Downloads](https://poser.pugx.org/cakephp-fr/recaptcha/d/total.png)](https://packagist.org/packages/cakephp-fr/recaptcha) +Be careful version 1.0 is for CakePHP 3.6 minimum. +For a previous CakePHP version please use the 0.4.2 version. + ## Plugin's Objective ## This plugin adds functionalities to use the new reCAPTCHA API version 2.0 in CakePHP projects. -This plugin is still under development... For now multiple widgets on a single page is not available. +This plugin is still under development... For now, multiple widgets on a single page is not available. ## Requirements ## @@ -28,7 +31,7 @@ _[Using [Composer](http://getcomposer.org/)]_ Add the plugin to your project's `composer.json` - something like this: ```bash -composer require cakephp-fr/recaptcha:~0.4 +composer require cakephp-fr/recaptcha:~1.0 ``` You then need to load the plugin, by running: @@ -85,7 +88,7 @@ For example: ```php public function initialize() { parent::initialize(); - if ($this->request->action === 'contact') { + if ($this->request->getParam('action') === 'contact') { $this->loadComponent('Recaptcha.Recaptcha'); } } @@ -96,7 +99,7 @@ public function contact() { if ($this->request->is('post')) { if ($this->Recaptcha->verify()) { // Here you can validate your data - if (!empty($this->request->data)) { + if (!empty($this->request->getData())) { $this->Flash->success(__('We will get back to you soon.')); return $this->redirect($this->referer()); } else { @@ -120,11 +123,11 @@ For example: ```php Form->create() ?> -Form->input('name', [ +Form->control('name', [ 'label' => __('Your Name'), // 'default' => $this->request->query('name'); // in case you add the Prg Component ]) ?> -Form->input('message', [ +Form->control('message', [ 'type' => 'textarea', // 'default' => $this->request->query('message'); // in case you add the Prg Component 'label' => __('Your Message') @@ -178,7 +181,7 @@ high. ## License ## -Copyright (c) [2014-2016] [cakephp-fr] +Copyright (c) [2014-2018] [cakephp-fr] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/config/bootstrap.php b/config/bootstrap.php index 42f59b9..662ed20 100644 --- a/config/bootstrap.php +++ b/config/bootstrap.php @@ -17,7 +17,7 @@ // Validate the Configure Data $validator = new ConfigValidator(); -$errors = $validator->errors(Configure::read('Recaptcha')); +$errors = $validator->setErrors(Configure::read('Recaptcha')); if (!empty($errors)) { $errMsg = ''; diff --git a/src/Controller/Component/RecaptchaComponent.php b/src/Controller/Component/RecaptchaComponent.php index d7c46df..11db89d 100644 --- a/src/Controller/Component/RecaptchaComponent.php +++ b/src/Controller/Component/RecaptchaComponent.php @@ -49,8 +49,8 @@ class RecaptchaComponent extends Component public function setController($controller) { // Add the helper on the fly - if (!in_array('Recaptcha.Recaptcha', $controller->viewBuilder()->helpers())) { - $controller->viewBuilder()->helpers(['Recaptcha.Recaptcha'], true); + if (!in_array('Recaptcha.Recaptcha', $controller->viewBuilder()->setHelpers())) { + $controller->viewBuilder()->setHelpers(['Recaptcha.Recaptcha'], true); } } @@ -71,7 +71,7 @@ public function startup(Event $event) // instantiate Recaptcha object that deals with retrieving data from google recaptcha $this->recaptcha = new Recaptcha(new RecaptchaResponse(), $secret); - $controller = $event->subject(); + $controller = $event->getSubject(); $this->setController($controller); } @@ -84,8 +84,8 @@ public function startup(Event $event) public function verify() { $controller = $this->_registry->getController(); - if (isset($controller->request->data["g-recaptcha-response"])) { - $gRecaptchaResponse = $controller->request->data["g-recaptcha-response"]; + $gRecaptchaResponse = $controller->request->getData("g-recaptcha-response"); + if (!empty($gRecaptchaResponse)) { $resp = $this->recaptcha->verifyResponse( new Client(), @@ -107,6 +107,6 @@ public function verify() */ public function errors() { - return $this->recaptcha->errors(); + return $this->recaptcha->setErrors(); } } diff --git a/src/Controller/ContactController.php b/src/Controller/ContactController.php index 3bed4d9..e669535 100644 --- a/src/Controller/ContactController.php +++ b/src/Controller/ContactController.php @@ -27,7 +27,7 @@ class ContactController extends AppController public function initialize() { parent::initialize(); - if (in_array($this->request->action, ['index', 'multipleWidgets'])) { + if (in_array($this->request->getParam('action'), ['index', 'multipleWidgets'])) { $this->loadComponent('Recaptcha.Recaptcha'); } } @@ -43,7 +43,7 @@ public function index() if ($this->request->is('post')) { if ($this->Recaptcha->verify()) { // Here you can validate your data instead - if ($contact->execute($this->request->data)) { + if ($contact->execute($this->request->getData())) { $this->Flash->success(__('We will get back to you soon.')); } else { $this->Flash->error(__('There was a problem submitting your form.')); @@ -67,14 +67,14 @@ public function multipleWidgets() $contact = new ContactForm(); if ($this->request->is('post')) { if ($this->Recaptcha->verify()) { - if ($contact->execute($this->request->data)) { + if ($contact->execute($this->request->getData())) { $this->Flash->success(__('We will get back to you soon.')); } else { $this->Flash->error(__('There was a problem submitting your form.')); } } else { - debug($contact); - debug($this->Recaptcha); + // debug($contact); + // debug($this->Recaptcha); $this->Flash->error(__('Please check your Recaptcha Box.')); } } diff --git a/src/Form/ContactForm.php b/src/Form/ContactForm.php index 9bd75d0..2c38803 100644 --- a/src/Form/ContactForm.php +++ b/src/Form/ContactForm.php @@ -40,13 +40,15 @@ protected function _buildSchema(Schema $schema) */ protected function _buildValidator(Validator $validator) { - return $validator->add('name', 'length', [ - 'rule' => ['minLength', 2], - 'message' => 'A name is required' - ])->add('email', 'format', [ - 'rule' => 'email', - 'message' => 'A valid email address is required', - ]); + return $validator + ->add('name', 'length', [ + 'rule' => ['minLength', 2], + 'message' => 'A name is required' + ]) + ->add('email', 'format', [ + 'rule' => 'email', + 'message' => 'A valid email address is required', + ]); } /** diff --git a/src/Template/Contact/index.ctp b/src/Template/Contact/index.ctp index c7ed04a..b0eb1e4 100644 --- a/src/Template/Contact/index.ctp +++ b/src/Template/Contact/index.ctp @@ -15,9 +15,9 @@ */ ?> Form->create($contact) ?> -Form->input('name') ?> -Form->input('email') ?> -Form->input('body') ?> +Form->control('name') ?> +Form->control('email') ?> +Form->control('body') ?> Recaptcha->display([ // This options override global configs 'theme' => 'dark', diff --git a/src/Template/Contact/multiple_widgets.ctp b/src/Template/Contact/multiple_widgets.ctp index dff6279..269cc94 100644 --- a/src/Template/Contact/multiple_widgets.ctp +++ b/src/Template/Contact/multiple_widgets.ctp @@ -15,9 +15,9 @@ */ ?> Form->create($contact) ?> -Form->input('name') ?> -Form->input('email') ?> -Form->input('body') ?> +Form->control('name') ?> +Form->control('email') ?> +Form->control('body') ?> Form->button('Submit') ?> Form->end() ?> diff --git a/src/View/Helper/RecaptchaHelper.php b/src/View/Helper/RecaptchaHelper.php index 94339e2..b992d96 100644 --- a/src/View/Helper/RecaptchaHelper.php +++ b/src/View/Helper/RecaptchaHelper.php @@ -61,13 +61,13 @@ public function __construct(View $view, $config = []) // Merge Options given by user in config/recaptcha $this->config(Configure::read('Recaptcha')); - $lang = $this->config('lang'); + $lang = $this->getConfig('lang'); if (empty($lang)) { - $this->config('lang', I18n::locale()); + $this->setConfig('lang', I18n::locale()); } // Validate the Configure Data $validator = new RecaptchaValidator(); - $errors = $validator->errors($this->config()); + $errors = $validator->setErrors($this->getConfig()); if (!empty($errors)) { throw new \Exception(__d('recaptcha', 'One of your recaptcha config value is incorrect')); // throw an exception with config error that is raised @@ -91,11 +91,11 @@ public function __construct(View $view, $config = []) public function display(array $options = []) { // merge options - $options = array_merge($this->config(), $options); + $options = array_merge($this->getConfig(), $options); // Validate the Configure Data $validator = new RecaptchaValidator(); - $errors = $validator->errors($options); + $errors = $validator->setErrors($options); if (!empty($errors)) { throw new \Exception(__d('recaptcha', 'One of your recaptcha config value is incorrect')); // throw an exception with config error that is raised @@ -133,11 +133,11 @@ public function render() */ public function widget(array $options = []) { - $options = array_merge($this->config(), $options); + $options = array_merge($this->getConfig(), $options); // Validate the Configure Data $validator = new RecaptchaValidator(); - $errors = $validator->errors($options); + $errors = $validator->setErrors($options); if (!empty($errors)) { throw new \Exception(__d('recaptcha', 'One of your recaptcha config value is incorrect in a widget')); // throw an exception with config error that is raised