Added Zend 1.10.8 library.

This commit is contained in:
paul.baranowski 2010-11-04 17:40:43 -04:00
parent 6041230326
commit 1a5edd9a4e
2736 changed files with 682384 additions and 0 deletions

View file

@ -0,0 +1,284 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Count.php 21326 2010-03-04 20:32:39Z thomas $
*/
/**
* @see Zend_Validate_Abstract
*/
require_once 'Zend/Validate/Abstract.php';
/**
* Validator for counting all given files
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_File_Count extends Zend_Validate_Abstract
{
/**#@+
* @const string Error constants
*/
const TOO_MANY = 'fileCountTooMany';
const TOO_FEW = 'fileCountTooFew';
/**#@-*/
/**
* @var array Error message templates
*/
protected $_messageTemplates = array(
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 = array(
'min' => '_min',
'max' => '_max',
'count' => '_count'
);
/**
* Minimum file count
*
* If null, there is no minimum file count
*
* @var integer
*/
protected $_min;
/**
* Maximum file count
*
* If null, there is no maximum file count
*
* @var integer|null
*/
protected $_max;
/**
* Actual filecount
*
* @var integer
*/
protected $_count;
/**
* Internal file array
* @var array
*/
protected $_files;
/**
* 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 a integer, it will be used as maximum file count
* As Array is accepts the following keys:
* 'min': Minimum filecount
* 'max': Maximum filecount
*
* @param integer|array|Zend_Config $options Options for the adapter
* @return void
*/
public function __construct($options)
{
if ($options instanceof Zend_Config) {
$options = $options->toArray();
} elseif (is_string($options) || is_numeric($options)) {
$options = array('max' => $options);
} elseif (!is_array($options)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception ('Invalid options to validator provided');
}
if (1 < func_num_args()) {
$options['min'] = func_get_arg(0);
$options['max'] = func_get_arg(1);
}
if (isset($options['min'])) {
$this->setMin($options);
}
if (isset($options['max'])) {
$this->setMax($options);
}
}
/**
* Returns the minimum file count
*
* @return integer
*/
public function getMin()
{
return $this->_min;
}
/**
* Sets the minimum file count
*
* @param integer|array $min The minimum file count
* @return Zend_Validate_File_Count Provides a fluent interface
* @throws Zend_Validate_Exception When min is greater than max
*/
public function setMin($min)
{
if (is_array($min) and isset($min['min'])) {
$min = $min['min'];
}
if (!is_string($min) and !is_numeric($min)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception ('Invalid options to validator provided');
}
$min = (integer) $min;
if (($this->_max !== null) && ($min > $this->_max)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum file count, but $min >"
. " {$this->_max}");
}
$this->_min = $min;
return $this;
}
/**
* Returns the maximum file count
*
* @return integer
*/
public function getMax()
{
return $this->_max;
}
/**
* Sets the maximum file count
*
* @param integer|array $max The maximum file count
* @return Zend_Validate_StringLength Provides a fluent interface
* @throws Zend_Validate_Exception When max is smaller than min
*/
public function setMax($max)
{
if (is_array($max) and isset($max['max'])) {
$max = $max['max'];
}
if (!is_string($max) and !is_numeric($max)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception ('Invalid options to validator provided');
}
$max = (integer) $max;
if (($this->_min !== null) && ($max < $this->_min)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum file count, but "
. "$max < {$this->_min}");
}
$this->_max = $max;
return $this;
}
/**
* Adds a file for validation
*
* @param string|array $file
*/
public function addFile($file)
{
if (is_string($file)) {
$file = array($file);
}
if (is_array($file)) {
foreach ($file as $name) {
if (!isset($this->_files[$name]) && !empty($name)) {
$this->_files[$name] = $name;
}
}
}
return $this;
}
/**
* Defined by Zend_Validate_Interface
*
* 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 an false.
*
* @param string|array $value Filenames to check for count
* @param array $file File data from Zend_File_Transfer
* @return boolean
*/
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->_max !== null) && ($this->_count > $this->_max)) {
return $this->_throw($file, self::TOO_MANY);
}
if (($this->_min !== null) && ($this->_count < $this->_min)) {
return $this->_throw($file, self::TOO_FEW);
}
return true;
}
/**
* Throws an error of the given type
*
* @param string $file
* @param string $errorType
* @return false
*/
protected function _throw($file, $errorType)
{
if ($file !== null) {
$this->_value = $file['name'];
}
$this->_error($errorType);
return false;
}
}

View file

@ -0,0 +1,179 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Crc32.php 22697 2010-07-26 21:14:47Z alexander $
*/
/**
* @see Zend_Validate_File_Hash
*/
require_once 'Zend/Validate/File/Hash.php';
/**
* Validator for the crc32 hash of given files
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_File_Crc32 extends Zend_Validate_File_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 = array(
self::DOES_NOT_MATCH => "File '%value%' 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 '%value%' could not be found",
);
/**
* Hash of the file
*
* @var string
*/
protected $_hash;
/**
* Sets validator options
*
* @param string|array|Zend_Config $options
* @return void
*/
public function __construct($options)
{
if ($options instanceof Zend_Config) {
$options = $options->toArray();
} elseif (is_scalar($options)) {
$options = array('hash1' => $options);
} elseif (!is_array($options)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception('Invalid options to validator provided');
}
$this->setCrc32($options);
}
/**
* 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 Zend_Validate_File_Hash Provides a fluent interface
*/
public function setHash($options)
{
if (!is_array($options)) {
$options = array($options);
}
$options['algorithm'] = 'crc32';
parent::setHash($options);
return $this;
}
/**
* Sets the crc32 hash for one or multiple files
*
* @param string|array $options
* @return Zend_Validate_File_Hash 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 Zend_Validate_File_Hash Provides a fluent interface
*/
public function addHash($options)
{
if (!is_array($options)) {
$options = array($options);
}
$options['algorithm'] = 'crc32';
parent::addHash($options);
return $this;
}
/**
* Adds the crc32 hash for one or multiple files
*
* @param string|array $options
* @return Zend_Validate_File_Hash Provides a fluent interface
*/
public function addCrc32($options)
{
$this->addHash($options);
return $this;
}
/**
* Defined by Zend_Validate_Interface
*
* Returns true if and only if the given file confirms the set hash
*
* @param string $value Filename to check for hash
* @param array $file File data from Zend_File_Transfer
* @return boolean
*/
public function isValid($value, $file = null)
{
// Is file readable ?
require_once 'Zend/Loader.php';
if (!Zend_Loader::isReadable($value)) {
return $this->_throw($file, self::NOT_FOUND);
}
$hashes = array_unique(array_keys($this->_hash));
$filehash = hash_file('crc32', $value);
if ($filehash === false) {
return $this->_throw($file, self::NOT_DETECTED);
}
foreach($hashes as $hash) {
if ($filehash === $hash) {
return true;
}
}
return $this->_throw($file, self::DOES_NOT_MATCH);
}
}

View file

@ -0,0 +1,94 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: ExcludeExtension.php 22697 2010-07-26 21:14:47Z alexander $
*/
/**
* @see Zend_Validate_Abstract
*/
require_once 'Zend/Validate/File/Extension.php';
/**
* Validator for the excluding file extensions
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_File_ExcludeExtension extends Zend_Validate_File_Extension
{
/**
* @const string Error constants
*/
const FALSE_EXTENSION = 'fileExcludeExtensionFalse';
const NOT_FOUND = 'fileExcludeExtensionNotFound';
/**
* @var array Error message templates
*/
protected $_messageTemplates = array(
self::FALSE_EXTENSION => "File '%value%' has a false extension",
self::NOT_FOUND => "File '%value%' could not be found",
);
/**
* Defined by Zend_Validate_Interface
*
* Returns true if and only if the fileextension of $value is not included in the
* set extension list
*
* @param string $value Real file to check for extension
* @param array $file File data from Zend_File_Transfer
* @return boolean
*/
public function isValid($value, $file = null)
{
// Is file readable ?
require_once 'Zend/Loader.php';
if (!Zend_Loader::isReadable($value)) {
return $this->_throw($file, self::NOT_FOUND);
}
if ($file !== null) {
$info['extension'] = substr($file['name'], strrpos($file['name'], '.') + 1);
} else {
$info = pathinfo($value);
}
$extensions = $this->getExtension();
if ($this->_case and (!in_array($info['extension'], $extensions))) {
return true;
} else if (!$this->_case) {
$found = false;
foreach ($extensions as $extension) {
if (strtolower($extension) == strtolower($info['extension'])) {
$found = true;
}
}
if (!$found) {
return true;
}
}
return $this->_throw($file, self::FALSE_EXTENSION);
}
}

View file

@ -0,0 +1,109 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: ExcludeMimeType.php 21936 2010-04-18 16:23:34Z thomas $
*/
/**
* @see Zend_Validate_File_MimeType
*/
require_once 'Zend/Validate/File/MimeType.php';
/**
* Validator for the mime type of a file
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_File_ExcludeMimeType extends Zend_Validate_File_MimeType
{
const FALSE_TYPE = 'fileExcludeMimeTypeFalse';
const NOT_DETECTED = 'fileExcludeMimeTypeNotDetected';
const NOT_READABLE = 'fileExcludeMimeTypeNotReadable';
/**
* Defined by Zend_Validate_Interface
*
* 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 $value Real file to check for mimetype
* @param array $file File data from Zend_File_Transfer
* @return boolean
*/
public function isValid($value, $file = null)
{
if ($file === null) {
$file = array(
'type' => null,
'name' => $value
);
}
// Is file readable ?
require_once 'Zend/Loader.php';
if (!Zend_Loader::isReadable($value)) {
return $this->_throw($file, self::NOT_READABLE);
}
$mimefile = $this->getMagicFile();
if (class_exists('finfo', false)) {
$const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
if (!empty($mimefile)) {
$mime = new finfo($const, $mimefile);
} else {
$mime = new finfo($const);
}
if (!empty($mime)) {
$this->_type = $mime->file($value);
}
unset($mime);
}
if (empty($this->_type)) {
if (function_exists('mime_content_type') && ini_get('mime_magic.magicfile')) {
$this->_type = mime_content_type($value);
} elseif ($this->_headerCheck) {
$this->_type = $file['type'];
}
}
if (empty($this->_type)) {
return $this->_throw($file, self::NOT_DETECTED);
}
$mimetype = $this->getMimeType(true);
if (in_array($this->_type, $mimetype)) {
return $this->_throw($file, self::FALSE_TYPE);
}
$types = explode('/', $this->_type);
$types = array_merge($types, explode('-', $this->_type));
foreach($mimetype as $mime) {
if (in_array($mime, $types)) {
return $this->_throw($file, self::FALSE_TYPE);
}
}
return true;
}
}

View file

