Created
October 13, 2020 14:22
-
-
Save Sammyjo20/bc75d3d18867aaebcaf9c944901a7017 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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