Commit d8720ad2 authored by Avris's avatar Avris

Merge branch 'v4.0' into 'master'

v4.0

See merge request !1
parents d8242258 447b8ff4
tests/_output/*
vendor/*
composer.lock
.idea/*
/.idea/*
**/.DS_Store
/vendor/*
/tests/_output/*
## Micrus ReCaptcha Plugin ##
# Micrus ReCaptcha Plugin ##
This is a module for [Micrus framework](http://micrus.avris.it)
that adds Google's [ReCaptcha](https://www.google.com/recaptcha) as a form widget.
To install this module, run:
## Installation
Run:
composer require avris/micrus-recaptcha
Go to [ReCaptcha](https://www.google.com/recaptcha), sign up, and get your `Site key` and `Secret key`.
Put them in `App\parameters.yml`:
recaptcha:
site_key: (paste_site_key_here)
secret_key: (paste_secret_key_here)
In your view, add this code right before the closing of `<head>`:
Then register the module in your `App\App:registerModules`:
yield new \Avris\Micrus\ReCaptcha\ReCaptchaModule;
Finally, run:
<script src='https://www.google.com/recaptcha/api.js'></script>
bin/env
When creating your form, remember to pass container to it:
You will be asked for your `Site key` and `Secret key`.
You can get them after signing up at [ReCaptcha](https://www.google.com/recaptcha).
They will be saved in the `.env` file.
$form = new PostForm(new Post, $this->container);
## Usage
And inside its `configure()` simply add this widget:
In your form's `configure()` method simply add this widget:
->add('captcha', ReCaptchaWidget::class)
->add('captcha', \Avris\Micrus\ReCaptcha\Widget\ReCaptcha::class)
Voilà!
### Copyright ###
## Copyright
* **Author:** Andrzej Prusinowski [(Avris.it)](https://avris.it)
* **Author:** Andre Prusinowski [(Avris.it)](https://avris.it)
* **Licence:** [MIT](https://mit.avris.it)
......@@ -11,7 +11,7 @@
"homepage": "https://avris.it"
}],
"require": {
"avris/micrus": "^3.0",
"avris/micrus": "^4.0",
"google/recaptcha": "^1.1"
},
"autoload": {
......
Avris\Micrus\ReCaptcha\:
dir: '%MODULE_DIR%/src/'
<?php
namespace Avris\Micrus\ReCaptcha\Assert;
use Avris\Forms\Assert\Assert;
use Avris\Forms\Assert\IsRequired;
final class ReCaptcha extends Assert implements IsRequired
{
/** @var string */
private $secretKey;
public function __construct(string $secret, $message = null)
{
$this->secretKey = $secret;
parent::__construct($message);
}
public function validate($value): bool
{
$recaptcha = new \ReCaptcha\ReCaptcha($this->secretKey);
return $recaptcha->verify($_POST['g-recaptcha-response'])->isSuccess();
}
}
<?php
namespace Avris\Micrus\ReCaptcha;
use Avris\Micrus\Forms\Assert\Assert;
use Avris\Micrus\Forms\Assert\IsRequired;
class ReCaptchaAssert extends Assert implements IsRequired
{
/** @var string */
protected $secret;
public function __construct($secret, $message = 'Captcha is invalid')
{
parent::__construct($message);
$this->secret = $secret;
}
public function validate($value)
{
$recaptcha = new \ReCaptcha\ReCaptcha($this->secret);
$resp = $recaptcha->verify($_POST['g-recaptcha-response']);
return $resp->isSuccess() ? true : $this->message;
}
}
<?php
namespace Avris\Micrus\ReCaptcha;
use Avris\Micrus\Bootstrap\ModuleInterface;
use Avris\Micrus\Bootstrap\ModuleTrait;
use Avris\Micrus\Tool\Config\ParametersProvider;
class ReCaptchaModule implements ModuleInterface, ParametersProvider
{
use ModuleTrait;
public function getParametersDefaults(): array
{
return [
'RECAPTCHA_SITE_KEY' => '',
'RECAPTCHA_SECRET_KEY' => '',
];
}
}
<?php
namespace Avris\Micrus\ReCaptcha;
use Avris\Bag\Bag;
use Avris\Micrus\Forms\Widget\DontMapOnObject;
use Avris\Micrus\Forms\Widget\Widget;
class ReCaptchaWidget extends Widget implements DontMapOnObject
{
protected function getTemplate($widgetValue = null)
{
if (!isset($this->options['label'])) { $this->options['label'] = ''; }
return '<div class="g-recaptcha" data-sitekey="' . $this->getParameter('site_key') . '"></div>';
}
protected function getDefaultAssert()
{
return new ReCaptchaAssert($this->getParameter('secret_key'));
}
private function getParameter($name)
{
$parameters = new Bag($this->form->getContainer()->get('config.parameters.?recaptcha') ?: []);
return $parameters[$name];
}
}
<?php
namespace Avris\Micrus\ReCaptcha\Widget;
use Avris\Forms\Widget\DontMapOnObject;
use Avris\Forms\Widget\Widget;
use Avris\Micrus\ReCaptcha\Assert\ReCaptcha as ReCaptchaAssert;
final class ReCaptcha extends Widget implements DontMapOnObject
{
/** @var string */
private $siteKey;
/** @var string */
private $secretKey;
public function __construct(string $envRecaptchaSiteKey, string $envRecaptchaSecretKey)
{
parent::__construct();
$this->siteKey = $envRecaptchaSiteKey;
$this->secretKey = $envRecaptchaSecretKey;
}
public function setOptions(array $options)
{
return parent::setOptions($options + ['label' => '']);
}
public function getSiteKey(): string
{
return $this->siteKey;
}
protected function getDefaultAssert()
{
return new ReCaptchaAssert($this->secretKey);
}
}
{% extends 'Form/wrapper' %}
{% block widget %}
<div class="g-recaptcha" data-sitekey="{{ widget.siteKey }}"></div>
{{ widgetAssets(_self) }}
{% endblock %}
<script src='https://www.google.com/recaptcha/api.js'></script>
ReCaptcha: Captcha is invalid
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment