Skip to content

Instantly share code, notes, and snippets.

@Sammyjo20
Created October 13, 2020 14:22
Show Gist options
  • Save Sammyjo20/bc75d3d18867aaebcaf9c944901a7017 to your computer and use it in GitHub Desktop.
Save Sammyjo20/bc75d3d18867aaebcaf9c944901a7017 to your computer and use it in GitHub Desktop.
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class DangerousExtensions implements Rule
{
/**
* @var array
*/
public $extensions;
/**
* Array of common extensions that can be used to infect.
*
* @var array
*/
protected $bad_extensions = [
'exe',
'sh',
'jar',
'dll',
'tmp',
'php',
'py'
];
/**
* Create a new rule instance.
*
* @param array|null $extensions
*/
public function __construct(?array $extensions = [])
{
$this->extensions = array_merge($this->bad_extensions, $extensions);
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return !in_array($value->getClientOriginalExtension(), $this->extensions);
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The :attribute file is a type that isn\'t allowed.';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment