├── Resources ├── translations │ ├── EUCookieLaw.es.yml │ ├── EUCookieLaw.en.yml │ └── EUCookieLaw.fr.yml ├── config │ └── services.yml └── views │ └── eu_cookie_law.html.twig ├── LeblancSimonEUCookieLawBundle.php ├── composer.json ├── LICENSE.md ├── DependencyInjection ├── LeblancSimonEUCookieLawExtension.php └── Configuration.php ├── EventSubscriber └── EUCookieLawDivSubscriber.php ├── README.md └── Injector └── EUCookieLawTemplate.php /Resources/translations/EUCookieLaw.es.yml: -------------------------------------------------------------------------------- 1 | cookie_law: 2 | message: | 3 | Para ofrecerte el mejor servicio posible, este sitio usa cookies. 4 | Si continúas navegando, aceptas el uso de cookies. 5 | accept: Acepto el uso de cookies 6 | read_more: Leer más 7 | -------------------------------------------------------------------------------- /Resources/translations/EUCookieLaw.en.yml: -------------------------------------------------------------------------------- 1 | cookie_law: 2 | message: | 3 | In order to offer you the best possible service, this site uses cookies. 4 | By agreeing to continue on this site, declare that you accept their use. 5 | accept: I'm agree 6 | read_more: Read more 7 | -------------------------------------------------------------------------------- /Resources/translations/EUCookieLaw.fr.yml: -------------------------------------------------------------------------------- 1 | cookie_law: 2 | message: | 3 | Afin de vous proposer le meilleur service possible, ce site utilise des cookies. 4 | En acceptant de continuer sur ce site, vous déclarez accepter leur utilisation. 5 | accept: J'accepte 6 | read_more: Voir plus 7 | -------------------------------------------------------------------------------- /LeblancSimonEUCookieLawBundle.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace LeblancSimon\EUCookieLawBundle; 13 | 14 | use Symfony\Component\HttpKernel\Bundle\Bundle; 15 | 16 | class LeblancSimonEUCookieLawBundle extends Bundle 17 | { 18 | } 19 | -------------------------------------------------------------------------------- /Resources/config/services.yml: -------------------------------------------------------------------------------- 1 | services: 2 | eucookielaw.injector.template: 3 | class: LeblancSimon\EUCookieLawBundle\Injector\EUCookieLawTemplate 4 | arguments: 5 | - "@templating" 6 | - "%eu_cookie_law.template%" 7 | - "%eu_cookie_law.cookie_name%" 8 | - "%eu_cookie_law.cookie_value%" 9 | - "%eu_cookie_law.read_more_link%" 10 | public: false 11 | 12 | eucookielaw.reponse.listener: 13 | class: LeblancSimon\EUCookieLawBundle\EventSubscriber\EUCookieLawDivSubscriber 14 | arguments: ["@eucookielaw.injector.template"] 15 | public: true 16 | tags: 17 | - { name: kernel.event_subscriber } 18 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "leblanc-simon/eu-cookie-law-bundle", 3 | "type": "symfony-bundle", 4 | "description": "Add the cookie div explanation according to EU law", 5 | "license": "MIT", 6 | "authors": [{ 7 | "name": "Simon Leblanc", 8 | "email": "contact@leblanc-simon.eu", 9 | "homepage": "http://www.leblanc-simon.fr", 10 | "role": "Developer" 11 | }], 12 | "autoload": { 13 | "psr-4": { 14 | "LeblancSimon\\EUCookieLawBundle\\": "" 15 | } 16 | }, 17 | "require": { 18 | "php": ">=5.5.9", 19 | "symfony/framework-bundle": "^2.8|^3" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Simon Leblanc 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /DependencyInjection/LeblancSimonEUCookieLawExtension.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace LeblancSimon\EUCookieLawBundle\DependencyInjection; 13 | 14 | use Symfony\Component\Config\FileLocator; 15 | use Symfony\Component\DependencyInjection\ContainerBuilder; 16 | use Symfony\Component\DependencyInjection\Extension\Extension; 17 | use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; 18 | 19 | class LeblancSimonEUCookieLawExtension extends Extension 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function load(array $configs, ContainerBuilder $container) 25 | { 26 | $configuration = new Configuration(); 27 | $config = $this->processConfiguration($configuration, $configs); 28 | 29 | $container->setParameter('eu_cookie_law.cookie_name', $config['cookie_name']); 30 | $container->setParameter('eu_cookie_law.cookie_value', $config['cookie_value']); 31 | $container->setParameter('eu_cookie_law.template', $config['template']); 32 | $container->setParameter('eu_cookie_law.read_more_link', $config['read_more_link']); 33 | 34 | $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); 35 | $loader->load('services.yml'); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /DependencyInjection/Configuration.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace LeblancSimon\EUCookieLawBundle\DependencyInjection; 13 | 14 | use Symfony\Component\Config\Definition\Builder\TreeBuilder; 15 | use Symfony\Component\Config\Definition\ConfigurationInterface; 16 | 17 | class Configuration implements ConfigurationInterface 18 | { 19 | public function getConfigTreeBuilder() 20 | { 21 | $tree_builder = new TreeBuilder(); 22 | $root_node = $tree_builder->root('eu_cookie_law'); 23 | 24 | $root_node 25 | ->children() 26 | ->scalarNode('cookie_name') 27 | ->defaultValue('eu_cookie_law') 28 | ->end() 29 | ->scalarNode('cookie_value') 30 | ->defaultValue('accept') 31 | ->end() 32 | ->scalarNode('cookie_value') 33 | ->defaultValue('accept') 34 | ->end() 35 | ->scalarNode('read_more_link') 36 | ->defaultValue(null) 37 | ->end() 38 | ->scalarNode('template') 39 | ->defaultValue('LeblancSimonEUCookieLawBundle::eu_cookie_law.html.twig') 40 | ->end() 41 | ->end() 42 | ; 43 | 44 | return $tree_builder; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /EventSubscriber/EUCookieLawDivSubscriber.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace LeblancSimon\EUCookieLawBundle\EventSubscriber; 13 | 14 | use LeblancSimon\EUCookieLawBundle\Injector\EUCookieLawTemplate; 15 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; 16 | use Symfony\Component\HttpKernel\Event\FilterResponseEvent; 17 | use Symfony\Component\HttpKernel\HttpKernelInterface; 18 | use Symfony\Component\HttpKernel\KernelEvents; 19 | 20 | class EUCookieLawDivSubscriber implements EventSubscriberInterface 21 | { 22 | /** 23 | * @var EUCookieLawTemplate 24 | */ 25 | private $injector; 26 | 27 | /** 28 | * EUCookieLawDivSubscriber constructor. 29 | * 30 | * @param EUCookieLawTemplate $template_injector 31 | */ 32 | public function __construct(EUCookieLawTemplate $template_injector) 33 | { 34 | $this->injector = $template_injector; 35 | } 36 | 37 | /** 38 | * {@inheritdoc} 39 | */ 40 | static public function getSubscribedEvents() 41 | { 42 | return [ 43 | KernelEvents::RESPONSE => ['onKernelResponse', -128], 44 | ]; 45 | } 46 | 47 | /** 48 | * Inject the cookie law template 49 | * 50 | * @param FilterResponseEvent $event 51 | */ 52 | public function onKernelResponse(FilterResponseEvent $event) 53 | { 54 | if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { 55 | return; 56 | } 57 | 58 | $this->injector->inject($event->getResponse(), $event->getRequest()); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Resources/views/eu_cookie_law.html.twig: -------------------------------------------------------------------------------- 1 | {% spaceless %}{% trans_default_domain 'EUCookieLaw' %} 2 |
11 | 36 | {% endspaceless %} 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EUCookieLawBundle 2 | 3 | ## Description 4 | 5 | this bundle allow you to add the information message for your users about the cookies. 6 | It's a requirement since the [ePrivacy directive](http://ec.europa.eu/ipg/basics/legal/cookies/index_en.htm#section_2) 7 | 8 | ## Installation 9 | 10 | * install the bundle 11 | 12 | ```bash 13 | composer require leblanc-simon/eu-cookie-law-bundle 14 | ``` 15 | 16 | * activate the bundle 17 | 18 | ```php 19 | // app/AppKernel.php 20 | 21 | class AppKernel extends Kernel 22 | { 23 | public function registerBundles() 24 | { 25 | $bundles = [ 26 | // ... 27 | new LeblancSimon\EUCookieLawBundle\LeblancSimonEUCookieLawBundle(), 28 | ]; 29 | } 30 | } 31 | ``` 32 | 33 | Nothing else to do. The HTML will be automatically injected for the text/html response. 34 | 35 | ## Customization 36 | 37 | ### Message 38 | 39 | The message and text button can be customized via translation. Add a 40 | ```Resources/translations/EUCookieLaw.[locale].yml``` in your project and customize your text. 41 | 42 | ```yml 43 | cookie_law: 44 | message: | 45 | Afin de vous proposer le meilleur service possible, ce site utilise des cookies. 46 | En acceptant de continuer sur ce site, vous déclarer accepter leur utilisation. 47 | accept: J'accepte 48 | read_more: Voir plus 49 | ``` 50 | 51 | ### Design 52 | 53 | to override the design, use id in your CSS files : 54 | 55 | ```css 56 | #eu-cookie-law { 57 | 58 | } 59 | #eu-cookie-law-accept { 60 | 61 | } 62 | ``` 63 | 64 | ### Configuration 65 | 66 | you can customized the bundle with a configuration : 67 | 68 | ```yml 69 | leblanc_simon_eu_cookie_law: 70 | # The name of the cookie use to know if the user is agree 71 | cookie_name: eu_cookie_law 72 | # The value of the cookie use to know if the user is agree 73 | cookie_value: accept 74 | # The template use to show the message 75 | template: 'LeblancSimonEUCookieLawBundle::eu_cookie_law.html.twig' 76 | # The name of route to be open 77 | read_more_link: name_to_route 78 | ``` 79 | -------------------------------------------------------------------------------- /Injector/EUCookieLawTemplate.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace LeblancSimon\EUCookieLawBundle\Injector; 13 | 14 | use Symfony\Bundle\TwigBundle\TwigEngine; 15 | use Symfony\Component\HttpFoundation\Request; 16 | use Symfony\Component\HttpFoundation\Response; 17 | 18 | class EUCookieLawTemplate 19 | { 20 | /** 21 | * @var TwigEngine 22 | */ 23 | private $templating; 24 | 25 | /** 26 | * @var string 27 | */ 28 | private $template_name; 29 | 30 | /** 31 | * @var string 32 | */ 33 | private $cookie_name; 34 | 35 | /** 36 | * @var string 37 | */ 38 | private $cookie_value; 39 | 40 | /** 41 | * EUCookieLawTemplate constructor. 42 | * 43 | * @param TwigEngine $templating 44 | * @param $template_name 45 | * @param $cookie_name 46 | * @param $cookie_value 47 | * @param $read_more_link 48 | */ 49 | public function __construct(TwigEngine $templating, $template_name, $cookie_name, $cookie_value, $read_more_link) 50 | { 51 | $this->templating = $templating; 52 | $this->template_name = $template_name; 53 | $this->cookie_name = $cookie_name; 54 | $this->cookie_value = $cookie_value; 55 | $this->read_more_link = $read_more_link; 56 | } 57 | 58 | /** 59 | * Inject in the response the cookie law template 60 | * 61 | * @param Response $response 62 | * @param Request $request 63 | */ 64 | public function inject(Response $response, Request $request) 65 | { 66 | if ($this->checkIfMustBeInjected($response, $request) === false) { 67 | return; 68 | } 69 | 70 | $render_template = $this->templating->render($this->template_name, [ 71 | 'cookie_name' => $this->cookie_name, 72 | 'cookie_value' => $this->cookie_value, 73 | 'cookie_read_more_link' => $this->read_more_link 74 | ]); 75 | 76 | $content = $response->getContent(); 77 | $position = mb_strripos($content, '