@ -0,0 +1,203 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Exists.php 20358 2010-01-17 19:03:49Z thomas $
*/
/**
* @see Zend_Validate_Abstract
*/
require_once 'Zend/Validate/Abstract.php';
/**
* Validator which checks if the file already exists in the directory
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_File_Exists extends Zend_Validate_Abstract
{
/**
* @const string Error constants
*/
const DOES_NOT_EXIST = 'fileExistsDoesNotExist';
/**
* @var array Error message templates
*/
protected $_messageTemplates = array(
self::DOES_NOT_EXIST => "File '%value%' does not exist",
);
/**
* Internal list of directories
* @var string
*/
protected $_directory = '';
/**
* @var array Error message template variables
*/
protected $_messageVariables = array(
'directory' => '_directory'
);
/**
* Sets validator options
*
* @param string|array|Zend_Config $directory
* @return void
*/
public function __construct($directory = array())
{
if ($directory instanceof Zend_Config) {
$directory = $directory->toArray();
} else if (is_string($directory)) {
$directory = explode(',', $directory);
} else if (!is_array($directory)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception ('Invalid options to validator provided');
}
$this->setDirectory($directory);
}
/**
* Returns the set file directories which are checked
*
* @param boolean $asArray Returns the values as array, when false an concated string is returned
* @return string
*/
public function getDirectory($asArray = false)
{
$asArray = (bool) $asArray;
$directory = (string) $this->_directory;
if ($asArray) {
$directory = explode(',', $directory);
}
return $directory;
}
/**
* Sets the file directory which will be checked
*
* @param string|array $directory The directories to validate
* @return Zend_Validate_File_Extension Provides a fluent interface
*/
public function setDirectory($directory)
{
$this->_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 Zend_Validate_File_Extension Provides a fluent interface
*/
public function addDirectory($directory)
{
$directories = $this->getDirectory(true);
if (is_string($directory)) {
$directory = explode(',', $directory);
} else if (!is_array($directory)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception ('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->_directory = implode(',', $directories);
return $this;
}
/**
* Defined by Zend_Validate_Interface
*
* Returns true if and only if the file already exists in the set directories
*
* @param string $value Real file to check for existance
* @param array $file File data from Zend_File_Transfer
* @return boolean
*/
public function isValid($value, $file = null)
{
$directories = $this->getDirectory(true);
if (($file !== null) and (!empty($file['destination']))) {
$directories[] = $file['destination'];
} else if (!isset($file['name'])) {
$file['name'] = $value;
}
$check = false;
foreach ($directories as $directory) {
if (empty($directory)) {
continue;
}
$check = true;
if (!file_exists($directory . DIRECTORY_SEPARATOR . $file['name'])) {
return $this->_throw($file, self::DOES_NOT_EXIST);
}
}
if (!$check) {
return $this->_throw($file, self::DOES_NOT_EXIST);
}
return true;
}
/**
* Throws an error of the given type
*
* @param string $file
* @param string $errorType
* @return false
*/
protected function _throw($file, $errorType)
{
if ($file !== null) {
$this->_value = $file['name'];
}
$this->_error($errorType);
return false;
}
}

View file

@ -0,0 +1,232 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Extension.php 22697 2010-07-26 21:14:47Z alexander $
*/
/**
* @see Zend_Validate_Abstract
*/
require_once 'Zend/Validate/Abstract.php';
/**
* Validator for the file extension of a file
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_File_Extension extends Zend_Validate_Abstract
{
/**
* @const string Error constants
*/
const FALSE_EXTENSION = 'fileExtensionFalse';
const NOT_FOUND = 'fileExtensionNotFound';
/**
* @var array Error message templates
*/
protected $_messageTemplates = array(
self::FALSE_EXTENSION => "File '%value%' has a false extension",
self::NOT_FOUND => "File '%value%' could not be found",
);
/**
* Internal list of extensions
* @var string
*/
protected $_extension = '';
/**
* Validate case sensitive
*
* @var boolean
*/
protected $_case = false;
/**
* @var array Error message template variables
*/
protected $_messageVariables = array(
'extension' => '_extension'
);
/**
* Sets validator options
*
* @param string|array|Zend_Config $options
* @return void
*/
public function __construct($options)
{
if ($options instanceof Zend_Config) {
$options = $options->toArray();
}
if (1 < func_num_args()) {
$case = func_get_arg(1);
$this->setCase($case);
}
if (is_array($options) and isset($options['case'])) {
$this->setCase($options['case']);
unset($options['case']);
}
$this->setExtension($options);
}
/**
* Returns the case option
*
* @return boolean
*/
public function getCase()
{
return $this->_case;
}
/**
* Sets the case to use
*
* @param boolean $case
* @return Zend_Validate_File_Extension Provides a fluent interface
*/
public function setCase($case)
{
$this->_case = (boolean) $case;
return $this;
}
/**
* Returns the set file extension
*
* @return array
*/
public function getExtension()
{
$extension = explode(',', $this->_extension);
return $extension;
}
/**
* Sets the file extensions
*
* @param string|array $extension The extensions to validate
* @return Zend_Validate_File_Extension Provides a fluent interface
*/
public function setExtension($extension)
{
$this->_extension = null;
$this->addExtension($extension);
return $this;
}
/**
* Adds the file extensions
*
* @param string|array $extension The extensions to add for validation
* @return Zend_Validate_File_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->_extension = implode(',', $extensions);
return $this;
}
/**
* Defined by Zend_Validate_Interface
*
* Returns true if and only if the fileextension of $value is included in the
* set extension list
*
* @param string $value Real file to check for extension
* @param array $file File data from Zend_File_Transfer
* @return boolean
*/
public function isValid($value, $file = null)
{
// Is file readable ?
require_once 'Zend/Loader.php';
if (!Zend_Loader::isReadable($value)) {
return $this->_throw($file, self::NOT_FOUND);
}
if ($file !== null) {
$info['extension'] = substr($file['name'], strrpos($file['name'], '.') + 1);
} else {
$info = pathinfo($value);
}
$extensions = $this->getExtension();
if ($this->_case && (in_array($info['extension'], $extensions))) {
return true;
} else if (!$this->getCase()) {
foreach ($extensions as $extension) {
if (strtolower($extension) == strtolower($info['extension'])) {
return true;
}
}
}
return $this->_throw($file, self::FALSE_EXTENSION);
}
/**
* Throws an error of the given type
*
* @param string $file
* @param string $errorType
* @return false
*/
protected function _throw($file, $errorType)
{
if (null !== $file) {
$this->_value = $file['name'];
}
$this->_error($errorType);
return false;
}
}

View file

@ -0,0 +1,164 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: FilesSize.php 20455 2010-01-20 22:54:18Z thomas $
*/
/**
* @see Zend_Validate_File_Size
*/
require_once 'Zend/Validate/File/Size.php';
/**
* Validator for the size of all files which will be validated in sum
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_File_FilesSize extends Zend_Validate_File_Size
{
/**
* @const string Error constants
*/
const TOO_BIG = 'fileFilesSizeTooBig';
const TOO_SMALL = 'fileFilesSizeTooSmall';
const NOT_READABLE = 'fileFilesSizeNotReadable';
/**
* @var array Error message templates
*/
protected $_messageTemplates = array(
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 diskspace for all files, when used with max=null it is the maximum filesize
* It also accepts an array with the keys 'min' and 'max'
*
* @param integer|array|Zend_Config $options Options for this validator
* @return void
*/
public function __construct($options)
{
$this->_files = array();
$this->_setSize(0);
if ($options instanceof Zend_Config) {
$options = $options->toArray();
} elseif (is_scalar($options)) {
$options = array('max' => $options);
} elseif (!is_array($options)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception('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['bytestring'] = array_shift($argv);
}
}
parent::__construct($options);
}
/**
* Defined by Zend_Validate_Interface
*
* 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
* @return boolean
*/
public function isValid($value, $file = null)
{
require_once 'Zend/Loader.php';
if (is_string($value)) {
$value = array($value);
}
$min = $this->getMin(true);
$max = $this->getMax(true);
$size = $this->_getSize();
foreach ($value as $files) {
// Is file readable ?
if (!Zend_Loader::isReadable($files)) {
$this->_throw($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
$size += @filesize($files);
$this->_size = $size;
if (($max !== null) && ($max < $size)) {
if ($this->useByteString()) {
$this->_max = $this->_toByteString($max);
$this->_size = $this->_toByteString($size);
$this->_throw($file, self::TOO_BIG);
$this->_max = $max;
$this->_size = $size;
} else {
$this->_throw($file, self::TOO_BIG);
}
}
}
// Check that aggregate files are >= minimum size
if (($min !== null) && ($size < $min)) {
if ($this->useByteString()) {
$this->_min = $this->_toByteString($min);
$this->_size = $this->_toByteString($size);
$this->_throw($file, self::TOO_SMALL);
$this->_min = $min;
$this->_size = $size;
} else {
$this->_throw($file, self::TOO_SMALL);
}
}
if (count($this->_messages) > 0) {
return false;
}
return true;
}
}

View file

@ -0,0 +1,194 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Hash.php 22697 2010-07-26 21:14:47Z alexander $
*/
/**
* @see Zend_Validate_Abstract
*/
require_once 'Zend/Validate/Abstract.php';
/**
* Validator for the hash of given files
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_File_Hash extends Zend_Validate_Abstract
{
/**
* @const string Error constants
*/
const DOES_NOT_MATCH = 'fileHashDoesNotMatch';
const NOT_DETECTED = 'fileHashHashNotDetected';
const NOT_FOUND = 'fileHashNotFound';
/**
* @var array Error message templates
*/
protected $_messageTemplates = array(
self::DOES_NOT_MATCH => "File '%value%' does not match the given hashes",
self::NOT_DETECTED => "A hash could not be evaluated for the given file",
self::NOT_FOUND => "File '%value%' could not be found"
);
/**
* Hash of the file
*
* @var string
*/
protected $_hash;
/**
* Sets validator options
*
* @param string|array $options
* @return void
*/
public function __construct($options)
{
if ($options instanceof Zend_Config) {
$options = $options->toArray();
} elseif (is_scalar($options)) {
$options = array('hash1' => $options);
} elseif (!is_array($options)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception('Invalid options to validator provided');
}
if (1 < func_num_args()) {
$options['algorithm'] = func_get_arg(1);
}
$this->setHash($options);
}
/**
* Returns the set hash values as array, the hash as key and the algorithm the value
*
* @return array
*/
public function getHash()
{
return $this->_hash;
}
/**
* Sets the hash for one or multiple files
*
* @param string|array $options
* @return Zend_Validate_File_Hash Provides a fluent interface
*/
public function setHash($options)
{
$this->_hash = null;
$this->addHash($options);
return $this;
}
/**
* Adds the hash for one or multiple files
*
* @param string|array $options
* @return Zend_Validate_File_Hash Provides a fluent interface
*/
public function addHash($options)
{
if (is_string($options)) {
$options = array($options);
} else if (!is_array($options)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception("False parameter given");
}
$known = hash_algos();
if (!isset($options['algorithm'])) {
$algorithm = 'crc32';
} else {
$algorithm = $options['algorithm'];
unset($options['algorithm']);
}
if (!in_array($algorithm, $known)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception("Unknown algorithm '{$algorithm}'");
}
foreach ($options as $value) {
$this->_hash[$value] = $algorithm;
}
return $this;
}
/**
* Defined by Zend_Validate_Interface
*
* Returns true if and only if the given file confirms the set hash
*
* @param string $value Filename to check for hash
* @param array $file File data from Zend_File_Transfer
* @return boolean
*/
public function isValid($value, $file = null)
{
// Is file readable ?
require_once 'Zend/Loader.php';
if (!Zend_Loader::isReadable($value)) {
return $this->_throw($file, self::NOT_FOUND);
}
$algos = array_unique(array_values($this->_hash));
$hashes = array_unique(array_keys($this->_hash));
foreach ($algos as $algorithm) {
$filehash = hash_file($algorithm, $value);
if ($filehash === false) {
return $this->_throw($file, self::NOT_DETECTED);
}
foreach($hashes as $hash) {
if ($filehash === $hash) {
return true;
}
}
}
return $this->_throw($file, self::DOES_NOT_MATCH);
}
/**
* Throws an error of the given type
*
* @param string $file
* @param string $errorType
* @return false
*/
protected function _throw($file, $errorType)
{
if ($file !== null) {
$this->_value = $file['name'];
}
$this->_error($errorType);
return false;
}
}

View file

@ -0,0 +1,364 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: ImageSize.php 22697 2010-07-26 21:14:47Z alexander $
*/
/**
* @see Zend_Validate_Abstract
*/
require_once 'Zend/Validate/Abstract.php';
/**
* Validator for the image size of a image file
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_File_ImageSize extends Zend_Validate_Abstract
{
/**
* @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 = array(
self::WIDTH_TOO_BIG => "Maximum allowed width for image '%value%' should be '%maxwidth%' but '%width%' detected",
self::WIDTH_TOO_SMALL => "Minimum expected width for image '%value%' should be '%minwidth%' but '%width%' detected",
self::HEIGHT_TOO_BIG => "Maximum allowed height for image '%value%' should be '%maxheight%' but '%height%' detected",
self::HEIGHT_TOO_SMALL => "Minimum expected height for image '%value%' should be '%minheight%' but '%height%' detected",
self::NOT_DETECTED => "The size of image '%value%' could not be detected",
self::NOT_READABLE => "File '%value%' can not be read",
);
/**
* @var array Error message template variables
*/
protected $_messageVariables = array(
'minwidth' => '_minwidth',
'maxwidth' => '_maxwidth',
'minheight' => '_minheight',
'maxheight' => '_maxheight',
'width' => '_width',
'height' => '_height'
);
/**
* Minimum image width
*
* @var integer
*/
protected $_minwidth;
/**
* Maximum image width
*
* @var integer
*/
protected $_maxwidth;
/**
* Minimum image height
*
* @var integer
*/
protected $_minheight;
/**
* Maximum image height
*
* @var integer
*/
protected $_maxheight;
/**
* Detected width
*
* @var integer
*/
protected $_width;
/**
* Detected height
*
* @var integer
*/
protected $_height;
/**
* Sets validator options
*
* Accepts the following option keys:
* - minheight
* - minwidth
* - maxheight
* - maxwidth
*
* @param Zend_Config|array $options
* @return void
*/
public function __construct($options)
{
if ($options instanceof Zend_Config) {
$options = $options->toArray();
} elseif (1 < func_num_args()) {
if (!is_array($options)) {
$options = array('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);
}
}
} else if (!is_array($options)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception ('Invalid options to validator provided');
}
if (isset($options['minheight']) || isset($options['minwidth'])) {
$this->setImageMin($options);
}
if (isset($options['maxheight']) || isset($options['maxwidth'])) {
$this->setImageMax($options);
}
}
/**
* Returns the set minimum image sizes
*
* @return array
*/
public function getImageMin()
{
return array('minwidth' => $this->_minwidth, 'minheight' => $this->_minheight);
}
/**
* Returns the set maximum image sizes
*
* @return array
*/
public function getImageMax()
{
return array('maxwidth' => $this->_maxwidth, 'maxheight' => $this->_maxheight);
}
/**
* Returns the set image width sizes
*
* @return array
*/
public function getImageWidth()
{
return array('minwidth' => $this->_minwidth, 'maxwidth' => $this->_maxwidth);
}
/**
* Returns the set image height sizes
*
* @return array
*/
public function getImageHeight()
{
return array('minheight' => $this->_minheight, 'maxheight' => $this->_maxheight);
}
/**
* Sets the minimum image size
*
* @param array $options The minimum image dimensions
* @throws Zend_Validate_Exception When minwidth is greater than maxwidth
* @throws Zend_Validate_Exception When minheight is greater than maxheight
* @return Zend_Validate_File_ImageSize Provides a fluent interface
*/
public function setImageMin($options)
{
if (isset($options['minwidth'])) {
if (($this->_maxwidth !== null) and ($options['minwidth'] > $this->_maxwidth)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception("The minimum image width must be less than or equal to the "
. " maximum image width, but {$options['minwidth']} > {$this->_maxwidth}");
}
}
if (isset($options['maxheight'])) {
if (($this->_maxheight !== null) and ($options['minheight'] > $this->_maxheight)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception("The minimum image height must be less than or equal to the "
. " maximum image height, but {$options['minheight']} > {$this->_maxheight}");
}
}
if (isset($options['minwidth'])) {
$this->_minwidth = (int) $options['minwidth'];
}
if (isset($options['minheight'])) {
$this->_minheight = (int) $options['minheight'];
}
return $this;
}
/**
* Sets the maximum image size
*
* @param array $options The maximum image dimensions
* @throws Zend_Validate_Exception When maxwidth is smaller than minwidth
* @throws Zend_Validate_Exception When maxheight is smaller than minheight
* @return Zend_Validate_StringLength Provides a fluent interface
*/
public function setImageMax($options)
{
if (isset($options['maxwidth'])) {
if (($this->_minwidth !== null) and ($options['maxwidth'] < $this->_minwidth)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception("The maximum image width must be greater than or equal to the "
. "minimum image width, but {$options['maxwidth']} < {$this->_minwidth}");
}
}
if (isset($options['maxheight'])) {
if (($this->_minheight !== null) and ($options['maxheight'] < $this->_minheight)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception("The maximum image height must be greater than or equal to the "
. "minimum image height, but {$options['maxheight']} < {$this->_minwidth}");
}
}
if (isset($options['maxwidth'])) {
$this->_maxwidth = (int) $options['maxwidth'];
}
if (isset($options['maxheight'])) {
$this->_maxheight = (int) $options['maxheight'];
}
return $this;
}
/**
* Sets the mimimum and maximum image width
*
* @param array $options The image width dimensions
* @return Zend_Validate_File_ImageSize Provides a fluent interface
*/
public function setImageWidth($options)
{
$this->setImageMin($options);
$this->setImageMax($options);
return $this;
}
/**
* Sets the mimimum and maximum image height
*
* @param array $options The image height dimensions
* @return Zend_Validate_File_ImageSize Provides a fluent interface
*/
public function setImageHeight($options)
{
$this->setImageMin($options);
$this->setImageMax($options);
return $this;
}
/**
* Defined by Zend_Validate_Interface
*
* Returns true if and only if the imagesize of $value is at least min and
* not bigger than max
*
* @param string $value Real file to check for image size
* @param array $file File data from Zend_File_Transfer
* @return boolean
*/
public function isValid($value, $file = null)
{
// Is file readable ?
require_once 'Zend/Loader.php';
if (!Zend_Loader::isReadable($value)) {
return $this->_throw($file, self::NOT_READABLE);
}
$size = @getimagesize($value);
$this->_setValue($file);
if (empty($size) or ($size[0] === 0) or ($size[1] === 0)) {
return $this->_throw($file, self::NOT_DETECTED);
}
$this->_width = $size[0];
$this->_height = $size[1];
if ($this->_width < $this->_minwidth) {
$this->_throw($file, self::WIDTH_TOO_SMALL);
}
if (($this->_maxwidth !== null) and ($this->_maxwidth < $this->_width)) {
$this->_throw($file, self::WIDTH_TOO_BIG);
}
if ($this->_height < $this->_minheight) {
$this->_throw($file, self::HEIGHT_TOO_SMALL);
}
if (($this->_maxheight !== null) and ($this->_maxheight < $this->_height)) {
$this->_throw($file, self::HEIGHT_TOO_BIG);
}
if (count($this->_messages) > 0) {
return false;
}
return true;
}
/**
* Throws an error of the given type
*
* @param string $file
* @param string $errorType
* @return false
*/
protected function _throw($file, $errorType)
{
if ($file !== null) {
$this->_value = $file['name'];
}
$this->_error($errorType);
return false;
}
}

View file

@ -0,0 +1,149 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: IsCompressed.php 22697 2010-07-26 21:14:47Z alexander $
*/
/**
* @see Zend_Validate_File_MimeType
*/
require_once 'Zend/Validate/File/MimeType.php';
/**
* Validator which checks if the file already exists in the directory
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_File_IsCompressed extends Zend_Validate_File_MimeType
{
/**
* @const string Error constants
*/
const FALSE_TYPE = 'fileIsCompressedFalseType';
const NOT_DETECTED = 'fileIsCompressedNotDetected';
const NOT_READABLE = 'fileIsCompressedNotReadable';
/**
* @var array Error message templates
*/
protected $_messageTemplates = array(
self::FALSE_TYPE => "File '%value%' is not compressed, '%type%' detected",
self::NOT_DETECTED => "The mimetype of file '%value%' could not been detected",
self::NOT_READABLE => "File '%value%' can not be read",
);
/**
* Sets validator options
*
* @param string|array|Zend_Config $compression
* @return void
*/
public function __construct($mimetype = array())
{
if ($mimetype instanceof Zend_Config) {
$mimetype = $mimetype->toArray();
}
$temp = array();
// http://de.wikipedia.org/wiki/Liste_von_Dateiendungen
$default = array(
'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/zoo',
'multipart/x-gzip',
);
if (is_array($mimetype)) {
$temp = $mimetype;
if (array_key_exists('magicfile', $temp)) {
unset($temp['magicfile']);
}
if (array_key_exists('headerCheck', $temp)) {
unset($temp['headerCheck']);
}
if (empty($temp)) {
$mimetype += $default;
}
}
if (empty($mimetype)) {
$mimetype = $default;
}
parent::__construct($mimetype);
}
/**
* Throws an error of the given type
* Duplicates parent method due to OOP Problem with late static binding in PHP 5.2
*
* @param string $file
* @param string $errorType
* @return false
*/
protected function _throw($file, $errorType)
{
$this->_value = $file['name'];
switch($errorType) {
case Zend_Validate_File_MimeType::FALSE_TYPE :
$errorType = self::FALSE_TYPE;
break;
case Zend_Validate_File_MimeType::NOT_DETECTED :
$errorType = self::NOT_DETECTED;
break;
case Zend_Validate_File_MimeType::NOT_READABLE :
$errorType = self::NOT_READABLE;
break;
}
$this->_error($errorType);
return false;
}
}

