update v1.0.7.9 R.C.

This is a Release Candidate. We are still testing.
This commit is contained in:
Sujit Prasad
2016-08-03 20:04:36 +05:30
parent 8b6b924d09
commit ffa56a43cb
3830 changed files with 181529 additions and 495353 deletions

View File

@@ -0,0 +1,254 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\File;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;
/**
* Validator for counting all given files
*
*/
class Count extends AbstractValidator
{
/**#@+
* @const string Error constants
*/
const TOO_MANY = 'fileCountTooMany';
const TOO_FEW = 'fileCountTooFew';
/**#@-*/
/**
* @var array Error message templates
*/
protected $messageTemplates = [
self::TOO_MANY => "Too many files, maximum '%max%' are allowed but '%count%' are given",
self::TOO_FEW => "Too few files, minimum '%min%' are expected but '%count%' are given",
];
/**
* @var array Error message template variables
*/
protected $messageVariables = [
'min' => ['options' => 'min'],
'max' => ['options' => 'max'],
'count' => 'count'
];
/**
* Actual filecount
*
* @var int
*/
protected $count;
/**
* Internal file array
* @var array
*/
protected $files;
/**
* Options for this validator
*
* @var array
*/
protected $options = [
'min' => null, // Minimum file count, if null there is no minimum file count
'max' => null, // Maximum file count, if null there is no maximum file count
];
/**
* Sets validator options
*
* Min limits the file count, when used with max=null it is the maximum file count
* It also accepts an array with the keys 'min' and 'max'
*
* If $options is an integer, it will be used as maximum file count
* As Array is accepts the following keys:
* 'min': Minimum filecount
* 'max': Maximum filecount
*
* @param int|array|\Traversable $options Options for the adapter
*/
public function __construct($options = null)
{
if (1 < func_num_args()) {
$args = func_get_args();
$options = [
'min' => array_shift($args),
'max' => array_shift($args),
];
}
if (is_string($options) || is_numeric($options)) {
$options = ['max' => $options];
}
parent::__construct($options);
}
/**
* Returns the minimum file count
*
* @return int
*/
public function getMin()
{
return $this->options['min'];
}
/**
* Sets the minimum file count
*
* @param int|array $min The minimum file count
* @return Count Provides a fluent interface
* @throws Exception\InvalidArgumentException When min is greater than max
*/
public function setMin($min)
{
if (is_array($min) && isset($min['min'])) {
$min = $min['min'];
}
if (! is_numeric($min)) {
throw new Exception\InvalidArgumentException('Invalid options to validator provided');
}
$min = (int) $min;
if (($this->getMax() !== null) && ($min > $this->getMax())) {
throw new Exception\InvalidArgumentException(
"The minimum must be less than or equal to the maximum file count, but {$min} > {$this->getMax()}"
);
}
$this->options['min'] = $min;
return $this;
}
/**
* Returns the maximum file count
*
* @return int
*/
public function getMax()
{
return $this->options['max'];
}
/**
* Sets the maximum file count
*
* @param int|array $max The maximum file count
* @return Count Provides a fluent interface
* @throws Exception\InvalidArgumentException When max is smaller than min
*/
public function setMax($max)
{
if (is_array($max) && isset($max['max'])) {
$max = $max['max'];
}
if (! is_numeric($max)) {
throw new Exception\InvalidArgumentException('Invalid options to validator provided');
}
$max = (int) $max;
if (($this->getMin() !== null) && ($max < $this->getMin())) {
throw new Exception\InvalidArgumentException(
"The maximum must be greater than or equal to the minimum file count, but {$max} < {$this->getMin()}"
);
}
$this->options['max'] = $max;
return $this;
}
/**
* Adds a file for validation
*
* @param string|array $file
* @return Count
*/
public function addFile($file)
{
if (is_string($file)) {
$file = [$file];
}
if (is_array($file)) {
foreach ($file as $name) {
if (!isset($this->files[$name]) && !empty($name)) {
$this->files[$name] = $name;
}
}
}
return $this;
}
/**
* Returns true if and only if the file count of all checked files is at least min and
* not bigger than max (when max is not null). Attention: When checking with set min you
* must give all files with the first call, otherwise you will get a false.
*
* @param string|array $value Filenames to check for count
* @param array $file File data from \Zend\File\Transfer\Transfer
* @return bool
*/
public function isValid($value, $file = null)
{
if (($file !== null) && !array_key_exists('destination', $file)) {
$file['destination'] = dirname($value);
}
if (($file !== null) && array_key_exists('tmp_name', $file)) {
$value = $file['destination'] . DIRECTORY_SEPARATOR . $file['name'];
}
if (($file === null) || !empty($file['tmp_name'])) {
$this->addFile($value);
}
$this->count = count($this->files);
if (($this->getMax() !== null) && ($this->count > $this->getMax())) {
return $this->throwError($file, self::TOO_MANY);
}
if (($this->getMin() !== null) && ($this->count < $this->getMin())) {
return $this->throwError($file, self::TOO_FEW);
}
return true;
}
/**
* Throws an error of the given type
*
* @param string $file
* @param string $errorType
* @return false
*/
protected function throwError($file, $errorType)
{
if ($file !== null) {
if (is_array($file)) {
if (array_key_exists('name', $file)) {
$this->value = $file['name'];
}
} elseif (is_string($file)) {
$this->value = $file;
}
}
$this->error($errorType);
return false;
}
}

View File

