Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
liverbool committed Dec 21, 2017
1 parent 2bf4c22 commit 1db55bd
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 129 deletions.
7 changes: 3 additions & 4 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('dos_recaptcha');
$rootNode = $treeBuilder->root('phpmob_recaptcha');

$rootNode
->children()
->scalarNode("site_key")->isRequired()->end()
->scalarNode("secret_key")->isRequired()->end()
->scalarNode("theme")->defaultValue('light')->end()
->booleanNode("enabled")->defaultTrue()->end()
->booleanNode("verify_host")->defaultFalse()->end()
->scalarNode("locale_key")->defaultValue("%kernel.default_locale%")->end()
->booleanNode("locale_from_request")->defaultFalse()->end()
->booleanNode("verify_host")->defaultTrue()->end()
->end()
;

Expand Down
8 changes: 7 additions & 1 deletion Form/Type/RecaptchaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ class RecaptchaType extends AbstractType
*/
private $siteKey;

public function __construct($siteKey)
/**
* @var string
*/
private $theme;

public function __construct($siteKey, $theme)
{
$this->siteKey = $siteKey;
$this->theme = $theme;
}

/**
Expand Down
165 changes: 46 additions & 119 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# TwigModifyBundle
# PhpMobReCaptchaBundle

TwigModify is a twig tag that can modifies everythink you want eg. `cssmin`, `jsmin` and more.
PhpMobReCaptchaBundle is a spam protection using google reCaptcha.


## Installing

Installing via `composer` is
recommended.
Installing via `composer` is recommended.

```yaml
"require": {
"phpmob/twig-modify-bundle": "~1.0"
"phpmob/recaptcha-bundle": "~1.0"
}
```

Expand All @@ -23,133 +22,61 @@ public function registerBundles()
{
$bundles = [
...
new \PhpMob\TwigModifyBundle\PhpMobTwigModifyBundle(),
new \PhpMob\ReCaptchaBundle\PhpMobReCaptchaBundle(),
];
}
```

## Usage

In order to modify your twig is simple just wrap `{% modify modifier %}...{% modify %}` like this:

```twig
{% modify jsmin %}
<script type="text/javascript">
$(function() {
....
});
</script>
{% endmodify %}
```

That was it!

## Build-in Modifiers
There are supported modifiers in this bundle.

#### JSMin
A wrapped modifier for `\JShrink\Minifier::minify`, Thanks [tedivm/jshrink](https://github.com/tedious/JShrink)
```twig
{% modify jsmin %}
<script type="text/javascript">
$(function() {
....
});
</script>
{% endmodify %}
```

#### CSSMin
A wrapped modifier for `\tubalmartin\CssMin\Minifier::minify`, Thanks [tubalmartin/cssmin](https://github.com/tubalmartin/YUI-CSS-compressor-PHP-port)
```twig
{% modify cssmin %}
<style type="text/css">
body {
color: red;
}
</style>
{% endmodify %}
```

#### HtmlPurify
A wrapped modifier for `\HTMLPurifier::purify`, Thanks [ezyang/htmlpurifier](https://github.com/ezyang/htmlpurifier)
```twig
{% modify htmlpurify %}
<SCRIPT »
SRC=http://ha.ckers.org/xss. »
js></SCRIPT>
{% endmodify %}
```

## Cache
TwigModifyBundle use local cache folder by default, however you can use any cache that implemented `\Doctrine\Common\Cache\Cache` interface and then change the confiuration for your cache servie:
In order to using it you need to setup a bit, First create google recatpcha via [https://www.google.com/recaptcha/admin](https://www.google.com/recaptcha/admin)
and then config in your symfony app.

1. Configuration
```yaml
phpmob_twig_modify:
cache: "my_cache_service_id"
phpmob_recaptcha
site_key: <google_recaptcha_site_key>
secret_key: <google_recaptcha_secret_key>
# optional
enabled: true/false # toggle enable or disable to using it
verify_host: true/false # strict verify hostname (not allow to submit from remote host)
theme: light/dark # default light
```
You can also use doctrine cache.
```yaml

doctrine_cache:
providers:
modifier_cache:
type: file_system

phpmob_twig_modify:
cache: "doctrine_cache.providers.modifier_cache"
```
Don't fogot enable `DoctrineCacheBundle` in your `AppKernel.php` - See https://symfony.com/doc/current/bundles/DoctrineCacheBundle/index.html

## Your own modifiers
You can add/override modifiers by easy configuration:

```yaml
phpmob_twig_modify:
modifiers:
xss: # a modifier name
enabled: true # default true, toggle enable/disable this modifier.
class: \PhpMob\TwigModifyBundle\Modifier\HTMLPurify # static class or any service
method: purify # service method that's allowed to accept `[$content, (array) $options]
options: # are array options you want to past into your modifier method - `\PhpMob\TwigModifyBundle\Modifier\HTMLPurify::purify` in this case.
Cache.SerializerPath: "%kernel.cache_dir%/htmlpurify"
```
After that you already to use your new modifier.
```twig
{% modify xss %}
<SCRIPT »
SRC=http://ha.ckers.org/xss. »
js></SCRIPT>
{% endmodify %}
```

## Configuration
TwigModifyBundle use local cache folder by default, however you can use any cache that implemented `\Doctrine\Common\Cache\Cache` interface and then change the confiuration for your cache servie:
2. Using
```php
<?php

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use PhpMob\ReCaptchaBundle\Form\Type\RecaptchaType;
use PhpMob\ReCaptchaBundle\Validator\Constraints\IsValid;

/**
* @author Ishmael Doss <nukboon@gmail.com>
*/
class YourType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('field', TextType::class, [])