View file

@ -0,0 +1,173 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: IsImage.php 22697 2010-07-26 21:14:47Z alexander $
*/
/**
* @see Zend_Validate_File_MimeType
*/
require_once 'Zend/Validate/File/MimeType.php';
/**
* Validator which checks if the file already exists in the directory
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_File_IsImage extends Zend_Validate_File_MimeType
{
/**
* @const string Error constants
*/
const FALSE_TYPE = 'fileIsImageFalseType';
const NOT_DETECTED = 'fileIsImageNotDetected';
const NOT_READABLE = 'fileIsImageNotReadable';
/**
* @var array Error message templates
*/
protected $_messageTemplates = array(
self::FALSE_TYPE => "File '%value%' is no image, '%type%' detected",
self::NOT_DETECTED => "The mimetype of file '%value%' could not be detected",
self::NOT_READABLE => "File '%value%' can not be read",
);
/**
* Sets validator options
*
* @param string|array|Zend_Config $mimetype
* @return void
*/
public function __construct($mimetype = array())
{
if ($mimetype instanceof Zend_Config) {
$mimetype = $mimetype->toArray();
}
$temp = array();
// http://de.wikipedia.org/wiki/Liste_von_Dateiendungen
// http://www.iana.org/assignments/media-types/image/
$default = array(
'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-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 (is_array($mimetype)) {
$temp = $mimetype;
if (array_key_exists('magicfile', $temp)) {
unset($temp['magicfile']);
}
if (array_key_exists('headerCheck', $temp)) {
unset($temp['headerCheck']);
}
if (empty($temp)) {
$mimetype += $default;
}
}
if (empty($mimetype)) {
$mimetype = $default;
}
parent::__construct($mimetype);
}
/**
* Throws an error of the given type
* Duplicates parent method due to OOP Problem with late static binding in PHP 5.2
*
* @param string $file
* @param string $errorType
* @return false
*/
protected function _throw($file, $errorType)
{
$this->_value = $file['name'];
switch($errorType) {
case Zend_Validate_File_MimeType::FALSE_TYPE :
$errorType = self::FALSE_TYPE;
break;
case Zend_Validate_File_MimeType::NOT_DETECTED :
$errorType = self::NOT_DETECTED;
break;
case Zend_Validate_File_MimeType::NOT_READABLE :
$errorType = self::NOT_READABLE;
break;
}
$this->_error($errorType);
return false;
}
}

