updated-packages

This commit is contained in:
RafficMohammed
2023-01-08 00:13:22 +05:30
parent 3ff7df7487
commit da241bacb6
12659 changed files with 563377 additions and 510538 deletions

View File

@@ -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
*/
@@ -85,32 +87,18 @@ class Crc32 extends Hash
*/
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;

View File

@@ -9,6 +9,7 @@
namespace Zend\Validator\File;
use Zend\Validator\File\FileInformationTrait;
use Zend\Validator\Exception;
/**
@@ -16,6 +17,8 @@ use Zend\Validator\Exception;
*/
class ExcludeExtension extends Extension
{
use FileInformationTrait;
/**
* @const string Error constants
*/
@@ -40,31 +43,21 @@ 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);
// Is file readable ?
if (empty($file) || false === is_readable($file)) {
if (! $this->getAllowNonExistentFile()
&& (empty($fileInfo['file']) || false === is_readable($fileInfo['file']))
) {
if (preg_match('/nofile\.mo$/', $fileInfo['file'])) {
}
$this->error(self::NOT_FOUND);
return false;
}
$extension = substr($filename, strrpos($filename, '.') + 1);
$this->setValue($fileInfo['filename']);
$extension = substr($fileInfo['filename'], strrpos($fileInfo['filename'], '.') + 1);
$extensions = $this->getExtension();
if ($this->getCase() && (! in_array($extension, $extensions))) {
@@ -72,6 +65,8 @@ class ExcludeExtension extends Extension
} elseif (! $this->getCase()) {
foreach ($extensions as $ext) {
if (strtolower($ext) == strtolower($extension)) {
if (preg_match('/nofile\.mo$/', $fileInfo['file'])) {
}
$this->error(self::FALSE_EXTENSION);
return false;
}

View File

@@ -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';
@@ -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;
}
@@ -80,12 +65,12 @@ class ExcludeMimeType extends MimeType
$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)) {

View File

@@ -11,12 +11,15 @@ namespace Zend\Validator\File;
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
*/
@@ -144,31 +147,15 @@ class Exists extends AbstractValidator
*/
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;
}
@@ -179,7 +166,7 @@ class Exists extends AbstractValidator
}
$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;
}

View File