->add("recaptcha", RecaptchaType::class, [
'mapped' => false,
'label' => false,
"constraints" => [
new IsValid(['groups' => ['some_group_if_need']])
]
])
;
}
}

```yaml
phpmob_twig_modify:
# cache serivce implemented `\Doctrine\Common\Cache\Cache` interface.
cache: "my_cache_service_id"
# application wide toggle enable/disable all modifiers
enabled: true
# custom/override modifiers (same key of modifier will override other previous defined)
modifiers:
name:
class: ModifierClassOrService
method: ModifierMethod
options: ArrayOfSecondModifierMethod
enabled: ToggleDisableEnable
```

## Thanks
- https://github.com/nibsirahsieu/SalvaJshrinkBundle to nice demonstration.
- https://github.com/Exercise/HTMLPurifierBundle to nice demonstration.
- https://github.com/ezyang/htmlpurifier
- https://github.com/tubalmartin/YUI-CSS-compressor-PHP-port
- https://github.com/tedious/JShrink
That was it!

## License

Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<services>
<service class="PhpMob\ReCaptchaBundle\Form\Type\RecaptchaType">
<argument>%phpmob.recaptcha.site_key%</argument>
<argument>%phpmob.recaptcha.theme%</argument>
<tag name="form.type"/>
</service>
<service class="PhpMob\ReCaptchaBundle\Validator\Constraints\IsValidValidator">
Expand Down
3 changes: 2 additions & 1 deletion Resources/views/form.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
var onRecaptchaloadCallback = function () {
grecaptcha.render('{{ form.vars.id }}', {
'sitekey': '{{ form.vars.site_key }}'
'sitekey': '{{ form.vars.site_key }}',
'theme': '{{ form.vars.theme }}'
});
};
</script>
Expand Down
12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "phpmob/twig-modify-bundle",
"name": "phpmob/recaptcha-bundle",
"description": "Spam protect with google reCAPTCHA",
"keywords": [
"google",
Expand All @@ -16,12 +16,16 @@
}
],
"require": {
"php": "^5.6|^7.0",
"php": "^5.5.9|>=7.0.8",
"symfony/form": "^3.4",
"symfony/validator": "^3.4"
"symfony/validator": "^3.4",
"symfony/http-foundation": "^3.4",
"symfony/config": "^3.4",
"symfony/http-kernel": "^3.4",
"symfony/dependency-injection": "^3.4"
},
"require-dev": {
"phpunit/phpunit": "4.0.*"
"phpunit/phpunit": "^6.5"
},
"autoload": {
"psr-4": {
Expand Down

0 comments on commit 1db55bd

Please sign in to comment.