183
3rd_party/php/Zend/Validate/File/Md5.php vendored Normal file
View file

@ -0,0 +1,183 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Md5.php 22697 2010-07-26 21:14:47Z alexander $
*/
/**
* @see Zend_Validate_File_Hash
*/
require_once 'Zend/Validate/File/Hash.php';
/**
* Validator for the md5 hash of given files
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_File_Md5 extends Zend_Validate_File_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 = array(
self::DOES_NOT_MATCH => "File '%value%' does not match the given md5 hashes",
self::NOT_DETECTED => "A md5 hash could not be evaluated for the given file",
self::NOT_FOUND => "File '%value%' could not be found",
);
/**
* Hash of the file
*
* @var string
*/
protected $_hash;
/**
* Sets validator options
*
* $hash is the hash we accept for the file $file
*
* @param string|array $options
* @return void
*/
public function __construct($options)
{
if ($options instanceof Zend_Config) {
$options = $options->toArray();
} elseif (is_scalar($options)) {
$options = array('hash1' => $options);
} elseif (!is_array($options)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception('Invalid options to validator provided');
}
$this->setMd5($options);
}
/**
* 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
* @param string $algorithm (Deprecated) Algorithm to use, fixed to md5
* @return Zend_Validate_File_Hash Provides a fluent interface
*/
public function setHash($options)
{
if (!is_array($options)) {
$options = (array) $options;
}
$options['algorithm'] = 'md5';
parent::setHash($options);
return $this;
}
/**
* Sets the md5 hash for one or multiple files
*
* @param string|array $options
* @return Zend_Validate_File_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
* @param string $algorithm (Deprecated) Algorithm to use, fixed to md5
* @return Zend_Validate_File_Hash Provides a fluent interface
*/
public function addHash($options)
{
if (!is_array($options)) {
$options = (array) $options;
}
$options['algorithm'] = 'md5';
parent::addHash($options);
return $this;
}
/**
* Adds the md5 hash for one or multiple files
*
* @param string|array $options
* @return Zend_Validate_File_Hash Provides a fluent interface
*/
public function addMd5($options)
{
$this->addHash($options);
return $this;
}
/**
* Defined by Zend_Validate_Interface
*
* Returns true if and only if the given file confirms the set hash
*
* @param string $value Filename to check for hash
* @param array $file File data from Zend_File_Transfer
* @return boolean
*/
public function isValid($value, $file = null)
{
// Is file readable ?
require_once 'Zend/Loader.php';
if (!Zend_Loader::isReadable($value)) {
return $this->_throw($file, self::NOT_FOUND);
}
$hashes = array_unique(array_keys($this->_hash));
$filehash = hash_file('md5', $value);
if ($filehash === false) {
return $this->_throw($file, self::NOT_DETECTED);
}
foreach($hashes as $hash) {
if ($filehash === $hash) {
return true;
}
}
return $this->_throw($file, self::DOES_NOT_MATCH);
}
}