@@ -12,6 +12,7 @@ namespace Zend\Validator\File;
use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\Validator\AbstractValidator;
use Zend\Validator\File\FileInformationTrait;
use Zend\Validator\Exception;
/**
@@ -19,6 +20,8 @@ use Zend\Validator\Exception;
*/
class Extension extends AbstractValidator
{
use FileInformationTrait;
/**
* @const string Error constants
*/
@@ -41,6 +44,7 @@ class Extension extends AbstractValidator
protected $options = [
'case' => false, // Validate case sensitive
'extension' => '', // List of extensions
'allowNonExistentFile' => false, // Allow validation even if file does not exist
];
/**
@@ -167,6 +171,28 @@ class Extension extends AbstractValidator
return $this;
}
/**
* Returns whether or not to allow validation of non-existent files.
*
* @return bool
*/
public function getAllowNonExistentFile()
{
return $this->options['allowNonExistentFile'];
}
/**
* Sets the flag indicating whether or not to allow validation of non-existent files.
*
* @param bool $flag Whether or not to allow validation of non-existent files.
* @return self Provides a fluent interface
*/
public function setAllowNonExistentFile($flag)
{
$this->options['allowNonExistentFile'] = (bool) $flag;
return $this;
}
/**
* Returns true if and only if the file extension of $value is included in the
* set extension list
@@ -177,31 +203,19 @@ class Extension extends AbstractValidator
*/
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);
// Is file readable ?
if (empty($file) || false === is_readable($file)) {
if (! $this->getAllowNonExistentFile()
&& (empty($fileInfo['file']) || false === is_readable($fileInfo['file']))
) {
$this->error(self::NOT_FOUND);
return false;
}
$extension = substr($filename, strrpos($filename, '.') + 1);
$this->setValue($fileInfo['filename']);
$extension = substr($fileInfo['filename'], strrpos($fileInfo['filename'], '.') + 1);
$extensions = $this->getExtension();
if ($this->getCase() && (in_array($extension, $extensions))) {

View File

@@ -0,0 +1,166 @@
<?php
/**
* @see https://github.com/zendframework/zend-validator for the canonical source repository
* @copyright Copyright (c) 2019 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-validator/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Validator\File;
use Psr\Http\Message\UploadedFileInterface;
use Zend\Validator\Exception;
trait FileInformationTrait
{
/**
* Returns array if the procedure is identified
*
* @param string|array|object $value Filename to check
* @param null|array $file File data (when using legacy Zend_File_Transfer API)
* @param bool $hasType Return with filetype (optional)
* @param bool $basename Return with basename - is calculated from location path (optional)
* @return array
*/
protected function getFileInfo(
$value,
array $file = null,
$hasType = false,
$hasBasename = false
) {
if (is_string($value) && is_array($file)) {
return $this->getLegacyFileInfo($file, $hasType, $hasBasename);
}
if (is_array($value)) {
return $this->getSapiFileInfo($value, $hasType, $hasBasename);
}
if ($value instanceof UploadedFileInterface) {
return $this->getPsr7FileInfo($value, $hasType, $hasBasename);
}
return $this->getFileBasedFileInfo($value, $hasType, $hasBasename);
}
/**
* Generate file information array with legacy Zend_File_Transfer API
*
* @param array $file File data
* @param bool $hasType Return with filetype
* @param bool $hasBasename Basename is calculated from location path
* @return array
*/
private function getLegacyFileInfo(
array $file,
$hasType = false,
$hasBasename = false
) {
$fileInfo = [];
$fileInfo['filename'] = $file['name'];
$fileInfo['file'] = $file['tmp_name'];
if ($hasBasename) {
$fileInfo['basename'] = basename($fileInfo['file']);
}
if ($hasType) {
$fileInfo['filetype'] = $file['type'];
}
return $fileInfo;
}
/**
* Generate file information array with SAPI
*
* @param array $file File data from SAPI
* @param bool $hasType Return with filetype
* @param bool $hasBasename Filename is calculated from location path
* @return array
*/
private function getSapiFileInfo(
array $file,
$hasType = false,
$hasBasename = false
) {
if (! isset($file['tmp_name']) || ! isset($file['name'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
);
}
$fileInfo = [];
$fileInfo['file'] = $file['tmp_name'];
$fileInfo['filename'] = $file['name'];
if ($hasBasename) {
$fileInfo['basename'] = basename($fileInfo['file']);
}
if ($hasType) {
$fileInfo['filetype'] = $file['type'];
}
return $fileInfo;
}
/**
* Generate file information array with PSR-7 UploadedFileInterface
*
* @param UploadedFileInterface $file
* @param bool $hasType Return with filetype
* @param bool $hasBasename Filename is calculated from location path
* @return array
*/
private function getPsr7FileInfo(
UploadedFileInterface $file,
$hasType = false,
$hasBasename = false
) {
$fileInfo = [];
$fileInfo['file'] = $file->getStream()->getMetadata('uri');
$fileInfo['filename'] = $file->getClientFilename();
if ($hasBasename) {
$fileInfo['basename'] = basename($fileInfo['file']);
}
if ($hasType) {
$fileInfo['filetype'] = $file->getClientMediaType();
}
return $fileInfo;
}
/**
* Generate file information array with base method
*
* @param string $file File path
* @param bool $hasType Return with filetype
* @param bool $hasBasename Filename is calculated from location path
* @return array
*/
private function getFileBasedFileInfo(
$file,
$hasType = false,
$hasBasename = false
) {
$fileInfo = [];
$fileInfo['file'] = $file;
$fileInfo['filename'] = basename($fileInfo['file']);
if ($hasBasename) {
$fileInfo['basename'] = basename($fileInfo['file']);
}
if ($hasType) {
$fileInfo['filetype'] = null;
}
return $fileInfo;
}
}

View File

@@ -151,7 +151,7 @@ class FilesSize extends Size
}
}
if (count($this->getMessages()) > 0) {
if ($this->getMessages()) {
return false;
}

View File

@@ -11,12 +11,15 @@ namespace Zend\Validator\File;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;
use Zend\Validator\File\FileInformationTrait;
/**
* Validator for the hash of given files
*/
class Hash extends AbstractValidator
{
use FileInformationTrait;
/**
* @const string Error constants
*/
@@ -114,6 +117,12 @@ class Hash extends AbstractValidator
}
foreach ($options as $value) {
if (! is_string($value)) {
throw new Exception\InvalidArgumentException(sprintf(
'Hash must be a string, %s received',
is_object($value) ? get_class($value) : gettype($value)
));
}
$this->options['hash'][$value] = $algorithm;
}
@@ -129,43 +138,27 @@ class Hash extends AbstractValidator
*/
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;
}
$algos = array_unique(array_values($this->getHash()));
$hashes = array_unique(array_keys($this->getHash()));
$algos = array_unique(array_values($this->getHash()));
foreach ($algos as $algorithm) {
$filehash = hash_file($algorithm, $file);
$filehash = hash_file($algorithm, $fileInfo['file']);
if ($filehash === false) {
$this->error(self::NOT_DETECTED);
return false;
}
foreach ($hashes as $hash) {
if ($filehash === $hash) {
return true;
}
if (isset($this->getHash()[$filehash]) && $this->getHash()[$filehash] === $algorithm) {
return true;
}
}

View File

@@ -12,12 +12,15 @@ namespace Zend\Validator\File;
use Zend\Stdlib\ErrorHandler;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;
use Zend\Validator\File\FileInformationTrait;
/**
* Validator for the image size of an image file
*/
class ImageSize extends AbstractValidator
{
use FileInformationTrait;
/**
* @const string Error constants
*/
@@ -332,32 +335,18 @@ class ImageSize extends AbstractValidator
*/
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_READABLE);
return false;
}
ErrorHandler::start();
$size = getimagesize($file);
$size = getimagesize($fileInfo['file']);
ErrorHandler::stop();
if (empty($size) || ($size[0] === 0) || ($size[1] === 0)) {
@@ -383,7 +372,7 @@ class ImageSize extends AbstractValidator
$this->error(self::HEIGHT_TOO_BIG);
}
if (count($this->getMessages()) > 0) {
if ($this->getMessages()) {
return false;
}

View File

@@ -74,6 +74,7 @@ class IsImage extends MimeType
'image/vnd.djvu',
'image/vnd.fpx',
'image/vnd.net-fpx',
'image/webp',
'image/x-cmu-raster',
'image/x-cmx',
'image/x-coreldraw',

View File

@@ -9,13 +9,15 @@
namespace Zend\Validator\File;
use Zend\Validator\Exception;
use Zend\Validator\File\FileInformationTrait;
/**
* Validator for the md5 hash of given files
*/
class Md5 extends Hash
{
use FileInformationTrait;
/**
* @const string Error constants
*/
@@ -85,32 +87,18 @@ class Md5 extends Hash
*/
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('md5', $file);
$filehash = hash_file('md5', $fileInfo['file']);
if ($filehash === false) {
$this->error(self::NOT_DETECTED);
return false;

View File

@@ -14,12 +14,15 @@ use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\ErrorHandler;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;
use Zend\Validator\File\FileInformationTrait;
/**
* Validator for the mime type of a file
*/
class MimeType extends AbstractValidator
{
use FileInformationTrait;
/**#@+
* @const Error type constants
*/
@@ -341,29 +344,12 @@ class MimeType extends AbstractValidator
*/
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(static::NOT_READABLE);
return false;
}
@@ -384,13 +370,13 @@ class MimeType extends AbstractValidator
$this->type = null;
if (! empty($this->finfo)) {
$this->type = finfo_file($this->finfo, $file);
$this->type = finfo_file($this->finfo, $fileInfo['file']);
unset($this->finfo);
}
}
if (empty($this->type) && $this->getHeaderCheck()) {
$this->type = $filetype;
$this->type = $fileInfo['filetype'];
}
if (empty($this->type)) {

View File

@@ -10,12 +10,15 @@
namespace Zend\Validator\File;
use Zend\Validator\Exception;
use Zend\Validator\File\FileInformationTrait;
/**
* Validator which checks if the destination file does not exist
*/
class NotExists extends Exists
{
use FileInformationTrait;
/**
* @const string Error constants
*/
@@ -37,31 +40,15 @@ class NotExists extends Exists
*/
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_EXIST);
return false;
}
@@ -72,7 +59,7 @@ class NotExists extends Exists
}
$check = true;
if (file_exists($directory . DIRECTORY_SEPARATOR . $filename)) {
if (file_exists($directory . DIRECTORY_SEPARATOR . $fileInfo['basename'])) {
$this->error(self::DOES_EXIST);
return false;
}

View File

@@ -9,13 +9,15 @@
namespace Zend\Validator\File;
use Zend\Validator\Exception;
use Zend\Validator\File\FileInformationTrait;
/**
* Validator for the sha1 hash of given files
*/
class Sha1 extends Hash
{
use FileInformationTrait;
/**
* @const string Error constants
*/
@@ -85,32 +87,18 @@ class Sha1 extends Hash
*/
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('sha1', $file);
$filehash = hash_file('sha1', $fileInfo['file']);
if ($filehash === false) {
$this->error(self::NOT_DETECTED);
return false;

View File

@@ -12,12 +12,15 @@ namespace Zend\Validator\File;
use Zend\Stdlib\ErrorHandler;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;
use Zend\Validator\File\FileInformationTrait;
/**
* Validator for the maximum size of a file up to a max of 2GB
*/
class Size extends AbstractValidator
{
use FileInformationTrait;
/**
* @const string Error constants
*/
@@ -234,33 +237,19 @@ class Size extends AbstractValidator
*/
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;
}
// limited to 4GB files
ErrorHandler::start();
$size = sprintf("%u", filesize($file));
$size = sprintf("%u", filesize($fileInfo['file']));
ErrorHandler::stop();
$this->size = $size;
@@ -292,7 +281,7 @@ class Size extends AbstractValidator
}
}
if (count($this->getMessages()) > 0) {
if ($this->getMessages()) {
return false;
}

View File

@@ -10,6 +10,7 @@
namespace Zend\Validator\File;
use Countable;
use Psr\Http\Message\UploadedFileInterface;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;
@@ -37,13 +38,14 @@ class Upload extends AbstractValidator
* @var array Error message templates
*/
protected $messageTemplates = [
self::INI_SIZE => "File '%value%' exceeds the defined ini size",
self::FORM_SIZE => "File '%value%' exceeds the defined form size",
self::INI_SIZE => "File '%value%' exceeds upload_max_filesize directive in php.ini",
self::FORM_SIZE => "File '%value%' exceeds the MAX_FILE_SIZE directive that was "
. 'specified in the HTML form',
self::PARTIAL => "File '%value%' was only partially uploaded",
self::NO_FILE => "File '%value%' was not uploaded",
self::NO_TMP_DIR => "No temporary directory was found for file '%value%'",
self::CANT_WRITE => "File '%value%' can't be written",
self::EXTENSION => "A PHP extension returned an error while uploading the file '%value%'",
self::NO_TMP_DIR => "Missing a temporary folder to store '%value%'",
self::CANT_WRITE => "Failed to write file '%value%' to disk",
self::EXTENSION => "A PHP extension stopped uploading the file '%value%'",
self::ATTACK => "File '%value%' was illegally uploaded. This could be a possible attack",
self::FILE_NOT_FOUND => "File '%value%' was not found",
self::UNKNOWN => "Unknown error while uploading file '%value%'"
@@ -87,12 +89,16 @@ class Upload extends AbstractValidator
$return[$file] = $this->options['files'][$name];
}
if ($content['name'] === $file) {
if ($content instanceof UploadedFileInterface) {
if ($content->getClientFilename() === $file) {
$return[$name] = $this->options['files'][$name];
}
} elseif ($content['name'] === $file) {
$return[$name] = $this->options['files'][$name];
}
}
if (count($return) === 0) {
if (! $return) {
throw new Exception\InvalidArgumentException("The file '$file' was not found");
}
@@ -124,7 +130,9 @@ class Upload extends AbstractValidator
}
foreach ($this->options['files'] as $file => $content) {
if (! isset($content['error'])) {
if (! $content instanceof UploadedFileInterface
&& ! isset($content['error'])
) {
unset($this->options['files'][$file]);
}
}
@@ -148,6 +156,18 @@ class Upload extends AbstractValidator
$files = array_merge($files, $this->getFiles($value));
} else {
foreach ($this->getFiles() as $file => $content) {
if ($content instanceof UploadedFileInterface) {
if ($content->getClientFilename() === $value) {
$files = array_merge($files, $this->getFiles($file));
}
// PSR cannot search by tmp_name because it does not have
// a public interface to get it, only user defined name
// from form field.
continue;
}
if (isset($content['name']) && ($content['name'] === $value)) {
$files = array_merge($files, $this->getFiles($file));
}
@@ -164,8 +184,19 @@ class Upload extends AbstractValidator
foreach ($files as $file => $content) {
$this->value = $file;
switch ($content['error']) {
$error = $content instanceof UploadedFileInterface
? $content->getError()
: $content['error'];
switch ($error) {
case 0:
if ($content instanceof UploadedFileInterface) {
// done!
break;
}
// For standard SAPI environments, check that the upload
// was valid
if (! is_uploaded_file($content['tmp_name'])) {
$this->throwError($content, self::ATTACK);
}
@@ -205,7 +236,7 @@ class Upload extends AbstractValidator
}
}
if (count($this->getMessages()) > 0) {
if ($this->getMessages()) {
return false;
}
@@ -215,7 +246,7 @@ class Upload extends AbstractValidator
/**
* Throws an error of the given type
*
* @param string $file
* @param array|string|UploadedFileInterface $file
* @param string $errorType
* @return false
*/
@@ -228,6 +259,8 @@ class Upload extends AbstractValidator
}
} elseif (is_string($file)) {
$this->value = $file;
} elseif ($file instanceof UploadedFileInterface) {
$this->value = $file->getClientFilename();
}
}

View File

@@ -9,6 +9,7 @@
namespace Zend\Validator\File;
use Psr\Http\Message\UploadedFileInterface;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;
@@ -35,22 +36,23 @@ class UploadFile extends AbstractValidator
* @var array Error message templates
*/
protected $messageTemplates = [
self::INI_SIZE => "File exceeds the defined ini size",
self::FORM_SIZE => "File exceeds the defined form size",
self::PARTIAL => "File was only partially uploaded",
self::NO_FILE => "File was not uploaded",
self::NO_TMP_DIR => "No temporary directory was found for file",
self::CANT_WRITE => "File can't be written",
self::EXTENSION => "A PHP extension returned an error while uploading the file",
self::ATTACK => "File was illegally uploaded. This could be a possible attack",
self::FILE_NOT_FOUND => "File was not found",
self::UNKNOWN => "Unknown error while uploading file",
self::INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
self::FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was '
. 'specified in the HTML form',
self::PARTIAL => 'The uploaded file was only partially uploaded',
self::NO_FILE => 'No file was uploaded',
self::NO_TMP_DIR => 'Missing a temporary folder',
self::CANT_WRITE => 'Failed to write file to disk',
self::EXTENSION => 'A PHP extension stopped the file upload',
self::ATTACK => 'File was illegally uploaded. This could be a possible attack',
self::FILE_NOT_FOUND => 'File was not found',
self::UNKNOWN => 'Unknown error while uploading file',
];
/**
* Returns true if and only if the file was uploaded without errors
*
* @param string $value File to check for upload errors
* @param string|array|UploadedFileInterface $value File to check for upload errors
* @return bool
* @throws Exception\InvalidArgumentException
*/
@@ -62,62 +64,106 @@ class UploadFile extends AbstractValidator
'Value array must be in $_FILES format'
);
}
$file = $value['tmp_name'];
$filename = $value['name'];
$error = $value['error'];
} else {
$file = $value;
$filename = basename($file);
$error = 0;
}
$this->setValue($filename);
return $this->validateUploadedFile(
$value['error'],
$value['name'],
$value['tmp_name']
);
}
if ($value instanceof UploadedFileInterface) {
return $this->validatePsr7UploadedFile($value);
}
if (is_string($value)) {
return $this->validateUploadedFile(0, basename($value), $value);
}
$this->error(self::UNKNOWN);
return false;
}
/**
* @param int $error UPLOAD_ERR_* constant value
* @return bool
*/
private function validateFileFromErrorCode($error)
{
switch ($error) {
case UPLOAD_ERR_OK:
if (empty($file) || false === is_file($file)) {
$this->error(self::FILE_NOT_FOUND);
} elseif (! is_uploaded_file($file)) {
$this->error(self::ATTACK);
}
break;
return true;
case UPLOAD_ERR_INI_SIZE:
$this->error(self::INI_SIZE);
break;
return false;
case UPLOAD_ERR_FORM_SIZE:
$this->error(self::FORM_SIZE);
break;
return false;
case UPLOAD_ERR_PARTIAL:
$this->error(self::PARTIAL);
break;
return false;
case UPLOAD_ERR_NO_FILE:
$this->error(self::NO_FILE);
break;
return false;
case UPLOAD_ERR_NO_TMP_DIR:
$this->error(self::NO_TMP_DIR);
break;
return false;
case UPLOAD_ERR_CANT_WRITE:
$this->error(self::CANT_WRITE);
break;
return false;
case UPLOAD_ERR_EXTENSION:
$this->error(self::EXTENSION);
break;
return false;
default:
$this->error(self::UNKNOWN);
break;
return false;
}
}
/**
* @param int $error UPLOAD_ERR_* constant
* @param string $filename
* @param string $uploadedFile Name of uploaded file (gen tmp_name)
* @return bool
*/
private function validateUploadedFile($error, $filename, $uploadedFile)
{
$this->setValue($filename);
// Normal errors can be validated normally
if ($error !== UPLOAD_ERR_OK) {
return $this->validateFileFromErrorCode($error);
}
if (count($this->getMessages()) > 0) {
// Did we get no name? Is the file missing?
if (empty($uploadedFile) || false === is_file($uploadedFile)) {
$this->error(self::FILE_NOT_FOUND);
return false;
}
// Do we have an invalid upload?
if (! is_uploaded_file($uploadedFile)) {
$this->error(self::ATTACK);
return false;
}
return true;
}
/**
* @return bool
*/
private function validatePsr7UploadedFile(UploadedFileInterface $uploadedFile)
{
$this->setValue($uploadedFile);
return $this->validateFileFromErrorCode($uploadedFile->getError());
}
}

View File

@@ -11,12 +11,15 @@ namespace Zend\Validator\File;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;
use Zend\Validator\File\FileInformationTrait;
/**
* Validator for counting all words in a file
*/
class WordCount extends AbstractValidator
{
use FileInformationTrait;
/**
* @const string Error constants
*/
@@ -175,31 +178,17 @@ class WordCount extends AbstractValidator
*/
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;
}
$content = file_get_contents($file);
$content = file_get_contents($fileInfo['file']);
$this->count = str_word_count($content);
if (($this->getMax() !== null) && ($this->count > $this->getMax())) {
$this->error(self::TOO_MUCH);