Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

PSR-7 file validation implemented #251

Merged
merged 6 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ All notable changes to this project will be documented in this file, in reverse

### Changed

- Nothing.
- [#251](https://github.com/zendframework/zend-validator/pull/251) updates the logic of each of the various `Zend\Validator\File` validators
to allow validating against PSR-7 `UploadedFileInterface` instances, expanding
the support originally provided in version 2.11.0.

### Deprecated

Expand All @@ -20,7 +22,6 @@ All notable changes to this project will be documented in this file, in reverse

- [#250](https://github.com/zendframework/zend-validator/pull/250) removes support for zend-stdlib v2 releases.


### Fixed

- Nothing.
Expand Down
28 changes: 8 additions & 20 deletions src/File/Crc32.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@

namespace Zend\Validator\File;

use Zend\Validator\Exception;
use Zend\Validator\File\FileInformationTrait;

/**
* Validator for the crc32 hash of given files
*/
class Crc32 extends Hash
{
use FileInformationTrait;

/**
* @const string Error constants
*/
Expand Down Expand Up @@ -85,32 +87,18 @@ public function addCrc32($options)
*/
public function isValid($value, $file = null)
{
if (is_string($value) && is_array($file)) {
// Legacy Zend\Transfer API support
$filename = $file['name'];
$file = $file['tmp_name'];
} elseif (is_array($value)) {
if (! isset($value['tmp_name']) || ! isset($value['name'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
);
}
$file = $value['tmp_name'];
$filename = $value['name'];
} else {
$file = $value;
$filename = basename($file);
}
$this->setValue($filename);
$fileInfo = $this->getFileInfo($value, $file);

$this->setValue($fileInfo['filename']);

// Is file readable ?
if (empty($file) || false === is_readable($file)) {
if (empty($fileInfo['file']) || false === is_readable($fileInfo['file'])) {
$this->error(self::NOT_FOUND);
return false;
}

$hashes = array_unique(array_keys($this->getHash()));
$filehash = hash_file('crc32', $file);
$filehash = hash_file('crc32', $fileInfo['file']);
if ($filehash === false) {
$this->error(self::NOT_DETECTED);
return false;
Expand Down
27 changes: 8 additions & 19 deletions src/File/ExcludeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@

namespace Zend\Validator\File;

use Zend\Validator\File\FileInformationTrait;
use Zend\Validator\Exception;

/**
* Validator for the excluding file extensions
*/
class ExcludeExtension extends Extension
{
use FileInformationTrait;

/**
* @const string Error constants
*/
Expand All @@ -40,31 +43,17 @@ class ExcludeExtension extends Extension
*/
public function isValid($value, $file = null)
{
if (is_string($value) && is_array($file)) {
// Legacy Zend\Transfer API support
$filename = $file['name'];
$file = $file['tmp_name'];
} elseif (is_array($value)) {
if (! isset($value['tmp_name']) || ! isset($value['name'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
);
}
$file = $value['tmp_name'];
$filename = $value['name'];
} else {
$file = $value;
$filename = basename($file);
}
$this->setValue($filename);
$fileInfo = $this->getFileInfo($value, $file);

$this->setValue($fileInfo['filename']);

// Is file readable ?
if (empty($file) || false === is_readable($file)) {
if (empty($fileInfo['file']) || false === is_readable($fileInfo['file'])) {
$this->error(self::NOT_FOUND);
return false;
}

$extension = substr($filename, strrpos($filename, '.') + 1);
$extension = substr($fileInfo['filename'], strrpos($fileInfo['filename'], '.') + 1);
$extensions = $this->getExtension();

if ($this->getCase() && (! in_array($extension, $extensions))) {
Expand Down
33 changes: 9 additions & 24 deletions src/File/ExcludeMimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
namespace Zend\Validator\File;

use finfo;
use Zend\Validator\Exception;
use Zend\Validator\File\FileInformationTrait;

/**
* Validator for the mime type of a file
*/
class ExcludeMimeType extends MimeType
{
use FileInformationTrait;

const FALSE_TYPE = 'fileExcludeMimeTypeFalse';
const NOT_DETECTED = 'fileExcludeMimeTypeNotDetected';
const NOT_READABLE = 'fileExcludeMimeTypeNotReadable';
Expand All @@ -41,29 +43,12 @@ class ExcludeMimeType extends MimeType
*/
public function isValid($value, $file = null)
{
if (is_string($value) && is_array($file)) {
// Legacy Zend\Transfer API support
$filename = $file['name'];
$filetype = $file['type'];
$file = $file['tmp_name'];
} elseif (is_array($value)) {
if (! isset($value['tmp_name']) || ! isset($value['name']) || ! isset($value['type'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
);
}
$file = $value['tmp_name'];
$filename = $value['name'];
$filetype = $value['type'];
} else {
$file = $value;
$filename = basename($file);
$filetype = null;
}
$this->setValue($filename);
$fileInfo = $this->getFileInfo($value, $file, true);

$this->setValue($fileInfo['filename']);

// Is file readable ?
if (empty($file) || false === is_readable($file)) {
if (empty($fileInfo['file']) || false === is_readable($fileInfo['file'])) {
$this->error(self::NOT_READABLE);
return false;
}
Expand All @@ -80,12 +65,12 @@ public function isValid($value, $file = null)

$this->type = null;
if (! empty($this->finfo)) {
$this->type = finfo_file($this->finfo, $file);
$this->type = finfo_file($this->finfo, $fileInfo['file']);
}
}

if (empty($this->type) && $this->getHeaderCheck()) {
$this->type = $filetype;
$this->type = $fileInfo['filetype'];
}

if (empty($this->type)) {
Expand Down
29 changes: 8 additions & 21 deletions src/File/Exists.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@

use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;
use Zend\Validator\File\FileInformationTrait;

/**
* Validator which checks if the file already exists in the directory
*/
class Exists extends AbstractValidator
{
use FileInformationTrait;

/**
* @const string Error constants
*/
Expand Down Expand Up @@ -144,31 +147,15 @@ public function addDirectory($directory)
*/
public function isValid($value, $file = null)
{
if (is_string($value) && is_array($file)) {
// Legacy Zend\Transfer API support
$filename = $file['name'];
$file = $file['tmp_name'];
$this->setValue($filename);
} elseif (is_array($value)) {
if (! isset($value['tmp_name']) || ! isset($value['name'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
);
}
$file = $value['tmp_name'];
$filename = basename($file);
$this->setValue($value['name']);
} else {
$file = $value;
$filename = basename($file);
$this->setValue($filename);
}
$fileInfo = $this->getFileInfo($value, $file, false, true);

$this->setValue($fileInfo['filename']);

$check = false;
$directories = $this->getDirectory(true);
if (! isset($directories)) {
$check = true;
if (! file_exists($file)) {
if (! file_exists($fileInfo['file'])) {
$this->error(self::DOES_NOT_EXIST);
return false;
}
Expand All @@ -179,7 +166,7 @@ public function isValid($value, $file = null)
}

$check = true;
if (! file_exists($directory . DIRECTORY_SEPARATOR . $filename)) {
if (! file_exists($directory . DIRECTORY_SEPARATOR . $fileInfo['basename'])) {
$this->error(self::DOES_NOT_EXIST);
return false;
}
Expand Down
27 changes: 8 additions & 19 deletions src/File/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\Validator\AbstractValidator;
use Zend\Validator\File\FileInformationTrait;
use Zend\Validator\Exception;

/**
* Validator for the file extension of a file
*/
class Extension extends AbstractValidator
{
use FileInformationTrait;

/**
* @const string Error constants
*/
Expand Down Expand Up @@ -177,31 +180,17 @@ public function addExtension($extension)
*/
public function isValid($value, $file = null)
{
if (is_string($value) && is_array($file)) {
// Legacy Zend\Transfer API support
$filename = $file['name'];
$file = $file['tmp_name'];
} elseif (is_array($value)) {
if (! isset($value['tmp_name']) || ! isset($value['name'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
);
}
$file = $value['tmp_name'];
$filename = $value['name'];
} else {
$file = $value;
$filename = basename($file);
}
$this->setValue($filename);
$fileInfo = $this->getFileInfo($value, $file);

$this->setValue($fileInfo['filename']);

// Is file readable ?
if (empty($file) || false === is_readable($file)) {
if (empty($fileInfo['file']) || false === is_readable($fileInfo['file'])) {
$this->error(self::NOT_FOUND);
return false;
}

$extension = substr($filename, strrpos($filename, '.') + 1);
$extension = substr($fileInfo['filename'], strrpos($fileInfo['filename'], '.') + 1);
$extensions = $this->getExtension();

if ($this->getCase() && (in_array($extension, $extensions))) {
Expand Down
Loading