View file

@ -0,0 +1,392 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: MimeType.php 22697 2010-07-26 21:14:47Z alexander $
*/
/**
* @see Zend_Validate_Abstract
*/
require_once 'Zend/Validate/Abstract.php';
/**
* Validator for the mime type of a file
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_File_MimeType extends Zend_Validate_Abstract
{
/**#@+
* @const Error type constants
*/
const FALSE_TYPE = 'fileMimeTypeFalse';
const NOT_DETECTED = 'fileMimeTypeNotDetected';
const NOT_READABLE = 'fileMimeTypeNotReadable';
/**#@-*/
/**
* @var array Error message templates
*/
protected $_messageTemplates = array(
self::FALSE_TYPE => "File '%value%' has a false mimetype of '%type%'",
self::NOT_DETECTED => "The mimetype of file '%value%' could not be detected",
self::NOT_READABLE => "File '%value%' can not be read",
);
/**
* @var array
*/
protected $_messageVariables = array(
'type' => '_type'
);
/**
* @var string
*/
protected $_type;
/**
* Mimetypes
*
* If null, there is no mimetype
*
* @var string|null
*/
protected $_mimetype;
/**
* Magicfile to use
*
* @var string|null
*/
protected $_magicfile;
/**
* Finfo object to use
*
* @var resource
*/
protected $_finfo;
/**
* If no $_ENV['MAGIC'] is set, try and autodiscover it based on common locations
* @var array
*/
protected $_magicFiles = array(
'/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',
);
/**
* Option to allow header check
*
* @var boolean
*/
protected $_headerCheck = false;
/**
* Sets validator options
*
* Mimetype to accept
*
* @param string|array $mimetype MimeType
* @return void
*/
public function __construct($mimetype)
{
if ($mimetype instanceof Zend_Config) {
$mimetype = $mimetype->toArray();
} elseif (is_string($mimetype)) {
$mimetype = explode(',', $mimetype);
} elseif (!is_array($mimetype)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception("Invalid options to validator provided");
}
if (isset($mimetype['magicfile'])) {
$this->setMagicFile($mimetype['magicfile']);
unset($mimetype['magicfile']);
}
if (isset($mimetype['headerCheck'])) {
$this->enableHeaderCheck($mimetype['headerCheck']);
unset($mimetype['headerCheck']);
}
$this->setMimeType($mimetype);
}
/**
* Returns the actual set magicfile
*
* @return string
*/
public function getMagicFile()
{
if (null === $this->_magicfile) {
if (!empty($_ENV['MAGIC'])) {
$this->setMagicFile($_ENV['MAGIC']);
} elseif (!(@ini_get("safe_mode") == 'On' || @ini_get("safe_mode") === 1)) {
require_once 'Zend/Validate/Exception.php';
foreach ($this->_magicFiles as $file) {
// supressing errors which are thrown due to openbase_dir restrictions
try {
$this->setMagicFile($file);
if ($this->_magicfile !== null) {
break;
}
} catch (Zend_Validate_Exception $e) {
// Intentionally, catch and fall through
}
}
}
if ($this->_magicfile === null) {
$this->_magicfile = false;
}
}
return $this->_magicfile;
}
/**
* Sets the magicfile to use
* if null, the MAGIC constant from php is used
* if the MAGIC file is errorous, no file will be set
*
* @param string $file
* @throws Zend_Validate_Exception When finfo can not read the magicfile
* @return Zend_Validate_File_MimeType Provides fluid interface
*/
public function setMagicFile($file)
{
if (empty($file)) {
$this->_magicfile = null;
} else if (!(class_exists('finfo', false))) {
$this->_magicfile = null;
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception('Magicfile can not be set. There is no finfo extension installed');
} else if (!is_readable($file)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception('The given magicfile can not be read');
} else {
$const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
$this->_finfo = @finfo_open($const, $file);
if (empty($this->_finfo)) {
$this->_finfo = null;
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception('The given magicfile is not accepted by finfo');
} else {
$this->_magicfile = $file;
}
}
return $this;
}
/**
* Returns the Header Check option
*
* @return boolean
*/
public function getHeaderCheck()
{
return $this->_headerCheck;
}
/**
* Defines if the http header should be used
* Note that this is unsave and therefor the default value is false
*
* @param boolean $checkHeader
* @return Zend_Validate_File_MimeType Provides fluid interface
*/
public function enableHeaderCheck($headerCheck = true)
{
$this->_headerCheck = (boolean) $headerCheck;
return $this;
}
/**
* Returns the set mimetypes
*
* @param boolean $asArray Returns the values as array, when false an concated string is returned
* @return string|array
*/
public function getMimeType($asArray = false)
{
$asArray = (bool) $asArray;
$mimetype = (string) $this->_mimetype;
if ($asArray) {
$mimetype = explode(',', $mimetype);
}
return $mimetype;
}
/**
* Sets the mimetypes
*
* @param string|array $mimetype The mimetypes to validate
* @return Zend_Validate_File_Extension Provides a fluent interface
*/
public function setMimeType($mimetype)
{
$this->_mimetype = null;
$this->addMimeType($mimetype);
return $this;
}
/**
* Adds the mimetypes
*
* @param string|array $mimetype The mimetypes to add for validation
* @return Zend_Validate_File_Extension Provides a fluent interface
*/
public function addMimeType($mimetype)
{
$mimetypes = $this->getMimeType(true);
if (is_string($mimetype)) {
$mimetype = explode(',', $mimetype);
} elseif (!is_array($mimetype)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception("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->_mimetype = implode(',', $mimetypes);
return $this;
}
/**
* Defined by Zend_Validate_Interface
*
* 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 $value Real file to check for mimetype
* @param array $file File data from Zend_File_Transfer
* @return boolean
*/
public function isValid($value, $file = null)
{
if ($file === null) {
$file = array(
'type' => null,
'name' => $value
);
}
// Is file readable ?
require_once 'Zend/Loader.php';
if (!Zend_Loader::isReadable($value)) {
return $this->_throw($file, self::NOT_READABLE);
}
$mimefile = $this->getMagicFile();
if (class_exists('finfo', false)) {
$const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
if (!empty($mimefile) && empty($this->_finfo)) {
$this->_finfo = @finfo_open($const, $mimefile);
}
if (empty($this->_finfo)) {
$this->_finfo = @finfo_open($const);
}
$this->_type = null;
if (!empty($this->_finfo)) {
$this->_type = finfo_file($this->_finfo, $value);
}
}
if (empty($this->_type) &&
(function_exists('mime_content_type') && ini_get('mime_magic.magicfile'))) {
$this->_type = mime_content_type($value);
}
if (empty($this->_type) && $this->_headerCheck) {
$this->_type = $file['type'];
}
if (empty($this->_type)) {
return $this->_throw($file, self::NOT_DETECTED);
}
$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;
}
}
return $this->_throw($file, self::FALSE_TYPE);
}
/**
* Throws an error of the given type
*
* @param string $file
* @param string $errorType
* @return false
*/
protected function _throw($file, $errorType)
{
$this->_value = $file['name'];
$this->_error($errorType);
return false;
}
}

View file