@@ -0,0 +1,128 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\File;
use Zend\Validator\Exception;
/**
* Validator for the crc32 hash of given files
*/
class Crc32 extends Hash
{
/**
* @const string Error constants
*/
const DOES_NOT_MATCH = 'fileCrc32DoesNotMatch';
const NOT_DETECTED = 'fileCrc32NotDetected';
const NOT_FOUND = 'fileCrc32NotFound';
/**
* @var array Error message templates
*/
protected $messageTemplates = [
self::DOES_NOT_MATCH => "File does not match the given crc32 hashes",
self::NOT_DETECTED => "A crc32 hash could not be evaluated for the given file",
self::NOT_FOUND => "File is not readable or does not exist",
];
/**
* Options for this validator
*
* @var string
*/
protected $options = [
'algorithm' => 'crc32',
'hash' => null,
];
/**
* Returns all set crc32 hashes
*
* @return array
*/
public function getCrc32()
{
return $this->getHash();
}
/**
* Sets the crc32 hash for one or multiple files
*
* @param string|array $options
* @return Crc32 Provides a fluent interface
*/
public function setCrc32($options)
{
$this->setHash($options);
return $this;
}
/**
* Adds the crc32 hash for one or multiple files
*
* @param string|array $options
* @return Crc32 Provides a fluent interface
*/
public function addCrc32($options)
{
$this->addHash($options);
return $this;
}
/**
* Returns true if and only if the given file confirms the set hash
*
* @param string|array $value Filename to check for hash
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
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);
// Is file readable ?
if (empty($file) || false === stream_resolve_include_path($file)) {
$this->error(self::NOT_FOUND);
return false;
}
$hashes = array_unique(array_keys($this->getHash()));
$filehash = hash_file('crc32', $file);
if ($filehash === false) {
$this->error(self::NOT_DETECTED);
return false;
}
foreach ($hashes as $hash) {
if ($filehash === $hash) {
return true;
}
}
$this->error(self::DOES_NOT_MATCH);
return false;
}
}

View File

@@ -0,0 +1,86 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\File;
use Zend\Validator\Exception;
/**
* Validator for the excluding file extensions
*/
class ExcludeExtension extends Extension
{
/**
* @const string Error constants
*/
const FALSE_EXTENSION = 'fileExcludeExtensionFalse';
const NOT_FOUND = 'fileExcludeExtensionNotFound';
/**
* @var array Error message templates
*/
protected $messageTemplates = [
self::FALSE_EXTENSION => "File has an incorrect extension",
self::NOT_FOUND => "File is not readable or does not exist",
];
/**
* Returns true if and only if the file extension of $value is not included in the
* set extension list
*
* @param string|array $value Real file to check for extension
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
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);
// Is file readable ?
if (empty($file) || false === stream_resolve_include_path($file)) {
$this->error(self::NOT_FOUND);
return false;
}
$extension = substr($filename, strrpos($filename, '.') + 1);
$extensions = $this->getExtension();
if ($this->getCase() && (!in_array($extension, $extensions))) {
return true;
} elseif (!$this->getCase()) {
foreach ($extensions as $ext) {
if (strtolower($ext) == strtolower($extension)) {
$this->error(self::FALSE_EXTENSION);
return false;
}
}
return true;
}
$this->error(self::FALSE_EXTENSION);
return false;
}
}

View File

@@ -0,0 +1,114 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\File;
use finfo;
use Zend\Validator\Exception;
/**
* Validator for the mime type of a file
*/
class ExcludeMimeType extends MimeType
{
const FALSE_TYPE = 'fileExcludeMimeTypeFalse';
const NOT_DETECTED = 'fileExcludeMimeTypeNotDetected';
const NOT_READABLE = 'fileExcludeMimeTypeNotReadable';
/**
* @var array Error message templates
*/
protected $messageTemplates = [
self::FALSE_TYPE => "File has an incorrect mimetype of '%type%'",
self::NOT_DETECTED => "The mimetype could not be detected from the file",
self::NOT_READABLE => "File is not readable or does not exist",
];
/**
* Returns true if the mimetype of the file does not matche the given ones. Also parts
* of mimetypes can be checked. If you give for example "image" all image
* mime types will not be accepted like "image/gif", "image/jpeg" and so on.
*
* @param string|array $value Real file to check for mimetype
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
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);
// Is file readable ?
if (empty($file) || false === stream_resolve_include_path($file)) {
$this->error(self::NOT_READABLE);
return false;
}
$mimefile = $this->getMagicFile();
if (class_exists('finfo', false)) {
if (!$this->isMagicFileDisabled() && (!empty($mimefile) && empty($this->finfo))) {
$this->finfo = finfo_open(FILEINFO_MIME_TYPE, $mimefile);
}
if (empty($this->finfo)) {
$this->finfo = finfo_open(FILEINFO_MIME_TYPE);
}
$this->type = null;
if (!empty($this->finfo)) {
$this->type = finfo_file($this->finfo, $file);
}
}
if (empty($this->type) && $this->getHeaderCheck()) {
$this->type = $filetype;
}
if (empty($this->type)) {
$this->error(self::NOT_DETECTED);
return false;
}
$mimetype = $this->getMimeType(true);
if (in_array($this->type, $mimetype)) {
$this->error(self::FALSE_TYPE);
return false;
}
$types = explode('/', $this->type);
$types = array_merge($types, explode('-', $this->type));
$types = array_merge($types, explode(';', $this->type));
foreach ($mimetype as $mime) {
if (in_array($mime, $types)) {
$this->error(self::FALSE_TYPE);
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,196 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\File;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;
/**
* Validator which checks if the file already exists in the directory
*/
class Exists extends AbstractValidator
{
/**
* @const string Error constants
*/
const DOES_NOT_EXIST = 'fileExistsDoesNotExist';
/**
* @var array Error message templates
*/
protected $messageTemplates = [
self::DOES_NOT_EXIST => "File does not exist",
];
/**
* Options for this validator
*
* @var array
*/
protected $options = [
'directory' => null, // internal list of directories
];
/**
* @var array Error message template variables
*/
protected $messageVariables = [
'directory' => ['options' => 'directory'],
];
/**
* Sets validator options
*
* @param string|array|\Traversable $options
*/
public function __construct($options = null)
{
if (is_string($options)) {
$options = explode(',', $options);
}
if (is_array($options) && !array_key_exists('directory', $options)) {
$options = ['directory' => $options];
}
parent::__construct($options);
}
/**
* Returns the set file directories which are checked
*
* @param bool $asArray Returns the values as array; when false, a concatenated string is returned
* @return string|null
*/
public function getDirectory($asArray = false)
{
$asArray = (bool) $asArray;
$directory = $this->options['directory'];
if ($asArray && isset($directory)) {
$directory = explode(',', (string) $directory);
}
return $directory;
}
/**
* Sets the file directory which will be checked
*
* @param string|array $directory The directories to validate
* @return Extension Provides a fluent interface
*/
public function setDirectory($directory)
{
$this->options['directory'] = null;
$this->addDirectory($directory);
return $this;
}
/**
* Adds the file directory which will be checked
*
* @param string|array $directory The directory to add for validation
* @return Extension Provides a fluent interface
* @throws Exception\InvalidArgumentException
*/
public function addDirectory($directory)
{
$directories = $this->getDirectory(true);
if (!isset($directories)) {
$directories = [];
}
if (is_string($directory)) {
$directory = explode(',', $directory);
} elseif (!is_array($directory)) {
throw new Exception\InvalidArgumentException('Invalid options to validator provided');
}
foreach ($directory as $content) {
if (empty($content) || !is_string($content)) {
continue;
}
$directories[] = trim($content);
}
$directories = array_unique($directories);
// Sanity check to ensure no empty values
foreach ($directories as $key => $dir) {
if (empty($dir)) {
unset($directories[$key]);
}
}
$this->options['directory'] = (!empty($directory))
? implode(',', $directories) : null;
return $this;
}
/**
* Returns true if and only if the file already exists in the set directories
*
* @param string|array $value Real file to check for existence
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
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);
}
$check = false;
$directories = $this->getDirectory(true);
if (!isset($directories)) {
$check = true;
if (!file_exists($file)) {
$this->error(self::DOES_NOT_EXIST);
return false;
}
} else {
foreach ($directories as $directory) {
if (!isset($directory) || '' === $directory) {
continue;
}
$check = true;
if (!file_exists($directory . DIRECTORY_SEPARATOR . $filename)) {
$this->error(self::DOES_NOT_EXIST);
return false;
}
}
}
if (!$check) {
$this->error(self::DOES_NOT_EXIST);
return false;
}
return true;
}
}

View File

@@ -0,0 +1,220 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\File;
use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;
/**
* Validator for the file extension of a file
*/
class Extension extends AbstractValidator
{
/**
* @const string Error constants
*/
const FALSE_EXTENSION = 'fileExtensionFalse';
const NOT_FOUND = 'fileExtensionNotFound';
/**
* @var array Error message templates
*/
protected $messageTemplates = [
self::FALSE_EXTENSION => "File has an incorrect extension",
self::NOT_FOUND => "File is not readable or does not exist",
];
/**
* Options for this validator
*
* @var array
*/
protected $options = [
'case' => false, // Validate case sensitive
'extension' => '', // List of extensions
];
/**
* @var array Error message template variables
*/
protected $messageVariables = [
'extension' => ['options' => 'extension'],
];
/**
* Sets validator options
*
* @param string|array|Traversable $options
*/
public function __construct($options = null)
{
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
}
$case = null;
if (1 < func_num_args()) {
$case = func_get_arg(1);
}
if (is_array($options)) {
if (isset($options['case'])) {
$case = $options['case'];
unset($options['case']);
}
if (!array_key_exists('extension', $options)) {
$options = ['extension' => $options];
}
} else {
$options = ['extension' => $options];
}
if ($case !== null) {
$options['case'] = $case;
}
parent::__construct($options);
}
/**
* Returns the case option
*
* @return bool
*/
public function getCase()
{
return $this->options['case'];
}
/**
* Sets the case to use
*
* @param bool $case
* @return Extension Provides a fluent interface
*/
public function setCase($case)
{
$this->options['case'] = (bool) $case;
return $this;
}
/**
* Returns the set file extension
*
* @return array
*/
public function getExtension()
{
$extension = explode(',', $this->options['extension']);
return $extension;
}
/**
* Sets the file extensions
*
* @param string|array $extension The extensions to validate
* @return Extension Provides a fluent interface
*/
public function setExtension($extension)
{
$this->options['extension'] = null;
$this->addExtension($extension);
return $this;
}
/**
* Adds the file extensions
*
* @param string|array $extension The extensions to add for validation
* @return Extension Provides a fluent interface
*/
public function addExtension($extension)
{
$extensions = $this->getExtension();
if (is_string($extension)) {
$extension = explode(',', $extension);
}
foreach ($extension as $content) {
if (empty($content) || !is_string($content)) {
continue;
}
$extensions[] = trim($content);
}
$extensions = array_unique($extensions);
// Sanity check to ensure no empty values
foreach ($extensions as $key => $ext) {
if (empty($ext)) {
unset($extensions[$key]);
}
}
$this->options['extension'] = implode(',', $extensions);
return $this;
}
/**
* Returns true if and only if the file extension of $value is included in the
* set extension list
*
* @param string|array $value Real file to check for extension
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
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);
// Is file readable ?
if (empty($file) || false === stream_resolve_include_path($file)) {
$this->error(self::NOT_FOUND);
return false;
}
$extension = substr($filename, strrpos($filename, '.') + 1);
$extensions = $this->getExtension();
if ($this->getCase() && (in_array($extension, $extensions))) {
return true;
} elseif (!$this->getCase()) {
foreach ($extensions as $ext) {
if (strtolower($ext) == strtolower($extension)) {
return true;
}
}
}
$this->error(self::FALSE_EXTENSION);
return false;
}
}

View File

@@ -0,0 +1,183 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\File;
use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\ErrorHandler;
use Zend\Validator\Exception;
/**
* Validator for the size of all files which will be validated in sum
*
*/
class FilesSize extends Size
{
/**
* @const string Error constants
*/
const TOO_BIG = 'fileFilesSizeTooBig';
const TOO_SMALL = 'fileFilesSizeTooSmall';
const NOT_READABLE = 'fileFilesSizeNotReadable';
/**
* @var array Error message templates
*/
protected $messageTemplates = [
self::TOO_BIG => "All files in sum should have a maximum size of '%max%' but '%size%' were detected",
self::TOO_SMALL => "All files in sum should have a minimum size of '%min%' but '%size%' were detected",
self::NOT_READABLE => "One or more files can not be read",
];
/**
* Internal file array
*
* @var array
*/
protected $files;
/**
* Sets validator options
*
* Min limits the used disk space for all files, when used with max=null it is the maximum file size
* It also accepts an array with the keys 'min' and 'max'
*
* @param int|array|Traversable $options Options for this validator
* @throws \Zend\Validator\Exception\InvalidArgumentException
*/
public function __construct($options = null)
{
$this->files = [];
$this->setSize(0);
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
} elseif (is_scalar($options)) {
$options = ['max' => $options];
} elseif (!is_array($options)) {
throw new Exception\InvalidArgumentException('Invalid options to validator provided');
}
if (1 < func_num_args()) {
$argv = func_get_args();
array_shift($argv);
$options['max'] = array_shift($argv);
if (!empty($argv)) {
$options['useByteString'] = array_shift($argv);
}
}
parent::__construct($options);
}
/**
* Returns true if and only if the disk usage of all files is at least min and
* not bigger than max (when max is not null).
*
* @param string|array $value Real file to check for size
* @param array $file File data from \Zend\File\Transfer\Transfer
* @return bool
*/
public function isValid($value, $file = null)
{
if (is_string($value)) {
$value = [$value];
} elseif (is_array($value) && isset($value['tmp_name'])) {
$value = [$value];
}
$min = $this->getMin(true);
$max = $this->getMax(true);
$size = $this->getSize();
foreach ($value as $files) {
if (is_array($files)) {
if (!isset($files['tmp_name']) || !isset($files['name'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
);
}
$file = $files;
$files = $files['tmp_name'];
}
// Is file readable ?
if (empty($files) || false === stream_resolve_include_path($files)) {
$this->throwError($file, self::NOT_READABLE);
continue;
}
if (!isset($this->files[$files])) {
$this->files[$files] = $files;
} else {
// file already counted... do not count twice
continue;
}
// limited to 2GB files
ErrorHandler::start();
$size += filesize($files);
ErrorHandler::stop();
$this->size = $size;
if (($max !== null) && ($max < $size)) {
if ($this->getByteString()) {
$this->options['max'] = $this->toByteString($max);
$this->size = $this->toByteString($size);
$this->throwError($file, self::TOO_BIG);
$this->options['max'] = $max;
$this->size = $size;
} else {
$this->throwError($file, self::TOO_BIG);
}
}
}
// Check that aggregate files are >= minimum size
if (($min !== null) && ($size < $min)) {
if ($this->getByteString()) {
$this->options['min'] = $this->toByteString($min);
$this->size = $this->toByteString($size);
$this->throwError($file, self::TOO_SMALL);
$this->options['min'] = $min;
$this->size = $size;
} else {
$this->throwError($file, self::TOO_SMALL);
}
}
if (count($this->getMessages()) > 0) {
return false;
}
return true;
}
/**
* Throws an error of the given type
*
* @param string $file
* @param string $errorType
* @return false
*/
protected function throwError($file, $errorType)
{
if ($file !== null) {
if (is_array($file)) {
if (array_key_exists('name', $file)) {
$this->value = $file['name'];
}
} elseif (is_string($file)) {
$this->value = $file;
}
}
$this->error($errorType);
return false;
}
}

View File

@@ -0,0 +1,175 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\File;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;
/**
* Validator for the hash of given files
*/
class Hash extends AbstractValidator
{
/**
* @const string Error constants
*/
const DOES_NOT_MATCH = 'fileHashDoesNotMatch';
const NOT_DETECTED = 'fileHashHashNotDetected';
const NOT_FOUND = 'fileHashNotFound';
/**
* @var array Error message templates
*/
protected $messageTemplates = [
self::DOES_NOT_MATCH => "File does not match the given hashes",
self::NOT_DETECTED => "A hash could not be evaluated for the given file",
self::NOT_FOUND => "File is not readable or does not exist"
];
/**
* Options for this validator
*
* @var string
*/
protected $options = [
'algorithm' => 'crc32',
'hash' => null,
];
/**
* Sets validator options
*
* @param string|array $options
*/
public function __construct($options = null)
{
if (is_scalar($options) ||
(is_array($options) && !array_key_exists('hash', $options))) {
$options = ['hash' => $options];
}
if (1 < func_num_args()) {
$options['algorithm'] = func_get_arg(1);
}
parent::__construct($options);
}
/**
* Returns the set hash values as array, the hash as key and the algorithm the value
*
* @return array
*/
public function getHash()
{
return $this->options['hash'];
}
/**
* Sets the hash for one or multiple files
*
* @param string|array $options
* @return Hash Provides a fluent interface
*/
public function setHash($options)
{
$this->options['hash'] = null;
$this->addHash($options);
return $this;
}
/**
* Adds the hash for one or multiple files
*
* @param string|array $options
* @return Hash Provides a fluent interface
* @throws Exception\InvalidArgumentException
*/
public function addHash($options)
{
if (is_string($options)) {
$options = [$options];
} elseif (!is_array($options)) {
throw new Exception\InvalidArgumentException("False parameter given");
}
$known = hash_algos();
if (!isset($options['algorithm'])) {
$algorithm = $this->options['algorithm'];
} else {
$algorithm = $options['algorithm'];
unset($options['algorithm']);
}
if (!in_array($algorithm, $known)) {
throw new Exception\InvalidArgumentException("Unknown algorithm '{$algorithm}'");
}
foreach ($options as $value) {
$this->options['hash'][$value] = $algorithm;
}
return $this;
}
/**
* Returns true if and only if the given file confirms the set hash
*
* @param string|array $value File to check for hash
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
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);
// Is file readable ?
if (empty($file) || false === stream_resolve_include_path($file)) {
$this->error(self::NOT_FOUND);
return false;
}
$algos = array_unique(array_values($this->getHash()));
$hashes = array_unique(array_keys($this->getHash()));
foreach ($algos as $algorithm) {
$filehash = hash_file($algorithm, $file);
if ($filehash === false) {
$this->error(self::NOT_DETECTED);
return false;
}
foreach ($hashes as $hash) {
if ($filehash === $hash) {
return true;
}
}
}
$this->error(self::DOES_NOT_MATCH);
return false;
}
}

View File

@@ -0,0 +1,392 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\File;
use Zend\Stdlib\ErrorHandler;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;
/**
* Validator for the image size of an image file
*/
class ImageSize extends AbstractValidator
{
/**
* @const string Error constants
*/
const WIDTH_TOO_BIG = 'fileImageSizeWidthTooBig';
const WIDTH_TOO_SMALL = 'fileImageSizeWidthTooSmall';
const HEIGHT_TOO_BIG = 'fileImageSizeHeightTooBig';
const HEIGHT_TOO_SMALL = 'fileImageSizeHeightTooSmall';
const NOT_DETECTED = 'fileImageSizeNotDetected';
const NOT_READABLE = 'fileImageSizeNotReadable';
/**
* @var array Error message template
*/
protected $messageTemplates = [
self::WIDTH_TOO_BIG => "Maximum allowed width for image should be '%maxwidth%' but '%width%' detected",
self::WIDTH_TOO_SMALL => "Minimum expected width for image should be '%minwidth%' but '%width%' detected",
self::HEIGHT_TOO_BIG => "Maximum allowed height for image should be '%maxheight%' but '%height%' detected",
self::HEIGHT_TOO_SMALL => "Minimum expected height for image should be '%minheight%' but '%height%' detected",
self::NOT_DETECTED => "The size of image could not be detected",
self::NOT_READABLE => "File is not readable or does not exist",
];
/**
* @var array Error message template variables
*/
protected $messageVariables = [
'minwidth' => ['options' => 'minWidth'],
'maxwidth' => ['options' => 'maxWidth'],
'minheight' => ['options' => 'minHeight'],
'maxheight' => ['options' => 'maxHeight'],
'width' => 'width',
'height' => 'height'
];
/**
* Detected width
*
* @var int
*/
protected $width;
/**
* Detected height
*
* @var int
*/
protected $height;
/**
* Options for this validator
*
* @var array
*/
protected $options = [
'minWidth' => null, // Minimum image width
'maxWidth' => null, // Maximum image width
'minHeight' => null, // Minimum image height
'maxHeight' => null, // Maximum image height
];
/**
* Sets validator options
*
* Accepts the following option keys:
* - minheight
* - minwidth
* - maxheight
* - maxwidth
*
* @param array|\Traversable $options
*/
public function __construct($options = null)
{
if (1 < func_num_args()) {
if (!is_array($options)) {
$options = ['minWidth' => $options];
}
$argv = func_get_args();
array_shift($argv);
$options['minHeight'] = array_shift($argv);
if (!empty($argv)) {
$options['maxWidth'] = array_shift($argv);
if (!empty($argv)) {
$options['maxHeight'] = array_shift($argv);
}
}
}
parent::__construct($options);
}
/**
* Returns the minimum allowed width
*
* @return int
*/
public function getMinWidth()
{
return $this->options['minWidth'];
}
/**
* Sets the minimum allowed width
*
* @param int $minWidth
* @return ImageSize Provides a fluid interface
* @throws Exception\InvalidArgumentException When minwidth is greater than maxwidth
*/
public function setMinWidth($minWidth)
{
if (($this->getMaxWidth() !== null) && ($minWidth > $this->getMaxWidth())) {
throw new Exception\InvalidArgumentException(
"The minimum image width must be less than or equal to the "
. " maximum image width, but {$minWidth} > {$this->getMaxWidth()}"
);
}
$this->options['minWidth'] = (int) $minWidth;
return $this;
}
/**
* Returns the maximum allowed width
*
* @return int
*/
public function getMaxWidth()
{
return $this->options['maxWidth'];
}
/**
* Sets the maximum allowed width
*
* @param int $maxWidth
* @return ImageSize Provides a fluid interface
* @throws Exception\InvalidArgumentException When maxwidth is less than minwidth
*/
public function setMaxWidth($maxWidth)
{
if (($this->getMinWidth() !== null) && ($maxWidth < $this->getMinWidth())) {
throw new Exception\InvalidArgumentException(
"The maximum image width must be greater than or equal to the "
. "minimum image width, but {$maxWidth} < {$this->getMinWidth()}"
);
}
$this->options['maxWidth'] = (int) $maxWidth;
return $this;
}
/**
* Returns the minimum allowed height
*
* @return int
*/
public function getMinHeight()
{
return $this->options['minHeight'];
}
/**
* Sets the minimum allowed height
*
* @param int $minHeight
* @return ImageSize Provides a fluid interface
* @throws Exception\InvalidArgumentException When minheight is greater than maxheight
*/
public function setMinHeight($minHeight)
{
if (($this->getMaxHeight() !== null) && ($minHeight > $this->getMaxHeight())) {
throw new Exception\InvalidArgumentException(
"The minimum image height must be less than or equal to the "
. " maximum image height, but {$minHeight} > {$this->getMaxHeight()}"
);
}
$this->options['minHeight'] = (int) $minHeight;
return $this;
}
/**
* Returns the maximum allowed height
*
* @return int
*/
public function getMaxHeight()
{
return $this->options['maxHeight'];
}
/**
* Sets the maximum allowed height
*
* @param int $maxHeight
* @return ImageSize Provides a fluid interface
* @throws Exception\InvalidArgumentException When maxheight is less than minheight
*/
public function setMaxHeight($maxHeight)
{
if (($this->getMinHeight() !== null) && ($maxHeight < $this->getMinHeight())) {
throw new Exception\InvalidArgumentException(
"The maximum image height must be greater than or equal to the "
. "minimum image height, but {$maxHeight} < {$this->getMinHeight()}"
);
}
$this->options['maxHeight'] = (int) $maxHeight;
return $this;
}
/**
* Returns the set minimum image sizes
*
* @return array
*/
public function getImageMin()
{
return ['minWidth' => $this->getMinWidth(), 'minHeight' => $this->getMinHeight()];
}
/**
* Returns the set maximum image sizes
*
* @return array
*/
public function getImageMax()
{
return ['maxWidth' => $this->getMaxWidth(), 'maxHeight' => $this->getMaxHeight()];
}
/**
* Returns the set image width sizes
*
* @return array
*/
public function getImageWidth()
{
return ['minWidth' => $this->getMinWidth(), 'maxWidth' => $this->getMaxWidth()];
}
/**
* Returns the set image height sizes
*
* @return array
*/
public function getImageHeight()
{
return ['minHeight' => $this->getMinHeight(), 'maxHeight' => $this->getMaxHeight()];
}
/**
* Sets the minimum image size
*
* @param array $options The minimum image dimensions
* @return ImageSize Provides a fluent interface
*/
public function setImageMin($options)
{
$this->setOptions($options);
return $this;
}
/**
* Sets the maximum image size
*
* @param array|\Traversable $options The maximum image dimensions
* @return ImageSize Provides a fluent interface
*/
public function setImageMax($options)
{
$this->setOptions($options);
return $this;
}
/**
* Sets the minimum and maximum image width
*
* @param array $options The image width dimensions
* @return ImageSize Provides a fluent interface
*/
public function setImageWidth($options)
{
$this->setImageMin($options);
$this->setImageMax($options);
return $this;
}
/**
* Sets the minimum and maximum image height
*
* @param array $options The image height dimensions
* @return ImageSize Provides a fluent interface
*/
public function setImageHeight($options)
{
$this->setImageMin($options);
$this->setImageMax($options);
return $this;
}
/**
* Returns true if and only if the image size of $value is at least min and
* not bigger than max
*
* @param string|array $value Real file to check for image size
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
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);
// Is file readable ?
if (empty($file) || false === stream_resolve_include_path($file)) {
$this->error(self::NOT_READABLE);
return false;
}
ErrorHandler::start();
$size = getimagesize($file);
ErrorHandler::stop();
if (empty($size) || ($size[0] === 0) || ($size[1] === 0)) {
$this->error(self::NOT_DETECTED);
return false;
}
$this->width = $size[0];
$this->height = $size[1];
if ($this->width < $this->getMinWidth()) {
$this->error(self::WIDTH_TOO_SMALL);
}
if (($this->getMaxWidth() !== null) && ($this->getMaxWidth() < $this->width)) {
$this->error(self::WIDTH_TOO_BIG);
}
if ($this->height < $this->getMinHeight()) {
$this->error(self::HEIGHT_TOO_SMALL);
}
if (($this->getMaxHeight() !== null) && ($this->getMaxHeight() < $this->height)) {
$this->error(self::HEIGHT_TOO_BIG);
}
if (count($this->getMessages()) > 0) {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,93 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\File;
use Traversable;
use Zend\Stdlib\ArrayUtils;
/**
* Validator which checks if the file already exists in the directory
*/
class IsCompressed extends MimeType
{
/**
* @const string Error constants
*/
const FALSE_TYPE = 'fileIsCompressedFalseType';
const NOT_DETECTED = 'fileIsCompressedNotDetected';
const NOT_READABLE = 'fileIsCompressedNotReadable';
/**
* @var array Error message templates
*/
protected $messageTemplates = [
self::FALSE_TYPE => "File is not compressed, '%type%' detected",
self::NOT_DETECTED => "The mimetype could not be detected from the file",
self::NOT_READABLE => "File is not readable or does not exist",
];
/**
* Sets validator options
*
* @param string|array|Traversable $options
*/
public function __construct($options = [])
{
// http://hul.harvard.edu/ois/systems/wax/wax-public-help/mimetypes.htm
$default = [
'application/arj',
'application/gnutar',
'application/lha',
'application/lzx',
'application/vnd.ms-cab-compressed',
'application/x-ace-compressed',
'application/x-arc',
'application/x-archive',
'application/x-arj',
'application/x-bzip',
'application/x-bzip2',
'application/x-cab-compressed',
'application/x-compress',
'application/x-compressed',
'application/x-cpio',
'application/x-debian-package',
'application/x-eet',
'application/x-gzip',
'application/x-java-pack200',
'application/x-lha',
'application/x-lharc',
'application/x-lzh',
'application/x-lzma',
'application/x-lzx',
'application/x-rar',
'application/x-sit',
'application/x-stuffit',
'application/x-tar',
'application/zip',
'application/x-zip',
'application/zoo',
'multipart/x-gzip',
];
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
}
if ($options === null) {
$options = [];
}
parent::__construct($options);
if (!$this->getMimeType()) {
$this->setMimeType($default);
}
}
}

View File

@@ -0,0 +1,116 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\File;
use Traversable;
use Zend\Stdlib\ArrayUtils;
/**
* Validator which checks if the file is an image
*/
class IsImage extends MimeType
{
/**
* @const string Error constants
*/
const FALSE_TYPE = 'fileIsImageFalseType';
const NOT_DETECTED = 'fileIsImageNotDetected';
const NOT_READABLE = 'fileIsImageNotReadable';
/**
* @var array Error message templates
*/
protected $messageTemplates = [
self::FALSE_TYPE => "File is no image, '%type%' detected",
self::NOT_DETECTED => "The mimetype could not be detected from the file",
self::NOT_READABLE => "File is not readable or does not exist",
];
/**
* Sets validator options
*
* @param array|Traversable|string $options
*/
public function __construct($options = [])
{
// http://www.iana.org/assignments/media-types/media-types.xhtml#image
$default = [
'application/cdf',
'application/dicom',
'application/fractals',
'application/postscript',
'application/vnd.hp-hpgl',
'application/vnd.oasis.opendocument.graphics',
'application/x-cdf',
'application/x-cmu-raster',
'application/x-ima',
'application/x-inventor',
'application/x-koan',
'application/x-portable-anymap',
'application/x-world-x-3dmf',
'image/bmp',
'image/c',
'image/cgm',
'image/fif',
'image/gif',
'image/jpeg',
'image/jpm',
'image/jpx',
'image/jp2',
'image/naplps',
'image/pjpeg',
'image/png',
'image/svg',
'image/svg+xml',
'image/tiff',
'image/vnd.adobe.photoshop',
'image/vnd.djvu',
'image/vnd.fpx',
'image/vnd.net-fpx',
'image/x-cmu-raster',
'image/x-cmx',
'image/x-coreldraw',
'image/x-cpi',
'image/x-emf',
'image/x-ico',
'image/x-icon',
'image/x-jg',
'image/x-ms-bmp',
'image/x-niff',
'image/x-pict',
'image/x-pcx',
'image/x-png',
'image/x-portable-anymap',
'image/x-portable-bitmap',
'image/x-portable-greymap',
'image/x-portable-pixmap',
'image/x-quicktime',
'image/x-rgb',
'image/x-tiff',
'image/x-unknown',
'image/x-windows-bmp',
'image/x-xpmi',
];
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
}
if ($options === null) {
$options = [];
}
parent::__construct($options);
if (!$this->getMimeType()) {
$this->setMimeType($default);
}
}
}

View File

@@ -0,0 +1,128 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\File;
use Zend\Validator\Exception;
/**
* Validator for the md5 hash of given files
*/
class Md5 extends Hash
{
/**
* @const string Error constants
*/
const DOES_NOT_MATCH = 'fileMd5DoesNotMatch';
const NOT_DETECTED = 'fileMd5NotDetected';
const NOT_FOUND = 'fileMd5NotFound';
/**
* @var array Error message templates
*/
protected $messageTemplates = [
self::DOES_NOT_MATCH => "File does not match the given md5 hashes",
self::NOT_DETECTED => "An md5 hash could not be evaluated for the given file",
self::NOT_FOUND => "File is not readable or does not exist",
];
/**
* Options for this validator
*
* @var string
*/
protected $options = [
'algorithm' => 'md5',
'hash' => null,
];
/**
* Returns all set md5 hashes
*
* @return array
*/
public function getMd5()
{
return $this->getHash();
}
/**
* Sets the md5 hash for one or multiple files
*
* @param string|array $options
* @return Hash Provides a fluent interface
*/
public function setMd5($options)
{
$this->setHash($options);
return $this;
}
/**
* Adds the md5 hash for one or multiple files
*
* @param string|array $options
* @return Hash Provides a fluent interface
*/
public function addMd5($options)
{
$this->addHash($options);
return $this;
}
/**
* Returns true if and only if the given file confirms the set hash
*
* @param string|array $value Filename to check for hash
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
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);
// Is file readable ?
if (empty($file) || false === stream_resolve_include_path($file)) {
$this->error(self::NOT_FOUND);
return false;
}
$hashes = array_unique(array_keys($this->getHash()));
$filehash = hash_file('md5', $file);
if ($filehash === false) {
$this->error(self::NOT_DETECTED);
return false;
}
foreach ($hashes as $hash) {
if ($filehash === $hash) {
return true;
}
}
$this->error(self::DOES_NOT_MATCH);
return false;
}
}

View File

@@ -0,0 +1,417 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\File;
use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\ErrorHandler;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;
/**
* Validator for the mime type of a file
*/
class MimeType extends AbstractValidator
{
/**#@+
* @const Error type constants
*/
const FALSE_TYPE = 'fileMimeTypeFalse';
const NOT_DETECTED = 'fileMimeTypeNotDetected';
const NOT_READABLE = 'fileMimeTypeNotReadable';
/**#@-*/
/**
* @var array Error message templates
*/
protected $messageTemplates = [
self::FALSE_TYPE => "File has an incorrect mimetype of '%type%'",
self::NOT_DETECTED => "The mimetype could not be detected from the file",
self::NOT_READABLE => "File is not readable or does not exist",
];
/**
* @var array
*/
protected $messageVariables = [
'type' => 'type'
];
/**
* @var string
*/
protected $type;
/**
* Finfo object to use
*
* @var resource
*/
protected $finfo;
/**
* If no environment variable 'MAGIC' is set, try and autodiscover it based on common locations
* @var array
*/
protected $magicFiles = [
'/usr/share/misc/magic',
'/usr/share/misc/magic.mime',
'/usr/share/misc/magic.mgc',
'/usr/share/mime/magic',
'/usr/share/mime/magic.mime',
'/usr/share/mime/magic.mgc',
'/usr/share/file/magic',
'/usr/share/file/magic.mime',
'/usr/share/file/magic.mgc',
];
/**
* Options for this validator
*
* @var array
*/
protected $options = [
'enableHeaderCheck' => false, // Allow header check
'disableMagicFile' => false, // Disable usage of magicfile
'magicFile' => null, // Magicfile to use
'mimeType' => null, // Mimetype to allow
];
/**
* Sets validator options
*
* Mimetype to accept
* - NULL means default PHP usage by using the environment variable 'magic'
* - FALSE means disabling searching for mimetype, should be used for PHP 5.3
* - A string is the mimetype file to use
*
* @param string|array|Traversable $options
*/
public function __construct($options = null)
{
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
} elseif (is_string($options)) {
$this->setMimeType($options);
$options = [];
} elseif (is_array($options)) {
if (isset($options['magicFile'])) {
$this->setMagicFile($options['magicFile']);
unset($options['magicFile']);
}
if (isset($options['enableHeaderCheck'])) {
$this->enableHeaderCheck($options['enableHeaderCheck']);
unset($options['enableHeaderCheck']);
}
if (array_key_exists('mimeType', $options)) {
$this->setMimeType($options['mimeType']);
unset($options['mimeType']);
}
// Handle cases where mimetypes are interspersed with options, or
// options are simply an array of mime types
foreach (array_keys($options) as $key) {
if (!is_int($key)) {
continue;
}
$this->addMimeType($options[$key]);
unset($options[$key]);
}
}
parent::__construct($options);
}
/**
* Returns the actual set magicfile
*
* @return string
*/
public function getMagicFile()
{
if (null === $this->options['magicFile']) {
$magic = getenv('magic');
if (!empty($magic)) {
$this->setMagicFile($magic);
if ($this->options['magicFile'] === null) {
$this->options['magicFile'] = false;
}
return $this->options['magicFile'];
}
foreach ($this->magicFiles as $file) {
try {
$this->setMagicFile($file);
} catch (Exception\ExceptionInterface $e) {
// suppressing errors which are thrown due to open_basedir restrictions
continue;
}
if ($this->options['magicFile'] !== null) {
return $this->options['magicFile'];
}
}
if ($this->options['magicFile'] === null) {
$this->options['magicFile'] = false;
}
}
return $this->options['magicFile'];
}
/**
* Sets the magicfile to use
* if null, the MAGIC constant from php is used
* if the MAGIC file is erroneous, no file will be set
* if false, the default MAGIC file from PHP will be used
*
* @param string $file
* @return MimeType Provides fluid interface
* @throws Exception\RuntimeException When finfo can not read the magicfile
* @throws Exception\InvalidArgumentException
* @throws Exception\InvalidMagicMimeFileException
*/
public function setMagicFile($file)
{
if ($file === false) {
$this->options['magicFile'] = false;
} elseif (empty($file)) {
$this->options['magicFile'] = null;
} elseif (!(class_exists('finfo', false))) {
$this->options['magicFile'] = null;
throw new Exception\RuntimeException('Magicfile can not be set; there is no finfo extension installed');
} elseif (!is_file($file) || !is_readable($file)) {
throw new Exception\InvalidArgumentException(sprintf(
'The given magicfile ("%s") could not be read',
$file
));
} else {
ErrorHandler::start(E_NOTICE|E_WARNING);
$this->finfo = finfo_open(FILEINFO_MIME_TYPE, $file);
$error = ErrorHandler::stop();
if (empty($this->finfo)) {
$this->finfo = null;
throw new Exception\InvalidMagicMimeFileException(sprintf(
'The given magicfile ("%s") could not be used by ext/finfo',
$file
), 0, $error);
}
$this->options['magicFile'] = $file;
}
return $this;
}
/**
* Disables usage of MagicFile
*
* @param $disable boolean False disables usage of magic file
* @return MimeType Provides fluid interface
*/
public function disableMagicFile($disable)
{
$this->options['disableMagicFile'] = (bool) $disable;
return $this;
}
/**
* Is usage of MagicFile disabled?
*
* @return bool
*/
public function isMagicFileDisabled()
{
return $this->options['disableMagicFile'];
}
/**
* Returns the Header Check option
*
* @return bool
*/
public function getHeaderCheck()
{
return $this->options['enableHeaderCheck'];
}
/**
* Defines if the http header should be used
* Note that this is unsafe and therefor the default value is false
*
* @param bool $headerCheck
* @return MimeType Provides fluid interface
*/
public function enableHeaderCheck($headerCheck = true)
{
$this->options['enableHeaderCheck'] = (bool) $headerCheck;
return $this;
}
/**
* Returns the set mimetypes
*
* @param bool $asArray Returns the values as array, when false a concatenated string is returned
* @return string|array
*/
public function getMimeType($asArray = false)
{
$asArray = (bool) $asArray;
$mimetype = (string) $this->options['mimeType'];
if ($asArray) {
$mimetype = explode(',', $mimetype);
}
return $mimetype;
}
/**
* Sets the mimetypes
*
* @param string|array $mimetype The mimetypes to validate
* @return MimeType Provides a fluent interface
*/
public function setMimeType($mimetype)
{
$this->options['mimeType'] = null;
$this->addMimeType($mimetype);
return $this;
}
/**
* Adds the mimetypes
*
* @param string|array $mimetype The mimetypes to add for validation
* @return MimeType Provides a fluent interface
* @throws Exception\InvalidArgumentException
*/
public function addMimeType($mimetype)
{
$mimetypes = $this->getMimeType(true);
if (is_string($mimetype)) {
$mimetype = explode(',', $mimetype);
} elseif (!is_array($mimetype)) {
throw new Exception\InvalidArgumentException("Invalid options to validator provided");
}
if (isset($mimetype['magicFile'])) {
unset($mimetype['magicFile']);
}
foreach ($mimetype as $content) {
if (empty($content) || !is_string($content)) {
continue;
}
$mimetypes[] = trim($content);
}
$mimetypes = array_unique($mimetypes);
// Sanity check to ensure no empty values
foreach ($mimetypes as $key => $mt) {
if (empty($mt)) {
unset($mimetypes[$key]);
}
}
$this->options['mimeType'] = implode(',', $mimetypes);
return $this;
}
/**
* Defined by Zend\Validator\ValidatorInterface
*
* Returns true if the mimetype of the file matches the given ones. Also parts
* of mimetypes can be checked. If you give for example "image" all image
* mime types will be accepted like "image/gif", "image/jpeg" and so on.
*
* @param string|array $value Real file to check for mimetype
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
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);
// Is file readable ?
if (empty($file) || false === stream_resolve_include_path($file)) {
$this->error(static::NOT_READABLE);
return false;
}
$mimefile = $this->getMagicFile();
if (class_exists('finfo', false)) {
if (!$this->isMagicFileDisabled() && (!empty($mimefile) && empty($this->finfo))) {
ErrorHandler::start(E_NOTICE|E_WARNING);
$this->finfo = finfo_open(FILEINFO_MIME_TYPE, $mimefile);
ErrorHandler::stop();
}
if (empty($this->finfo)) {
ErrorHandler::start(E_NOTICE|E_WARNING);
$this->finfo = finfo_open(FILEINFO_MIME_TYPE);
ErrorHandler::stop();
}
$this->type = null;
if (!empty($this->finfo)) {
$this->type = finfo_file($this->finfo, $file);
}
}
if (empty($this->type) && $this->getHeaderCheck()) {
$this->type = $filetype;
}
if (empty($this->type)) {
$this->error(static::NOT_DETECTED);
return false;
}
$mimetype = $this->getMimeType(true);
if (in_array($this->type, $mimetype)) {
return true;
}
$types = explode('/', $this->type);
$types = array_merge($types, explode('-', $this->type));
$types = array_merge($types, explode(';', $this->type));
foreach ($mimetype as $mime) {
if (in_array($mime, $types)) {
return true;
}
}
$this->error(static::FALSE_TYPE);
return false;
}
}

View File

@@ -0,0 +1,89 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\File;
use Zend\Validator\Exception;
/**
* Validator which checks if the destination file does not exist
*/
class NotExists extends Exists
{
/**
* @const string Error constants
*/
const DOES_EXIST = 'fileNotExistsDoesExist';
/**
* @var array Error message templates
*/
protected $messageTemplates = [
self::DOES_EXIST => "File exists",
];
/**
* Returns true if and only if the file does not exist in the set destinations
*
* @param string|array $value Real file to check for existence
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
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);
}
$check = false;
$directories = $this->getDirectory(true);
if (!isset($directories)) {
$check = true;
if (file_exists($file)) {
$this->error(self::DOES_EXIST);
return false;
}
} else {
foreach ($directories as $directory) {
if (!isset($directory) || '' === $directory) {
continue;
}
$check = true;
if (file_exists($directory . DIRECTORY_SEPARATOR . $filename)) {
$this->error(self::DOES_EXIST);
return false;
}
}
}
if (!$check) {
$this->error(self::DOES_EXIST);
return false;
}
return true;
}
}

View File

@@ -0,0 +1,128 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\File;
use Zend\Validator\Exception;
/**
* Validator for the sha1 hash of given files
*/
class Sha1 extends Hash
{
/**
* @const string Error constants
*/
const DOES_NOT_MATCH = 'fileSha1DoesNotMatch';
const NOT_DETECTED = 'fileSha1NotDetected';
const NOT_FOUND = 'fileSha1NotFound';
/**
* @var array Error message templates
*/
protected $messageTemplates = [
self::DOES_NOT_MATCH => "File does not match the given sha1 hashes",
self::NOT_DETECTED => "A sha1 hash could not be evaluated for the given file",
self::NOT_FOUND => "File is not readable or does not exist",
];
/**
* Options for this validator
*
* @var string
*/
protected $options = [
'algorithm' => 'sha1',
'hash' => null,
];
/**
* Returns all set sha1 hashes
*
* @return array
*/
public function getSha1()
{
return $this->getHash();
}
/**
* Sets the sha1 hash for one or multiple files
*
* @param string|array $options
* @return Hash Provides a fluent interface
*/
public function setSha1($options)
{
$this->setHash($options);
return $this;
}
/**
* Adds the sha1 hash for one or multiple files
*
* @param string|array $options
* @return Hash Provides a fluent interface
*/
public function addSha1($options)
{
$this->addHash($options);
return $this;
}
/**
* Returns true if and only if the given file confirms the set hash
*
* @param string $value|array Filename to check for hash
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
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);
// Is file readable ?
if (empty($file) || false === stream_resolve_include_path($file)) {
$this->error(self::NOT_FOUND);
return false;
}
$hashes = array_unique(array_keys($this->getHash()));
$filehash = hash_file('sha1', $file);
if ($filehash === false) {
$this->error(self::NOT_DETECTED);
return false;
}
foreach ($hashes as $hash) {
if ($filehash === $hash) {
return true;
}
}
$this->error(self::DOES_NOT_MATCH);
return false;
}
}

View File

@@ -0,0 +1,368 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\File;
use Zend\Stdlib\ErrorHandler;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;
/**
* Validator for the maximum size of a file up to a max of 2GB
*/
class Size extends AbstractValidator
{
/**
* @const string Error constants
*/
const TOO_BIG = 'fileSizeTooBig';
const TOO_SMALL = 'fileSizeTooSmall';
const NOT_FOUND = 'fileSizeNotFound';
/**
* @var array Error message templates
*/
protected $messageTemplates = [
self::TOO_BIG => "Maximum allowed size for file is '%max%' but '%size%' detected",
self::TOO_SMALL => "Minimum expected size for file is '%min%' but '%size%' detected",
self::NOT_FOUND => "File is not readable or does not exist",
];
/**
* @var array Error message template variables
*/
protected $messageVariables = [
'min' => ['options' => 'min'],
'max' => ['options' => 'max'],
'size' => 'size',
];
/**
* Detected size
*
* @var int
*/
protected $size;
/**
* Options for this validator
*
* @var array
*/
protected $options = [
'min' => null, // Minimum file size, if null there is no minimum
'max' => null, // Maximum file size, if null there is no maximum
'useByteString' => true, // Use byte string?
];
/**
* Sets validator options
*
* If $options is an integer, it will be used as maximum file size
* As Array is accepts the following keys:
* 'min': Minimum file size
* 'max': Maximum file size
* 'useByteString': Use bytestring or real size for messages
*
* @param int|array|\Traversable $options Options for the adapter
*/
public function __construct($options = null)
{
if (is_string($options) || is_numeric($options)) {
$options = ['max' => $options];
}
if (1 < func_num_args()) {
$argv = func_get_args();
array_shift($argv);
$options['max'] = array_shift($argv);
if (!empty($argv)) {
$options['useByteString'] = array_shift($argv);
}
}
parent::__construct($options);
}
/**
* Should messages return bytes as integer or as string in SI notation
*
* @param bool $byteString Use bytestring ?
* @return int
*/
public function useByteString($byteString = true)
{
$this->options['useByteString'] = (bool) $byteString;
return $this;
}
/**
* Will bytestring be used?
*
* @return bool
*/
public function getByteString()
{
return $this->options['useByteString'];
}
/**
* Returns the minimum file size
*
* @param bool $raw Whether or not to force return of the raw value (defaults off)
* @return int|string
*/
public function getMin($raw = false)
{
$min = $this->options['min'];
if (!$raw && $this->getByteString()) {
$min = $this->toByteString($min);
}
return $min;
}
/**
* Sets the minimum file size
*
* File size can be an integer or a byte string
* This includes 'B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'
* For example: 2000, 2MB, 0.2GB
*
* @param int|string $min The minimum file size
* @return Size Provides a fluent interface
* @throws Exception\InvalidArgumentException When min is greater than max
*/
public function setMin($min)
{
if (! is_string($min) && ! is_numeric($min)) {
throw new Exception\InvalidArgumentException('Invalid options to validator provided');
}
$min = (int) $this->fromByteString($min);
$max = $this->getMax(true);
if (($max !== null) && ($min > $max)) {
throw new Exception\InvalidArgumentException(
"The minimum must be less than or equal to the maximum file size, but $min > $max"
);
}
$this->options['min'] = $min;
return $this;
}
/**
* Returns the maximum file size
*
* @param bool $raw Whether or not to force return of the raw value (defaults off)
* @return int|string
*/
public function getMax($raw = false)
{
$max = $this->options['max'];
if (!$raw && $this->getByteString()) {
$max = $this->toByteString($max);
}
return $max;
}
/**
* Sets the maximum file size
*
* File size can be an integer or a byte string
* This includes 'B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'
* For example: 2000, 2MB, 0.2GB
*
* @param int|string $max The maximum file size
* @return Size Provides a fluent interface
* @throws Exception\InvalidArgumentException When max is smaller than min
*/
public function setMax($max)
{
if (! is_string($max) && ! is_numeric($max)) {
throw new Exception\InvalidArgumentException('Invalid options to validator provided');
}
$max = (int) $this->fromByteString($max);
$min = $this->getMin(true);
if (($min !== null) && ($max < $min)) {
throw new Exception\InvalidArgumentException(
"The maximum must be greater than or equal to the minimum file size, but $max < $min"
);
}
$this->options['max'] = $max;
return $this;
}
/**
* Retrieve current detected file size
*
* @return int
*/
protected function getSize()
{
return $this->size;
}
/**
* Set current size
*
* @param int $size
* @return Size
*/
protected function setSize($size)
{
$this->size = $size;
return $this;
}
/**
* Returns true if and only if the file size of $value is at least min and
* not bigger than max (when max is not null).
*
* @param string|array $value File to check for size
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
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);
// Is file readable ?
if (empty($file) || false === stream_resolve_include_path($file)) {
$this->error(self::NOT_FOUND);
return false;
}
// limited to 4GB files
ErrorHandler::start();
$size = sprintf("%u", filesize($file));
ErrorHandler::stop();
$this->size = $size;
// Check to see if it's smaller than min size
$min = $this->getMin(true);
$max = $this->getMax(true);
if (($min !== null) && ($size < $min)) {
if ($this->getByteString()) {
$this->options['min'] = $this->toByteString($min);
$this->size = $this->toByteString($size);
$this->error(self::TOO_SMALL);
$this->options['min'] = $min;
$this->size = $size;
} else {
$this->error(self::TOO_SMALL);
}
}
// Check to see if it's larger than max size
if (($max !== null) && ($max < $size)) {
if ($this->getByteString()) {
$this->options['max'] = $this->toByteString($max);
$this->size = $this->toByteString($size);
$this->error(self::TOO_BIG);
$this->options['max'] = $max;
$this->size = $size;
} else {
$this->error(self::TOO_BIG);
}
}
if (count($this->getMessages()) > 0) {
return false;
}
return true;
}
/**
* Returns the formatted size
*
* @param int $size
* @return string
*/
protected function toByteString($size)
{
$sizes = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
for ($i=0; $size >= 1024 && $i < 9; $i++) {
$size /= 1024;
}
return round($size, 2) . $sizes[$i];
}
/**
* Returns the unformatted size
*
* @param string $size
* @return int
*/
protected function fromByteString($size)
{
if (is_numeric($size)) {
return (int) $size;
}
$type = trim(substr($size, -2, 1));
$value = substr($size, 0, -1);
if (!is_numeric($value)) {
$value = substr($value, 0, -1);
}
switch (strtoupper($type)) {
case 'Y':
$value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
break;
case 'Z':
$value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
break;
case 'E':
$value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024);
break;
case 'P':
$value *= (1024 * 1024 * 1024 * 1024 * 1024);
break;
case 'T':
$value *= (1024 * 1024 * 1024 * 1024);
break;
case 'G':
$value *= (1024 * 1024 * 1024);
break;
case 'M':
$value *= (1024 * 1024);
break;
case 'K':
$value *= 1024;
break;
default:
break;
}
return $value;
}
}

View File

@@ -0,0 +1,233 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\File;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;
/**
* Validator for the maximum size of a file up to a max of 2GB
*
*/
class Upload extends AbstractValidator
{
/**
* @const string Error constants
*/
const INI_SIZE = 'fileUploadErrorIniSize';
const FORM_SIZE = 'fileUploadErrorFormSize';
const PARTIAL = 'fileUploadErrorPartial';
const NO_FILE = 'fileUploadErrorNoFile';
const NO_TMP_DIR = 'fileUploadErrorNoTmpDir';
const CANT_WRITE = 'fileUploadErrorCantWrite';
const EXTENSION = 'fileUploadErrorExtension';
const ATTACK = 'fileUploadErrorAttack';
const FILE_NOT_FOUND = 'fileUploadErrorFileNotFound';
const UNKNOWN = 'fileUploadErrorUnknown';
/**
* @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::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::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%'"
];
protected $options = [
'files' => [],
];
/**
* Sets validator options
*
* The array $files must be given in syntax of Zend\File\Transfer\Transfer to be checked
* If no files are given the $_FILES array will be used automatically.
* NOTE: This validator will only work with HTTP POST uploads!
*
* @param array|\Traversable $options Array of files in syntax of \Zend\File\Transfer\Transfer
*/
public function __construct($options = [])
{
if (is_array($options) && !array_key_exists('files', $options)) {
$options = ['files' => $options];
}
parent::__construct($options);
}
/**
* Returns the array of set files
*
* @param string $file (Optional) The file to return in detail
* @return array
* @throws Exception\InvalidArgumentException If file is not found
*/
public function getFiles($file = null)
{
if ($file !== null) {
$return = [];
foreach ($this->options['files'] as $name => $content) {
if ($name === $file) {
$return[$file] = $this->options['files'][$name];
}
if ($content['name'] === $file) {
$return[$name] = $this->options['files'][$name];
}
}
if (count($return) === 0) {
throw new Exception\InvalidArgumentException("The file '$file' was not found");
}
return $return;
}
return $this->options['files'];
}
/**
* Sets the files to be checked
*
* @param array $files The files to check in syntax of \Zend\File\Transfer\Transfer
* @return Upload Provides a fluent interface
*/
public function setFiles($files = [])
{
if (count($files) === 0) {
$this->options['files'] = $_FILES;
} else {
$this->options['files'] = $files;
}
if ($this->options['files'] === null) {
$this->options['files'] = [];
}
foreach ($this->options['files'] as $file => $content) {
if (!isset($content['error'])) {
unset($this->options['files'][$file]);
}
}
return $this;
}
/**
* Returns true if and only if the file was uploaded without errors
*
* @param string $value Single file to check for upload errors, when giving null the $_FILES array
* from initialization will be used
* @param mixed $file
* @return bool
*/
public function isValid($value, $file = null)
{
$files = [];
$this->setValue($value);
if (array_key_exists($value, $this->getFiles())) {
$files = array_merge($files, $this->getFiles($value));
} else {
foreach ($this->getFiles() as $file => $content) {
if (isset($content['name']) && ($content['name'] === $value)) {
$files = array_merge($files, $this->getFiles($file));
}
if (isset($content['tmp_name']) && ($content['tmp_name'] === $value)) {
$files = array_merge($files, $this->getFiles($file));
}
}
}
if (empty($files)) {
return $this->throwError($file, self::FILE_NOT_FOUND);
}
foreach ($files as $file => $content) {
$this->value = $file;
switch ($content['error']) {
case 0:
if (!is_uploaded_file($content['tmp_name'])) {
$this->throwError($content, self::ATTACK);
}
break;
case 1:
$this->throwError($content, self::INI_SIZE);
break;
case 2:
$this->throwError($content, self::FORM_SIZE);
break;
case 3:
$this->throwError($content, self::PARTIAL);
break;
case 4:
$this->throwError($content, self::NO_FILE);
break;
case 6:
$this->throwError($content, self::NO_TMP_DIR);
break;
case 7:
$this->throwError($content, self::CANT_WRITE);
break;
case 8:
$this->throwError($content, self::EXTENSION);
break;
default:
$this->throwError($content, self::UNKNOWN);
break;
}
}
if (count($this->getMessages()) > 0) {
return false;
}
return true;
}
/**
* Throws an error of the given type
*
* @param string $file
* @param string $errorType
* @return false
*/
protected function throwError($file, $errorType)
{
if ($file !== null) {
if (is_array($file)) {
if (array_key_exists('name', $file)) {
$this->value = $file['name'];
}
} elseif (is_string($file)) {
$this->value = $file;
}
}
$this->error($errorType);
return false;
}
}

View File

@@ -0,0 +1,123 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\File;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;
/**
* Validator for the maximum size of a file up to a max of 2GB
*/
class UploadFile extends AbstractValidator
{
/**
* @const string Error constants
*/
const INI_SIZE = 'fileUploadFileErrorIniSize';
const FORM_SIZE = 'fileUploadFileErrorFormSize';
const PARTIAL = 'fileUploadFileErrorPartial';
const NO_FILE = 'fileUploadFileErrorNoFile';
const NO_TMP_DIR = 'fileUploadFileErrorNoTmpDir';
const CANT_WRITE = 'fileUploadFileErrorCantWrite';
const EXTENSION = 'fileUploadFileErrorExtension';
const ATTACK = 'fileUploadFileErrorAttack';
const FILE_NOT_FOUND = 'fileUploadFileErrorFileNotFound';
const UNKNOWN = 'fileUploadFileErrorUnknown';
/**
* @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",
];
/**
* Returns true if and only if the file was uploaded without errors
*
* @param string $value File to check for upload errors
* @return bool
* @throws Exception\InvalidArgumentException
*/
public function isValid($value)
{
if (is_array($value)) {
if (!isset($value['tmp_name']) || !isset($value['name']) || !isset($value['error'])) {
throw new Exception\InvalidArgumentException(
'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);
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;
case UPLOAD_ERR_INI_SIZE:
$this->error(self::INI_SIZE);
break;
case UPLOAD_ERR_FORM_SIZE:
$this->error(self::FORM_SIZE);
break;
case UPLOAD_ERR_PARTIAL:
$this->error(self::PARTIAL);
break;
case UPLOAD_ERR_NO_FILE:
$this->error(self::NO_FILE);
break;
case UPLOAD_ERR_NO_TMP_DIR:
$this->error(self::NO_TMP_DIR);
break;
case UPLOAD_ERR_CANT_WRITE:
$this->error(self::CANT_WRITE);
break;
case UPLOAD_ERR_EXTENSION:
$this->error(self::EXTENSION);
break;
default:
$this->error(self::UNKNOWN);
break;
}
if (count($this->getMessages()) > 0) {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,216 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Validator\File;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;
/**
* Validator for counting all words in a file
*/
class WordCount extends AbstractValidator
{
/**
* @const string Error constants
*/
const TOO_MUCH = 'fileWordCountTooMuch';
const TOO_LESS = 'fileWordCountTooLess';
const NOT_FOUND = 'fileWordCountNotFound';
/**
* @var array Error message templates
*/
protected $messageTemplates = [
self::TOO_MUCH => "Too many words, maximum '%max%' are allowed but '%count%' were counted",
self::TOO_LESS => "Too few words, minimum '%min%' are expected but '%count%' were counted",
self::NOT_FOUND => "File is not readable or does not exist",
];
/**
* @var array Error message template variables
*/
protected $messageVariables = [
'min' => ['options' => 'min'],
'max' => ['options' => 'max'],
'count' => 'count'
];
/**
* Word count
*
* @var int
*/
protected $count;
/**
* Options for this validator
*
* @var array
*/
protected $options = [
'min' => null, // Minimum word count, if null there is no minimum word count
'max' => null, // Maximum word count, if null there is no maximum word count
];
/**
* Sets validator options
*
* Min limits the word count, when used with max=null it is the maximum word count
* It also accepts an array with the keys 'min' and 'max'
*
* If $options is an integer, it will be used as maximum word count
* As Array is accepts the following keys:
* 'min': Minimum word count
* 'max': Maximum word count
*
* @param int|array|\Traversable $options Options for the adapter
*/
public function __construct($options = null)
{
if (1 < func_num_args()) {
$args = func_get_args();
$options = [
'min' => array_shift($args),
'max' => array_shift($args),
];
}
if (is_string($options) || is_numeric($options)) {
$options = ['max' => $options];
}
parent::__construct($options);
}
/**
* Returns the minimum word count
*
* @return int
*/
public function getMin()
{
return $this->options['min'];
}
/**
* Sets the minimum word count
*
* @param int|array $min The minimum word count
* @return WordCount Provides a fluent interface
* @throws Exception\InvalidArgumentException When min is greater than max
*/
public function setMin($min)
{
if (is_array($min) and isset($min['min'])) {
$min = $min['min'];
}
if (! is_numeric($min)) {
throw new Exception\InvalidArgumentException('Invalid options to validator provided');
}
$min = (int) $min;
if (($this->getMax() !== null) && ($min > $this->getMax())) {
throw new Exception\InvalidArgumentException(
"The minimum must be less than or equal to the maximum word count, but $min > {$this->getMax()}"
);
}
$this->options['min'] = $min;
return $this;
}
/**
* Returns the maximum word count
*
* @return int
*/
public function getMax()
{
return $this->options['max'];
}
/**
* Sets the maximum file count
*
* @param int|array $max The maximum word count
* @return WordCount Provides a fluent interface
* @throws Exception\InvalidArgumentException When max is smaller than min
*/
public function setMax($max)
{
if (is_array($max) and isset($max['max'])) {
$max = $max['max'];
}
if (! is_numeric($max)) {
throw new Exception\InvalidArgumentException('Invalid options to validator provided');
}
$max = (int) $max;
if (($this->getMin() !== null) && ($max < $this->getMin())) {
throw new Exception\InvalidArgumentException(
"The maximum must be greater than or equal to the minimum word count, but $max < {$this->getMin()}"
);
}
$this->options['max'] = $max;
return $this;
}
/**
* Returns true if and only if the counted words are at least min and
* not bigger than max (when max is not null).
*
* @param string|array $value Filename to check for word count
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
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);
// Is file readable ?
if (empty($file) || false === stream_resolve_include_path($file)) {
$this->error(self::NOT_FOUND);
return false;
}
$content = file_get_contents($file);
$this->count = str_word_count($content);
if (($this->getMax() !== null) && ($this->count > $this->getMax())) {
$this->error(self::TOO_MUCH);
return false;
}
if (($this->getMin() !== null) && ($this->count < $this->getMin())) {
$this->error(self::TOO_LESS);
return false;
}
return true;
}
}