From 8a0490235baac2e3f292b991a8fd3c0e2ac71078 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr> Date: Wed, 28 Jan 2015 15:28:39 +0100 Subject: [PATCH] =?UTF-8?q?Mise=20en=20place=20d'une=20analyse=20antivirus?= =?UTF-8?q?=20si=20CLTissueBundle=20est=20install=C3=A9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IrsteaFileUploadExtension.php | 6 ++ Listener/VirusScannerListener.php | 65 +++++++++++++++++++ Resources/config/services.yml | 8 +++ 3 files changed, 79 insertions(+) create mode 100644 Listener/VirusScannerListener.php diff --git a/DependencyInjection/IrsteaFileUploadExtension.php b/DependencyInjection/IrsteaFileUploadExtension.php index 5e64c374..d84b81ae 100644 --- a/DependencyInjection/IrsteaFileUploadExtension.php +++ b/DependencyInjection/IrsteaFileUploadExtension.php @@ -22,6 +22,12 @@ class IrsteaFileUploadExtension extends Extension implements PrependExtensionInt $loader->load('services.yml'); $container->setParameter('irstea_file_upload.max_chunk_size', $config['max_chunk_size']); + + $bundles = $container->getParameter('kernel.bundles'); + if(!isset($bundles['CLTissueBundle'])) { + // On a pas le CLTissueBundle => pas d'antivirus + $container->removeDefinition('irstea_file_upload.virus_scanner'); + } } /** diff --git a/Listener/VirusScannerListener.php b/Listener/VirusScannerListener.php new file mode 100644 index 00000000..d9f37c17 --- /dev/null +++ b/Listener/VirusScannerListener.php @@ -0,0 +1,65 @@ +<?php + +/* + * Copyright (C) 2015 IRSTEA + * All rights reserved. + */ + +namespace Irstea\FileUploadBundle\Listener; + +use CL\Tissue\Adapter\AdapterInterface; +use CL\Tissue\Model\Detection; +use CL\Tissue\Model\ScanResult; +use Irstea\FileUploadBundle\Event\FileUploadCompleteEvent; +use Irstea\FileUploadBundle\Exception\RejectedFileException; + +/** + * Description of AntivirusListener + * + * @author Guillaume Perréal <guillaume.perreal@irstea.fr> + */ +class VirusScannerListener +{ + /** + * + * @var AdapterInterface + */ + private $scanner; + + /** + * + * @param AdapterInterface $scanner + */ + public function __construct(AdapterInterface $scanner) + { + $this->scanner = $scanner; + } + + /** + * + * @param FileUploadCompleteEvent $event + */ + public function onFileUploadCompleted(FileUploadCompleteEvent $event) + { + $file = $event->getUploadedFile(); + $path = $file->getLocalPath(); + + $result = $this->scanner->scan([$path]); + + $meta = $file->getMetadata(); + + $meta['virus_scanner'] = ['has_virus' => $result->hasVirus()]; + + if($result->hasVirus()) { + if(null !== $desc = $result->getDetections()[0]->getDescription()) { + $meta['virus_scanner']['detected'] = $desc; + } + } + + $file->setMetadata($meta); + + if($result->hasVirus()) { + throw new RejectedFileException($file, "Found a virus !"); + } + } +} diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 14b63a70..38b58f0b 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -57,3 +57,11 @@ services: factory: [ @knp_gaufrette.filesystem_map, get ] arguments: - %irstea_file_upload.filesystem.name% + + # Scanner anti-virus + irstea_file_upload.virus_scanner: + class: Irstea\FileUploadBundle\Listener\VirusScannerListener + arguments: + - @cl_tissue.scanner + tags: + - { name: kernel.event_listener, event: file_upload.complete, method: onFileUploadCompleted } -- GitLab