@ -0,0 +1,84 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: NotExists.php 20358 2010-01-17 19:03:49Z thomas $
*/
/**
* @see Zend_Validate_File_Exists
*/
require_once 'Zend/Validate/File/Exists.php';
/**
* Validator which checks if the destination file does not exist
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_File_NotExists extends Zend_Validate_File_Exists
{
/**
* @const string Error constants
*/
const DOES_EXIST = 'fileNotExistsDoesExist';
/**
* @var array Error message templates
*/
protected $_messageTemplates = array(
self::DOES_EXIST => "File '%value%' exists",
);
/**
* Defined by Zend_Validate_Interface
*
* Returns true if and only if the file does not exist in the set destinations
*
* @param string $value Real file to check for
* @param array $file File data from Zend_File_Transfer
* @return boolean
*/
public function isValid($value, $file = null)
{
$directories = $this->getDirectory(true);
if (($file !== null) and (!empty($file['destination']))) {
$directories[] = $file['destination'];
} else if (!isset($file['name'])) {
$file['name'] = $value;
}
foreach ($directories as $directory) {
if (empty($directory)) {
continue;
}
$check = true;
if (file_exists($directory . DIRECTORY_SEPARATOR . $file['name'])) {
return $this->_throw($file, self::DOES_EXIST);
}
}
if (!isset($check)) {
return $this->_throw($file, self::DOES_EXIST);
}
return true;
}
}

View file

@ -0,0 +1,181 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Sha1.php 22697 2010-07-26 21:14:47Z alexander $
*/
/**
* @see Zend_Validate_File_Hash
*/
require_once 'Zend/Validate/File/Hash.php';
/**
* Validator for the sha1 hash of given files
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_File_Sha1 extends Zend_Validate_File_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 = array(
self::DOES_NOT_MATCH => "File '%value%' 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 '%value%' could not be found",
);
/**
* Hash of the file
*
* @var string
*/
protected $_hash;
/**
* Sets validator options
*
* $hash is the hash we accept for the file $file
*
* @param string|array $options
* @return void
*/
public function __construct($options)
{
if ($options instanceof Zend_Config) {
$options = $options->toArray();
} elseif (is_scalar($options)) {
$options = array('hash1' => $options);
} elseif (!is_array($options)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception('Invalid options to validator provided');
}
$this->setHash($options);
}
/**
* 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 Zend_Validate_File_Hash Provides a fluent interface
*/
public function setHash($options)
{
if (!is_array($options)) {
$options = (array) $options;
}
$options['algorithm'] = 'sha1';
parent::setHash($options);
return $this;
}
/**
* Sets the sha1 hash for one or multiple files
*
* @param string|array $options
* @return Zend_Validate_File_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 Zend_Validate_File_Hash Provides a fluent interface
*/
public function addHash($options)
{
if (!is_array($options)) {
$options = (array) $options;
}
$options['algorithm'] = 'sha1';
parent::addHash($options);
return $this;
}
/**
* Adds the sha1 hash for one or multiple files
*
* @param string|array $options
* @return Zend_Validate_File_Hash Provides a fluent interface
*/
public function addSha1($options)
{
$this->addHash($options);
return $this;
}
/**
* Defined by Zend_Validate_Interface
*
* Returns true if and only if the given file confirms the set hash
*
* @param string $value Filename to check for hash
* @param array $file File data from Zend_File_Transfer
* @return boolean
*/
public function isValid($value, $file = null)
{
// Is file readable ?
require_once 'Zend/Loader.php';
if (!Zend_Loader::isReadable($value)) {
return $this->_throw($file, self::NOT_FOUND);
}
$hashes = array_unique(array_keys($this->_hash));
$filehash = hash_file('sha1', $value);
if ($filehash === false) {
return $this->_throw($file, self::NOT_DETECTED);
}
foreach ($hashes as $hash) {
if ($filehash === $hash) {
return true;
}
}
return $this->_throw($file, self::DOES_NOT_MATCH);
}
}

View file

