Skip to content

Commit

Permalink
Update form upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
matrunchyk committed Apr 7, 2019
1 parent 5488d37 commit 373412c
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 131 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1

before_install:
- sudo apt-get update -qq
Expand All @@ -16,6 +16,6 @@ before_script:
- composer install --prefer-source --no-interaction --dev

script:
- phpunit --colors --verbose --coverage-clover build/logs/clover.xml
- php vendor/bin/phpunit --colors --verbose --coverage-clover build/logs/clover.xml

after_script: if [ $(phpenv version-name) = "5.6" ]; then php vendor/bin/ocular code-coverage:upload --format=php-clover build/logs/clover.xml; fi
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Custom Laravel 5 anti-virus validator for file uploads.
* [Requirements](#requirements)
* [Installation](#installation)
* [Usage](#usage)
* [Change Log](#changelog)
* [Configuration](#configuration)
* [Author](#author)

<a name="requirements"></a>
Expand Down Expand Up @@ -41,11 +41,17 @@ This package is not tested on windows, but if you have ClamAV running (usually o
<a name="installation"></a>
## Installation

Install the package through [Composer](http://getcomposer.org).
#### 1. Install the package through [Composer](http://getcomposer.org).

```bash
$ composer require sunspikes/clamav-validator
```

Run `composer require digitalideastudio/clamav-validator`
#### 2. Add the service provider (for Laravel 5.4 or below)

Add the following to your `providers` array in `config/app.php`:
This package supports Laravel new [Package Discovery](https://laravel.com/docs/5.5/packages#package-discovery).

If you are using Laravel < 5.5, you need to add `Sunspikes\ClamavValidator\ClamavValidatorServiceProvider::class` to your `providers` array in `config/app.php`:

```php
'providers' => array(
Expand All @@ -66,18 +72,12 @@ $rules = array(
);
```

<a name="changelog"></a>
## Change Log
<a name="configuration"></a>
## Configuration

2014.12.05 - Initial version, using extension php-clamav
By default the package will try to connect the clamav daemon via the default socket file (/var/run/clamav/clamd.ctl) and if it fails it will try the tcp port (127.0.0.1:3310)

2014.12.05 - Removed the dependency php-clamav, Now using [Quahog](https://github.com/jonjomckay/quahog)

2015.10.20 - Updated for Laravel 5

2015.11.20 - Updated to use PSR-4

2015.12.03 - Added limited windows support
But you can set the `CLAMAV_UNIX_SOCKET` (socket file path) or `CLAMAV_TCP_SOCKET` (host:port) environment variables to override this.

<a name="author"></a>
## Author
Expand Down
25 changes: 12 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "digitalideastudio/clamav-validator",
"name": "sunspikes/clamav-validator",
"description": "Custom Laravel 5 anti-virus validator for file uploads.",
"keywords": [
"laravel",
"validator",
"clamav",
"virus"
],
"homepage": "https://github.com/digitalideastudio/clamav-validator",
"homepage": "https://github.com/sunspikes/clamav-validator",
"license": "MIT",
"authors": [
{
Expand All @@ -16,34 +16,33 @@
}
],
"require": {
"php": ">=5.4.0",
"php": ">=5.5.0",
"xenolope/quahog": "2.*",
"illuminate/support": ">=4.2",
"laravel/framework": ">=4.2",
"illuminate/validation": ">=4.1.21"
},
"require-dev": {
"phpunit/phpunit": "4.*",
"mockery/mockery": "dev-master",
"mockery/mockery": "0.9.*",
"scrutinizer/ocular": "dev-master"
},
"autoload": {
"psr-4": {
"Sunspikes\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Sunspikes\\Tests\\ClamavValidator\\": "src/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
"laravel": {
"providers": [
"Sunspikes\\ClamavValidator\\ClamavValidatorServiceProvider"
]
}
}
},
"autoload-dev": {
"psr-4": {
"Sunspikes\\Tests\\ClamavValidator\\": "src/"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
54 changes: 20 additions & 34 deletions src/ClamavValidator/ClamavValidator.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php namespace Sunspikes\ClamavValidator;

use Illuminate\Validation\Validator;
use Illuminate\Contracts\Translation\Translator;
use Xenolope\Quahog\Client;
use Socket\Raw\Factory;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class ClamavValidator extends Validator
class ClamavValidator
{
/**
* @const string CLAMAV_STATUS_OK
Expand All @@ -28,59 +28,45 @@ class ClamavValidator extends Validator
const CLAMAV_LOCAL_TCP_SOCKET = 'tcp://127.0.0.1:3310';

/**
* Creates a new instance of ClamavValidator
* @const string CLAMAV_SOCKET_READ_TIMEOUT
*/
public function __construct($translator, $data, $rules, $messages)
{
parent::__construct($translator, $data, $rules, $messages);
}
const CLAMAV_SOCKET_READ_TIMEOUT = 30;

/**
* Validate the uploaded file for virus/malware with ClamAV
*
* @param $attribute string
* @param $attribute string
* @param $value mixed
* @param $parameters array
* @param $parameters array
*
* @return boolean
* @throws ClamavValidatorException
*/
public function validateClamav($attribute, $value, $parameters)
{
$file = $this->getFilePath($value);

$clamavSocket = $this->getClamavSocket();

try {
$socket = (new Factory())->createClient($clamavSocket);
} catch (\Exception $e) {
throw new ClamavValidatorException($e, $attribute, $this);
}
// Create a new socket instance
$socket = (new Factory())->createClient($clamavSocket);

// Create a new instance of the Client
$quahog = new Client($socket, 30, PHP_NORMAL_READ);
$quahog = new Client($socket, self::CLAMAV_SOCKET_READ_TIMEOUT, PHP_NORMAL_READ);

// Ensure that clamav user is able to read the file
$oldPerms = fileperms($file);
chmod($file, 0666);
clearstatcache(true, $file);
// Check if the file is readable
if (! is_readable($file)) {
throw new ClamavValidatorException(sprintf('The file "%s" is not readable', $file));
}

// Scan the file
$result = $quahog->scanFile($file);

// Restore permissions
chmod($file, $oldPerms);
clearstatcache(true, $file);

$result = $quahog->scanResourceStream(fopen($file, 'rb'));

if (self::CLAMAV_STATUS_ERROR === $result['status']) {
throw new ClamavValidatorException($result['reason']);
}

// Check if scan result is not clean
if (self::CLAMAV_STATUS_OK != $result['status']) {
return false;
}

return true;
return !(self::CLAMAV_STATUS_OK !== $result['status']);
}

/**
Expand All @@ -90,11 +76,11 @@ public function validateClamav($attribute, $value, $parameters)
*/
protected function getClamavSocket()
{
if (file_exists(self::CLAMAV_UNIX_SOCKET)) {
return 'unix://' . self::CLAMAV_UNIX_SOCKET;
if (file_exists(env('CLAMAV_UNIX_SOCKET', self::CLAMAV_UNIX_SOCKET))) {
return 'unix://' . env('CLAMAV_UNIX_SOCKET', self::CLAMAV_UNIX_SOCKET);
}

return self::CLAMAV_LOCAL_TCP_SOCKET;
return env('CLAMAV_TCP_SOCKET', self::CLAMAV_LOCAL_TCP_SOCKET);
}

/**
Expand Down
34 changes: 2 additions & 32 deletions src/ClamavValidator/ClamavValidatorException.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,37 +1,7 @@
<?php namespace Sunspikes\ClamavValidator;

use Illuminate\Validation\ValidationException;
use Exception;

class ClamavValidatorException extends ValidationException
class ClamavValidatorException extends Exception
{
protected $exception;

public $attribute;

public function __construct(\Exception $exception, string $attribute, \Illuminate\Contracts\Validation\Validator $validator, $response = null)
{
$this->exception = $exception;
$this->attribute = $attribute;

parent::__construct($validator, $this->makeResponse());
}

public function makeResponse()
{
$message = $this->exception->getMessage();

if (substr_count($message, 'SOCKET_ECONNREFUSED')) {
$message = 'Unable to initiate antivirus check. Please try later.';
}

if (request()->expectsJson()) {
return response()->json([
$this->attribute => $message
], 422);
}

return redirect()->back()->withInput(
request()->input()
);
}
}
15 changes: 8 additions & 7 deletions src/ClamavValidator/ClamavValidatorServiceProvider.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class ClamavValidatorServiceProvider extends ServiceProvider
*
* @var bool
*/
protected $rules = array(
protected $rules = [
'clamav',
);
];

/**
* Bootstrap the application events.
Expand All @@ -31,12 +31,13 @@ public function boot()
$this->loadTranslationsFrom(__DIR__ . '/../lang', 'clamav-validator');

$this->app['validator']
->resolver(function ($translator, $data, $rules, $messages) {
->resolver(function ($translator, $data, $rules, $messages, $customAttributes = []) {
return new ClamavValidator(
$translator,
$data,
$rules,
$messages
$messages,
$customAttributes
);
});

Expand Down Expand Up @@ -75,8 +76,8 @@ protected function extendValidator($rule)
{
$method = studly_case($rule);
$translation = $this->app['translator']->get('clamav-validator::validation');
$this->app['validator']->extend($rule, 'Sunspikes\ClamavValidator\ClamavValidator@validate' . $method,
isset($translation[$rule]) ? $translation[$rule] : array());
$this->app['validator']->extend($rule, ClamavValidator::class .'@validate' . $method,
isset($translation[$rule]) ? $translation[$rule] : []);
}


Expand All @@ -97,6 +98,6 @@ public function register()
*/
public function provides()
{
return array();
return [];
}
}
Empty file modified src/lang/en/validation.php
100644 → 100755
Empty file.
Loading

0 comments on commit 373412c

Please sign in to comment.