import v1.1.0_beta1 | 2009-08-21
This commit is contained in:
@ -144,7 +144,7 @@ class Zend_Validate_File_Count extends Zend_Validate_Abstract
|
||||
* Sets the minimum file count
|
||||
*
|
||||
* @param integer|array $min The minimum file count
|
||||
* @return Zend_Validate_File_Size Provides a fluent interface
|
||||
* @return Zend_Validate_File_Count Provides a fluent interface
|
||||
* @throws Zend_Validate_Exception When min is greater than max
|
||||
*/
|
||||
public function setMin($min)
|
||||
@ -208,6 +208,28 @@ class Zend_Validate_File_Count extends Zend_Validate_Abstract
|
||||
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
|
||||
*
|
||||
@ -221,16 +243,7 @@ class Zend_Validate_File_Count extends Zend_Validate_Abstract
|
||||
*/
|
||||
public function isValid($value, $file = null)
|
||||
{
|
||||
if (is_string($value)) {
|
||||
$value = array($value);
|
||||
}
|
||||
|
||||
foreach ($value as $file) {
|
||||
if (!isset($this->_files[$file])) {
|
||||
$this->_files[$file] = $file;
|
||||
}
|
||||
}
|
||||
|
||||
$this->addFile($value);
|
||||
$this->_count = count($this->_files);
|
||||
if (($this->_max !== null) && ($this->_count > $this->_max)) {
|
||||
return $this->_throw($file, self::TOO_MUCH);
|
||||
|
@ -72,6 +72,13 @@ class Zend_Validate_File_MimeType extends Zend_Validate_Abstract
|
||||
*/
|
||||
protected $_mimetype;
|
||||
|
||||
/**
|
||||
* Magicfile to use
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $_magicfile;
|
||||
|
||||
/**
|
||||
* Sets validator options
|
||||
*
|
||||
@ -91,9 +98,44 @@ class Zend_Validate_File_MimeType extends Zend_Validate_Abstract
|
||||
throw new Zend_Validate_Exception("Invalid options to validator provided");
|
||||
}
|
||||
|
||||
if (isset($mimetype['magicfile'])) {
|
||||
$this->setMagicFile($mimetype['magicfile']);
|
||||
}
|
||||
|
||||
$this->setMimeType($mimetype);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returna the actual set magicfile
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMagicFile()
|
||||
{
|
||||
return $this->_magicfile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the magicfile to use
|
||||
* if null, the MAGIC constant from php is used
|
||||
*
|
||||
* @param string $file
|
||||
* @return Zend_Validate_File_MimeType Provides fluid interface
|
||||
*/
|
||||
public function setMagicFile($file)
|
||||
{
|
||||
if (empty($file)) {
|
||||
$this->_magicfile = null;
|
||||
} else if (!is_readable($file)) {
|
||||
require_once 'Zend/Validate/Exception.php';
|
||||
throw new Zend_Validate_Exception('The given magicfile can not be read');
|
||||
} else {
|
||||
$this->_magicfile = (string) $file;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the set mimetypes
|
||||
*
|
||||
@ -141,6 +183,10 @@ class Zend_Validate_File_MimeType extends Zend_Validate_Abstract
|
||||
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;
|
||||
@ -181,8 +227,14 @@ class Zend_Validate_File_MimeType extends Zend_Validate_Abstract
|
||||
}
|
||||
|
||||
if ($file !== null) {
|
||||
if (class_exists('finfo', false) && defined('MAGIC')) {
|
||||
$mime = new finfo(FILEINFO_MIME);
|
||||
$mimefile = $this->getMagicFile();
|
||||
if (class_exists('finfo', false) && ((!empty($mimefile)) or (defined('MAGIC')))) {
|
||||
if (!empty($mimefile)) {
|
||||
$mime = new finfo(FILEINFO_MIME, $mimefile);
|
||||
} else {
|
||||
$mime = new finfo(FILEINFO_MIME);
|
||||
}
|
||||
|
||||
$this->_type = $mime->file($value);
|
||||
unset($mime);
|
||||
} elseif (function_exists('mime_content_type') && ini_get('mime_magic.magicfile')) {
|
||||
|
@ -282,16 +282,17 @@ class Zend_Validate_File_Size extends Zend_Validate_Abstract
|
||||
|
||||
// limited to 4GB files
|
||||
$size = sprintf("%u", @filesize($value));
|
||||
$this->_setSize($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->setMin($this->_toByteString($min));
|
||||
$this->_min = $this->_toByteString($min);
|
||||
$this->_size = $this->_toByteString($size);
|
||||
$this->_throw($file, self::TOO_SMALL);
|
||||
$this->setMin($min);
|
||||
$this->_min = $min;
|
||||
$this->_size = $size;
|
||||
} else {
|
||||
$this->_throw($file, self::TOO_SMALL);
|
||||
}
|
||||
@ -300,9 +301,11 @@ class Zend_Validate_File_Size extends Zend_Validate_Abstract
|
||||
// Check to see if it's larger than max size
|
||||
if (($max !== null) && ($max < $size)) {
|
||||
if ($this->useByteString()) {
|
||||
$this->setMax($this->_toByteString($max));
|
||||
$this->_max = $this->_toByteString($max);
|
||||
$this->_size = $this->_toByteString($size);
|
||||
$this->_throw($file, self::TOO_BIG);
|
||||
$this->setMax($max);
|
||||
$this->_max = $max;
|
||||
$this->_size = $size;
|
||||
} else {
|
||||
$this->_throw($file, self::TOO_BIG);
|
||||
}
|
||||
@ -327,6 +330,7 @@ class Zend_Validate_File_Size extends Zend_Validate_Abstract
|
||||
for ($i=0; $size >= 1024 && $i < 9; $i++) {
|
||||
$size /= 1024;
|
||||
}
|
||||
|
||||
return round($size, 2) . $sizes[$i];
|
||||
}
|
||||
|
||||
@ -342,31 +346,36 @@ class Zend_Validate_File_Size extends Zend_Validate_Abstract
|
||||
return (integer) $size;
|
||||
}
|
||||
|
||||
$type = trim(substr($size, -2));
|
||||
$value = substr($size, 0, -2);
|
||||
$type = trim(substr($size, -2, 1));
|
||||
|
||||
$value = substr($size, 0, -1);
|
||||
if (!is_numeric($value)) {
|
||||
$value = substr($value, 0, -1);
|
||||
}
|
||||
|
||||
switch (strtoupper($type)) {
|
||||
case 'YB':
|
||||
case 'Y':
|
||||
$value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
|
||||
break;
|
||||
case 'ZB':
|
||||
case 'Z':
|
||||
$value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
|
||||
break;
|
||||
case 'EB':
|
||||
case 'E':
|
||||
$value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024);
|
||||
break;
|
||||
case 'PB':
|
||||
case 'P':
|
||||
$value *= (1024 * 1024 * 1024 * 1024 * 1024);
|
||||
break;
|
||||
case 'TB':
|
||||
case 'T':
|
||||
$value *= (1024 * 1024 * 1024 * 1024);
|
||||
break;
|
||||
case 'GB':
|
||||
case 'G':
|
||||
$value *= (1024 * 1024 * 1024);
|
||||
break;
|
||||
case 'MB':
|
||||
case 'M':
|
||||
$value *= (1024 * 1024);
|
||||
break;
|
||||
case 'KB':
|
||||
case 'K':
|
||||
$value *= 1024;
|
||||
break;
|
||||
default:
|
||||
|
@ -119,7 +119,7 @@ class Zend_Validate_File_Upload extends Zend_Validate_Abstract
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the minimum filesize
|
||||
* 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
|
||||
@ -131,6 +131,13 @@ class Zend_Validate_File_Upload extends Zend_Validate_Abstract
|
||||
} else {
|
||||
$this->_files = $files;
|
||||
}
|
||||
|
||||
foreach($this->_files as $file => $content) {
|
||||
if (!isset($content['error'])) {
|
||||
unset($this->_files[$file]);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -143,25 +150,24 @@ class Zend_Validate_File_Upload extends Zend_Validate_Abstract
|
||||
* from initialization will be used
|
||||
* @return boolean
|
||||
*/
|
||||
public function isValid($value)
|
||||
public function isValid($value, $file = null)
|
||||
{
|
||||
if (array_key_exists($value, $this->_files)) {
|
||||
$files[$value] = $this->_files[$value];
|
||||
} else {
|
||||
foreach ($this->_files as $file => $content) {
|
||||
if ($content['name'] === $value) {
|
||||
if (isset($content['name']) && ($content['name'] === $value)) {
|
||||
$files[$file] = $this->_files[$file];
|
||||
}
|
||||
|
||||
if ($content['tmp_name'] === $value) {
|
||||
if (isset($content['tmp_name']) && ($content['tmp_name'] === $value)) {
|
||||
$files[$file] = $this->_files[$file];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($files)) {
|
||||
$this->_error(self::FILE_NOT_FOUND);
|
||||
return false;
|
||||
return $this->_throw($file, self::FILE_NOT_FOUND);
|
||||
}
|
||||
|
||||
foreach ($files as $file => $content) {
|
||||
@ -169,40 +175,40 @@ class Zend_Validate_File_Upload extends Zend_Validate_Abstract
|
||||
switch($content['error']) {
|
||||
case 0:
|
||||
if (!is_uploaded_file($content['tmp_name'])) {
|
||||
$this->_error(self::ATTACK);
|
||||
$this->_throw($file, self::ATTACK);
|
||||
}
|
||||
break;
|
||||
|
||||
case 1:
|
||||
$this->_error(self::INI_SIZE);
|
||||
$this->_throw($file, self::INI_SIZE);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
$this->_error(self::FORM_SIZE);
|
||||
$this->_throw($file, self::FORM_SIZE);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
$this->_error(self::PARTIAL);
|
||||
$this->_throw($file, self::PARTIAL);
|
||||
break;
|
||||
|
||||
case 4:
|
||||
$this->_error(self::NO_FILE);
|
||||
$this->_throw($file, self::NO_FILE);
|
||||
break;
|
||||
|
||||
case 6:
|
||||
$this->_error(self::NO_TMP_DIR);
|
||||
$this->_throw($file, self::NO_TMP_DIR);
|
||||
break;
|
||||
|
||||
case 7:
|
||||
$this->_error(self::CANT_WRITE);
|
||||
$this->_throw($file, self::CANT_WRITE);
|
||||
break;
|
||||
|
||||
case 8:
|
||||
$this->_error(self::EXTENSION);
|
||||
$this->_throw($file, self::EXTENSION);
|
||||
break;
|
||||
|
||||
default:
|
||||
$this->_error(self::UNKNOWN);
|
||||
$this->_throw($file, self::UNKNOWN);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -213,4 +219,23 @@ class Zend_Validate_File_Upload extends Zend_Validate_Abstract
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
101
libs/Zend/Validate/File/WordCount.php
Normal file
101
libs/Zend/Validate/File/WordCount.php
Normal 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: $
|
||||
*/
|
||||
|
||||
/**
|
||||
* @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-2008 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 => "The 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user