@ -0,0 +1,404 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Size.php 22697 2010-07-26 21:14:47Z alexander $
*/
/**
* @see Zend_Validate_Abstract
*/
require_once 'Zend/Validate/Abstract.php';
/**
* Validator for the maximum size of a file up to a max of 2GB
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_File_Size extends Zend_Validate_Abstract
{
/**#@+
* @const string Error constants
*/
const TOO_BIG = 'fileSizeTooBig';
const TOO_SMALL = 'fileSizeTooSmall';
const NOT_FOUND = 'fileSizeNotFound';
/**#@-*/
/**
* @var array Error message templates
*/
protected $_messageTemplates = array(
self::TOO_BIG => "Maximum allowed size for file '%value%' is '%max%' but '%size%' detected",
self::TOO_SMALL => "Minimum expected size for file '%value%' is '%min%' but '%size%' detected",
self::NOT_FOUND => "File '%value%' could not be found",
);
/**
* @var array Error message template variables
*/
protected $_messageVariables = array(
'min' => '_min',
'max' => '_max',
'size' => '_size',
);
/**
* Minimum filesize
* @var integer
*/
protected $_min;
/**
* Maximum filesize
*
* If null, there is no maximum filesize
*
* @var integer|null
*/
protected $_max;
/**
* Detected size
*
* @var integer
*/
protected $_size;
/**
* Use bytestring ?
*
* @var boolean
*/
protected $_useByteString = true;
/**
* Sets validator options
*
* If $options is a integer, it will be used as maximum filesize
* As Array is accepts the following keys:
* 'min': Minimum filesize
* 'max': Maximum filesize
* 'bytestring': Use bytestring or real size for messages
*
* @param integer|array $options Options for the adapter
*/
public function __construct($options)
{
if ($options instanceof Zend_Config) {
$options = $options->toArray();
} elseif (is_string($options) || is_numeric($options)) {
$options = array('max' => $options);
} elseif (!is_array($options)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception ('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['bytestring'] = array_shift($argv);
}
}
if (isset($options['bytestring'])) {
$this->setUseByteString($options['bytestring']);
}
if (isset($options['min'])) {
$this->setMin($options['min']);
}
if (isset($options['max'])) {
$this->setMax($options['max']);
}
}
/**
* Returns the minimum filesize
*
* @param boolean $byteString Use bytestring ?
* @return integer
*/
public function setUseByteString($byteString = true)
{
$this->_useByteString = (bool) $byteString;
return $this;
}
/**
* Will bytestring be used?
*
* @return boolean
*/
public function useByteString()
{
return $this->_useByteString;
}
/**
* Returns the minimum filesize
*
* @param bool $raw Whether or not to force return of the raw value (defaults off)
* @return integer|string
*/
public function getMin($raw = false)
{
$min = $this->_min;
if (!$raw && $this->useByteString()) {
$min = $this->_toByteString($min);
}
return $min;
}
/**
* Sets the minimum filesize
*
* @param integer $min The minimum filesize
* @throws Zend_Validate_Exception When min is greater than max
* @return Zend_Validate_File_Size Provides a fluent interface
*/
public function setMin($min)
{
if (!is_string($min) and !is_numeric($min)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception ('Invalid options to validator provided');
}
$min = (integer) $this->_fromByteString($min);
$max = $this->getMax(true);
if (($max !== null) && ($min > $max)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum filesize, but $min >"
. " $max");
}
$this->_min = $min;
return $this;
}
/**
* Returns the maximum filesize
*
* @param bool $raw Whether or not to force return of the raw value (defaults off)
* @return integer|string
*/
public function getMax($raw = false)
{
$max = $this->_max;
if (!$raw && $this->useByteString()) {
$max = $this->_toByteString($max);
}
return $max;
}
/**
* Sets the maximum filesize
*
* @param integer $max The maximum filesize
* @throws Zend_Validate_Exception When max is smaller than min
* @return Zend_Validate_StringLength Provides a fluent interface
*/
public function setMax($max)
{
if (!is_string($max) && !is_numeric($max)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception ('Invalid options to validator provided');
}
$max = (integer) $this->_fromByteString($max);
$min = $this->getMin(true);
if (($min !== null) && ($max < $min)) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum filesize, but "
. "$max < $min");
}
$this->_max = $max;
return $this;
}
/**
* Retrieve current detected file size
*
* @return int
*/
protected function _getSize()
{
return $this->_size;
}
/**
* Set current size
*
* @param int $size
* @return Zend_Validate_File_Size
*/
protected function _setSize($size)
{
$this->_size = $size;
return $this;
}
/**
* Defined by Zend_Validate_Interface
*
* Returns true if and only if the filesize of $value is at least min and
* not bigger than max (when max is not null).
*
* @param string $value Real file to check for size
* @param array $file File data from Zend_File_Transfer
* @return boolean
*/
public function isValid($value, $file = null)
{
// Is file readable ?
require_once 'Zend/Loader.php';
if (!Zend_Loader::isReadable($value)) {
return $this->_throw($file, self::NOT_FOUND);
}
// limited to 4GB files
$size = sprintf("%u", @filesize($value));
$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->useByteString()) {
$this->_min = $this->_toByteString($min);
$this->_size = $this->_toByteString($size);
$this->_throw($file, self::TOO_SMALL);
$this->_min = $min;
$this->_size = $size;
} else {
$this->_throw($file, self::TOO_SMALL);
}
}
// Check to see if it's larger than max size
if (($max !== null) && ($max < $size)) {
if ($this->useByteString()) {
$this->_max = $this->_toByteString($max);
$this->_size = $this->_toByteString($size);
$this->_throw($file, self::TOO_BIG);
$this->_max = $max;
$this->_size = $size;
} else {
$this->_throw($file, self::TOO_BIG);
}
}
if (count($this->_messages) > 0) {
return false;
}
return true;
}
/**
* Returns the formatted size
*
* @param integer $size
* @return string
*/
protected function _toByteString($size)
{
$sizes = array('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 integer
*/
protected function _fromByteString($size)
{
if (is_numeric($size)) {
return (integer) $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;
}
/**
* Throws an error of the given type
*
* @param string $file
* @param string $errorType
* @return false
*/
protected function _throw($file, $errorType)
{
if ($file !== null) {
$this->_value = $file['name'];
}
$this->_error($errorType);
return false;
}
}

View file

@ -0,0 +1,246 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Upload.php 22399 2010-06-09 19:08:28Z thomas $
*/
/**
* @see Zend_Validate_Abstract
*/
require_once 'Zend/Validate/Abstract.php';
/**
* Validator for the maximum size of a file up to a max of 2GB
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_File_Upload extends Zend_Validate_Abstract
{
/**@#+
* @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 = array(
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%'"
);
/**
* Internal array of files
* @var array
*/
protected $_files = array();
/**
* Sets validator options
*
* The array $files must be given in syntax of Zend_File_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|Zend_Config $files Array of files in syntax of Zend_File_Transfer
* @return void
*/
public function __construct($files = array())
{
if ($files instanceof Zend_Config) {
$files = $files->toArray();
}
$this->setFiles($files);
}
/**
* Returns the array of set files
*
* @param string $files (Optional) The file to return in detail
* @return array
* @throws Zend_Validate_Exception If file is not found
*/
public function getFiles($file = null)
{
if ($file !== null) {
$return = array();
foreach ($this->_files as $name => $content) {
if ($name === $file) {
$return[$file] = $this->_files[$name];
}
if ($content['name'] === $file) {
$return[$name] = $this->_files[$name];
}
}
if (count($return) === 0) {
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception("The file '$file' was not found");
}
return $return;
}
return $this->_files;
}
/**
* Sets the files to be checked
*
* @param array $files The files to check in syntax of Zend_File_Transfer
* @return Zend_Validate_File_Upload Provides a fluent interface
*/
public function setFiles($files = array())
{
if (count($files) === 0) {
$this->_files = $_FILES;
} else {
$this->_files = $files;
}
foreach($this->_files as $file => $content) {
if (!isset($content['error'])) {
unset($this->_files[$file]);
}
}
return $this;
}
/**
* Defined by Zend_Validate_Interface
*
* 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
* @return boolean
*/
public function isValid($value, $file = null)
{
$this->_messages = null;
if (array_key_exists($value, $this->_files)) {
$files[$value] = $this->_files[$value];
} else {
foreach ($this->_files as $file => $content) {
if (isset($content['name']) && ($content['name'] === $value)) {
$files[$file] = $this->_files[$file];
}
if (isset($content['tmp_name']) && ($content['tmp_name'] === $value)) {
$files[$file] = $this->_files[$file];
}
}
}
if (empty($files)) {
return $this->_throw($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->_throw($file, self::ATTACK);
}
break;
case 1:
$this->_throw($file, self::INI_SIZE);
break;
case 2:
$this->_throw($file, self::FORM_SIZE);
break;
case 3:
$this->_throw($file, self::PARTIAL);
break;
case 4:
$this->_throw($file, self::NO_FILE);
break;
case 6:
$this->_throw($file, self::NO_TMP_DIR);
break;
case 7:
$this->_throw($file, self::CANT_WRITE);
break;
case 8:
$this->_throw($file, self::EXTENSION);
break;
default:
$this->_throw($file, self::UNKNOWN);
break;
}
}
if (count($this->_messages) > 0) {
return false;
} else {
return true;
}
}
/**
* Throws an error of the given type
*
* @param string $file
* @param string $errorType
* @return false
*/
protected function _throw($file, $errorType)
{
if ($file !== null) {
if (is_array($file) and !empty($file['name'])) {
$this->_value = $file['name'];
}
}
$this->_error($errorType);
return false;
}
}

View file

@ -0,0 +1,101 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: WordCount.php 22697 2010-07-26 21:14:47Z alexander $
*/
/**
* @see Zend_Validate_File_Count
*/
require_once 'Zend/Validate/File/Count.php';
/**
* Validator for counting all words in a file
*
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_File_WordCount extends Zend_Validate_File_Count
{
/**#@+
* @const string Error constants
*/
const TOO_MUCH = 'fileWordCountTooMuch';
const TOO_LESS = 'fileWordCountTooLess';
const NOT_FOUND = 'fileWordCountNotFound';
/**#@-*/
/**
* @var array Error message templates
*/
protected $_messageTemplates = array(
self::TOO_MUCH => "Too much words, maximum '%max%' are allowed but '%count%' were counted",
self::TOO_LESS => "Too less words, minimum '%min%' are expected but '%count%' were counted",
self::NOT_FOUND => "File '%value%' could not be found",
);
/**
* Defined by Zend_Validate_Interface
*
* 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 $value Filename to check for word count
* @param array $file File data from Zend_File_Transfer
* @return boolean
*/
public function isValid($value, $file = null)
{
// Is file readable ?
require_once 'Zend/Loader.php';
if (!Zend_Loader::isReadable($value)) {
return $this->_throw($file, self::NOT_FOUND);
}
$content = file_get_contents($value);
$this->_count = str_word_count($content);
if (($this->_max !== null) && ($this->_count > $this->_max)) {
return $this->_throw($file, self::TOO_MUCH);
}
if (($this->_min !== null) && ($this->_count < $this->_min)) {
return $this->_throw($file, self::TOO_LESS);
}
return true;
}
/**
* Throws an error of the given type
*
* @param string $file
* @param string $errorType
* @return false
*/
protected function _throw($file, $errorType)
{
if ($file !== null) {
$this->_value = $file['name'];
}
$this->_error($errorType);
return false;
}
}